Пример #1
0
def retrieve_template_from_file(file):
    file = generics_utils.get_file(file)
    if not file:
        raise ArgumentParserError("Wrong file argument")
    template = validate(file)
    if not template:
        raise ValueError("Could not extract information from file")
    return template
Пример #2
0
def retrieve_template_from_file(file):
    file = generics_utils.get_file(file)
    if not file:
        raise ArgumentParserError("Wrong file argument")
    template = validate(file)
    if not template:
        raise ValueError("Could not extract information from file")
    return template
Пример #3
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")
Пример #4
0
    def do_deploy(self, args):
        try:
            # add arguments
            do_parser = self.arg_deploy()
            do_args = do_parser.parse_args(shlex.split(args))

            # if the help command is called, parse_args returns None object
            if not do_args:
                return 2

            publish_image = self.get_publish_image_from_publish_id(do_args.pid)

            if not self.is_publish_image_ready_to_deploy(publish_image):
                raise ValueError("Published image with id '" + do_args.pid +
                                 " is not ready to be deployed")

            deploy_file = generics_utils.get_file(do_args.file)
            if deploy_file is None:
                raise TypeError("Deploy file not found")

            if publish_image.targetFormat is None:
                raise TypeError("Publish image target format not found")

            target_plateform_name = publish_image.targetFormat.name
            if "Amazon" in target_plateform_name:
                return self.deploy_aws(deploy_file, publish_image)

            elif "OpenStack" in target_plateform_name:
                return self.deploy_openstack(deploy_file, publish_image)

            elif "Azure" in target_plateform_name:
                return self.deploy_azure(deploy_file, publish_image)

            else:
                printer.out(
                    "Hammr only supports deployments for Amazon AWS, OpenStack and Microsoft Azure ARM.",
                    printer.ERROR)
                return 2

        except (TypeError, ValueError) as e:
            printer.out(str(e), printer.ERROR)
            return 2

        except ArgumentParserError as e:
            printer.out("In Arguments: " + str(e), printer.ERROR)
            self.help_deploy()
        except KeyboardInterrupt:
            printer.out(
                "You have exited the command-line, however the deployment may still be in progress. Please go to the cloud's console for more information",
                printer.WARNING)
            pass
        except Exception as e:
            return handle_uforge_exception(e)
Пример #5
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")
Пример #6
0
    def do_deploy(self, args):
        try:
            # add arguments
            do_parser = self.arg_deploy()
            do_args = do_parser.parse_args(shlex.split(args))

            # if the help command is called, parse_args returns None object
            if not do_args:
                return 2

            publish_image = self.get_publish_image_from_publish_id(do_args.pid)

            if not self.is_publish_image_ready_to_deploy(publish_image):
                raise ValueError("Published image with id '" + do_args.pid + " is not ready to be deployed")

            deploy_file = generics_utils.get_file(do_args.file)
            if deploy_file is None:
                raise TypeError("Deploy file not found")

            if publish_image.targetFormat is None:
                raise TypeError("Publish image target format not found")

            target_plateform_name = publish_image.targetFormat.name
            if "Amazon" in target_plateform_name:
                return self.deploy_aws(deploy_file, publish_image)

            elif "OpenStack" in target_plateform_name:
                return self.deploy_openstack(deploy_file, publish_image)

            elif "Azure" in target_plateform_name:
                return self.deploy_azure(deploy_file, publish_image)

            else:
                printer.out("Hammr only supports deployments for Amazon AWS, OpenStack and Microsoft Azure ARM.",
                            printer.ERROR)
                return 2

        except (TypeError, ValueError) as e:
            printer.out(str(e), printer.ERROR)
            return 2

        except ArgumentParserError as e:
            printer.out("In Arguments: " + str(e), printer.ERROR)
            self.help_deploy()
        except KeyboardInterrupt:
            printer.out(
                "You have exited the command-line, however the deployment may still be in progress. Please go to the cloud's console for more information",
                printer.WARNING)
            pass
        except Exception as e:
            return handle_uforge_exception(e)
Пример #7
0
def get_account_name_for_publish(image_object, builder):
    builder_name = ""
    if builder["account"].has_key("name"):
        builder_name = builder["account"]["name"]

    elif builder["account"].has_key("file"):

        file = generics_utils.get_file(builder["account"]["file"])
        if file is None:
            return ""
        template = validate(file)
        if template is None:
            return ""

        builder_name = image_object.get_account_name_from_template(template, builder)

    return builder_name
