def run(self): self.config.reload_if_updated() with Wapt(config_filename = self.config.config_filename) as tmp_wapt: logger.info('Starting socketio on "%s://%s:%s" ...' % (self.config.websockets_proto,self.config.websockets_host,self.config.websockets_port)) logger.debug('Certificate checking : %s' % self.config.websockets_verify_cert) while True: try: if not self.socketio_client and self.config.websockets_host: logger.debug('Creating socketio client') logger.debug('Proxies : %s'%self.config.waptserver.proxies) # bug in socketio... ? we must not pass proxies at all (even None) if we don"t want to switch to polling mode... kwargs = {} if self.config.waptserver.proxies and self.config.waptserver.proxies.get(self.config.websockets_proto,None) is not None: kwargs['proxies'] = self.config.waptserver.proxies if not self.socketio_client: self.socketio_client = SocketIO( host="%s://%s" % (self.config.websockets_proto,self.config.websockets_host), port=self.config.websockets_port, Namespace = WaptSocketIORemoteCalls, verify=self.config.websockets_verify_cert, wait_for_connection = False, transport = ['websocket'], ping_interval = self.config.websockets_ping, hurry_interval_in_seconds = self.config.websockets_hurry_interval, params = {'uuid': tmp_wapt.host_uuid,'login':self.config.websockets_auth}, **kwargs) #self.socketio_client.get_namespace().wapt = tmp_wapt if self.socketio_client and self.config.websockets_host: if not self.socketio_client.connected: self.socketio_client._http_session.params.update({'uuid': tmp_wapt.host_uuid,'login':self.config.websockets_auth}) self.socketio_client.define(WaptSocketIORemoteCalls) #self.socketio_client.get_namespace().wapt = tmp_wapt self.socketio_client.connect('') if self.socketio_client.connected: logger.info('Socket IO listening for %ss' % self.config.websockets_check_config_interval ) self.socketio_client.wait(self.config.websockets_check_config_interval) self.config.reload_if_updated() except Exception as e: print('Error in socket io connection %s' % repr(e)) self.config.reload_if_updated() if self.socketio_client: print('stop sio client') self.socketio_client._close() del self.socketio_client self.socketio_client = None if self.socketio_client and self.config.websockets_host: self.socketio_client._http_session.params.update({'uuid': tmp_wapt.host_uuid,'login':self.config.websockets_auth}) logger.info('Socket IO Stopped, waiting %ss before retrying' % self.config.websockets_retry_delay) time.sleep(self.config.websockets_retry_delay)
def update_waptupgrade(checkout_dir): print('Building a new waptupgrade package') sys.path.append('c:/wapt') from common import Wapt wapt = Wapt() wapt.build_upload(os.path.join(checkout_dir, 'waptupgrade'))
'default_source_url':'', 'gpgkey':'', 'default_development_base':'c:\tranquilit', 'default_package_prefix':'tis', 'default_sources_suffix':'wapt', 'default_sources_url':'', 'upload_cmd':'', 'wapt_server':'', } cp = RawConfigParser(defaults = defaults) cp.add_section('global') cp.read(config_file) if len(logger.handlers)<1: hdlr = logging.StreamHandler(sys.stdout) hdlr.setFormatter(logging.Formatter('%(asctime)s %(levelname)s %(message)s')) logger.addHandler(hdlr) # set loglevel if loglevel in ('debug','warning','info','error','critical'): numeric_level = getattr(logging, loglevel.upper(), None) if not isinstance(numeric_level, int): raise ValueError('Invalid log level: %s' % loglevel) logger.setLevel(numeric_level) mywapt = Wapt(config=cp) mywapt.wapt_repourl = mywapt.find_wapt_server() print mywapt.wapt_repourl
def run(self): self.config.reload_if_updated() with Wapt(config_filename = self.config.config_filename) as tmp_wapt: logger.info('Starting socketio on "%s://%s:%s" ...' % (self.config.websockets_proto,self.config.websockets_host,self.config.websockets_port)) logger.debug('Certificate checking : %s' % self.config.websockets_verify_cert) while True: try: connect_params = dict( uuid = tmp_wapt.host_uuid, ) host_key = tmp_wapt.get_host_key() host_cert = tmp_wapt.get_host_certificate() if not self.socketio_client and self.config.websockets_host: logger.debug('Creating socketio client') logger.debug('Proxies : %s'%self.config.waptserver.proxies) # bug in socketio... ? we must not pass proxies at all (even None) if we don"t want to switch to polling mode... kwargs = {} if self.config.waptserver.proxies and self.config.waptserver.proxies.get(self.config.websockets_proto,None) is not None: kwargs['proxies'] = self.config.waptserver.proxies signed_connect_params = host_key.sign_claim(connect_params,signer_certificate_chain = host_cert) self.socketio_client = SocketIO( host="%s://%s" % (self.config.websockets_proto,self.config.websockets_host), port=self.config.websockets_port, Namespace = WaptSocketIORemoteCalls, resource=self.config.websockets_root, verify=self.config.websockets_verify_cert, wait_for_connection = False, transport = ['websocket'], ping_interval = self.config.websockets_ping, hurry_interval_in_seconds = self.config.websockets_hurry_interval, params = {'uuid': tmp_wapt.host_uuid, 'login':jsondump(signed_connect_params)}, **kwargs) self.socketio_client.get_namespace().wapt = tmp_wapt self.socketio_client.get_namespace().task_manager = self.task_manager if self.socketio_client and self.config.websockets_host: if not self.socketio_client.connected: signed_connect_params = host_key.sign_claim(connect_params,signer_certificate_chain = host_cert) self.socketio_client._http_session.params.update({'uuid': tmp_wapt.host_uuid,'login':jsondump(signed_connect_params)}) self.socketio_client.define(WaptSocketIORemoteCalls) self.socketio_client.get_namespace().wapt = tmp_wapt self.socketio_client.get_namespace().task_manager = self.task_manager self.socketio_client.connect('') else: self.socketio_client.emit('wapt_pong') if self.socketio_client.connected: logger.info('Socket IO listening for %ss' % self.config.websockets_check_config_interval ) startwait = time.time() self.socketio_client.wait(self.config.websockets_check_config_interval) # QAD workaround for cases where server disconnect but client is between 2 states. # In this case; wait() immediately returns, leading to an indefinite loop eating 1 core. if time.time() - startwait < self.config.websockets_check_config_interval-2: raise Exception('Websocket client seems disconnected. Force Websocket connection to be recreated') elif not self.config.websockets_host: self.socketio_client = None time.sleep(self.config.websockets_retry_delay) else: time.sleep(self.config.websockets_retry_delay) if self.config.reload_if_updated(): if self.socketio_client: self.socketio_client.disconnect() raise EWaptException('Configuration changed, force Websocket connection to be recreated') except Exception as e: try: logger.info('Error in socket io connection %s' % repr(e)) self.config.reload_if_updated() if self.socketio_client: logger.debug('stop sio client') self.socketio_client = None finally: logger.debug('Socket IO Stopped, waiting %ss before retrying' % self.config.websockets_retry_delay) time.sleep(self.config.websockets_retry_delay)
def main(): if len(args) == 0: print "ERROR : You must provide one action to perform" parser.print_usage() sys.exit(2) action = args[0] # Config file if not os.path.isfile(config_file): logger.error("Error : could not find file : " + config_file + ", please check the path") logger.debug("Config file: %s" % config_file) defaults = { "repositories": "", "repo_url": "", "default_source_url": "", "private_key": "", "public_cert": "", "default_development_base": "c:\tranquilit", "default_package_prefix": "tis", "default_sources_suffix": "wapt", "default_sources_url": "", "upload_cmd": "", "wapt_server": "", "loglevel": "info", } cp = RawConfigParser(defaults=defaults) cp.add_section("global") cp.read(config_file) global loglevel if not loglevel and cp.has_option("global", "loglevel"): loglevel = cp.get("global", "loglevel") setloglevel(loglevel) mywapt = Wapt(config=cp) if options.wapt_url: mywapt.wapt_repourl = options.wapt_url if options.private_key: mywapt.private_key = options.private_key else: mywapt.private_key = cp.get("global", "private_key") mywapt.dry_run = options.dry_run # logger.info("Main wapt Repository %s" % mywapt.wapt_repourl) logger.debug("WAPT base directory : %s" % mywapt.wapt_base_dir) logger.debug("Package cache dir : %s" % mywapt.packagecachedir) logger.debug("WAPT DB Structure version;: %s" % mywapt.waptdb.db_version) try: params_dict = {} try: params_dict = json.loads(options.params.replace("'", '"')) except: raise Exception("Install Parameters must be in json format") if action == "install" or action == "download": if len(args) < 2: print "You must provide at least one package name" sys.exit(1) if os.path.isdir(args[1]) or os.path.isfile(args[1]): print "installing WAPT file %s" % args[1] if action == "install": mywapt.install_wapt(args[1], params_dict=params_dict) else: print "%sing WAPT packages %s" % (action, ",".join(args[1:])) if options.update_packages: print "Update package list" mywapt.update() result = mywapt.install( args[1:], force=options.force, params_dict=params_dict, download_only=(action == "download") ) print "\nResults :" if action <> "download": for k in ("install", "additional", "upgrade", "skipped", "errors"): if result.get(k, []): print "\n=== %s packages ===\n%s" % ( k, "\n".join( [" %-30s | %s (%s)" % (s[0], s[1].package, s[1].version) for s in result[k]] ), ) else: for k in ("downloaded", "skipped", "errors"): if result.get("downloads", {"downloaded": [], "skipped": [], "errors": []})[k]: print "\n=== %s packages ===\n%s" % ( k, "\n".join([" %s" % (s,) for s in result["downloads"][k]]), ) elif action == "download": if len(args) < 2: print "You must provide at least one package name to download" sys.exit(1) if options.update_packages: print "Update package list" mywapt.update() print "Downloading packages %s" % (",".join(args[1:]),) result = mywapt.download_packages(args[1:], usecache=not options.force) if result["downloaded"]: print "\nDownloaded packages : \n%s" % "\n".join([" %s" % p for p in result["downloaded"]]) if result["skipped"]: print "Skipped packages : \n%s" % "\n".join([" %s" % p for p in result["skipped"]]) if result["errors"]: logger.critical("Unable to download some files : %s" % (result["errors"],)) sys.exit(1) elif action == "show": if len(args) < 2: print "You must provide at least one package name to show" sys.exit(1) if os.path.isdir(args[1]) or os.path.isfile(args[1]): entry = PackageEntry().load_control_from_wapt(args[1]) print "%s" % entry else: print "Display package control data for %s\n" % (",".join(args[1:]),) if options.update_packages: print "Update package list" mywapt.update() for packagename in args[1:]: entries = mywapt.waptdb.packages_matching(packagename) if entries: for e in entries: print "%s\n" % e.ascontrol(with_non_control_attributes=True) else: print "None packages found matching package name and version" elif action == "show-params": if len(args) < 2: print "You must provide at one package name to show params for" sys.exit(1) for packagename in args[1:]: params = mywapt.waptdb.params(packagename) print "%s" % params elif action == "list-registry": print "%-39s%-70s%-20s%-70s" % ("UninstallKey", "Software", "Version", "Uninstallstring") print "-" * 39 + "-" * 70 + "-" * 20 + "-" * 70 for p in setuphelpers.installed_softwares(" ".join(args[1:])): print u"%-39s%-70s%-20s%-70s" % (p["key"], p["name"], p["version"], p["uninstall_string"]) elif action == "showlog": if len(args) < 2: print "You must provide at least one package name" sys.exit(1) for packagename in args[1:]: result = mywapt.last_install_log(packagename) print "Package : %s\nStatus : %s\n\nInstallation log:\n%s" % ( packagename, result["status"], result["log"], ) elif action == "remove": if len(args) < 2: print "You must provide at least one package name to remove" sys.exit(1) for packagename in args[1:]: print "Removing %s ..." % (packagename,), mywapt.remove(packagename, force=options.force) print "done" elif action == "session-setup": if len(args) < 2: print "You must provide at least one package to be configured in user's session" sys.exit(1) for packagename in args[1:]: print "Configuring %s ..." % (packagename,), mywapt.session_setup(packagename, params_dict=params_dict) print "done" elif action == "uninstall": # launch the setup.uninstall() procedure for the given packages # can be used when registering in registry a custom install with a python script if len(args) < 2: print "You must provide at least one package to be uninstalled" sys.exit(1) for packagename in args[1:]: print "uninstalling %s ..." % (packagename,), mywapt.uninstall(packagename, params_dict=params_dict) print "uninstall done" elif action == "update": print "Update package list" result = mywapt.update() print "Total packages : %i" % result["count"] print "Added packages : \n%s" % "\n".join([" %s (%s)" % p for p in result["added"]]) print "Removed packages : \n%s" % "\n".join([" %s (%s)" % p for p in result["removed"]]) print "Repositories URL : \n%s" % "\n".join([" %s" % p for p in result["repos"]]) elif action == "upgradedb": (old, new) = mywapt.waptdb.upgradedb() if old == new: print "No database upgrade required, current %s, required %s" % (old, mywapt.waptdb.curr_db_version) else: print "Old version : %s to new : %s" % (old, new) elif action == "upgrade": if options.update_packages: print "Update package list" mywapt.update() result = mywapt.upgrade() if not result["install"] and not result["additional"] and not result["upgrade"] and not result["skipped"]: print "Nothing to upgrade" else: for k in ("install", "additional", "upgrade", "skipped", "errors"): if result[k]: print "\n=== %s packages ===\n%s" % ( k, "\n".join([" %-30s | %s (%s)" % (s[0], s[1].package, s[1].version) for s in result[k]]), ) sys.exit(0) elif action == "list-upgrade": if options.update_packages: print "Update package list" mywapt.update() q = mywapt.list_upgrade() if not q: print "Nothing to upgrade" else: print ppdicttable([p[0] for p in q], [("package", 20), ("version", 10)]) elif action == "download-upgrade": if options.update_packages: print "Update package list" mywapt.update() result = mywapt.download_upgrades() print "Downloaded packages : \n%s" % "\n".join([" %s" % p for p in result["downloaded"]]) print "Skipped packages : \n%s" % "\n".join([" %s" % p for p in result["skipped"]]) if result["errors"]: logger.critical("Unable to download some files : %s" % (result["errors"],)) sys.exit(1) elif action == "update-packages": if len(args) < 2: print "You must provide the directory" sys.exit(1) print update_packages(args[1]) elif action == "sources": if len(args) < 2: print "You must provide the package name" sys.exit(1) os.startfile(mywapt.get_sources(args[1])) elif action == "make-template": if len(args) < 2: print "You must provide the installer path" sys.exit(1) source_dir = mywapt.maketemplate(*args[1:]) print "Template created. You can build the WAPT package by launching\n %s build-package %s" % ( sys.argv[0], source_dir, ) os.startfile(source_dir) elif action == "make-host-template": source_dir = mywapt.makehosttemplate(*args[1:]) print "Template created. You can build the WAPT package by launching\n %s build-package %s" % ( sys.argv[0], source_dir, ) os.startfile(source_dir) elif action == "build-package": if len(args) < 2: print "You must provide at least one source directory for package build" sys.exit(1) for source_dir in [os.path.abspath(p) for p in args[1:]]: if os.path.isdir(source_dir): print ("Building %s" % source_dir) result = mywapt.buildpackage( source_dir, inc_package_release=options.increlease, excludes=options.excludes.split(",") ) package_fn = result["filename"] if package_fn: print "Package content:" for f in result["files"]: print " %s" % f[0] print ("...done. Package filename %s" % (package_fn,)) def pwd_callback(*args): """Default password callback for opening private keys""" return open(options.private_key_passwd, "r").read() if mywapt.private_key: print ("Signing %s" % package_fn) if options.private_key_passwd: signature = mywapt.signpackage( package_fn, excludes=options.excludes.split(","), callback=pwd_callback ) else: signature = mywapt.signpackage(package_fn, excludes=options.excludes.split(",")) print "Package %s signed : signature :\n%s" % (package_fn, signature) else: logger.warning("No private key provided, package %s is unsigned !" % package_fn) if mywapt.upload_cmd: print "\nYou can upload to repository with\n %s upload-package %s " % ( sys.argv[0], package_fn, ) return 0 else: logger.critical("package not created") return 1 else: logger.critical("Directory %s not found" % source_dir) elif action == "sign-package": if len(args) < 2: print "You must provide at least one source directory or package to sign" sys.exit(1) for waptfile in [os.path.abspath(p) for p in args[1:]]: if os.path.isdir(waptfile) or os.path.isfile(waptfile): print ("Signing %s" % waptfile) signature = mywapt.signpackage(waptfile, excludes=options.excludes.split(",")) print "Package %s signed : signature :\n%s" % (waptfile, signature) else: logger.critical("Package %s not found" % waptfile) return 1 elif action == "upload-package": if len(args) < 2: print "You must provide a package to upload" sys.exit(1) waptfiles = [] for a in args[1:]: waptfiles += glob.glob(a) waptfile_arg = " ".join(['"%s"' % f for f in waptfiles]) print setuphelpers.run(mywapt.upload_cmd % {"waptfile": waptfile_arg}) if mywapt.after_upload: print setuphelpers.run(mywapt.after_upload % {"waptfile": waptfile_arg}) elif action == "search": if options.update_packages: print "Update package list" mywapt.update() result = mywapt.waptdb.packages_search(args[1:]) print ppdicttable(result, (("package", 30), ("version", 10), ("description", 80))) elif action == "cleanup": result = mywapt.cleanup() print "Removed files : \n%s" % "\n".join([" %s" % p for p in result]) elif action == "inventory": print mywapt.inventory() elif action == "setup-tasks": print mywapt.setup_tasks() elif action == "list": def cb(fieldname, value): if value and fieldname == "install_date": return value[0:16] else: return value print ppdicttable( mywapt.waptdb.installed_search(args[1:]).values(), (("package", 20), ("version", 15), ("install_status", 10), ("install_date", 16), ("description", 80)), callback=cb, ) else: print "Unknown action %s" % action sys.exit(1) except Exception, e: print "FATAL ERROR : %s" % e if logger.level == logging.DEBUG: raise sys.exit(3)
def run(self): self.config.reload_if_updated() with Wapt(config_filename = self.config.config_filename) as tmp_wapt: logger.info('Starting socketio on "%s://%s:%s" ...' % (self.config.websockets_proto,self.config.websockets_host,self.config.websockets_port)) logger.debug('Certificate checking : %s' % self.config.websockets_verify_cert) def get_connect_params(wapt): connect_params = {'uuid': wapt.host_uuid} if not self.server_authorization_token: try: self.server_authorization_token = wapt.get_auth_token('websocket') logger.info('Websocket token: %s' % self.server_authorization_token) connect_params['token'] = self.server_authorization_token except Exception as e: logger.warning('Websocket connect params: %s' % e) self.server_authorization_token = None return {'params':connect_params, 'cert' :(wapt.get_host_certificate_filename(),wapt.get_host_key_filename())} while True: try: connect_params = get_connect_params(tmp_wapt) if not self.socketio_client and self.config.websockets_host: logger.debug('Creating socketio client') logger.debug('Proxies : %s'%self.config.waptserver.proxies) # bug in socketio... ? we must not pass proxies at all (even None) if we don"t want to switch to polling mode... kwargs = {} if self.config.waptserver.proxies and self.config.waptserver.proxies.get(self.config.websockets_proto,None) is not None: kwargs['proxies'] = self.config.waptserver.proxies kwargs.update(connect_params) self.socketio_client = SocketIO( host="%s://%s" % (self.config.websockets_proto,self.config.websockets_host), port=self.config.websockets_port, Namespace = WaptSocketIORemoteCalls, resource=self.config.websockets_root, verify=self.config.websockets_verify_cert, wait_for_connection = False, transport = ['websocket'], ping_interval = self.config.websockets_ping, hurry_interval_in_seconds = self.config.websockets_hurry_interval, request_timeout = self.request_timeout, **kwargs) self.socketio_client.get_namespace().wapt = tmp_wapt self.socketio_client.get_namespace().task_manager = self.task_manager if self.socketio_client and self.config.websockets_host: if not self.socketio_client.connected: self.socketio_client._http_session.update(connect_params) self.socketio_client.define(WaptSocketIORemoteCalls) self.socketio_client.get_namespace().wapt = tmp_wapt self.socketio_client.get_namespace().task_manager = self.task_manager self.socketio_client.connect('') else: # be sure server DB is aware of the current connection. # could be avoided self.socketio_client.emit('wapt_pong') if self.socketio_client.connected: logger.info('Socket IO listening for %ss' % self.config.websockets_check_config_interval ) startwait = time.time() self.socketio_client.wait(self.config.websockets_check_config_interval) # QAD workaround for cases where server disconnect but client is between 2 states. # In this case; wait() immediately returns, leading to an indefinite loop eating 1 core. if time.time() - startwait < self.config.websockets_check_config_interval-2: raise Exception('Websocket client seems disconnected. Force Websocket connection to be recreated') elif not self.config.websockets_host: self.socketio_client = None time.sleep(self.config.websockets_retry_delay) else: time.sleep(self.config.websockets_retry_delay) if self.config.reload_if_updated(): tmp_wapt.reload_config_if_updated() if self.socketio_client: self.socketio_client.disconnect() raise EWaptException('Configuration changed, force Websocket connection to be recreated') except Exception as e: try: # reset token self.server_authorization_token = None self.config.reload_if_updated() if self.socketio_client: self.socketio_client = None finally: logger.info(u'Exception %s, Socket IO Stopped, waiting %ss before retrying' % (repr(e),self.config.websockets_retry_delay)) time.sleep(self.config.websockets_retry_delay)
def add_ads_groups(waptconfigfile, hosts_list, wapt_server_user, wapt_server_passwd, key_password=None): # initialise wapt api with local config file wapt = Wapt(config_filename=waptconfigfile) wapt.dbpath = ':memory:' # get current packages status from repositories wapt.update(register=False, filter_on_host_cap=False) hosts_list = ensure_list(hosts_list) # get the collection of hosts from waptserver inventory all_hosts = wapt.waptserver.get( 'api/v1/hosts?columns=uuid,computer_fqdn,depends', auth=(wapt_server_user, wapt_server_passwd))['result'] if hosts_list: hosts = [h for h in all_hosts if h['computer_fqdn'] in hosts_list] else: hosts = hosts_list result = [] for h in hosts: try: hostname = h['computer_fqdn'] print('Computer %s... \r' % hostname, end='') groups = get_computer_groups(h['computer_name']) wapt_groups = h['depends'] additional = [ group for group in groups if not group in wapt_groups and wapt.is_available(group) ] if additional: # now update the host package : download and append missing packages tmpdir = mkdtemp() try: package = wapt.edit_host(hostname, target_directory=tmpdir) control = package['package'] depends = ensure_list(control.depends) control.depends = ','.join(depends + additional) control.save_control_to_wapt(package.sourcespath) buid_res = wapt.build_upload( package.sourcespath, private_key_passwd=key_password, wapt_server_user=wapt_server_user, wapt_server_passwd=wapt_server_passwd, inc_package_release=True)[0] print(" done, new packages: %s" % (','.join(additional))) if os.path.isfile(buid_res): os.remove(buid_res) result.append(hostname) finally: # cleanup of temporary if os.path.isdir(tmpdir): rmtree(tmpdir) except Exception as e: print(" error %s" % e) raise return result
def main(): jsonresult = {'output':[]} if options.json_output: # redirect output to json list old_stdout = sys.stdout old_stderr = sys.stderr sys.stderr = sys.stdout = output = JsonOutput(jsonresult['output'],logger) try: if len(args) == 0: print u"ERROR : You must provide one action to perform" parser.print_usage() sys.exit(2) action = args[0] # Config file if not os.path.isfile(config_file): logger.error(u"Error : could not find file : " + config_file + ", please check the path") logger.debug(u'Config file: %s' % config_file) mywapt = Wapt(config_filename=config_file) if options.wapt_url: mywapt.config.set('global','repo_url',options.wapt_url) global loglevel if not loglevel and mywapt.config.has_option('global','loglevel'): loglevel = mywapt.config.get('global','loglevel') setloglevel(loglevel) mywapt.options = options if options.private_key: mywapt.private_key = options.private_key if options.language: mywapt.language = options.language if options.usergroups: mywapt.usergroups = json.loads(options.usergroups.replace("'",'"')) logger.info(u'User Groups:%s' % (mywapt.usergroups,)) if options.user: mywapt.user = options.user logger.info(u'Interactive user :%s' % (mywapt.user,)) mywapt.dry_run = options.dry_run logger.debug(u'WAPT base directory : %s' % mywapt.wapt_base_dir) logger.debug(u'Package cache dir : %s' % mywapt.package_cache_dir) logger.debug(u'WAPT DB Structure version;: %s' % mywapt.waptdb.db_version) try: params_dict = {} try: params_dict = json.loads(options.params.replace("'",'"')) except: raise Exception('Installation Parameters must be in json format') # cleanup environement, remove stalled wapt-get, update install_status if action in ('install','download','remove','uninstall','update','upgrade'): running_install = mywapt.check_install_running(max_ttl=options.max_ttl) else: running_install = [] if action=='install' or action=='download': if len(args)<2: print u"You must provide at least one package name" sys.exit(1) if os.path.isdir(args[1]) or os.path.isfile(args[1]): print u"Installing WAPT file %s" % args[1] if action=='install': # abort if there is already a running install in progress if running_install: raise Exception(u'Running wapt progresses (%s), please wait...' % (running_install,)) result= {u'install':[ (args[1],mywapt.install_wapt(args[1],params_dict = params_dict))]} else: print u"%sing WAPT packages %s" % (action,','.join(args[1:])) if options.update_packages: print u"Update package list" mywapt.update() if running_install and action=='install': raise Exception(u'Running wapt processes (%s) in progress, please wait...' % (running_install,)) result = mywapt.install(args[1:],force = options.force,params_dict = params_dict, download_only= (action=='download'), usecache = not (action == 'download' and options.force) ) if options.json_output: jsonresult['result'] = result else: print u"\nResults :" if action != 'download': for k in ('install','additional','upgrade','skipped','errors'): if result.get(k,[]): print(u"\n === %s packages ===\n%s" % (k,'\n'.join( [" %-30s | %s (%s)" % (s[0],s[1].package,s[1].version) for s in result[k]]),)) else: for k in ('downloaded','skipped','errors'): if result.get('downloads', {'downloaded':[],'skipped':[],'errors':[]} )[k]: print u"\n=== %s packages ===\n%s" % (k,'\n'.join([" %s" % (s,) for s in result['downloads'][k]]),) if result['unavailable']: print(u'Critical : ') print(u' === Unavailable packages ===\n%s'% '\n'.join( [" %-30s" % s for s in result['unavailable']]) ) if mywapt.wapt_server: try: mywapt.update_server_status() except Exception,e: logger.critical('Unable to update server with current status : %s' % ensure_unicode(e)) elif action=='download': if len(args)<2: print u"You must provide at least one package name to download" sys.exit(1) if options.update_packages: print u"Update package list" mywapt.update() print u"Downloading packages %s" % (','.join(args[1:]),) result = mywapt.download_packages(args[1:],usecache = not options.force ) if options.json_output: jsonresult['result'] = result else: if result['downloaded']: print u"\nDownloaded packages : \n%s" % u"\n".join([ " %s" % p for p in result['downloaded'] ]) if result['skipped']: print u"Skipped packages : \n%s" % u"\n".join([ u" %s" % p for p in result['skipped'] ]) if result['errors']: logger.critical(u'Unable to download some files : %s'% (result['errors'],)) sys.exit(1) elif action=='show': if len(args)<2: print u"You must provide at least one package name to show" sys.exit(1) result = [] if os.path.isdir(args[1]) or os.path.isfile(args[1]): result.append[PackageEntry().load_control_from_wapt(args[1])] else: if options.update_packages: print u"Update packages list" mywapt.update() for packagename in args[1:]: result.extend(mywapt.waptdb.packages_matching(packagename)) if options.json_output: jsonresult['result'] = result else: print u"Display package control data for %s\n" % (','.join(args[1:]),) for p in result: print p.ascontrol(with_non_control_attributes=True)
'default_source_url': '', 'gpgkey': '', 'default_development_base': 'c:\tranquilit', 'default_package_prefix': 'tis', 'default_sources_suffix': 'wapt', 'default_sources_url': '', 'upload_cmd': '', 'wapt_server': '', } cp = RawConfigParser(defaults=defaults) cp.add_section('global') cp.read(config_file) if len(logger.handlers) < 1: hdlr = logging.StreamHandler(sys.stdout) hdlr.setFormatter( logging.Formatter('%(asctime)s %(levelname)s %(message)s')) logger.addHandler(hdlr) # set loglevel if loglevel in ('debug', 'warning', 'info', 'error', 'critical'): numeric_level = getattr(logging, loglevel.upper(), None) if not isinstance(numeric_level, int): raise ValueError('Invalid log level: %s' % loglevel) logger.setLevel(numeric_level) mywapt = Wapt(config=cp) mywapt.wapt_repourl = mywapt.find_wapt_server() print mywapt.wapt_repourl
def main(): if len(args) == 0: print "ERROR : You must provide one action to perform" parser.print_usage() sys.exit(2) action = args[0] # Config file if not os.path.isfile(config_file): logger.error("Error : could not find file : " + config_file + ", please check the path") logger.debug('Config file: %s' % config_file) defaults = { 'repositories': '', 'repo_url': '', 'default_source_url': '', 'private_key': '', 'public_cert': '', 'default_development_base': 'c:\tranquilit', 'default_package_prefix': 'tis', 'default_sources_suffix': 'wapt', 'default_sources_url': '', 'upload_cmd': '', 'wapt_server': '', 'loglevel': 'info', } cp = RawConfigParser(defaults=defaults) cp.add_section('global') cp.read(config_file) global loglevel if not loglevel and cp.has_option('global', 'loglevel'): loglevel = cp.get('global', 'loglevel') setloglevel(loglevel) mywapt = Wapt(config=cp) if options.wapt_url: mywapt.wapt_repourl = options.wapt_url if options.private_key: mywapt.private_key = options.private_key else: mywapt.private_key = cp.get('global', 'private_key') mywapt.dry_run = options.dry_run #logger.info("Main wapt Repository %s" % mywapt.wapt_repourl) logger.debug('WAPT base directory : %s' % mywapt.wapt_base_dir) logger.debug('Package cache dir : %s' % mywapt.packagecachedir) logger.debug('WAPT DB Structure version;: %s' % mywapt.waptdb.db_version) try: params_dict = {} try: params_dict = json.loads(options.params.replace("'", '"')) except: raise Exception('Install Parameters must be in json format') if action == 'install' or action == 'download': if len(args) < 2: print "You must provide at least one package name" sys.exit(1) if os.path.isdir(args[1]) or os.path.isfile(args[1]): print "installing WAPT file %s" % args[1] if action == 'install': mywapt.install_wapt(args[1], params_dict=params_dict) else: print "%sing WAPT packages %s" % (action, ','.join(args[1:])) if options.update_packages: print "Update package list" mywapt.update() result = mywapt.install( args[1:], force=options.force, params_dict=params_dict, download_only=(action == 'download'), ) print "\nResults :" if action <> 'download': for k in ('install', 'additional', 'upgrade', 'skipped', 'errors'): if result.get(k, []): print "\n=== %s packages ===\n%s" % ( k, '\n'.join([ " %-30s | %s (%s)" % (s[0], s[1].package, s[1].version) for s in result[k] ]), ) else: for k in ('downloaded', 'skipped', 'errors'): if result.get('downloads', { 'downloaded': [], 'skipped': [], 'errors': [] })[k]: print "\n=== %s packages ===\n%s" % ( k, '\n'.join([ " %s" % (s, ) for s in result['downloads'][k] ]), ) elif action == 'download': if len(args) < 2: print "You must provide at least one package name to download" sys.exit(1) if options.update_packages: print "Update package list" mywapt.update() print "Downloading packages %s" % (','.join(args[1:]), ) result = mywapt.download_packages(args[1:], usecache=not options.force) if result['downloaded']: print "\nDownloaded packages : \n%s" % "\n".join( [" %s" % p for p in result['downloaded']]) if result['skipped']: print "Skipped packages : \n%s" % "\n".join( [" %s" % p for p in result['skipped']]) if result['errors']: logger.critical('Unable to download some files : %s' % (result['errors'], )) sys.exit(1) elif action == 'show': if len(args) < 2: print "You must provide at least one package name to show" sys.exit(1) if os.path.isdir(args[1]) or os.path.isfile(args[1]): entry = PackageEntry().load_control_from_wapt(args[1]) print "%s" % entry else: print "Display package control data for %s\n" % (','.join( args[1:]), ) if options.update_packages: print "Update package list" mywapt.update() for packagename in args[1:]: entries = mywapt.waptdb.packages_matching(packagename) if entries: for e in entries: print "%s\n" % e.ascontrol( with_non_control_attributes=True) else: print "None packages found matching package name and version" elif action == 'show-params': if len(args) < 2: print "You must provide at one package name to show params for" sys.exit(1) for packagename in args[1:]: params = mywapt.waptdb.params(packagename) print "%s" % params elif action == 'list-registry': print "%-39s%-70s%-20s%-70s" % ('UninstallKey', 'Software', 'Version', 'Uninstallstring') print '-' * 39 + '-' * 70 + '-' * 20 + '-' * 70 for p in setuphelpers.installed_softwares(' '.join(args[1:])): print u"%-39s%-70s%-20s%-70s" % ( p['key'], p['name'], p['version'], p['uninstall_string']) elif action == 'showlog': if len(args) < 2: print "You must provide at least one package name" sys.exit(1) for packagename in args[1:]: result = mywapt.last_install_log(packagename) print "Package : %s\nStatus : %s\n\nInstallation log:\n%s" % ( packagename, result['status'], result['log']) elif action == 'remove': if len(args) < 2: print "You must provide at least one package name to remove" sys.exit(1) for packagename in args[1:]: print "Removing %s ..." % (packagename, ), mywapt.remove(packagename, force=options.force) print "done" elif action == 'session-setup': if len(args) < 2: print "You must provide at least one package to be configured in user's session" sys.exit(1) for packagename in args[1:]: print "Configuring %s ..." % (packagename, ), mywapt.session_setup(packagename, params_dict=params_dict) print "done" elif action == 'uninstall': # launch the setup.uninstall() procedure for the given packages # can be used when registering in registry a custom install with a python script if len(args) < 2: print "You must provide at least one package to be uninstalled" sys.exit(1) for packagename in args[1:]: print "uninstalling %s ..." % (packagename, ), mywapt.uninstall(packagename, params_dict=params_dict) print "uninstall done" elif action == 'update': print "Update package list" result = mywapt.update() print "Total packages : %i" % result['count'] print "Added packages : \n%s" % "\n".join( [" %s (%s)" % p for p in result['added']]) print "Removed packages : \n%s" % "\n".join( [" %s (%s)" % p for p in result['removed']]) print "Repositories URL : \n%s" % "\n".join( [" %s" % p for p in result['repos']]) elif action == 'upgradedb': (old, new) = mywapt.waptdb.upgradedb() if old == new: print "No database upgrade required, current %s, required %s" % ( old, mywapt.waptdb.curr_db_version) else: print "Old version : %s to new : %s" % (old, new) elif action == 'upgrade': if options.update_packages: print "Update package list" mywapt.update() result = mywapt.upgrade() if not result['install'] and not result[ 'additional'] and not result['upgrade'] and not result[ 'skipped']: print "Nothing to upgrade" else: for k in ('install', 'additional', 'upgrade', 'skipped', 'errors'): if result[k]: print "\n=== %s packages ===\n%s" % ( k, '\n'.join([ " %-30s | %s (%s)" % (s[0], s[1].package, s[1].version) for s in result[k] ]), ) sys.exit(0) elif action == 'list-upgrade': if options.update_packages: print "Update package list" mywapt.update() q = mywapt.list_upgrade() if not q: print "Nothing to upgrade" else: print ppdicttable([p[0] for p in q], [('package', 20), ('version', 10)]) elif action == 'download-upgrade': if options.update_packages: print "Update package list" mywapt.update() result = mywapt.download_upgrades() print "Downloaded packages : \n%s" % "\n".join( [" %s" % p for p in result['downloaded']]) print "Skipped packages : \n%s" % "\n".join( [" %s" % p for p in result['skipped']]) if result['errors']: logger.critical('Unable to download some files : %s' % (result['errors'], )) sys.exit(1) elif action == 'update-packages': if len(args) < 2: print "You must provide the directory" sys.exit(1) print update_packages(args[1]) elif action == 'sources': if len(args) < 2: print "You must provide the package name" sys.exit(1) os.startfile(mywapt.get_sources(args[1])) elif action == 'make-template': if len(args) < 2: print "You must provide the installer path" sys.exit(1) source_dir = mywapt.maketemplate(*args[1:]) print "Template created. You can build the WAPT package by launching\n %s build-package %s" % ( sys.argv[0], source_dir) os.startfile(source_dir) elif action == 'make-host-template': source_dir = mywapt.makehosttemplate(*args[1:]) print "Template created. You can build the WAPT package by launching\n %s build-package %s" % ( sys.argv[0], source_dir) os.startfile(source_dir) elif action == 'build-package': if len(args) < 2: print "You must provide at least one source directory for package build" sys.exit(1) for source_dir in [os.path.abspath(p) for p in args[1:]]: if os.path.isdir(source_dir): print('Building %s' % source_dir) result = mywapt.buildpackage( source_dir, inc_package_release=options.increlease, excludes=options.excludes.split(',')) package_fn = result['filename'] if package_fn: print "Package content:" for f in result['files']: print " %s" % f[0] print('...done. Package filename %s' % (package_fn, )) def pwd_callback(*args): """Default password callback for opening private keys""" return open(options.private_key_passwd, 'r').read() if mywapt.private_key: print('Signing %s' % package_fn) if options.private_key_passwd: signature = mywapt.signpackage( package_fn, excludes=options.excludes.split(','), callback=pwd_callback) else: signature = mywapt.signpackage( package_fn, excludes=options.excludes.split(',')) print "Package %s signed : signature :\n%s" % ( package_fn, signature) else: logger.warning( 'No private key provided, package %s is unsigned !' % package_fn) if mywapt.upload_cmd: print '\nYou can upload to repository with\n %s upload-package %s ' % ( sys.argv[0], package_fn) return 0 else: logger.critical('package not created') return 1 else: logger.critical('Directory %s not found' % source_dir) elif action == 'sign-package': if len(args) < 2: print "You must provide at least one source directory or package to sign" sys.exit(1) for waptfile in [os.path.abspath(p) for p in args[1:]]: if os.path.isdir(waptfile) or os.path.isfile(waptfile): print('Signing %s' % waptfile) signature = mywapt.signpackage( waptfile, excludes=options.excludes.split(',')) print "Package %s signed : signature :\n%s" % (waptfile, signature) else: logger.critical('Package %s not found' % waptfile) return 1 elif action == 'upload-package': if len(args) < 2: print "You must provide a package to upload" sys.exit(1) waptfiles = [] for a in args[1:]: waptfiles += glob.glob(a) waptfile_arg = " ".join(['"%s"' % f for f in waptfiles]) print setuphelpers.run(mywapt.upload_cmd % {'waptfile': waptfile_arg}) if mywapt.after_upload: print setuphelpers.run(mywapt.after_upload % {'waptfile': waptfile_arg}) elif action == 'search': if options.update_packages: print "Update package list" mywapt.update() result = mywapt.waptdb.packages_search(args[1:]) print ppdicttable(result, (('package', 30), ('version', 10), ('description', 80))) elif action == 'cleanup': result = mywapt.cleanup() print "Removed files : \n%s" % "\n".join( [" %s" % p for p in result]) elif action == 'inventory': print mywapt.inventory() elif action == 'setup-tasks': print mywapt.setup_tasks() elif action == 'list': def cb(fieldname, value): if value and fieldname == 'install_date': return value[0:16] else: return value print ppdicttable( mywapt.waptdb.installed_search(args[1:]).values(), (('package', 20), ('version', 15), ('install_status', 10), ('install_date', 16), ('description', 80)), callback=cb) else: print 'Unknown action %s' % action sys.exit(1) except Exception, e: print "FATAL ERROR : %s" % e if logger.level == logging.DEBUG: raise sys.exit(3)