def retrieve_account(api, login, account_name): cred_accounts = api.Users(login).Accounts.Getall() if not cred_accounts: raise Exception( "No CredAccounts available.\n You can use the command 'hammr account create' to create an account." ) cred_accounts = cred_accounts.credAccounts.credAccount if cred_accounts is None or len(cred_accounts) == 0: raise Exception( "No CredAccounts available.\n You can use the command 'hammr account create' to create an account." ) account_retrieved = None for cred_account in cred_accounts: if cred_account.name == account_name: account_retrieved = cred_account break if not account_retrieved: raise Exception( "CredAccount unknown: " + account_name + "\n You can use the command 'hammr account create' to create an account." ) create_account_method = getattr( account_utils, generics_utils.remove_special_chars(cred_account.targetPlatform.type), None) cred_account = create_account_method() cred_account.name = account_name cred_account.uri = account_retrieved.uri return cred_account
def build_publish_image(builder, target_format, cred_account): create_publish_image_method = getattr(publish_builders, "publish_" + generics_utils.remove_special_chars(target_format.format.name), None) if create_publish_image_method: publish_image = create_publish_image_method(builder, cred_account) publish_image.credAccount = cred_account return publish_image raise Exception("TargetFormat type is unsupported: " + target_format.format.name)
def retrieve_publish_image(builder, target_format): create_publish_image_method = getattr( publish_builders, "publish_" + generics_utils.remove_special_chars(target_format.format.name), None) if create_publish_image_method: return create_publish_image_method(builder) raise Exception("TargetFormat type is unsupported: " + target_format.format.name)
def retrieve_publish_image_with_target_format_builder(self, image, builder): format_type = image.targetFormat.format.name publish_method = getattr(publish_builders, "publish_" + generics_utils.remove_special_chars(format_type), None) if publish_method: publish_image = publish_method(builder) if publish_image is None: raise ValueError("Could not find the builder") else: raise ValueError("Builder type unknown: " + format_type) return publish_image
def build_publish_image(self, image, builder, cred_account): format_type = image.targetFormat.format.name publish_method = getattr(publish_builders, "publish_" + generics_utils.remove_special_chars(format_type), None) if publish_method: publish_image = publish_method(builder, cred_account) if publish_image is None: raise ValueError("Could not find the builder") else: raise ValueError("Builder type unknown: " + format_type) publish_image.credAccount = cred_account return publish_image
def retrieve_image(builder, target_format, api, login): image_format_name = target_format.format.name check_mandatory_installation(image_format_name, builder) create_image_method = getattr(generate_utils, "generate_" + generics_utils.remove_special_chars(image_format_name), None) if create_image_method: install_profile = uforge.installProfile() install_profile.diskSize = 0 image, install_profile = create_image_method(uforge.Image(), builder, install_profile, api, login) install_profile = set_install_profile_disk_size(install_profile, builder, image_format_name) image.installProfile = install_profile return image raise Exception("TargetFormat type is unsupported: " + target_format.format.name)
def retrieve_publish_image_with_target_format_builder( self, image, builder): format_type = image.targetFormat.format.name publish_method = getattr( publish_builders, "publish_" + generics_utils.remove_special_chars(format_type), None) if publish_method: publish_image = publish_method(builder) if publish_image is None: raise ValueError("Could not find the builder") else: raise ValueError("Builder type unknown: " + format_type) return publish_image
def build_publish_image(self, image, builder, cred_account): format_type = image.targetFormat.format.name publish_method = getattr( publish_builders, "publish_" + generics_utils.remove_special_chars(format_type), None) if publish_method: publish_image = publish_method(builder, cred_account) if publish_image is None: raise ValueError("Could not find the builder") else: raise ValueError("Builder type unknown: " + format_type) publish_image.credAccount = cred_account return publish_image
def retrieve_account(api, login, account_name): cred_accounts = api.Users(login).Accounts.Getall() if not cred_accounts: raise Exception("No CredAccounts available.\n You can use the command 'hammr account create' to create an account.") cred_accounts = cred_accounts.credAccounts.credAccount if cred_accounts is None or len(cred_accounts) == 0: raise Exception("No CredAccounts available.\n You can use the command 'hammr account create' to create an account.") account_retrieved = None for cred_account in cred_accounts: if cred_account.name == account_name: account_retrieved = cred_account break if not account_retrieved: raise Exception("CredAccount unknown: " + account_name + "\n You can use the command 'hammr account create' to create an account.") create_account_method = getattr(account_utils, generics_utils.remove_special_chars(cred_account.targetPlatform.type), None) cred_account = create_account_method() cred_account.name = account_name cred_account.uri = account_retrieved.uri return cred_account
def publish_builder(self, builder, template, appliance, rInstallProfile, i, comliantImage): try: if comliantImage is None: printer.out("Publishing '"+builder["type"]+"' image ("+str(i)+"/"+str(len(template["builders"]))+")") images = self.api.Users(self.login).Appliances(appliance.dbId).Images.Getall(Query="targetFormat.name=='"+builder["type"]+"'") images = images.images.image if images is None or len(images) == 0: printer.out("No images found for the template '"+template["stack"]["name"]+"' with type: "+builder["type"], printer.ERROR) return 2 compliantImages = [] for image in images: isOk = self.is_image_ready_to_publish(image, builder) if isOk: compliantImages.append(image) if(len(compliantImages)==0): printer.out("No images found for the template '"+template["stack"]["name"]+"' with type: "+builder["type"], printer.ERROR) return 2 elif(len(compliantImages)==1): comliantImage=compliantImages[0] else: #TODO get the last created image comliantImage=compliantImages[0] else: #publishing image in param --id printer.out("Publishing '"+builder["type"]+"' image ("+str(i)+"/"+str(1)+")") mypImage = publishImage() mypImage.imageUri = comliantImage.uri mypImage.applianceUri = appliance.uri if not "account" in builder: printer.out("Missing account section on builder: ["+builder["type"]+"]", printer.ERROR) return 2 #Get all cloud account on the plateform (for the user) accounts = self.api.Users(self.login).Accounts.Getall() accounts = accounts.credAccounts.credAccount if accounts is None or len(accounts) == 0: printer.out("No accounts available on the plateform", printer.ERROR) return 2 else: for account in accounts: if account.name == builder["account"]["name"]: #TODO refactor this account.uri=None account.parentUri=None account.dbId=None account.created=None account.lastModified=None account.digest=None mypImage.credAccount = account break if mypImage.credAccount is None: printer.out("No accounts available with name " +builder["account"]["name"], printer.ERROR) return 2 format_type = comliantImage.targetFormat.format.name func = getattr(publish_utils, "publish_"+generics_utils.remove_special_chars(format_type), None) if func: mypImage = func(mypImage, builder) else: printer.out("Builder type unknown: "+format_type, printer.ERROR) return 2 if mypImage is None: return 2 rpImage = self.api.Users(self.login).Appliances(appliance.dbId).Images(comliantImage.dbId).Pimages().Publish(mypImage) 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 '"+builder["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 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._url + "/" + 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 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_create(self, args): try: # add arguments doParser = self.arg_create() doArgs = doParser.parse_args(shlex.split(args)) #if the help command is called, parse_args returns None object if not doArgs: return 2 # -- file = generics_utils.get_file(doArgs.file) if file is None: return 2 data = load_data(file) if data is None: return 2 if "builders" in data: accounts_file_type = "builders" iterables = check_mandatory_create_account(data["builders"], accounts_file_type) elif "accounts" in data: accounts_file_type = "accounts" iterables = check_mandatory_create_account(data["accounts"], accounts_file_type) else: printer.out("Error: no builders or accounts section found", printer.ERROR) return 2 if iterables is None: return 2 try: for iterable in iterables: myCredAccount = None if "account" in iterable: account_type = iterable["account"]["type"] elif "type" in iterable: account_type = iterable["type"] targetPlatform = account_utils.get_target_platform_object(self.api, self.login, account_type) if targetPlatform is None: printer.out("Platform type unknown: "+account_type, printer.ERROR) return 2 func = getattr(account_utils, "fill_" + generics_utils.remove_special_chars(targetPlatform.type), None) if func: if accounts_file_type == "builders" and "account" in iterable: myCredAccount = func(iterable["account"]) elif accounts_file_type == "accounts": myCredAccount = func(iterable) else: pass # DO NOTHING - no account in builder to create # TODO # the account type must be in the account part, if no builder part (independant file) if myCredAccount is not None: myCredAccount = account_utils.assign_target_platform_account(myCredAccount, targetPlatform.name) printer.out("Create account for '" + account_type + "'...") self.api.Users(self.login).Accounts.Create(body=myCredAccount, element_name="ns1:credAccount") printer.out("Account create successfully for [" + account_type + "]", printer.OK) return 0 except KeyError as e: printer.out("unknown error template file", printer.ERROR) except IOError as e: printer.out("File error: " + str(e), printer.ERROR) except ArgumentParserError as e: printer.out("ERROR: In Arguments: " + str(e), printer.ERROR) self.help_create() 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 retrieve_publish_image(builder, target_format): create_publish_image_method = getattr(publish_builders, "publish_" + generics_utils.remove_special_chars(target_format.format.name), None) if create_publish_image_method: return create_publish_image_method(builder) raise Exception("TargetFormat type is unsupported: " + target_format.format.name)
def do_create(self, args): try: # add arguments doParser = self.arg_create() doArgs = doParser.parse_args(shlex.split(args)) #if the help command is called, parse_args returns None object if not doArgs: return 2 # -- file = generics_utils.get_file(doArgs.file) if file is None: return 2 data = load_data(file) if data is None: return 2 if "builders" in data: accounts_file_type = "builders" iterables = check_mandatory_create_account( data["builders"], accounts_file_type) elif "accounts" in data: accounts_file_type = "accounts" iterables = check_mandatory_create_account( data["accounts"], accounts_file_type) else: printer.out("Error: no builders or accounts section found", printer.ERROR) return 2 if iterables is None: return 2 try: for iterable in iterables: myCredAccount = None if "account" in iterable: account_type = iterable["account"]["type"] elif "type" in iterable: account_type = iterable["type"] targetPlatform = account_utils.get_target_platform_object( self.api, self.login, account_type) if targetPlatform is None: printer.out("Platform type unknown: " + account_type, printer.ERROR) return 2 func = getattr( account_utils, generics_utils.remove_special_chars( targetPlatform.type), None) if func: if accounts_file_type == "builders" and "account" in iterable: myCredAccount = func(iterable["account"]) elif accounts_file_type == "accounts": myCredAccount = func(iterable) else: pass # DO NOTHING - no account in builder to create # TODO # the account type must be in the account part, if no builder part (independant file) if myCredAccount is not None: myCredAccount = account_utils.assign_target_platform_account( myCredAccount, targetPlatform.name) printer.out("Create account for '" + account_type + "'...") self.api.Users(self.login).Accounts.Create( body=myCredAccount, element_name="ns1:credAccount") printer.out( "Account create successfully for [" + account_type + "]", printer.OK) return 0 except KeyError as e: printer.out("unknown error template file", printer.ERROR) except IOError as e: printer.out("File error: " + str(e), printer.ERROR) except ArgumentParserError as e: printer.out("ERROR: In Arguments: " + str(e), printer.ERROR) self.help_create() except Exception as e: return handle_uforge_exception(e)
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_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)