Пример #8
0
    def get_account_name_for_publish(self, builder, account):
        builder_name = ""
        if builder["account"].has_key("name"):
            builder_name = builder["account"]["name"]

        elif builder["account"].has_key("file"):

            file = generics_utils.get_file(builder["account"]["file"])
            if file is None:
                return ""
            template = validate(file)
            if template is None:
                return ""

            builder_name = self.get_account_name_from_template(template, builder)

        return builder_name
Пример #9
0
 def do_validate(self, args):
     try:
         #add arguments
         doParser = self.arg_validate()
         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=validate_json_file(file)
         if template is None:
             return 2
         return 0
     except ArgumentParserError as e:
         printer.out("ERROR: In Arguments: "+str(e), printer.ERROR)
         self.help_validate()
Пример #10
0
    def do_validate(self, args):
        try:
            #add arguments
            doParser = self.arg_validate()
            doArgs = doParser.parse_args(args.split())

            #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_json_file(file)
            if template is None:
                return 2
            return 0
        except ArgumentParserError as e:
            printer.out("ERROR: In Arguments: "+str(e), printer.ERROR)
            self.help_validate()
Пример #11
0
    def do_validate(self, args):
        try:
            # add arguments
            doParser = self.arg_validate()
            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 = load_data(file)
            if template is None:
                return 2
            print "OK : Syntax of template file [" + realpath(file) + "] is ok"
            return 0
        except ArgumentParserError as e:
            printer.out("ERROR: In Arguments: " + str(e), printer.ERROR)
            self.help_validate()
Пример #12
0
    def do_validate(self, args):
        try:
            #add arguments
            doParser = self.arg_validate()
            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 2
            print "OK : Syntax of template file [" + realpath(file) + "] is ok"
            return 0
        except ArgumentParserError as e:
            printer.out("ERROR: In Arguments: " + str(e), printer.ERROR)
            self.help_validate()
Пример #13
0
    def do_create(self, args):
        try:
            #add arguments
            doParser = self.arg_create()
            try:
                doArgs = doParser.parse_args(args.split())
            except SystemExit as e:
                return
            #--
            #get json file (remote or local)
            file = generics_utils.get_file(doArgs.file)
            if file is None:
                return 2
            template=validate_json_file(file)
            if template is None:
                return 2
            if "builders" in template:
                template["builders"]=None
            archive_files=[]
            if "config" in template["stack"]:
                for config in template["stack"]["config"]:
                    #add to list of file to tar
                    if "source" in config:
                        file_tar_path=constants.FOLDER_CONFIGS + os.sep + generics_utils.remove_URI_forbidden_char(ntpath.basename(config["source"]))
                        archive_files.append([file_tar_path,config["source"]])
                        #changing source path to archive related source path
                        config["source"]=file_tar_path
                    else:
                        printer.out("No source file found in config", printer.ERROR)
                        return 2
            try:
                if "bundles" in template["stack"]:
                    for bundle in template["stack"]["bundles"]:
                        if "files" in bundle:
                            for files in bundle["files"]:
                                #add to list of file to tar
                                file_tar_path=constants.FOLDER_BUNDLES + os.sep + generics_utils.remove_URI_forbidden_char(bundle["name"]) + os.sep + generics_utils.remove_URI_forbidden_char(bundle["version"]) + os.sep + generics_utils.remove_URI_forbidden_char(ntpath.basename(files["source"]))
                                archive_files.append([file_tar_path,files["source"]])
                                #changing source path to archive related source path
                                files["source"]=file_tar_path
                        else:
                            printer.out("No files section found for bundle", printer.ERROR)
                            return 2
                        if "license" in bundle and "source" in bundle["license"]:
                            #add to list of file to tar
                            file_tar_path=constants.FOLDER_BUNDLES + os.sep + generics_utils.remove_URI_forbidden_char(bundle["name"]) + os.sep + generics_utils.remove_URI_forbidden_char(ntpath.basename(bundle["license"]["source"]))
                            archive_files.append([file_tar_path,bundle["license"]["source"]])
                            #changing source path to archive related source path
                            bundle["license"]["source"]=file_tar_path
            except KeyError as e:
                printer.out("Error in bundle", printer.ERROR)
                return 2
            if "source_logo" in template["stack"]:
                #add to list of file to tar
                file_tar_path=constants.FOLDER_LOGO + os.sep + generics_utils.remove_URI_forbidden_char(ntpath.basename(template["stack"]["source_logo"]))
                archive_files.append([file_tar_path,template["stack"]["source_logo"]])
                #changing source path to archive related source path
                template["stack"]["source_logo"]=file_tar_path


            if os.path.isdir(constants.TMP_WORKING_DIR):
                #delete tmp dir
                shutil.rmtree(constants.TMP_WORKING_DIR)
            os.mkdir(constants.TMP_WORKING_DIR)
            file = open(constants.TMP_WORKING_DIR + os.sep + constants.TEMPLATE_JSON_NEW_FILE_NAME, "w")
            json.dump(template, file, indent=4, separators=(',', ': '))
            file.close()
            archive_files.append([constants.TEMPLATE_JSON_FILE_NAME, constants.TMP_WORKING_DIR+ os.sep +constants.TEMPLATE_JSON_NEW_FILE_NAME])


            if doArgs.archive_path is not None:
                tar_path = doArgs.archive_path
            else:
                tar_path = constants.TMP_WORKING_DIR+os.sep+"archive.tar.gz"
            tar = tarfile.open(tar_path, "w|gz")
            for file_tar_path,file_global_path in archive_files:
                file = generics_utils.get_file(file_global_path, constants.TMP_WORKING_DIR+os.sep+os.path.basename(file_global_path))
                if file is None:
                    printer.out("Downloaded bunlde file not found", printer.ERROR)
                    return 2
                tar.add(file, arcname=file_tar_path)
            tar.close()

            #arhive is created, doing import
            r = self.import_stack(tar_path, False, doArgs.force, doArgs.rbundles, doArgs.use_major)
            if r != 0:
                return r

            #delete tmp dir
            shutil.rmtree(constants.TMP_WORKING_DIR)
            return 0
        except OSError as e:
            printer.out("OSError: "+str(e), printer.ERROR)
        except IOError as e:
            printer.out("File error: "+str(e), printer.ERROR)
        except ArgumentParserError as e:
            printer.out("In Arguments: "+str(e), printer.ERROR)
            self.help_create()
        except Exception as e:
            return handle_uforge_exception(e)
