diff options
author | Tomás Touceda <chiiph@leap.se> | 2014-09-26 10:33:10 -0300 |
---|---|---|
committer | Tomás Touceda <chiiph@leap.se> | 2014-09-26 10:33:10 -0300 |
commit | dce318792f5abda706c5437f363dc8212c7c50f3 (patch) | |
tree | 63e8ddd8380a5f08b87c711318bf800ad132604b /src | |
parent | e22077a7cf91d4284216f443f03e96a6767a4622 (diff) | |
parent | e448f40dfbb913742fa2640c8af16fe000d2b401 (diff) |
Diffstat (limited to 'src')
-rw-r--r-- | src/launcher.conf | 5 | ||||
-rw-r--r-- | src/launcher.py | 137 | ||||
-rw-r--r-- | src/main.cpp | 38 |
3 files changed, 126 insertions, 54 deletions
diff --git a/src/launcher.conf b/src/launcher.conf new file mode 100644 index 0000000..7cc0043 --- /dev/null +++ b/src/launcher.conf @@ -0,0 +1,5 @@ +[General] +updater_delay = 60 + +[Mirror.localhost] +url_prefix = http://localhost:8001 diff --git a/src/launcher.py b/src/launcher.py index b62bd23..bbd4f42 100644 --- a/src/launcher.py +++ b/src/launcher.py @@ -1,46 +1,118 @@ import os +import shutil import platform import time import threading import ConfigParser -from leap.bitmask.app import main as leap_client -from leap.common.events import server - -from thandy.ClientCLI import update as thandy_update +from leap.bitmask.app import start_app as bitmask_client +from leap.common.events import server, signal +from leap.common.events import events_pb2 as proto +import tuf.client.updater bundles_per_platform = { - "Windows" : "/bundleinfo/LEAPClient-win/", - "Darwin" : "", - "Linux" : "/bundleinfo/LEAPClient/", + "Linux-i386": "linux-i368", + "Linux-i686": "linux-i368", + "Linux-x86_64": "linux-x86_64", } GENERAL_SECTION = "General" -UPDATES_KEY = "Updates" +DELAY_KEY = "updater_delay" + + +class TUF(threading.Thread): + def __init__(self, config): + """ + Initialize the list of mirrors, paths and other TUF dependencies from + the config file + """ + if config.has_section(GENERAL_SECTION) and \ + config.has_option(GENERAL_SECTION, DELAY_KEY): + self.delay = config.getint(GENERAL_SECTION, DELAY_KEY) + else: + self.delay = 60 + + self._load_mirrors(config) + if not self.mirrors: + print("ERROR: No updater mirrors found (missing or not well " + "formed launcher.conf)") + + self.bundle_path = os.getcwd() + self.source_path = self.bundle_path + self.dest_path = os.path.join(self.bundle_path, 'tmp') + self.update_path = os.path.join(self.bundle_path, 'updates') + + threading.Thread.__init__(self) -class Thandy(threading.Thread): def run(self): + """ + Check for updates periodically + """ + if not self.mirrors: + return + while True: try: - os.environ["THANDY_HOME"] = os.path.join(os.getcwd(), - "config", - "thandy") - os.environ["THP_DB_ROOT"] = os.path.join(os.getcwd(), - "packages") - os.environ["THP_INSTALL_ROOT"] = os.path.join(os.getcwd(), - "updates") - args = [ - "--repo=repo/", - "--install", - bundles_per_platform[platform.system()] - ] - thandy_update(args) + tuf.conf.repository_directory = os.path.join(self.bundle_path, + 'repo') + + updater = tuf.client.updater.Updater('leap-updater', + self.mirrors) + updater.refresh() + + targets = updater.all_targets() + updated_targets = updater.updated_targets(targets, + self.source_path) + if updated_targets: + print "There is updates needed. Start downloading updates." + for target in updated_targets: + updater.download_target(target, self.dest_path) + self._set_permissions(target) + if os.path.isdir(self.dest_path): + if os.path.isdir(self.update_path): + shutil.rmtree(self.update_path) + shutil.move(self.dest_path, self.update_path) + filepath = sorted([f['filepath'] for f in updated_targets]) + signal(proto.UPDATER_NEW_UPDATES, + content=", ".join(filepath)) + print "Updates ready: ", filepath + return + except NotImplemented as e: + print "NotImplemented: ", e + return except Exception as e: print "ERROR:", e finally: - # TODO: Make this delay configurable - time.sleep(60) + time.sleep(self.delay) + + def _load_mirrors(self, config): + self.mirrors = {} + for section in config.sections(): + if section[:6] != 'Mirror': + continue + url_prefix = config.get(section, 'url_prefix') + metadata_path = self._repo_path() + '/metadata' + targets_path = self._repo_path() + '/targets' + self.mirrors[section[7:]] = {'url_prefix': url_prefix, + 'metadata_path': metadata_path, + 'targets_path': targets_path, + 'confined_target_dirs': ['']} + + def _set_permissions(self, target): + file_permissions_str = target["fileinfo"]["custom"]["file_permissions"] + file_permissions = int(file_permissions_str, 8) + filepath = target['filepath'] + if filepath[0] == '/': + filepath = filepath[1:] + file_path = os.path.join(self.dest_path, filepath) + os.chmod(file_path, file_permissions) + + def _repo_path(self): + system = platform.system() + "-" + platform.machine() + if system not in bundles_per_platform: + raise NotImplementedError("Platform %s not supported" % (system,)) + return bundles_per_platform[system] if __name__ == "__main__": @@ -49,17 +121,8 @@ if __name__ == "__main__": config = ConfigParser.ConfigParser() config.read("launcher.conf") - launch_thandy = False - try: - launch_thandy = config.getboolean(GENERAL_SECTION, UPDATES_KEY) - except ConfigParser.NoSectionError as ns: - pass - except ConfigParser.NoOptionError as no: - pass - - if launch_thandy: - thandy_thread = Thandy() - thandy_thread.daemon = True - thandy_thread.start() + tuf_thread = TUF(config) + tuf_thread.daemon = True + tuf_thread.start() - leap_client() + bitmask_client() diff --git a/src/main.cpp b/src/main.cpp index e528289..e7169c2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,6 +3,7 @@ #include <string> #include <cmath> #include <cstdlib> +#include <unistd.h> #include <boost/python.hpp> #include <boost/filesystem/operations.hpp> @@ -94,14 +95,14 @@ mergeDirectories(const fs::path &source, } void -updateIfNeeded() +updateIfNeeded(fs::path &full_path) { - fs::path updatePath(fs::current_path() / fs::path(UPDATES_DIR)); + fs::path updatePath(full_path / fs::path(UPDATES_DIR)); if (fs::exists(updatePath)) { std::cout << "Found updates, merging directories before doing anything..." << std::endl; - mergeDirectories(updatePath, fs::current_path()); + mergeDirectories(updatePath, full_path); fs::remove_all(updatePath); } else @@ -115,28 +116,31 @@ int main(int argc, char** argv) { try { - fs::path full_path(fs::current_path()); + fs::path full_path(fs::system_complete(argv[0]).parent_path()); - updateIfNeeded(); - auto pypath = full_path.string() + ":" + full_path.string() + "/lib/"; + updateIfNeeded(full_path); + auto pypath = full_path.string() + "/apps/:" + full_path.string() + "/lib/"; + std::cout << pypath << std::endl; #if not defined _WIN32 && not defined _WIN64 - fs::path fromCore("./lib/libQtCore.so.4"); - fs::path toCore("./lib/libQtCore.NOTUSED"); - fs::path fromGui("./lib/libQtGui.so.4"); - fs::path toGui("./lib/libQtGui.NOTUSED"); + chdir("lib"); + fs::path fromCore("libQtCore.non-ubuntu"); + fs::path toCore("libQtCore.so.4"); + fs::path fromGui("libQtGui.non-ubuntu"); + fs::path toGui("libQtGui.so.4"); try { auto desk = std::string(getenv("DESKTOP_SESSION")); if(boost::starts_with(desk, "ubuntu")) { - fs::rename(fromCore, toCore); - fs::rename(fromGui, toGui); + fs::remove(toCore); + fs::remove(toGui); } else { - fs::rename(toCore, fromCore); - fs::rename(toGui, fromGui); + fs::create_symlink(fromCore, toCore); + fs::create_symlink(fromGui, toGui); } } catch(...) { } + chdir(".."); setenv("PYTHONPATH", pypath.c_str(), 1); #endif @@ -155,15 +159,15 @@ main(int argc, char** argv) py::exec( "import sys\n" - "sys.path = [_pwd + '/lib',\n" - " _pwd + '/apps',\n" + "sys.path = [_pwd + '/apps',\n" + " _pwd + '/lib',\n" " _pwd + '/apps/eip',\n" " _pwd]\n" "import os\n" "import encodings.idna\n" // we need to make sure this is imported "sys.argv.append('--standalone')\n" "sys.argv.append('--debug')\n" - "if any(map(lambda x: x.startswith('--logfile') or x.startswith('-l'), sys.argv))\n" + "if not any(map(lambda x: x.startswith('--logfile') or x.startswith('-l'), sys.argv)):\n" " sys.argv.append('--logfile=bitmask.log')\n", global, global); py::exec_file("apps/launcher.py", |