def do_delete(self, args): try: doParser = self.arg_delete() doArgs = doParser.parse_args(shlex.split(args)) if not doArgs: return 2 # call UForge API printer.out("Retrieving migration with id [" + doArgs.id + "]...") migration = self.api.Users(self.login).Migrations(doArgs.id).Get() if migration is None: printer.out("No migration available with id " + doArgs.id) return 2 print migration_utils.migration_table([migration]).draw() + "\n" if doArgs.no_confirm or generics_utils.query_yes_no( "Do you really want to delete migration with id " + doArgs.id + "?"): printer.out("Please wait...") self.api.Users(self.login).Migrations(doArgs.id).Delete() printer.out("Migration deleted", printer.OK) except ArgumentParserError as e: printer.out("ERROR: In Arguments: " + str(e), printer.ERROR) self.help_delete() except Exception as e: return hammr_utils.handle_uforge_exception(e)
def do_run(self, args): try: # add arguments do_parser = self.arg_run() do_args = do_parser.parse_args(shlex.split(args)) if not do_args: return 2 if not self.check_overlay_option_is_allowed( do_args.name, do_args.overlay): return 2 local_uforge_scan_path = hammr_utils.download_binary_in_local_temp_dir( self.api, constants.TMP_WORKING_DIR, constants.URI_SCAN_BINARY, constants.SCAN_BINARY_NAME) try: self.upload_and_launch_scan_binary(self.login, self.password, None, do_args, local_uforge_scan_path, self.api.getUrl()) except AttributeError: self.upload_and_launch_scan_binary(self.login, None, self.apikeys, do_args, local_uforge_scan_path, self.api.getUrl()) # delete tmp dir shutil.rmtree(constants.TMP_WORKING_DIR) printer.out("Searching scan on uforge ...") running = True while running: my_scanned_instances = self.api.Users( self.login).Scannedinstances.Getall(Includescans="true", Name=do_args.name) my_scanned_instances = my_scanned_instances.scannedInstances.scannedInstance if my_scanned_instances is None or not my_scanned_instances: time.sleep(5) else: if len(my_scanned_instances) > 1: printer.out("A scan with the same name already exists", printer.ERROR) my_scanned_instance = my_scanned_instances[0] if not my_scanned_instance.scans.scan: time.sleep(5) else: running = self.handle_scan_run_status( my_scanned_instance, running) except ArgumentParserError as e: printer.out("ERROR: In Arguments: " + str(e), printer.ERROR) self.help_run() except Exception as e: return hammr_utils.handle_uforge_exception(e)
def do_list(self, args): try: # call UForge API printer.out("Getting scans for [" + self.login + "] ...") myScannedInstances = self.api.Users(self.login).Scannedinstances.Getall(Includescans="true") myScannedInstances = myScannedInstances.scannedInstances.scannedInstance if myScannedInstances is None or len(myScannedInstances) == 0: printer.out("No scans available") return print scan_utils.scan_table(myScannedInstances).draw() + "\n" printer.out("Found " + str(len(myScannedInstances)) + " scans") except ArgumentParserError as e: printer.out("ERROR: In Arguments: " + str(e), printer.ERROR) self.help_list() except Exception as e: return hammr_utils.handle_uforge_exception(e)
def do_run(self, args): try: # add arguments do_parser = self.arg_run() do_args = do_parser.parse_args(shlex.split(args)) if not do_args: return 2 if not self.check_overlay_option_is_allowed(do_args.name, do_args.overlay): return 2 local_uforge_scan_path = hammr_utils.download_binary_in_local_temp_dir(self.api, constants.TMP_WORKING_DIR, constants.URI_SCAN_BINARY, constants.SCAN_BINARY_NAME) try: self.upload_and_launch_scan_binary( self.login, self.password, None, do_args, local_uforge_scan_path, self.api.getUrl()) except AttributeError: self.upload_and_launch_scan_binary( self.login, None, self.apikeys, do_args, local_uforge_scan_path, self.api.getUrl()) # delete tmp dir shutil.rmtree(constants.TMP_WORKING_DIR) printer.out("Searching scan on uforge ...") running = True while running: my_scanned_instances = self.api.Users(self.login).Scannedinstances.Getall(Includescans="true", Name=do_args.name) my_scanned_instances = my_scanned_instances.scannedInstances.scannedInstance if my_scanned_instances is None or not my_scanned_instances: time.sleep(5) else: if len(my_scanned_instances) > 1: printer.out("A scan with the same name already exists", printer.ERROR) my_scanned_instance = my_scanned_instances[0] if not my_scanned_instance.scans.scan: time.sleep(5) else: running = self.handle_scan_run_status(my_scanned_instance, running) except ArgumentParserError as e: printer.out("ERROR: In Arguments: " + str(e), printer.ERROR) self.help_run() except Exception as e: return hammr_utils.handle_uforge_exception(e)
def do_launch(self, args): try: # add arguments do_parser = self.arg_launch() do_args = do_parser.parse_args(shlex.split(args)) # if the help command is called, parse_args returns None object if not do_args: return 2 try: uforge_password = self.password except AttributeError: printer.out("Using API keys with command 'hammr migration launch' is not yet supported. Please use password.", printer.ERROR) return 2 migration_config = migration_utils.retrieve_migration_configuration(do_args.file) target_format = migration_utils.retrieve_target_format(self.api, self.login, migration_config["target"]["builder"]["type"]) image = migration_utils.retrieve_image(migration_config["target"]["builder"], target_format, self.api, self.login) if image is None: return 2 cred_account = migration_utils.retrieve_account(self.api, self.login, migration_config["target"]["builder"]["account"]["name"]) publish_image = migration_utils.build_publish_image(migration_config["target"]["builder"], target_format, cred_account) if publish_image is None: return 2 migration = self.create_migration(migration_config["name"], migration_config["os"], target_format.name, image, publish_image) self.api.Users(self.login).Migrations.Create(body=migration, element_name="ns1:migration") local_uforge_migration_path = hammr_utils.download_binary_in_local_temp_dir(self.api, constants.TMP_WORKING_DIR, constants.URI_MIGRATION_BINARY, constants.MIGRATION_BINARY_NAME) self.upload_and_launch_migration_binary(self.login, uforge_password, migration_config, local_uforge_migration_path, self.api.getUrl()) # delete temp dir shutil.rmtree(constants.TMP_WORKING_DIR) printer.out("Migration launched successfully, please go to the platform to follow steps of the migration.", printer.OK) except ArgumentParserError as e: printer.out("ERROR: In Arguments: " + str(e), printer.ERROR) self.help_launch() except Exception as e: if hammr_utils.is_uforge_exception(e): return hammr_utils.handle_uforge_exception(e) printer.out(str(e), printer.ERROR) return 0
def do_list(self, args): try: # call UForge API printer.out("Getting migrations for [" + self.login + "] ...") migrations = self.api.Users(self.login).Migrations.Getall() migrations = migrations.migrations.migration if len(migrations) == 0: printer.out("No migrations available") return print migration_utils.migration_table(migrations).draw() + "\n" except ArgumentParserError as e: printer.out("ERROR: In Arguments: " + str(e), printer.ERROR) self.help_list() except Exception as e: return hammr_utils.handle_uforge_exception(e)
def do_launch(self, args): try: # add arguments do_parser = self.arg_launch() do_args = do_parser.parse_args(shlex.split(args)) # if the help command is called, parse_args returns None object if not do_args: return 2 try: uforge_password = self.password except AttributeError: printer.out( "Using API keys with command 'hammr migration launch' is not yet supported. Please use password.", printer.ERROR) return 2 migration_config = migration_utils.retrieve_migration_configuration( do_args.file) target_format = migration_utils.retrieve_target_format( self.api, self.login, migration_config["target"]["builder"]["type"]) image = migration_utils.retrieve_image( migration_config["target"]["builder"], target_format, self.api, self.login) if image is None: return 2 cred_account = migration_utils.retrieve_account( self.api, self.login, migration_config["target"]["builder"]["account"]["name"]) publish_image = migration_utils.build_publish_image( migration_config["target"]["builder"], target_format, cred_account) if publish_image is None: return 2 migration = self.create_migration(migration_config["name"], migration_config["os"], target_format.name, image, publish_image) self.api.Users(self.login).Migrations.Create( body=migration, element_name="ns1:migration") local_uforge_migration_path = hammr_utils.download_binary_in_local_temp_dir( self.api, constants.TMP_WORKING_DIR, constants.URI_MIGRATION_BINARY, constants.MIGRATION_BINARY_NAME) self.upload_and_launch_migration_binary( self.login, uforge_password, migration_config, local_uforge_migration_path, self.api.getUrl()) # delete temp dir shutil.rmtree(constants.TMP_WORKING_DIR) printer.out( "Migration launched successfully, please go to the platform to follow steps of the migration.", printer.OK) except ArgumentParserError as e: printer.out("ERROR: In Arguments: " + str(e), printer.ERROR) self.help_launch() except Exception as e: if hammr_utils.is_uforge_exception(e): return hammr_utils.handle_uforge_exception(e) printer.out(str(e), printer.ERROR) return 0
def do_delete(self, args): try: doParser = self.arg_delete() try: doArgs = doParser.parse_args(shlex.split(args)) except SystemExit as e: return # call UForge API searchedScanType = 'scan' extraInfo = "Retrieving scan with id [" + doArgs.id + "] ..." if doArgs.scantype: if doArgs.scantype != 'scan' and doArgs.scantype != 'instance' and doArgs.scantype != 'all': printer.out( "ERROR: scantype can only be 'scan', 'instance' or 'all' not: '" + doArgs.scantype + "'", printer.ERROR) return searchedScanType = doArgs.scantype if searchedScanType != 'instance' and doArgs.scansonly: printer.out( "ERROR: 'scansonly' can only be used with 'instance' scantype but not with '" + searchedScanType + "'", printer.ERROR) return if searchedScanType == 'instance': extraInfo = "Retrieving scan instance with id [" + doArgs.id + "] ..." else: if searchedScanType == 'all': extraInfo = 'Retrieving all scan instances and associated scans' printer.out(extraInfo) myScannedInstances = self.api.Users(self.login).Scannedinstances.Getall(Includescans="true") myScannedInstances = myScannedInstances.scannedInstances.scannedInstance if myScannedInstances is None or len(myScannedInstances) == 0: printer.out("Nothing found") return if searchedScanType == 'all': print scan_utils.scan_table(myScannedInstances).draw() + "\n" if generics_utils.query_yes_no("Do you really want to delete all scan instances"): printer.out("Please wait...") self.api.Users(self.login).Scannedinstances().Deleteall() printer.out("All instances and scans deleted", printer.OK) return for myScannedInstance in myScannedInstances: if searchedScanType == 'instance' and str(myScannedInstance.dbId) == doArgs.id: print scan_utils.scan_table([myScannedInstance]).draw() + "\n" if doArgs.scansonly: extraInfo = "Do you really want to delete all scans in instance with id " + str(doArgs.id) else: extraInfo = "Do you really want to delete scan instance with id " + str( doArgs.id) + " and all associated scans" if generics_utils.query_yes_no(extraInfo): printer.out("Please wait...") if doArgs.scansonly: self.api.Users(self.login).Scannedinstances(doArgs.id).Scans().Deleteall() printer.out("Instance scans deleted", printer.OK) else: self.api.Users(self.login).Scannedinstances(doArgs.id).Delete() printer.out("Instance deleted", printer.OK) return return if searchedScanType == 'scan': for scan in myScannedInstance.scans.scan: if str(scan.dbId) == doArgs.id: print scan_utils.scan_table([myScannedInstance], scan).draw() + "\n" if generics_utils.query_yes_no( "Do you really want to delete scan with id " + str(doArgs.id)): printer.out("Please wait...") self.api.Users(self.login).Scannedinstances(myScannedInstance.dbId).Scans( doArgs.id).Delete() printer.out("Scan deleted", printer.OK) return printer.out("Scan not found", printer.ERROR) except ArgumentParserError as e: printer.out("ERROR: In Arguments: " + str(e), printer.ERROR) self.help_delete() except Exception as e: return hammr_utils.handle_uforge_exception(e)
def do_import(self, args): try: # add arguments doParser = self.arg_import() try: doArgs = doParser.parse_args(shlex.split(args)) doArgs.name = " ".join(doArgs.name) except SystemExit as e: return printer.out("Import scan id [" + doArgs.id + "] ...") myScannedInstances = self.api.Users(self.login).Scannedinstances.Getall(Includescans="true") if myScannedInstances is None or not hasattr(myScannedInstances, 'scannedInstances'): printer.out("scan not found", printer.ERROR) return else: myScan = None for myScannedInstance in myScannedInstances.scannedInstances.scannedInstance: for scan in myScannedInstance.scans.scan: if str(scan.dbId) == doArgs.id: myScan = scan myRScannedInstance = myScannedInstance break if myScan is not None: break if myScan is not None and myScan.status.complete and not myScan.status.error and not myScan.status.cancelled: myScanImport = scanImport() myScanImport.importedObjectName = doArgs.name myScanImport.importedObjectVersion = doArgs.version myScanImport.orgUri = (self.api.Users(self.login).Orgs().Getall()).orgs.org[0].uri rScanImport = self.api.Users(self.login).Scannedinstances(myRScannedInstance.dbId).Scans( myScan.dbId).Imports().Import(myScanImport) status = rScanImport.status statusWidget = progressbar_widget.Status() statusWidget.status = status widgets = [Bar('>'), ' ', statusWidget, ' ', ReverseBar('<')] progress = ProgressBar(widgets=widgets, maxval=100).start() while not (status.complete or status.error or status.cancelled): statusWidget.status = status progress.update(status.percentage) status = (self.api.Users(self.login).Scannedinstances(myRScannedInstance.dbId).Scans( myScan.dbId).Imports().Status.Get(I=rScanImport.uri)).statuses.status[0] time.sleep(2) statusWidget.status = status progress.finish() if status.error: printer.out("Importing error: " + status.message + "\n" + status.errorMessage, printer.ERROR) if status.detailedError: printer.out(status.detailedErrorMsg) elif status.cancelled: printer.out("Importing canceled: " + status.message, printer.WARNING) else: printer.out("Importing ok", printer.OK) except KeyboardInterrupt: printer.out("\n") if generics_utils.query_yes_no("Do you want to cancel the job ?"): if 'myRScannedInstance' in locals() and 'myScan' in locals() and 'rScanImport' in locals() \ and hasattr(myRScannedInstance, 'dbId') and hasattr(myScan, 'dbId') and hasattr(rScanImport, 'dbId'): self.api.Users(self.login).Scannedinstances(myRScannedInstance.dbId).Scans(myScan.dbId).Imports( rScanImport.dbId).Status.Cancel() else: printer.out("Exiting command") except ArgumentParserError as e: printer.out("ERROR: In Arguments: " + str(e), printer.ERROR) self.help_import() except Exception as e: return hammr_utils.handle_uforge_exception(e)
def do_build(self, args): try: # add arguments doParser = self.arg_build() try: doArgs = doParser.parse_args(shlex.split(args)) except SystemExit as e: return # -- file = generics_utils.get_file(doArgs.file) if file is None: return 2 data = hammr_utils.load_data(file) if "builders" in data: builders = hammr_utils.check_mandatory_builders(data["builders"]) builders = hammr_utils.check_mandatory_generate_scan(builders) else: printer.out("no builder section found", printer.ERROR) return if builders is None: return try: myScannedInstances = self.api.Users(self.login).Scannedinstances.Getall(Includescans="true") if myScannedInstances is None or not hasattr(myScannedInstances, 'scannedInstances'): printer.out("scan not found", printer.ERROR) return else: myScan = None for myScannedInstance in myScannedInstances.scannedInstances.scannedInstance: for scan in myScannedInstance.scans.scan: if str(scan.dbId) == doArgs.id: myScan = scan myRScannedInstance = myScannedInstance break if myScan is not None: break if myScan is not None and myScan.status.complete and not myScan.status.error and not myScan.status.cancelled: i = 1 for builder in builders: printer.out( "Generating '" + builder["type"] + "' image (" + str(i) + "/" + str(len(builders)) + ")") format_type = builder["type"] targetFormat = generate_utils.get_target_format_object(self.api, self.login, format_type) if targetFormat is None: printer.out("Builder type unknown: "+format_type, printer.ERROR) return 2 myimage = image() myinstallProfile = installProfile() if "swapSize" in builder["installation"]: myinstallProfile.swapSize = builder["installation"]["swapSize"] myinstallProfile.diskSize = builder["installation"]["diskSize"] func = getattr(generate_utils, "generate_"+generics_utils.remove_special_chars(targetFormat.format.name), None) if func: myimage, myinstallProfile = func(myimage, builder, myinstallProfile, self.api, self.login) else: printer.out("Builder type unknown: "+format_type, printer.ERROR) return 2 myimage.targetFormat = targetFormat myimage.installProfile = myinstallProfile rImage = self.api.Users(self.login).Scannedinstances(myRScannedInstance.dbId).Scans( myScan.dbId).Images().Generate(myimage) status = rImage.status statusWidget = progressbar_widget.Status() statusWidget.status = status widgets = [Bar('>'), ' ', statusWidget, ' ', ReverseBar('<')] progress = ProgressBar(widgets=widgets, maxval=100).start() while not (status.complete or status.error or status.cancelled): statusWidget.status = status progress.update(status.percentage) status = self.api.Users(self.login).Scannedinstances(myRScannedInstance.dbId).Scans( myScan.dbId).Images(Sitid=rImage.dbId).Status.Get() time.sleep(2) statusWidget.status = status progress.finish() if status.error: printer.out("Generation '" + builder[ "type"] + "' error: " + status.message + "\n" + status.errorMessage, printer.ERROR) if status.detailedError: printer.out(status.detailedErrorMsg) elif status.cancelled: printer.out("Generation '" + builder["type"] + "' canceled: " + status.message, printer.ERROR) else: printer.out("Generation '" + builder["type"] + "' ok", printer.OK) i += 1 else: printer.out("Impossible to generate this scan", printer.ERROR) except KeyError as e: printer.out("unknown error template file", printer.ERROR) except ArgumentParserError as e: printer.out("ERROR: In Arguments: " + str(e), printer.ERROR) self.help_build() except KeyboardInterrupt: printer.out("\n") if generics_utils.query_yes_no("Do you want to cancel the job ?"): if 'myRScannedInstance' in locals() and 'myScan' in locals() and 'rImage' in locals() \ and hasattr(myRScannedInstance, 'dbId') and hasattr(myScan, 'dbId') and hasattr(rImage, 'dbId'): self.api.Users(self.login).Scannedinstances(myRScannedInstance.dbId).Scans(myScan.dbId).Images( Sitid=rImage.dbId).Status.Cancel() else: print "Exiting command" except Exception as e: return hammr_utils.handle_uforge_exception(e)
def do_run(self, args): try: # add arguments doParser = self.arg_run() doArgs = doParser.parse_args(shlex.split(args)) #if the help command is called, parse_args returns None object if not doArgs: return 2 if not self.check_overlay_option_is_allowed( doArgs.name, doArgs.overlay): return 2 local_uforge_scan_path = hammr_utils.download_binary_in_local_temp_dir( self.api, constants.TMP_WORKING_DIR, constants.URI_SCAN_BINARY, constants.SCAN_BINARY_NAME) try: self.upload_and_launch_scan_binary(self.login, self.password, None, doArgs, local_uforge_scan_path, self.api.getUrl()) except AttributeError: self.upload_and_launch_scan_binary(self.login, None, self.apikeys, doArgs, local_uforge_scan_path, self.api.getUrl()) # delete tmp dir shutil.rmtree(constants.TMP_WORKING_DIR) printer.out("Searching scan on uforge ...") running = True while running: myScannedInstances = self.api.Users( self.login).Scannedinstances.Getall(Includescans="true", Name=doArgs.name) myScannedInstances = myScannedInstances.scannedInstances.scannedInstance if myScannedInstances is None or len(myScannedInstances) == 0: time.sleep(5) else: if len(myScannedInstances) > 1: printer.out("A scan with the same name already exists", printer.ERROR) myScannedInstance = myScannedInstances[0] if len(myScannedInstance.scans.scan) == 0: time.sleep(5) else: for scan in myScannedInstance.scans.scan: if (not scan.status.complete and not scan.status.error and not scan.status.cancelled): status = scan.status statusWidget = progressbar_widget.Status() statusWidget.status = status widgets = [ Bar('>'), ' ', statusWidget, ' ', ReverseBar('<') ] progress = ProgressBar(widgets=widgets, maxval=100).start() while not (status.complete or status.error or status.cancelled): statusWidget.status = status progress.update(status.percentage) status = (self.api.Users( self.login).Scannedinstances( myScannedInstance.dbId).Scans( scan.dbId).Get( "false", "false", "false", "false", None, None, None, None, None)).status time.sleep(2) statusWidget.status = status progress.finish() if status.error: printer.out( "Scan error: " + status.message + "\n" + status.errorMessage, printer.ERROR) if status.detailedError: printer.out(status.detailedErrorMsg) elif status.cancelled: printer.out( "Scan canceled: " + status.message, printer.WARNING) else: printer.out("Scan successfully", printer.OK) running = False break else: pass except ArgumentParserError as e: printer.out("ERROR: In Arguments: " + str(e), printer.ERROR) self.help_run() except Exception as e: return hammr_utils.handle_uforge_exception(e)