Пример #1
0
    def load_template_type(self, template_path, type_name):
        template_type_path = os.path.join(template_path, type_name)
        for package_name in os.listdir(template_type_path):
            template_package_path = os.path.join(template_type_path, package_name)
            template_package_home = os.path.join(self.template_dir, type_name, package_name)

            create_dir(template_package_home)

            for file_info in get_files(template_package_path):
                if len(file_info) == 2 and file_info[1] == 'index.yml':
                    package_module_config = load_yaml(os.path.join(*file_info))
                    template_home_index = os.path.join(template_package_home, 'index.yml')
                    package_home_config = load_yaml(template_home_index)

                    if package_home_config is None:
                        package_home_config = package_module_config
                    else:
                        package_home_config = deep_merge(package_home_config, package_module_config)

                    save_yaml(template_home_index, package_home_config)
                else:
                    if file_info[1:-1]:
                        create_dir(os.path.join(*[ template_package_home, *file_info[1:-1] ]))

                    if file_info[1:]:
                        copyfile(
                            os.path.join(*file_info),
                            os.path.join(*[ template_package_home, *file_info[1:] ])
                        )
Пример #2
0
 def load_data(self, reset = False):
     if reset or not self.data:
         save_data = False
         with self.lock:
             self.data = load_yaml(settings.RUNTIME_PATH)
             if self.data is None:
                 time = self.time.now
                 self.data = {
                     'active': settings.DEFAULT_ENV_NAME,
                     'environments': {
                         settings.DEFAULT_ENV_NAME: {
                             'repo': settings.DEFAULT_RUNTIME_REPO,
                             'base_image': settings.DEFAULT_RUNTIME_IMAGE,
                             'created': time,
                             'updated': time
                         }
                     }
                 }
                 save_data = True
             else:
                 for name, config in self.data['environments'].items():
                     self.data['environments'][name]['created'] = self.time.to_datetime(config['created'])
                     self.data['environments'][name]['updated'] = self.time.to_datetime(config['updated'])
         if save_data:
             self.save_data()
Пример #3
0
            def load_directory(base_path):
                if settings.APP_DIR in base_path:
                    module = 'core'
                    module_path = settings.APP_DIR
                else:
                    module = base_path.replace(settings.MODULE_BASE_PATH + '/',
                                               '').split('/')[1]
                    module_path = os.path.join(self.manager.module_dir, module)

                module_info = Collection(
                    module=module, path=self._get_module_lib_dir(module_path))

                for name in os.listdir(base_path):
                    file_path = os.path.join(base_path, name)
                    if os.path.isdir(file_path):
                        load_directory(file_path)

                    elif name[0] != '_' and re.match(r'^[^\.]+\.(yml|yaml)$',
                                                     name, re.IGNORECASE):
                        logger.debug(
                            "Loading specification from file: {}".format(
                                file_path))
                        spec_data = load_yaml(file_path)

                        if spec_data:
                            for key, info in spec_data.items():
                                if key[0] != '_':
                                    self.module_map.setdefault(key, {})
                                    if key == 'roles':
                                        for name, description in info.items():
                                            self.module_map[key][
                                                name] = module_info
                                    else:
                                        for name, spec in info.items():
                                            if key == 'command':
                                                set_command_module(
                                                    module, spec)
                                            else:
                                                app_name = spec.get(
                                                    'app', name)
                                                self.module_map[key][
                                                    app_name] = module_info

                                                if key in ('data', 'data_base',
                                                           'data_mixins'):
                                                    module_name = model_index.get_module_name(
                                                        key, app_name)
                                                    model_class = model_index.get_model_name(
                                                        name, spec)
                                                    dynamic_class = model_index.get_dynamic_class_name(
                                                        model_class)

                                                    self.model_class_path[
                                                        model_class] = module_name
                                                    self.model_class_path[
                                                        dynamic_class] = module_name

                            self._spec = deep_merge(self._spec, spec_data)
