示例#1
0
def mesh_deploy(client, input_yaml_paths, parameters=None):
    """ This function
        1. Uses sfmergeutility to merge, convert, and
        order the resources
        2. Deploys the resources in the order suggested by the utility
    :param client: (class) Auto generated client from swagger specification
    :param input_yaml_paths: (str) Relative/absolute directory path or comma seperated relative/absolute file paths of the yaml resource files  # pylint: disable=line-too-long
    """
    file_path_list = []

    if os.path.isdir(input_yaml_paths):
        if not os.path.exists(input_yaml_paths):
            raise CLIError('The specified directory "%s" does not exist or you do not have access to it' % (input_yaml_paths))  # pylint: disable=line-too-long
        file_path_list = list_files_in_directory(input_yaml_paths, ".yaml")

    else:
        file_path_list = input_yaml_paths.split(',')
        for file_path in file_path_list:
            if not os.path.exists(file_path):
                raise CLIError('The specified file "%s" does not exist or you do not have access to it' % (file_path))  # pylint: disable=line-too-long

    output_dir = os.path.join(os.getcwd(), "meshDeploy")
    if os.path.exists(output_dir):
        shutil.rmtree(output_dir, ignore_errors=True)

    SFMergeUtility.sf_merge_utility(file_path_list, "SF_SBZ_JSON", parameter_file=parameters, output_dir=output_dir, prefix="")  # pylint: disable=line-too-long
    resources = list_files_in_directory(output_dir, ".json")
    resources.sort()
    for resource in resources:
        deploy_resource(client, resource)
def _invoke_mergeutil(input_yaml_files=None, parameters=None):
    # call merge utility
    file_path_list = []
    prefix = "merged-"
    output_file_path = os.path.join(os.getcwd(), prefix + 'arm_rp.json')
    if os.path.isdir(input_yaml_files):
        for root, _, files in os.walk(input_yaml_files):
            for filename in files:
                if filename.endswith(".yaml"):
                    file_path_list.append(os.path.join(root, filename))
    else:
        file_path_list = input_yaml_files.split(',')
    if os.path.exists(output_file_path):
        os.remove(output_file_path)
    SFMergeUtility.sf_merge_utility(file_path_list, "SF_SBZ_RP_JSON", parameters=parameters, output_dir=None, prefix=prefix)
    return output_file_path
示例#3
0
 def test_merge_utility_cloud(self):
     """Test if merge utility if is generating the correct ARM template"""
     yaml_file_path_list = list_files_in_directory(self.sample_yaml_path,
                                                   ".yaml")
     output_file_path = os.path.join(os.getcwd(),
                                     self.arm_template_file_name)
     SFMergeUtility.sf_merge_utility(yaml_file_path_list, "SF_SBZ_RP_JSON", parameters=None, output_dir=None, prefix="merged-", region="westus")  # pylint: disable=line-too-long
     generated_json_file_fp = open(output_file_path, "r")
     generated_json = json.load(generated_json_file_fp)
     generated_json_file_fp.close()
     actual_json_file = os.path.join(self.sample_json_path,
                                     self.arm_template_file_name)
     actual_json_file_fp = open(actual_json_file, "r")
     actual_json = json.load(actual_json_file_fp)
     actual_json_file_fp.close()
     self.assertEqual(ordered_json(generated_json),
                      ordered_json(actual_json))
     if os.path.exists(output_file_path):
         os.remove(output_file_path)
示例#4
0
def _invoke_mergeutil(input_yaml_files=None, parameters=None):
    # call merge utility
    file_path_list = []
    prefix = "merged-"
    output_file_path = os.path.join(os.getcwd(), prefix + 'arm_rp.json')
    if os.path.isdir(input_yaml_files):
        for root, _, files in os.walk(input_yaml_files):
            for filename in files:
                if filename.endswith(".yaml"):
                    file_path_list.append(os.path.join(root, filename))
    else:
        file_path_list = input_yaml_files.split(',')
    if os.path.exists(output_file_path):
        os.remove(output_file_path)
    SFMergeUtility.sf_merge_utility(file_path_list,
                                    "SF_SBZ_RP_JSON",
                                    parameters=parameters,
                                    output_dir=None,
                                    prefix=prefix)
    return output_file_path