Пример #14
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)
Пример #15
0
    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)
Пример #16
0
    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

            # --
            # get file (remote or local)
            file = generics_utils.get_file(doArgs.file)
            if file is None:
                return 2
            # validate parsing and mandatory fields
            template = validate(file)
            if template is None:
                return 2
            isJsonFile = check_extension_is_json(file)

            if "builders" in template:
                template["builders"] = None
            archive_files = []
            if "config" in template["stack"]:
                for config in template["stack"]["config"]:
                    # add to list of file to tar
                    if "source" in config:
                        file_tar_path = (
                            constants.FOLDER_CONFIGS
                            + os.sep
                            + generics_utils.remove_URI_forbidden_char(ntpath.basename(config["source"]))
                        )
                        archive_files.append([file_tar_path, config["source"]])
                        # changing source path to archive related source path
                        config["source"] = file_tar_path
                    else:
                        printer.out("No source file found in config", printer.ERROR)
                        return 2
            try:
                checkList = []
                if "bundles" in template["stack"]:
                    for bundle in template["stack"]["bundles"]:
                        if "files" in bundle:
                            for files in bundle["files"]:
                                # if it's a directory
                                if os.path.isdir(files["source"]) and ntpath.basename(files["source"]) not in checkList:
                                    # add the source path to the check list
                                    checkList.append(ntpath.basename(files["source"]))
                                    # creating an archive and add it to the file_tar_path
                                    output_filename = files["name"] + ".tar.gz"
                                    file_tar_path = (
                                        constants.FOLDER_BUNDLES
                                        + os.sep
                                        + generics_utils.remove_URI_forbidden_char(bundle["name"])
                                        + os.sep
                                        + generics_utils.remove_URI_forbidden_char(bundle["version"])
                                        + os.sep
                                        + generics_utils.remove_URI_forbidden_char(output_filename)
                                    )
                                    source_dir = files["source"]
                                    with tarfile.open(output_filename, "w:gz") as tar:
                                        tar.add(source_dir, arcname=os.path.basename(source_dir))
                                        tar.close
                                    archive_files.append([file_tar_path, output_filename])
                                    # changing the name of the file
                                    files["name"] = output_filename
                                    # changing source path to archive related source path
                                    files["source"] = file_tar_path
                                elif (
                                    not os.path.isdir(files["source"])
                                    and ntpath.basename(files["source"]) not in checkList
                                ):
                                    # add the source path to the check list
                                    checkList.append(ntpath.basename(files["source"]))
                                    # add to list of file to tar
                                    file_tar_path = (
                                        constants.FOLDER_BUNDLES
                                        + os.sep
                                        + generics_utils.remove_URI_forbidden_char(bundle["name"])
                                        + os.sep
                                        + generics_utils.remove_URI_forbidden_char(bundle["version"])
                                        + os.sep
                                        + generics_utils.remove_URI_forbidden_char(ntpath.basename(files["source"]))
                                    )
                                    archive_files.append([file_tar_path, files["source"]])
                                    # changing source path to archive related source path
                                    files["source"] = file_tar_path
                                else:
                                    printer.out(
                                        "found two files with the same source path in the bundles section",
                                        printer.ERROR,
                                    )
                                    return 2
                        else:
                            printer.out("No files section found for bundle", printer.ERROR)
                            return 2
                        if "license" in bundle and "source" in bundle["license"]:
                            # add to list of file to tar
                            file_tar_path = (
                                constants.FOLDER_BUNDLES
                                + os.sep
                                + generics_utils.remove_URI_forbidden_char(bundle["name"])
                                + os.sep
                                + generics_utils.remove_URI_forbidden_char(ntpath.basename(bundle["license"]["source"]))
                            )
                            archive_files.append([file_tar_path, bundle["license"]["source"]])
                            # changing source path to archive related source path
                            bundle["license"]["source"] = file_tar_path
            except KeyError as e:
                printer.out("Error in bundle", printer.ERROR)
                return 2
            if "source_logo" in template["stack"]:
                # add to list of file to tar
                file_tar_path = (
                    constants.FOLDER_LOGO
                    + os.sep
                    + generics_utils.remove_URI_forbidden_char(ntpath.basename(template["stack"]["source_logo"]))
                )
                archive_files.append([file_tar_path, template["stack"]["source_logo"]])
                # changing source path to archive related source path
                template["stack"]["source_logo"] = file_tar_path

            if os.path.isdir(constants.TMP_WORKING_DIR):
                # delete tmp dir
                shutil.rmtree(constants.TMP_WORKING_DIR)
            os.mkdir(constants.TMP_WORKING_DIR)

            if isJsonFile:
                file = open(constants.TMP_WORKING_DIR + os.sep + constants.TEMPLATE_JSON_NEW_FILE_NAME, "w")
                json.dump(template, file, indent=4, separators=(",", ": "))
                file.close()
                archive_files.append(
                    [
                        constants.TEMPLATE_JSON_FILE_NAME,
                        constants.TMP_WORKING_DIR + os.sep + constants.TEMPLATE_JSON_NEW_FILE_NAME,
                    ]
                )
            else:
                file = open(constants.TMP_WORKING_DIR + os.sep + constants.TEMPLATE_YAML_NEW_FILE_NAME, "w")
                yaml.safe_dump(template, file, default_flow_style=False, indent=2, explicit_start="---")
                file.close()
                archive_files.append(
                    [
                        constants.TEMPLATE_YAML_FILE_NAME,
                        constants.TMP_WORKING_DIR + os.sep + constants.TEMPLATE_YAML_NEW_FILE_NAME,
                    ]
                )

            if doArgs.archive_path is not None:
                tar_path = doArgs.archive_path
            else:
                tar_path = constants.TMP_WORKING_DIR + os.sep + "archive.tar.gz"
            tar = tarfile.open(tar_path, "w|gz")
            for file_tar_path, file_global_path in archive_files:
                file = generics_utils.get_file(
                    file_global_path, constants.TMP_WORKING_DIR + os.sep + os.path.basename(file_global_path)
                )
                if file is None:
                    printer.out("Downloaded bunlde file not found", printer.ERROR)
                    return 2
                tar.add(file, arcname=file_tar_path)
            tar.close()

            # arhive is created, doing import
            r = self.import_stack(tar_path, False, doArgs.force, doArgs.rbundles, doArgs.use_major)
            if r != 0:
                return r

            # delete tmp dir
            shutil.rmtree(constants.TMP_WORKING_DIR)
            return 0
        except OSError as e:
            printer.out("OSError: " + str(e), printer.ERROR)
        except IOError as e:
            printer.out("File error: " + str(e), printer.ERROR)
        except ArgumentParserError as e:
            printer.out("In Arguments: " + str(e), printer.ERROR)
            self.help_create()
        except Exception as e:
            return handle_uforge_exception(e)