Пример #4
0
 def load_inner(data, help_path):
     for name in os.listdir(help_path):
         path = os.path.join(help_path, name)
         if os.path.isfile(path):
             if path.endswith('.yml'):
                 file_data = load_yaml(path)
                 data = deep_merge(data, file_data)
         else:
             load_inner(data, path)
Пример #5
0
    def save_module_config(self, module_name, config):
        module_path = os.path.join(self.manager.module_dir, module_name)
        zimagi_config_path = os.path.join(module_path, '.zimagi.yml')
        loaded_config = {}

        if os.path.isfile(zimagi_config_path):
            loaded_config = load_yaml(zimagi_config_path)
            if not isinstance(loaded_config, dict):
                loaded_config = {}

        config = deep_merge(loaded_config, config)
        save_yaml(zimagi_config_path, config)
Пример #6
0
    def get_module_name(self, instance):
        with temp_dir() as temp:
            temp_module_path = "{}/module".format(temp.base_path)
            repository = pygit2.clone_repository(instance.remote, temp_module_path,
                checkout_branch = instance.reference,
                callbacks = self._get_credentials(instance, temp)
            )
            config = load_yaml("{}/zimagi.yml".format(temp_module_path))

            if not isinstance(config, dict) or 'name' not in config:
                self.command.error("Module configuration required for {} at {}".format(instance.remote, instance.reference))

        return config['name']
Пример #7
0
    def _get_module_config(self, path):
        if path not in self.module_index:
            self.module_index[path] = {}

            for config_file in ('zimagi.yml', '.zimagi.yml'):
                zimagi_config = os.path.join(path, config_file)
                if os.path.isfile(zimagi_config):
                    self.module_index[path] = deep_merge(
                        self.module_index[path], load_yaml(zimagi_config))

            if not self.module_index.get(path, None):
                self.module_index[path] = {'lib': '.'}
        return self.module_index[path]
Пример #8
0
    def _store_template_map(self, module, index, template_fields,
                            display_only):
        self.notice('Template variables:')
        self.table([['Variable', 'Value', 'Help']] +
                   [[key, value, index.variables[key].get('help', 'NA')]
                    for key, value in template_fields.items()], 'variables')
        self.info('')

        for path, info in index.map.items():
            target = None

            if isinstance(info, str):
                target = info
                info = {}

            elif info.get('when', True) and 'target' in info:
                target = info['target']

            if target:
                path_components = os.path.split(
                    self.manager.get_module_path(module, target))
                target_path = os.path.join(*path_components)

                if info.get('template', True):
                    file_content = self._render_package_template(
                        index.name, path, template_fields)
                else:
                    file_content = load_file(
                        self.manager.get_template_path(index.name, path))

                if info.get('location', None) and path.endswith('.yml'):
                    file_data = normalize_value(load_yaml(target_path),
                                                strip_quotes=True,
                                                parse_json=True)
                    if not file_data:
                        file_data = {}

                    location = info['location'].split('.')
                    embed_data = normalize_value(oyaml.safe_load(file_content),
                                                 strip_quotes=True,
                                                 parse_json=True)
                    merge_data = {}
                    iter_data = merge_data

                    for index, key in enumerate(location):
                        if (index + 1) == len(location):
                            iter_data[key] = embed_data
                        else:
                            iter_data[key] = {}

                        iter_data = iter_data[key]

                    file_content = oyaml.dump(deep_merge(
                        file_data, merge_data))

                self.data('Path', path, 'path')
                self.data('Target', target, 'target')
                if info.get('location', None):
                    self.data('location', info['location'], 'location')
                self.notice('-' * self.display_width)
                if info.get('template', True):
                    self.info(file_content)
                self.info('')

                if not display_only:
                    create_dir(path_components[0])
                    save_file(target_path, file_content)
Пример #9
0
 def exec(self, type):
     data_file = self.manager.index.get_module_file(
         "tests/data/{}.yml".format(type))
     return load_yaml(data_file)