def do_download(self, args): try: # add arguments doParser = self.arg_download() try: doArgs = doParser.parse_args(shlex.split(args)) except SystemExit as e: return # call UForge API printer.out("Searching image with id [" + doArgs.id + "] ...", printer.INFO) images = self.get_all_images() if len(images) == 0: raise ValueError("No image available") dlImage = None for image in images: if str(image.dbId) == str(doArgs.id): dlImage = image if dlImage is not None and dlImage.status.complete and not dlImage.status.error and dlImage.compress: download_url = self.api.getUrl() + "/" + dlImage.downloadUri dlUtils = download_utils.Download( download_url, doArgs.file, not self.api.getDisableSslCertificateValidation()) try: dlUtils.start() except Exception, e: return printer.out("Image downloaded", printer.OK) elif dlImage is None: printer.out( "Unable to find the image to download in your library", printer.ERROR)
def download_binary_in_local_temp_dir(api, local_temp_dir, uri_binary, binary_name): uri = generics_utils.get_uforge_url_from_ws_url(api.getUrl()) download_url = uri + uri_binary if os.path.isdir(local_temp_dir): shutil.rmtree(local_temp_dir) os.mkdir(local_temp_dir) local_uforge_binary_path = local_temp_dir + os.sep + binary_name dlUtils = download_utils.Download( download_url, local_uforge_binary_path, not api.getDisableSslCertificateValidation()) try: dlUtils.start() return local_uforge_binary_path except Exception, e: raise Exception("Impossible to download binary [" + binary_name + "]: " + str(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