예제 #1
0
def retrieve_migration_configuration(args_file):
    file = generics_utils.get_file(args_file)
    if file is None:
        raise Exception("No such file or directory: " + args_file)
    data = hammr_utils.load_data(file)

    if "migration" in data:
        migration_config = data["migration"]
        check_mandatory_migrate(migration_config)
        return migration_config
    else:
        raise Exception("no migration section found")
예제 #2
0
def retrieve_migration_configuration(args_file):
    file = generics_utils.get_file(args_file)
    if file is None:
        raise Exception("No such file or directory: " + args_file)
    data = hammr_utils.load_data(file)

    if "migration" in data:
        migration_config = data["migration"]
        check_mandatory_migrate(migration_config)
        return migration_config
    else:
        raise Exception("no migration section found")
예제 #3
0
 def from_file(path):
     credentials = Credentials()
     data = hammr_utils.load_data(path)
     if data is None:
         raise CredentialsException("Parsing error in credentials file: " + path)
     if "user" in data:
         credentials.username = data["user"]
     if "password" in data:
         credentials.password = data["password"]
     if "publickey" in data:
         credentials.publicKey = data["publickey"]
     if "secretkey" in data:
         credentials.secretKey = data["secretkey"]
     if "url" in data:
         credentials.url = data["url"]
     if "acceptAutoSigned" in data:
         credentials.sslAutosigned = data["acceptAutoSigned"]
     return credentials
예제 #4
0
 def from_file(path):
     credentials = Credentials()
     data = hammr_utils.load_data(path)
     if data is None:
         raise CredentialsException("Parsing error in credentials file: " +
                                    path)
     if "user" in data:
         credentials.username = data["user"]
     if "password" in data:
         credentials.password = data["password"]
     if "publickey" in data:
         credentials.publicKey = data["publickey"]
     if "secretkey" in data:
         credentials.secretKey = data["secretkey"]
     if "url" in data:
         credentials.url = data["url"]
     if "acceptAutoSigned" in data:
         credentials.sslAutosigned = data["acceptAutoSigned"]
     return credentials
예제 #5
0
파일: scan.py 프로젝트: thachmai/hammr
    def do_build(self, args):
        try:
            # add arguments
            doParser = self.arg_build()
            try:
                doArgs = doParser.parse_args(shlex.split(args))
            except SystemExit as e:
                return
            # --
            file = generics_utils.get_file(doArgs.file)
            if file is None:
                return 2
            data = hammr_utils.load_data(file)
            if "builders" in data:
                builders = hammr_utils.check_mandatory_builders(data["builders"])
                builders = hammr_utils.check_mandatory_generate_scan(builders)
            else:
                printer.out("no builder section found", printer.ERROR)
                return
            if builders is None:
                return
            try:
                myScannedInstances = self.api.Users(self.login).Scannedinstances.Getall(Includescans="true")
                if myScannedInstances is None or not hasattr(myScannedInstances, 'scannedInstances'):
                    printer.out("scan not found", printer.ERROR)
                    return
                else:
                    myScan = None
                    for myScannedInstance in myScannedInstances.scannedInstances.scannedInstance:
                        for scan in myScannedInstance.scans.scan:
                            if str(scan.dbId) == doArgs.id:
                                myScan = scan
                                myRScannedInstance = myScannedInstance
                                break
                        if myScan is not None:
                            break

                if myScan is not None and myScan.status.complete and not myScan.status.error and not myScan.status.cancelled:
                    i = 1
                    for builder in builders:
                        printer.out(
                            "Generating '" + builder["type"] + "' image (" + str(i) + "/" + str(len(builders)) + ")")
                        format_type = builder["type"]
                        targetFormat = generate_utils.get_target_format_object(self.api, self.login, format_type)

                        if targetFormat is None:
                            printer.out("Builder type unknown: "+format_type, printer.ERROR)
                            return 2
                        myimage = image()

                        myinstallProfile = installProfile()
                        if "swapSize" in builder["installation"]:
                            myinstallProfile.swapSize = builder["installation"]["swapSize"]
                        myinstallProfile.diskSize = builder["installation"]["diskSize"]


                        func = getattr(generate_utils, "generate_"+generics_utils.remove_special_chars(targetFormat.format.name), None)
                        if func:
                            myimage, myinstallProfile = func(myimage, builder, myinstallProfile, self.api, self.login)
                        else:
                            printer.out("Builder type unknown: "+format_type, printer.ERROR)
                            return 2

                        myimage.targetFormat = targetFormat
                        myimage.installProfile = myinstallProfile
                        rImage = self.api.Users(self.login).Scannedinstances(myRScannedInstance.dbId).Scans(
                            myScan.dbId).Images().Generate(myimage)
                        status = rImage.status
                        statusWidget = progressbar_widget.Status()
                        statusWidget.status = status
                        widgets = [Bar('>'), ' ', statusWidget, ' ', ReverseBar('<')]
                        progress = ProgressBar(widgets=widgets, maxval=100).start()
                        while not (status.complete or status.error or status.cancelled):
                            statusWidget.status = status
                            progress.update(status.percentage)
                            status = self.api.Users(self.login).Scannedinstances(myRScannedInstance.dbId).Scans(
                                myScan.dbId).Images(Sitid=rImage.dbId).Status.Get()
                            time.sleep(2)
                        statusWidget.status = status
                        progress.finish()
                        if status.error:
                            printer.out("Generation '" + builder[
                                "type"] + "' error: " + status.message + "\n" + status.errorMessage, printer.ERROR)
                            if status.detailedError:
                                printer.out(status.detailedErrorMsg)
                        elif status.cancelled:
                            printer.out("Generation '" + builder["type"] + "' canceled: " + status.message,
                                        printer.ERROR)
                        else:
                            printer.out("Generation '" + builder["type"] + "' ok", printer.OK)
                        i += 1
                else:
                    printer.out("Impossible to generate this scan", printer.ERROR)

            except KeyError as e:
                printer.out("unknown error template file", printer.ERROR)




        except ArgumentParserError as e:
            printer.out("ERROR: In Arguments: " + str(e), printer.ERROR)
            self.help_build()
        except KeyboardInterrupt:
            printer.out("\n")
            if generics_utils.query_yes_no("Do you want to cancel the job ?"):
                if 'myRScannedInstance' in locals() and 'myScan' in locals() and 'rImage' in locals() \
                        and hasattr(myRScannedInstance, 'dbId') and hasattr(myScan, 'dbId') and hasattr(rImage, 'dbId'):
                    self.api.Users(self.login).Scannedinstances(myRScannedInstance.dbId).Scans(myScan.dbId).Images(
                        Sitid=rImage.dbId).Status.Cancel()
            else:
                print "Exiting command"
        except Exception as e:
            return hammr_utils.handle_uforge_exception(e)