Пример #17
0
    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)
Пример #18
0
    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

            #--
            #get file (remote or local)
            file = generics_utils.get_file(doArgs.file)
            if file is None:
                return 2

            bundle = validate_bundle(file)
            if bundle is None:
                return 2
            isJsonFile= check_extension_is_json(file)
            archive_files=[]
            try:
                checkList = []
                if "files" in bundle:
                    for files in bundle["files"]:
                        checkList,archive_files = recursivelyAppendToArchive(bundle, files, "", checkList, archive_files)
                else:
                    printer.out("No files section found for bundle", printer.ERROR)
                    return 2
                if "license" in bundle and "source" in bundle["license"]:
                    #add to list of file to tar
                    file_tar_path=constants.FOLDER_BUNDLES + os.sep + generics_utils.remove_URI_forbidden_char(bundle["name"]) + os.sep + generics_utils.remove_URI_forbidden_char(ntpath.basename(bundle["license"]["source"]))
                    archive_files.append([file_tar_path,bundle["license"]["source"]])
                    #changing source path to archive related source path
                    bundle["license"]["source"]=file_tar_path
                if "sourceLogo" in bundle:
                    #add to list of file to tar
                    file_tar_path=constants.FOLDER_BUNDLES + os.sep + generics_utils.remove_URI_forbidden_char(bundle["name"]) + os.sep + generics_utils.remove_URI_forbidden_char(ntpath.basename(bundle["sourceLogo"]))
                    archive_files.append([file_tar_path,bundle["sourceLogo"]])
                    #changing source path to archive related source path
                    bundle["sourceLogo"]=file_tar_path
            except ValueError as ve:
                printer.out(str(ve), printer.ERROR)
                return 2
            except KeyError as e:
                printer.out("Error in bundle", printer.ERROR)
                return 2

            if os.path.isdir(constants.TMP_WORKING_DIR):
                #delete tmp dir
                shutil.rmtree(constants.TMP_WORKING_DIR)
            os.mkdir(constants.TMP_WORKING_DIR)

            if isJsonFile:
                fileName = constants.BUNDLE_JSON_FILE_NAME
                newFileName = constants.BUNDLE_JSON_NEW_FILE_NAME
            else:
                fileName = constants.BUNDLE_YAML_FILE_NAME
                newFileName = constants.BUNDLE_YAML_NEW_FILE_NAME

            archive_files = dump_data_in_file(bundle, archive_files, isJsonFile, fileName, newFileName)

            if doArgs.archive_path is not None:
                tar_path = doArgs.archive_path
            else:
                tar_path = constants.TMP_WORKING_DIR+os.sep+"archive.tar.gz"
            tar = tarfile.open(tar_path, "w|gz")
            for file_tar_path,file_global_path in archive_files:
                if not os.path.isdir(file_global_path):
                    file = generics_utils.get_file(file_global_path, constants.TMP_WORKING_DIR+os.sep+os.path.basename(file_global_path))
                    if file is None:
                        printer.out("Downloaded bunlde file not found", printer.ERROR)
                        return 2
                    tar.add(file, arcname=file_tar_path)
                else:
                    tar.add(file_global_path, arcname=file_tar_path)
            tar.close()

            #arhive is created, doing import
            r = self.import_bundle(tar_path, False)
            if r != 0:
                return r

            #delete tmp dir
            shutil.rmtree(constants.TMP_WORKING_DIR)
            return 0
        except OSError as e:
            printer.out("OSError: "+str(e), printer.ERROR)
        except IOError as e:
            printer.out("File error: "+str(e), printer.ERROR)
        except ArgumentParserError as e:
            printer.out("In Arguments: "+str(e), printer.ERROR)
            self.help_create()
        except Exception as e:
            return handle_uforge_exception(e)
