Пример #1
0
    def load_file(self, file_name, binary=False, instance=None):
        if not instance:
            instance = self.check_instance('module load file')

        module_path = self.module_path(instance.name)
        path = os.path.join(module_path, file_name)
        return load_file(path, binary)
Пример #2
0
    def get_service(self, name, restart=True, create=True):
        if not self.client:
            return None

        service_file = self._service_file(name)
        services = self.get_spec('services')

        for dependent in dependents(services, [name]):
            if dependent != name:
                dependent_data = self.get_service(dependent,
                                                  restart=restart,
                                                  create=create)
                if not dependent_data:
                    return None

        service_spec = self.get_service_spec(name)

        if os.path.isfile(service_file):
            data = load_json(load_file(service_file))
            service = self._service_container(data['id'])
            if not service and create:
                service_id = self.start_service(name, **service_spec)
                service = self._service_container(service_id)
            if service:
                if service.status != 'running':
                    if create or restart:
                        self.print("{} {}".format(
                            self.notice_color('Restarting Zimagi service'),
                            self.key_color(name)))
                        service.start()
                        success, service = self._check_service(name, service)
                        if not success:
                            self._service_error(name, service)
                    else:
                        return None

                data['service'] = service
                data['ports'] = {}
                for port_name, port_list in service.attrs['NetworkSettings'][
                        'Ports'].items():
                    if port_list:
                        for port in port_list:
                            if port['HostIp'] == '0.0.0.0':
                                data['ports'][port_name] = int(
                                    port['HostPort'])
                                break
                return data

            elif create:
                raise ServiceError(
                    "Zimagi could not initialize and load service {}".format(
                        name))
        elif create:
            self.start_service(name, **service_spec)
            return self.get_service(name)
        return None
Пример #3
0
 def parse_requirements(self):
     requirements = []
     for path, config in self.index.get_ordered_modules().items():
         if 'requirements' in config:
             for requirement_path in ensure_list(config['requirements']):
                 requirement_path = os.path.join(path, requirement_path)
                 file_contents = load_file(requirement_path)
                 if file_contents:
                     requirements.extend([ req for req in file_contents.split("\n") if req and req[0].strip() != '#' ])
     return requirements
Пример #4
0
    def __init__(self, type, name, options = None, initialize = True):
        super().__init__(type, name)

        if not options:
            options = {}

        if initialize:
            if options.get('key', None):
                if os.path.isfile(options['key']):
                    options['key'] = load_file(options['key'])

            self.initialize(options)

        self.import_config(options)
Пример #5
0
    def load(cls, path, default=None):
        if not default:
            default = {}

        data = default

        if os.path.exists(path):
            data = {}
            for statement in load_file(path).split("\n"):
                statement = statement.strip()

                if statement and statement[0] != '#':
                    (variable, value) = statement.split("=")
                    data[variable] = normalize_value(value)
        return data
Пример #6
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)
Пример #7
0
 def get_version(self):
     if not getattr(self, '_version'):
         self._version = load_file(
             os.path.join(self.manager.app_dir, 'VERSION'))
     return self._version
Пример #8
0
 def load_file(self, file_path, encrypted = True):
     if os.path.isfile(file_path):
         self._load(load_file(file_path, encrypted), encrypted)