示例#5
0
 def test_merge_utility_local(self):
     """Test if merge utility is generating the correct jsons"""
     yaml_file_path_list = list_files_in_directory(self.sample_yaml_path,
                                                   ".yaml")
     output_dir = os.path.join(os.path.dirname(__file__),
                               self.local_deployment_folder_name)
     SFMergeUtility.sf_merge_utility(yaml_file_path_list, "SF_SBZ_JSON", parameters=None, output_dir=output_dir, prefix="resource", region="westus")  # pylint: disable=line-too-long
     generated_json_files = list_files_in_directory(output_dir, ".json")
     actual_json_files = list_files_in_directory(self.sample_json_path,
                                                 ".json")
     del actual_json_files[actual_json_files.index(
         os.path.join(self.sample_json_path, self.arm_template_file_name))]
     for generated_json_file in generated_json_files:
         generated_json_file_fp = open(generated_json_file, "r")
         generated_json = json.load(generated_json_file_fp)
         generated_json_file_fp.close()
         actual_json_file = get_actual_json_file(actual_json_files,
                                                 generated_json_file)
         actual_json_file_fp = open(actual_json_file, "r")
         actual_json = json.load(actual_json_file_fp)
         actual_json_file_fp.close()
         self.assertEqual(ordered_json(generated_json),
                          ordered_json(actual_json))
     shutil.rmtree(output_dir, ignore_errors=True)
示例#6
0
def _deploy_arm_template_core(
        cli_ctx,
        resource_group_name,  # pylint: disable=too-many-arguments
        template_file=None,
        template_uri=None,
        input_yaml_files=None,
        deployment_name=None,
        parameters=None,
        mode=None,
        validate_only=False,
        no_wait=False):
    DeploymentProperties, TemplateLink = get_sdk(
        cli_ctx,
        ResourceType.MGMT_RESOURCE_RESOURCES,
        'DeploymentProperties',
        'TemplateLink',
        mod='models')
    template = None
    template_link = None
    template_obj = None
    if template_uri:
        template_link = TemplateLink(uri=template_uri)
        template_obj = shell_safe_json_parse(
            _urlretrieve(template_uri).decode('utf-8'), preserve_order=True)
    elif template_file:
        template = get_file_json(template_file, preserve_order=True)
        template_obj = template
    else:
        # call merge utility
        file_path_list = []
        prefix = "merged-"
        output_file_path = os.path.join(os.getcwd(), prefix + 'arm_rp.json')
        if os.path.isdir(input_yaml_files):
            for root, _, files in os.walk(input_yaml_files):
                for filename in files:
                    if filename.endswith(".yaml"):
                        file_path_list.append(os.path.join(root, filename))
        else:
            file_path_list = input_yaml_files.split(',')
        if os.path.exists(output_file_path):
            os.remove(output_file_path)
        SFMergeUtility.sf_merge_utility(file_path_list,
                                        "SF_SBZ_RP_JSON",
                                        parameters=parameters,
                                        output_dir=None,
                                        prefix=prefix)
        parameters = None
        template = get_file_json(output_file_path, preserve_order=True)
        template_obj = template

    template_param_defs = template_obj.get('parameters', {})
    template_obj['resources'] = template_obj.get('resources', [])

    template = json.loads(json.dumps(template))

    if parameters is not None:
        parameters = _process_parameters(template_param_defs, parameters) or {}
        parameters = _get_missing_parameters(parameters, template_obj,
                                             _prompt_for_parameters)
        parameters = json.loads(json.dumps(parameters))

    properties = DeploymentProperties(template=template,
                                      template_link=template_link,
                                      parameters=parameters,
                                      mode=mode)
    # workaround
    properties.mode = 'incremental'
    smc = get_mgmt_service_client(cli_ctx,
                                  ResourceType.MGMT_RESOURCE_RESOURCES)

    logger.warning("Deploying . . .")
    logger.warning("You can get the state of the deployment with the cmd")
    logger.warning(
        "az group deployment show --name {0} --resource-group {1}".format(
            deployment_name, resource_group_name))
    if validate_only:
        return sdk_no_wait(no_wait, smc.deployments.validate,
                           resource_group_name, deployment_name, properties)

    return sdk_no_wait(no_wait, smc.deployments.create_or_update,
                       resource_group_name, deployment_name, properties)