def call_deploy(image_object, publish_image, deployment): image_id = generics_utils.extract_id(publish_image.imageUri) if image_id is None or image_id == "": raise TypeError("Cannot retrieve image id from publish image") if is_uri_based_on_appliance(publish_image.imageUri): source = image_object.api.Users(image_object.login).Appliances( generics_utils.extract_id(publish_image.applianceUri)).Get() if source is None or not hasattr(source, 'dbId'): raise TypeError("No template found for this image") else: return image_object.api.Users(image_object.login).Appliances(source.dbId).Images(image_id).Pimages( publish_image.dbId).Deploys.Deploy(body=deployment, element_name="ns1:deployment") elif is_uri_based_on_scan(publish_image.imageUri): scanned_instance_id = extract_scannedinstance_id(publish_image.imageUri) scan_id = extract_scan_id(publish_image.imageUri) source = image_object.api.Users(image_object.login).Scannedinstances(scanned_instance_id).Scans(scan_id).Get() if source is None or not hasattr(source, 'dbId'): raise TypeError("No scan found for this image") else: return image_object.api.Users(image_object.login).Scannedinstances(scanned_instance_id).Scans( scan_id).Images(Itid=image_id).Pimages(publish_image.dbId).Deploys.Deploy(body=deployment, element_name="ns1:deployment") else: raise TypeError("No source found for this image")
def call_deploy(image_object, publish_image, deployment): image_id = generics_utils.extract_id(publish_image.imageUri) if image_id is None or image_id == "": raise TypeError("Cannot retrieve image id from publish image") if is_uri_based_on_appliance(publish_image.imageUri): source = image_object.api.Users(image_object.login).Appliances( generics_utils.extract_id(publish_image.parentUri)).Get() if source is None or not hasattr(source, 'dbId'): raise TypeError("No template found for this image") else: return image_object.api.Users(image_object.login).Appliances( source.dbId).Images(image_id).Pimages( publish_image.dbId).Deploys.Deploy( body=deployment, element_name="ns1:deployment") elif is_uri_based_on_scan(publish_image.imageUri): scanned_instance_id = extract_scannedinstance_id( publish_image.imageUri) scan_id = extract_scan_id(publish_image.imageUri) source = image_object.api.Users(image_object.login).Scannedinstances( scanned_instance_id).Scans(scan_id).Get() if source is None or not hasattr(source, 'dbId'): raise TypeError("No scan found for this image") else: return image_object.api.Users(image_object.login).Scannedinstances( scanned_instance_id).Scans(scan_id).Images( Itid=image_id).Pimages(publish_image.dbId).Deploys.Deploy( body=deployment, element_name="ns1:deployment") else: raise TypeError("No source found for this image")
def retrieve_credaccount_from_scan(image_object, pimageId, pimage): image_id = generics_utils.extract_id(pimage.imageUri) scannedinstance_id = extract_scannedinstance_id(pimage.imageUri) scan_id = extract_scan_id(pimage.imageUri) account_id = pimage.credAccount.dbId return image_object.api.Users(image_object.login).Scannedinstances(scannedinstance_id).Scans(scan_id).\ Images(image_id).Pimages(pimageId).Accounts(account_id).Resources.Getaccountresources()
def print_deploy_info(image_object, status, deployed_instance_id): if status.message == "on-fire": printer.out("Deployment failed", printer.ERROR) if status.detailedError: printer.out(status.detailedErrorMsg, printer.ERROR) return 1 else: printer.out("Deployment is successful", printer.OK) deployment = image_object.api.Users(image_object.login).Deployments(deployed_instance_id).Get() instance = deployment.instances.instance[-1] deployment_name = deployment.name cloud_provider = format_cloud_provider(instance.cloudProvider) location = instance.location.provider hostname=instance.hostname deployment_status = deployment.state if instance.sourceSummary and type(instance.sourceSummary) == ScanSummaryLight: source_type = "Scan" source_id = str(extract_scannedinstance_id(instance.sourceSummary.uri)) source_name = instance.sourceSummary.name elif instance.sourceSummary and type(instance.sourceSummary) == ApplianceSummary: source_type = "Template" source_id = str(generics_utils.extract_id(instance.sourceSummary.uri)) source_name = instance.sourceSummary.name else: source_type = None source_id = None source_name = None table = print_deploy_info_line(deployment_name, deployed_instance_id, cloud_provider, location, hostname, source_type, source_id, source_name, deployment_status) print table.draw() + "\n" return 0
def import_stack(self, file, isImport, isForce, rbundles, isUseMajor): try: if isImport: printer.out("Importing template from [" + file + "] archive ...") else: if constants.TMP_WORKING_DIR in str(file): printer.out("Creating template from temporary [" + file + "] archive ...") else: printer.out("Creating template from [" + file + "] archive ...") file = open(file, "r") # The following code could not be used for the moment # appImport = applianceImport() # appImport.imported = isImport # appImport.forceRw = isForce # appImport.reuseBundles = rbundles # appImport.useMajor = isUseMajor # appImport = self.api.Users(self.login).Imports.Import(appImport) appImport = self.api.Users(self.login).Imports.Import( Imported=isImport, Force=isForce, Reusebundles=rbundles, Usemajor=isUseMajor ) if appImport is None: if isImport: printer.out("error importing appliance", printer.ERROR) else: printer.out("error creating appliance", printer.ERROR) return 2 else: status = self.api.Users(self.login).Imports(appImport.dbId).Uploads.Upload(file) progress = ProgressBar(widgets=[Percentage(), Bar()], maxval=100).start() while not (status.complete or status.error): progress.update(status.percentage) status = self.api.Users(self.login).Imports(appImport.dbId).Status.Get() time.sleep(2) progress.finish() if status.error: if isImport: printer.out("Template import: " + status.message + "\n" + status.errorMessage, printer.ERROR) if status.detailedError: printer.out(status.detailedErrorMsg) else: printer.out("Template create: " + status.message + "\n" + status.errorMessage, printer.ERROR) else: if isImport: printer.out("Template import: DONE", printer.OK) else: printer.out("Template create: DONE", printer.OK) # get appliance import appImport = self.api.Users(self.login).Imports(appImport.dbId).Get() printer.out("Template URI: " + appImport.referenceUri) printer.out("Template Id : " + generics_utils.extract_id(appImport.referenceUri)) return 0 except IOError as e: printer.out("File error: " + str(e), printer.ERROR) return 2 except Exception as e: return handle_uforge_exception(e)
def do_list(self, args): try: printer.out("Getting all deployments for [" + self.login + "] ...") deployments = self.api.Users(self.login).Deployments.Getall() deployments = deployments.deployments.deployment if deployments is None or len(deployments) == 0: printer.out("No deployment available") else: printer.out("Deployments:") table = print_deploy_header() deployments = generics_utils.order_list_object_by( deployments, "name") for deployment in deployments: deployment_id = deployment.applicationId deployment_name = deployment.name deployment_status = deployment.state instances = deployment.instances.instance instance = instances[-1] source_type = source_id = source_name = hostname = location = cloud_provider = str( None) if instance: if instance.sourceSummary and type( instance.sourceSummary) == ScanSummaryLight: source_type = "Scan" source_id = str( extract_scannedinstance_id( instance.sourceSummary.uri)) source_name = instance.sourceSummary.name elif instance.sourceSummary and type( instance.sourceSummary) == ApplianceSummary: source_type = "Template" source_id = str( generics_utils.extract_id( instance.sourceSummary.uri)) source_name = instance.sourceSummary.name if instance.hostname: hostname = instance.hostname if instance.location: location = instance.location.provider if instance.cloudProvider: cloud_provider = format_cloud_provider( instance.cloudProvider) table.add_row([ deployment_name, deployment_id, cloud_provider, location, hostname, source_type, source_id, source_name, deployment_status ]) print table.draw() + "\n" printer.out("Found " + str(len(deployments)) + " deployments") return 0 except ArgumentParserError as e: printer.out("ERROR: In Arguments: " + str(e), printer.ERROR) self.help_list() except Exception as e: return handle_uforge_exception(e)
def do_cancel(self, args): try: # add arguments doParser = self.arg_cancel() try: doArgs = doParser.parse_args(shlex.split(args)) except SystemExit as e: return # call UForge API printer.out("Searching image with id [" + doArgs.id + "] ...") images = self.api.Users(self.login).Images.Getall() images = images.images.image if images is None or len(images) == 0: printer.out("No images available") else: table = Texttable(800) table.set_cols_dtype( ["t", "t", "t", "t", "t", "t", "t", "c", "t"]) table.header([ "Id", "Name", "Version", "Rev.", "Format", "Created", "Size", "Compressed", "Status" ]) cancelImage = None for image in images: if str(image.dbId) == str(doArgs.id): imgStatus = self.get_image_status(image.status) table.add_row([ image.dbId, image.name, image.version, image.revision, image.targetFormat.name, image.created.strftime("%Y-%m-%d %H:%M:%S"), size(image.size), "X" if image.compress else "", imgStatus ]) print table.draw() + "\n" cancelImage = image if cancelImage is None or cancelImage.status.complete or cancelImage.status.cancelled: printer.out( "Image not being generated, impossible to canceled", printer.ERROR) return if cancelImage is not None: if generics_utils.query_yes_no( "Do you really want to cancel image with id " + str(cancelImage.dbId)): self.api.Users(self.login).Appliances( generics_utils.extract_id( cancelImage.applianceUri)).Images( cancelImage.dbId).Status.Cancel() printer.out("Image Canceled", printer.OK) else: printer.out("Image 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 handle_uforge_exception(e)
def retrieve_source_from_image(image_object, image): if is_uri_based_on_appliance(image.uri): source = image_object.api.Users(image_object.login).Appliances( generics_utils.extract_id(image.parentUri)).Get() elif is_uri_based_on_scan(image.uri): scanned_instance_id = extract_scannedinstance_id(image.uri) scan_id = extract_scan_id(image.uri) source = image_object.api.Users(image_object.login).Scannedinstances(scanned_instance_id).Scans(scan_id).Get() return source
def retrieve_source_from_image(image_object, image): if is_uri_based_on_appliance(image.uri): source = image_object.api.Users(image_object.login).Appliances( generics_utils.extract_id(image.applianceUri)).Get() elif is_uri_based_on_scan(image.uri): scanned_instance_id = extract_scannedinstance_id(image.uri) scan_id = extract_scan_id(image.uri) source = image_object.api.Users(image_object.login).Scannedinstances(scanned_instance_id).Scans(scan_id).Get() return source
def do_list(self, args): try: # call UForge API # get images printer.out("Getting all images and publications for [" + self.login + "] ...", printer.INFO) images = self.get_all_images() if len(images) == 0 : printer.out("No image available", printer.INFO) else : printer.out("Images:") table = self.initialize_text_table(800) table.set_cols_dtype(["t", "t", "t", "t", "t", "t", "t", "t", "t"]) table.header( ["Id", "Name", "Version", "Rev.", "Format", "Created", "Size", "Compressed", "Generation Status"]) images = generics_utils.order_list_object_by(images, "name") for image in images: imgStatus = self.get_image_status(image.status) table.add_row([image.dbId, image.name, image.version, image.revision, image.targetFormat.name, image.created.strftime("%Y-%m-%d %H:%M:%S"), size(image.fileSize), "X" if image.compress else "", imgStatus]) print table.draw() + "\n" printer.out("Found " + str(len(images)) + " images", printer.INFO) # get publications publish_images = self.api.Users(self.login).Pimages.Getall() publish_images = publish_images.publishImages.publishImage if publish_images is None or len(publish_images) == 0: printer.out("No publication available", printer.INFO) return 0 printer.out("Publications:") table = self.initialize_text_table(800) table.set_cols_dtype(["t", "t", "t", "t", "t", "t", "t"]) table.header(["Template name", "Image ID", "Publish ID", "Account name", "Format", "Cloud ID", "Status"]) publish_images = generics_utils.order_list_object_by(publish_images, "name") for publish_image in publish_images: pubStatus = get_publish_status(publish_image.status) table.add_row([publish_image.name, generics_utils.extract_id(publish_image.imageUri), publish_image.dbId, publish_image.credAccount.name if publish_image.credAccount is not None else "-", publish_image.credAccount.targetPlatform.name, publish_image.cloudId if publish_image.cloudId is not None else "-", pubStatus]) print table.draw() + "\n" printer.out("Found " + str(len(publish_images)) + " publications", printer.INFO) return 0 except ArgumentParserError as e: printer.out("In Arguments: " + str(e), printer.ERROR) self.help_list() except Exception as e: return handle_uforge_exception(e)
def import_bundle(self, file, isImport): try: if isImport: printer.out("Importing bundle from ["+file+"] archive ...") else: if constants.TMP_WORKING_DIR in str(file): printer.out("Creating bundle from temporary ["+file+"] archive ...") else: printer.out("Creating bundle from ["+file+"] archive ...") file = open(file, "r") bundleImport = self.api.Users(self.login).Bundleimports.Import(Imported=isImport) if bundleImport is None: if isImport: printer.out("error importing bundle", printer.ERROR) else: printer.out("error creating bundle", printer.ERROR) return 2 else: status = self.api.Users(self.login).Bundleimports(bundleImport.dbId).Uploads.Upload(file) progress = ProgressBar(widgets=[Percentage(), Bar()], maxval=100).start() while not (status.complete or status.error): progress.update(status.percentage) status = self.api.Users(self.login).Bundleimports(bundleImport.dbId).Status.Get() time.sleep(2) progress.finish() if status.error: if isImport: printer.out("Bundle import: "+status.message+"\n"+status.errorMessage, printer.ERROR) if status.detailedError: printer.out(status.detailedErrorMsg) else: printer.out("Bundle create: "+status.message+"\n"+status.errorMessage, printer.ERROR) else: if isImport: printer.out("Bundle import: DONE", printer.OK) else: printer.out("Bundle create: DONE", printer.OK) #get bundle import bundleImport = self.api.Users(self.login).Bundleimports(bundleImport.dbId).Get() printer.out("Bundle URI: "+bundleImport.referenceUri) printer.out("Bundle Id : "+generics_utils.extract_id(bundleImport.referenceUri)) return 0 except IOError as e: printer.out("File error: "+str(e), printer.ERROR) return 2 except Exception as e: return handle_uforge_exception(e)
def do_cancel(self, args): try: # add arguments doParser = self.arg_cancel() try: doArgs = doParser.parse_args(shlex.split(args)) except SystemExit as e: return # call UForge API printer.out("Searching image with id [" + doArgs.id + "] ...") images = self.api.Users(self.login).Images.Getall() images = images.images.image if images is None or len(images) == 0: printer.out("No images available") else: table = Texttable(800) table.set_cols_dtype(["t", "t", "t", "t", "t", "t", "t", "c", "t"]) table.header(["Id", "Name", "Version", "Rev.", "Format", "Created", "Size", "Compressed", "Status"]) cancelImage = None for image in images: if str(image.dbId) == str(doArgs.id): imgStatus = self.get_image_status(image.status) table.add_row([image.dbId, image.name, image.version, image.revision, image.targetFormat.name, image.created.strftime("%Y-%m-%d %H:%M:%S"), size(image.size), "X" if image.compress else "", imgStatus]) print table.draw() + "\n" cancelImage = image if cancelImage is None or cancelImage.status.complete or cancelImage.status.cancelled: printer.out("Image not being generated, impossible to canceled", printer.ERROR) return if cancelImage is not None: if generics_utils.query_yes_no( "Do you really want to cancel image with id " + str(cancelImage.dbId)): self.api.Users(self.login).Appliances( generics_utils.extract_id(cancelImage.applianceUri)).Images( cancelImage.dbId).Status.Cancel() printer.out("Image Canceled", printer.OK) else: printer.out("Image 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 handle_uforge_exception(e)
def do_list(self, args): try: printer.out("Getting all deployments for [" + self.login + "] ...") deployments = self.api.Users(self.login).Deployments.Getall() deployments = deployments.deployments.deployment if deployments is None or len(deployments) == 0: printer.out("No deployment available") else: printer.out("Deployments:") table = print_deploy_header() deployments = generics_utils.order_list_object_by(deployments, "name") for deployment in deployments: deployment_id = deployment.applicationId deployment_name = deployment.name deployment_status = deployment.state instances = deployment.instances.instance instance = instances[-1] source_type = source_id = source_name = hostname = location = cloud_provider = str(None) if instance: if instance.sourceSummary and type(instance.sourceSummary) == ScanSummaryLight: source_type = "Scan" source_id = str(extract_scannedinstance_id(instance.sourceSummary.uri)) source_name = instance.sourceSummary.name elif instance.sourceSummary and type(instance.sourceSummary) == ApplianceSummary: source_type = "Template" source_id = str(generics_utils.extract_id(instance.sourceSummary.uri)) source_name = instance.sourceSummary.name if instance.hostname: hostname = instance.hostname if instance.location: location = instance.location.provider if instance.cloudProvider: cloud_provider = format_cloud_provider(instance.cloudProvider) table.add_row([deployment_name, deployment_id, cloud_provider, location, hostname, source_type, source_id, source_name, deployment_status]) print table.draw() + "\n" printer.out("Found " + str(len(deployments)) + " deployments") return 0 except ArgumentParserError as e: printer.out("ERROR: In Arguments: " + str(e), printer.ERROR) self.help_list() except Exception as e: return handle_uforge_exception(e)
def print_deploy_info(image_object, status, deployed_instance_id): if status.message == "on-fire": printer.out("Deployment failed", printer.ERROR) if status.detailedError: printer.out(status.detailedErrorMsg, printer.ERROR) return 1 else: printer.out("Deployment is successful", printer.OK) deployment = image_object.api.Users( image_object.login).Deployments(Did=deployed_instance_id).Get() instance = deployment.instances.instance[-1] deployment_name = deployment.name cloud_provider = format_cloud_provider(instance.cloudProvider) location = instance.location.provider hostname = instance.hostname deployment_status = deployment.state if instance.sourceSummary and type( instance.sourceSummary) == ScanSummaryLight: source_type = "Scan" source_id = str( extract_scannedinstance_id(instance.sourceSummary.uri)) source_name = instance.sourceSummary.name elif instance.sourceSummary and type( instance.sourceSummary) == ApplianceSummary: source_type = "Template" source_id = str( generics_utils.extract_id(instance.sourceSummary.uri)) source_name = instance.sourceSummary.name else: source_type = None source_id = None source_name = None table = print_deploy_info_line(deployment_name, deployed_instance_id, cloud_provider, location, hostname, source_type, source_id, source_name, deployment_status) print table.draw() + "\n" return 0
def do_publish(self, args): try: #add arguments doParser = self.arg_publish() try: doArgs = doParser.parse_args(args.split()) except SystemExit as e: return #-- file = generics_utils.get_file(doArgs.file) if file is None: return 2 template=hammr_utils.validate_json_file(file) if template is None: return try: if doArgs.id: images = self.api.Users(self.login).Images.Get() images = images.iamges.image if images is None or len(images) == 0: printer.out("No images available") else: for iimage in images: if str(iimage.dbId) == str(doArgs.id): image=iimage if image is None: printer.out("image not found", printer.ERROR) return 2 if not self.is_image_ready_to_publish(image, None): printer.out("Image with name '"+image.name+" can not be published", printer.ERROR) return 2 appliance = self.api.Users(self.login).Appliances(generics_utils.extract_id(image.applianceUri)).Get() if appliance is None or not hasattr(appliance, 'dbId'): printer.out("No template found for image", printer.ERROR) return rInstallProfile = self.api.Users(self.login).Appliances(appliance.dbId).Installprofile("").Get() if rInstallProfile is None: printer.out("No installation found on the template '"+template["stack"]["name"]+"'", printer.ERROR) return builder = self.find_builder(image, template) if builder is None: #TODO unmap image format printer.out("No builder part found for image with format type: "+str(image.format.name), printer.ERROR) return 2 self.publish_builder(builder, template, appliance, rInstallProfile, 1, image) else: #Get template which correpond to the template file appliances = self.api.Users(self.login).Appliances().Getall(Query="name=='"+template["stack"]["name"]+"';version=='"+template["stack"]["version"]+"'") appliance = appliances.appliances.appliance if appliance is None or len(appliance)!=1: printer.out("No template found on the plateform", printer.ERROR) return 0 appliance=appliance[0] rInstallProfile = self.api.Users(self.login).Appliances(appliance.dbId).Installprofile("").Get() if rInstallProfile is None: printer.out("No installation found on the template '"+template["stack"]["name"]+"'", printer.ERROR) return i=1 for builder in template["builders"]: rCode = self.publish_builder(builder, template, appliance, rInstallProfile, i, None) if rCode>=2: return i+=1 except KeyError as e: printer.out("unknown error template json file, key: "+str(e), printer.ERROR) except ArgumentParserError as e: printer.out("ERROR: In Arguments: "+str(e), printer.ERROR) self.help_publish() except KeyboardInterrupt: pass except Exception as e: return handle_uforge_exception(e)
def import_bundle(self, file, isImport): try: if isImport: printer.out("Importing bundle from [" + file + "] archive ...") else: if constants.TMP_WORKING_DIR in str(file): printer.out("Creating bundle from temporary [" + file + "] archive ...") else: printer.out("Creating bundle from [" + file + "] archive ...") file = open(file, "r") bundleImport = self.api.Users( self.login).Bundleimports.Import(Imported=isImport) if bundleImport is None: if isImport: printer.out("error importing bundle", printer.ERROR) else: printer.out("error creating bundle", printer.ERROR) return 2 else: status = self.api.Users(self.login).Bundleimports( bundleImport.dbId).Uploads.Upload(file) progress = ProgressBar(widgets=[Percentage(), Bar()], maxval=100).start() while not (status.complete or status.error): progress.update(status.percentage) status = self.api.Users(self.login).Bundleimports( bundleImport.dbId).Status.Get() time.sleep(2) progress.finish() if status.error: if isImport: printer.out( "Bundle import: " + status.message + "\n" + status.errorMessage, printer.ERROR) if status.detailedError: printer.out(status.detailedErrorMsg) else: printer.out( "Bundle create: " + status.message + "\n" + status.errorMessage, printer.ERROR) else: if isImport: printer.out("Bundle import: DONE", printer.OK) else: printer.out("Bundle create: DONE", printer.OK) #get bundle import bundleImport = self.api.Users(self.login).Bundleimports( bundleImport.dbId).Get() printer.out("Bundle URI: " + bundleImport.referenceUri) printer.out( "Bundle Id : " + generics_utils.extract_id(bundleImport.referenceUri)) return 0 except IOError as e: printer.out("File error: " + str(e), printer.ERROR) return 2 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._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 retrieve_credaccount_from_app(image_object, pimageId, pimage): image_id = generics_utils.extract_id(pimage.imageUri) source_id = generics_utils.extract_id(pimage.applianceUri) account_id = pimage.credAccount.dbId return image_object.api.Users(image_object.login).Appliances(source_id).Images(image_id).Pimages(pimageId).\ Accounts(account_id).Resources.Getaccountresources()
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 import_stack(self, file, isImport, isForce, rbundles, isUseMajor): try: if isImport: printer.out("Importing template from [" + file + "] archive ...") else: if constants.TMP_WORKING_DIR in str(file): printer.out("Creating template from temporary [" + file + "] archive ...") else: printer.out("Creating template from [" + file + "] archive ...") file = open(file, "r") # The following code could not be used for the moment # appImport = applianceImport() # appImport.imported = isImport # appImport.forceRw = isForce # appImport.reuseBundles = rbundles # appImport.useMajor = isUseMajor # appImport = self.api.Users(self.login).Imports.Import(appImport) appImport = self.api.Users(self.login).Imports.Import( Imported=isImport, Force=isForce, Reusebundles=rbundles, Usemajor=isUseMajor) if appImport is None: if isImport: printer.out("error importing appliance", printer.ERROR) else: printer.out("error creating appliance", printer.ERROR) return 2 else: status = self.api.Users(self.login).Imports( appImport.dbId).Uploads.Upload(file) progress = ProgressBar(widgets=[Percentage(), Bar()], maxval=100).start() while not (status.complete or status.error): progress.update(status.percentage) status = self.api.Users(self.login).Imports( appImport.dbId).Status.Get() time.sleep(2) progress.finish() if status.error: if isImport: printer.out( "Template import: " + status.message + "\n" + status.errorMessage, printer.ERROR) if status.detailedError: printer.out(status.detailedErrorMsg) else: printer.out( "Template create: " + status.message + "\n" + status.errorMessage, printer.ERROR) else: if isImport: printer.out("Template import: DONE", printer.OK) else: printer.out("Template create: DONE", printer.OK) #get appliance import appImport = self.api.Users(self.login).Imports( appImport.dbId).Get() printer.out("Template URI: " + appImport.referenceUri) printer.out( "Template Id : " + generics_utils.extract_id(appImport.referenceUri)) return 0 except IOError as e: printer.out("File error: " + str(e), printer.ERROR) return 2 except Exception as e: return handle_uforge_exception(e)
def retrieve_credaccount_from_app(image_object, pimageId, pimage): image_id = generics_utils.extract_id(pimage.imageUri) source_id = generics_utils.extract_id(pimage.parentUri) account_id = pimage.credAccount.dbId return image_object.api.Users(image_object.login).Appliances(source_id).Images(image_id).Pimages(pimageId).\ Accounts(account_id).Resources.Getaccountresources()
def do_list(self, args): try: # call UForge API # get images printer.out("Getting all images and publications for [" + self.login + "] ...") images = self.api.Users(self.login).Images.Getall() images = images.images.image # get publications pimages = self.api.Users(self.login).Pimages.Getall() pimages = pimages.publishImages.publishImage if images is None or len(images) == 0: printer.out("No images available") else: printer.out("Images:") table = Texttable(800) table.set_cols_dtype( ["t", "t", "t", "t", "t", "t", "t", "c", "t"]) table.header([ "Id", "Name", "Version", "Rev.", "Format", "Created", "Size", "Compressed", "Generation Status" ]) images = generics_utils.order_list_object_by(images, "name") for image in images: imgStatus = self.get_image_status(image.status) table.add_row([ image.dbId, image.name, image.version, image.revision, image.targetFormat.name, image.created.strftime("%Y-%m-%d %H:%M:%S"), size(image.size), "X" if image.compress else "", imgStatus ]) print table.draw() + "\n" printer.out("Found " + str(len(images)) + " images") if pimages is None or len(pimages) == 0: printer.out("No publication available") else: printer.out("Publications:") table = Texttable(800) table.set_cols_dtype(["t", "t", "t", "t", "t", "t"]) table.header([ "Template name", "Image ID", "Account name", "Format", "Cloud ID", "Status" ]) pimages = generics_utils.order_list_object_by(pimages, "name") for pimage in pimages: pubStatus = self.get_publish_status(pimage.status) table.add_row([ pimage.name, generics_utils.extract_id(pimage.imageUri), pimage.credAccount.name if pimage.credAccount is not None else "-", pimage.credAccount.targetPlatform.name, pimage.cloudId if pimage.cloudId is not None else "-", pubStatus ]) print table.draw() + "\n" printer.out("Found " + str(len(pimages)) + " publications") return 0 except ArgumentParserError as e: printer.out("ERROR: In Arguments: " + str(e), printer.ERROR) self.help_list() except Exception as e: return handle_uforge_exception(e)
def do_publish(self, args): try: # add arguments doParser = self.arg_publish() 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 template = validate(file) if template is None: return try: if doArgs.id: images = self.api.Users(self.login).Images.Getall() images = images.images.image if images is None or len(images) == 0: printer.out("No images available") else: for iimage in images: if str(iimage.dbId) == str(doArgs.id): image = iimage if image is None: printer.out("image not found", printer.ERROR) return 2 if not self.is_image_ready_to_publish(image, None): printer.out( "Image with name '" + image.name + " can not be published", printer.ERROR) return 2 appliance = self.api.Users(self.login).Appliances( generics_utils.extract_id(image.applianceUri)).Get() if appliance is None or not hasattr(appliance, 'dbId'): printer.out("No template found for image", printer.ERROR) return rInstallProfile = self.api.Users(self.login).Appliances( appliance.dbId).Installprofile("").Get() if rInstallProfile is None: printer.out( "No installation found on the template '" + template["stack"]["name"] + "'", printer.ERROR) return builder = self.find_builder(image, template) if builder is None: # TODO unmap image format printer.out( "No builder part found for image with format type: " + str(template["type"]), printer.ERROR) return 2 self.publish_builder(builder, template, appliance, rInstallProfile, 1, image) else: # Get template which correpond to the template file appliances = self.api.Users( self.login).Appliances().Getall( Query="name=='" + template["stack"]["name"] + "';version=='" + template["stack"]["version"] + "'") appliance = appliances.appliances.appliance if appliance is None or len(appliance) != 1: printer.out("No template found on the plateform", printer.ERROR) return 0 appliance = appliance[0] rInstallProfile = self.api.Users(self.login).Appliances( appliance.dbId).Installprofile("").Get() if rInstallProfile is None: printer.out( "No installation found on the template '" + template["stack"]["name"] + "'", printer.ERROR) return i = 1 for builder in template["builders"]: rCode = self.publish_builder(builder, template, appliance, rInstallProfile, i, None) if rCode >= 2: return i += 1 except KeyError as e: printer.out("unknown error template file, key: " + str(e), printer.ERROR) except ArgumentParserError as e: printer.out("ERROR: In Arguments: " + str(e), printer.ERROR) self.help_publish() except KeyboardInterrupt: pass except Exception as e: return handle_uforge_exception(e)