示例#1
0
    def create_stack(self, service):
        cc = Catalog(self.CATALOG_URL, token=self.CATALOG_ADMIN_TOKEN)
        param = {
            'module_name': service['module_name'],
            'version': service['version']
        }
        mv = cc.get_module_version(param)
        if 'dynamic_service' not in mv:
            raise ValueError(
                'Specified module is not marked as a dynamic service. (' +
                mv['module_name'] + '-' + mv['git_commit_hash'] + ')')
        if mv['dynamic_service'] != 1:
            raise ValueError(
                'Specified module is not marked as a dynamic service. (' +
                mv['module_name'] + '-' + mv['git_commit_hash'] + ')')

        secure_param_list = cc.get_secure_config_params({
            'module_name':
            mv['module_name'],
            'version':
            mv['git_commit_hash']
        })
        # Construct the docker compose and rancher compose file
        param['client_group'] = 'service'
        param['function_name'] = 'service'

        mounts = []
        mounts_list = cc.list_volume_mounts(param)
        if len(mounts_list) > 0:
            mounts = mounts_list[0]['volume_mounts']
        docker_compose, rancher_compose, is_new_stack = self.create_compose_files(
            mv, secure_param_list, mounts)

        # To do: try to use API to send docker-compose directly instead of needing to write to disk
        ymlpath = self.SCRATCH_DIR + '/' + mv['module_name'] + '/' + str(
            int(time.time() * 1000))
        os.makedirs(ymlpath)
        docker_compose_path = ymlpath + '/docker-compose.yml'
        rancher_compose_path = ymlpath + '/rancher-compose.yml'

        with open(docker_compose_path, 'w') as outfile:
            outfile.write(
                yaml.safe_dump(docker_compose, default_flow_style=False))
        # can be extended later
        with open(rancher_compose_path, 'w') as outfile:
            outfile.write(
                yaml.safe_dump(rancher_compose, default_flow_style=False))

        return mv, is_new_stack, ymlpath
示例#2
0
class CatalogCache(object):
    def __init__(self, config):
        self.catalog_url = config.get('catalog-service-url')
        self.catalog = Catalog(self.catalog_url, token=config['admin_token'])
        self.module_cache = dict()

    def get_volume_mounts(self, module, method, cgroup):
        req = {
            'module_name': module,
            'function_name': method,
            'client_group': cgroup
        }
        resp = self.catalog.list_volume_mounts(req)
        if len(resp) > 0:
            return resp[0]['volume_mounts']
        else:
            return []

    def get_module_info(self, module, version):
        # Look up the module info
        if module not in self.module_cache:
            req = {'module_name': module}
            if version is not None:
                req['version'] = version
            # Get the image version from the catalog and cache it
            module_info = self.catalog.get_module_version(req)
            # Lookup secure params
            req['load_all_versions'] = 0
            sp = self.catalog.get_secure_config_params(req)
            module_info['secure_config_params'] = sp
            module_info['cached'] = False
            self.module_cache[module] = module_info
        else:
            # Use the cache
            module_info = self.module_cache[module]
            module_info['cached'] = True

        return module_info