def print_publish_status(image_object, source, image, published_image, builder, account_name): status = published_image.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 = call_status_publish_webservice(image_object, source, image, published_image) time.sleep(2) statusWidget.status = status progress.finish() if status.error: printer.out( "Publication to '" + builder["account"]["name"] + "' error: " + status.message + "\n" + status.errorMessage, printer.ERROR) if status.detailedError: printer.out(status.detailedErrorMsg) elif status.cancelled: printer.out("\nPublication to '" + builder["account"]["name"] + "' canceled: " + status.message.printer.WARNING) else: printer.out("Publication to " + account_name + " is ok", printer.OK) published_image = image_object.get_publish_image_from_publish_id( published_image.dbId) if published_image.cloudId is not None and published_image.cloudId != "": printer.out("Cloud ID : " + published_image.cloudId)
def create_progress_bar_openstack(bar_status): bar_status.message = "Retrieving information from OpenStack" bar_status.percentage = 0 statusWidget = progressbar_widget.Status() statusWidget.status = bar_status widgets = [Bar('>'), ' ', statusWidget, ' ', ReverseBar('<')] progress = ProgressBar(widgets=widgets, maxval=100).start() progress.start() bar_status.percentage = 10 progress.update(bar_status.percentage) return progress
def handle_scan_run_status(self, my_scanned_instance, running): for current_scan in my_scanned_instance.scans.scan: if (not current_scan.status.complete and not current_scan.status.error and not current_scan.status.cancelled): scan_status = current_scan.status status_widget = progressbar_widget.Status() status_widget.status = scan_status widgets = [Bar('>'), ' ', status_widget, ' ', ReverseBar('<')] progress = ProgressBar(widgets=widgets, maxval=100).start() while not (scan_status.complete or scan_status.error or scan_status.cancelled): scan_status = self.update_scan_run_status(status_widget, scan_status, progress, my_scanned_instance, current_scan) status_widget.status = scan_status progress.finish() self.print_scan_run_result_status(scan_status) running = False break else: pass return running
def publish_builder(self, builder, template, appliance, rInstallProfile, i, comliantImage): try: if comliantImage is None: comliantImage = self.get_image_to_publish( builder, template, appliance, i) # get target format to define publish method format_type = comliantImage.targetFormat.format.name publishMethod = getattr( publish_utils, "publish_" + generics_utils.remove_special_chars(format_type), None) if publishMethod: mypImage = publishMethod(builder) if mypImage is None: return 2 else: printer.out("Builder type unknown: " + format_type, printer.ERROR) return 2 mypImage.imageUri = comliantImage.uri mypImage.applianceUri = appliance.uri mypImage.credAccount = self.get_account_to_publish(builder) account_name = mypImage.credAccount.name rpImage = self.api.Users(self.login).Appliances( appliance.dbId).Images(comliantImage.dbId).Pimages().Publish( body=mypImage, element_name="ns1:publishImage") status = rpImage.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).Appliances( appliance.dbId).Images(comliantImage.dbId).Pimages( rpImage.dbId).Status.Get() time.sleep(2) statusWidget.status = status progress.finish() if status.error: printer.out( "Publication to '" + builder["account"]["name"] + "' error: " + status.message + "\n" + status.errorMessage, printer.ERROR) if status.detailedError: printer.out(status.detailedErrorMsg) elif status.cancelled: printer.out("\nPublication to '" + builder["account"]["name"] + "' canceled: " + status.message.printer.WARNING) else: printer.out("Publication to " + account_name + " is ok", printer.OK) rpImage = self.api.Users(self.login).Appliances( appliance.dbId).Images(comliantImage.dbId).Pimages( rpImage.dbId).Get() if rpImage.cloudId is not None and rpImage.cloudId != "": printer.out("Cloud ID : " + rpImage.cloudId) return 0 except KeyboardInterrupt: printer.out("\n") if generics_utils.query_yes_no("Do you want to cancel the job ?"): if 'appliance' in locals() and 'comliantImage' in locals() and 'rpImage' in locals() \ and hasattr(appliance, 'dbId') and hasattr(comliantImage, 'dbId') and hasattr(rpImage, 'dbId'): self.api.Users(self.login).Appliances( appliance.dbId).Images(comliantImage.dbId).Pimages( rpImage.dbId).Cancel.Cancel() else: printer.out("Impossible to cancel", printer.WARNING) else: printer.out("Exiting command") raise KeyboardInterrupt
def do_build(self, args): try: #add arguments doParser = self.arg_build() doArgs = doParser.parse_args(shlex.split(args)) #if the help command is called, parse_args returns None object if not doArgs: return 2 #-- template = validate(doArgs.file) if template is None: return 2 if doArgs.id: myAppliance = self.api.Users( self.login).Appliances().Getall(Query="dbId==" + doArgs.id) myAppliance = myAppliance.appliances.appliance else: #Get template which correpond to the template file myAppliance = self.api.Users(self.login).Appliances().Getall( Query="name=='" + template["stack"]["name"] + "';version=='" + template["stack"]["version"] + "'") myAppliance = myAppliance.appliances.appliance if myAppliance is None or len(myAppliance) != 1: printer.out("No template found on the plateform") return 0 myAppliance = myAppliance[0] rInstallProfile = self.api.Users(self.login).Appliances( myAppliance.dbId).Installprofile("").Getdeprecated() if rInstallProfile is None: printer.out( "No installation found on the template '" + template["stack"]["name"] + "'", printer.ERROR) return 0 try: i = 1 if doArgs.junit is not None: test_results = [] for builder in template["builders"]: try: printer.out("Generating '" + builder["type"] + "' image (" + str(i) + "/" + str(len(template["builders"])) + ")") if doArgs.junit is not None: test = TestCase('Generation ' + builder["type"]) test_results.append(test) start_time = time.time() 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 rInstallProfile.partitionAuto: if "installation" in builder: if "swapSize" in builder["installation"]: myinstallProfile.swapSize = builder[ "installation"]["swapSize"] if "diskSize" in builder["installation"]: myinstallProfile.diskSize = builder[ "installation"]["diskSize"] else: myinstallProfile.swapSize = rInstallProfile.swapSize myinstallProfile.diskSize = rInstallProfile.partitionTable.disks.disk[ 0].size 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 if myimage is None: return 2 myimage.targetFormat = targetFormat myimage.installProfile = myinstallProfile if doArgs.simulated is not None and doArgs.simulated: myimage.simulated = True if doArgs.forced is not None and doArgs.forced: myimage.forceCheckingDeps = True rImage = self.api.Users(self.login).Appliances( myAppliance.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).Appliances( myAppliance.dbId).Images( 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) if doArgs.junit is not None: test.elapsed_sec = time.time() - start_time test.add_error_info( "Error", status.message + "\n" + status.errorMessage) elif status.cancelled: printer.out( "Generation '" + builder["type"] + "' canceled: " + status.message, printer.WARNING) if doArgs.junit is not None: test.elapsed_sec = time.time() - start_time test.add_failure_info("Canceled", status.message) else: printer.out( "Generation '" + builder["type"] + "' ok", printer.OK) printer.out("Image URI: " + rImage.uri) printer.out("Image Id : " + generics_utils.extract_id(rImage.uri)) if doArgs.junit is not None: test.elapsed_sec = time.time() - start_time #the downloadUri already contains downloadKey at the end if rImage.downloadUri is not None: test.stdout = self.api.getUrl( ) + "/" + rImage.downloadUri i += 1 except Exception as e: if is_uforge_exception(e): print_uforge_exception(e) if doArgs.junit is not None and "test_results" in locals( ) and len(test_results) > 0: test = test_results[len(test_results) - 1] test.elapsed_sec = time.time() - start_time test.add_error_info("Error", get_uforge_exception(e)) else: raise if doArgs.junit is not None: testName = myAppliance.distributionName + " " + myAppliance.archName ts = TestSuite("Generation " + testName, test_results) with open(doArgs.junit, 'w') as f: TestSuite.to_file(f, [ts], prettyprint=False) return 0 except KeyError as e: printer.out("unknown error in 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 'myAppliance' in locals() and 'rImage' in locals( ) and hasattr(myAppliance, 'dbId') and hasattr(rImage, 'dbId'): self.api.Users(self.login).Appliances( myAppliance.dbId).Images(rImage.dbId).Status.Cancel() else: printer.out("Impossible to cancel", printer.WARNING) else: printer.out("Exiting command") except Exception as e: print_uforge_exception(e) if doArgs.junit is not None and "test_results" in locals( ) and len(test_results) > 0: test = test_results[len(test_results) - 1] if "start_time" in locals(): elapse = time.time() - start_time else: elapse = 0 test.elapsed_sec = elapse test.add_error_info("Error", get_uforge_exception(e)) else: return 2 finally: if "doArgs" in locals( ) and doArgs.junit is not None and "test_results" in locals( ) and len(test_results) > 0: if "myAppliance" in locals(): testName = myAppliance.distributionName + " " + myAppliance.archName else: testName = "" ts = TestSuite("Generation " + testName, test_results) with open(doArgs.junit, 'w') as f: TestSuite.to_file(f, [ts], prettyprint=False)
def do_import(self, args): try: # add arguments doParser = self.arg_import() try: doArgs = doParser.parse_args(shlex.split(args)) 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.applianceName = doArgs.name myScanImport.applianceVersion = 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 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 = 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 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 # download scan binary uri = generics_utils.get_uforge_url_from_ws_url(self.api.getUrl()) download_url = uri + constants.URI_SCAN_BINARY if os.path.isdir(constants.TMP_WORKING_DIR): # delete tmp dir shutil.rmtree(constants.TMP_WORKING_DIR) os.mkdir(constants.TMP_WORKING_DIR) local_uforge_scan_path = constants.TMP_WORKING_DIR + os.sep + constants.SCAN_BINARY_NAME dlUtils = download_utils.Download( download_url, local_uforge_scan_path, not self.api.getDisableSslCertificateValidation()) try: dlUtils.start() except Exception, e: return 2 r_code = self.deploy_and_launch_agent(self.login, self.password, doArgs, local_uforge_scan_path, self.api.getUrl()) if r_code != 0: return # 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
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)