Пример #19
0
    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)
Пример #20
0
    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)
Пример #21
0
    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)
Пример #22
0
    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

            #--
            #get file (remote or local)
            file = generics_utils.get_file(doArgs.file)
            if file is None:
                return 2
            #validate parsing and mandatory fields
            template = validate(file)
            if template is None:
                return 2
            isJsonFile = check_extension_is_json(file)

            if "builders" in template:
                template["builders"] = None
            archive_files = []
            if "config" in template["stack"]:
                for config in template["stack"]["config"]:
                    #add to list of file to tar
                    if "source" in config:
                        file_tar_path = constants.FOLDER_CONFIGS + os.sep + generics_utils.remove_URI_forbidden_char(
                            ntpath.basename(config["source"]))
                        archive_files.append([file_tar_path, config["source"]])
                        #changing source path to archive related source path
                        config["source"] = file_tar_path
                    else:
                        printer.out("No source file found in config",
                                    printer.ERROR)
                        return 2
            try:
                checkList = []
                if "bundles" in template["stack"]:
                    for bundle in template["stack"]["bundles"]:
                        if "files" in bundle:
                            for files in bundle["files"]:
                                checkList, archive_files = recursivelyAppendToArchive(
                                    bundle, files, "", checkList,
                                    archive_files)
                        else:
                            printer.out("No files section found for bundle",
                                        printer.ERROR)
                            return 2
                        if "license" in bundle and "source" in bundle[
                                "license"]:
                            #add to list of file to tar
                            file_tar_path = constants.FOLDER_BUNDLES + os.sep + generics_utils.remove_URI_forbidden_char(
                                bundle["name"]
                            ) + os.sep + generics_utils.remove_URI_forbidden_char(
                                ntpath.basename(bundle["license"]["source"]))
                            archive_files.append(
                                [file_tar_path, bundle["license"]["source"]])
                            #changing source path to archive related source path
                            bundle["license"]["source"] = file_tar_path
                        if "sourceLogo" in bundle:
                            #add to list of file to tar
                            file_tar_path = constants.FOLDER_BUNDLES + os.sep + generics_utils.remove_URI_forbidden_char(
                                bundle["name"]
                            ) + os.sep + generics_utils.remove_URI_forbidden_char(
                                ntpath.basename(bundle["sourceLogo"]))
                            archive_files.append(
                                [file_tar_path, bundle["sourceLogo"]])
                            #changing source path to archive related source path
                            bundle["sourceLogo"] = file_tar_path
            except ValueError as ve:
                printer.out(str(ve), printer.ERROR)
                return 2
            except KeyError as e:
                printer.out("Error in bundle", printer.ERROR)
                return 2
            if "source_logo" in template["stack"]:
                #add to list of file to tar
                file_tar_path = constants.FOLDER_LOGO + os.sep + generics_utils.remove_URI_forbidden_char(
                    ntpath.basename(template["stack"]["source_logo"]))
                archive_files.append(
                    [file_tar_path, template["stack"]["source_logo"]])
                #changing source path to archive related source path
                template["stack"]["source_logo"] = file_tar_path

            if os.path.isdir(constants.TMP_WORKING_DIR):
                #delete tmp dir
                shutil.rmtree(constants.TMP_WORKING_DIR)
            os.mkdir(constants.TMP_WORKING_DIR)

            if isJsonFile:
                fileName = constants.TEMPLATE_JSON_FILE_NAME
                newFileName = constants.TEMPLATE_JSON_NEW_FILE_NAME
            else:
                fileName = constants.TEMPLATE_YAML_FILE_NAME
                newFileName = constants.TEMPLATE_YAML_NEW_FILE_NAME

            archive_files = dump_data_in_file(template, archive_files,
                                              isJsonFile, fileName,
                                              newFileName)

            if doArgs.archive_path is not None:
                tar_path = doArgs.archive_path
            else:
                tar_path = constants.TMP_WORKING_DIR + os.sep + "archive.tar.gz"
            tar = tarfile.open(tar_path, "w|gz")
            for file_tar_path, file_global_path in archive_files:
                if not os.path.isdir(file_global_path):
                    file = generics_utils.get_file(
                        file_global_path, constants.TMP_WORKING_DIR + os.sep +
                        os.path.basename(file_global_path))
                    if file is None:
                        printer.out("Downloaded bunlde file not found",
                                    printer.ERROR)
                        return 2
                    tar.add(file, arcname=file_tar_path)
                else:
                    tar.add(file_global_path, arcname=file_tar_path)
            tar.close()

            #arhive is created, doing import
            r = self.import_stack(tar_path, False, doArgs.force,
                                  doArgs.rbundles, doArgs.use_major)
            if r != 0:
                return r

            #delete tmp dir
            shutil.rmtree(constants.TMP_WORKING_DIR)
            return 0
        except OSError as e:
            printer.out("OSError: " + str(e), printer.ERROR)
        except IOError as e:
            printer.out("File error: " + str(e), printer.ERROR)
        except ArgumentParserError as e:
            printer.out("In Arguments: " + str(e), printer.ERROR)
            self.help_create()
        except Exception as e:
            return handle_uforge_exception(e)