def generate_deployment_files(self, kolla_config, jinja_vars, temp_dir=None): _, proj, service = self._conf['name'].split('/') values = { 'service_name': self._conf['name'], 'chronos_service_id': self._conf['name'].replace('/', '-'), 'kolla_config': kolla_config, 'zookeeper_hosts': CONF.zookeeper.host, 'private_interface': CONF.network.private_interface, 'public_interface': CONF.network.public_interface, } app_file = os.path.join(self.base_dir, 'services', 'default.%s.j2' % self.type_name) content = jinja_utils.jinja_render(app_file, jinja_vars, extra=values) self.app_def = yaml.load(content) self._apply_service_def(self.app_def) if temp_dir is not None: self.app_file = os.path.join(temp_dir, proj, '%s.%s' % (service, self.type_name)) file_utils.mkdir_p(os.path.dirname(self.app_file)) LOG.info(self.app_file) with open(self.app_file, 'w') as f: f.write(json.dumps(self.app_def, indent=2))
def generate_deployment_files(self, kolla_config, jinja_vars, temp_dir): if not self._enabled: return _, proj, service = self._conf['name'].split('/') values = { 'role': service, 'group': proj, 'service_name': self._get_service_name(), 'kolla_config': kolla_config, 'zookeeper_hosts': CONF.zookeeper.host, 'private_interface': CONF.network.private_interface, 'public_interface': CONF.network.public_interface, } app_file = os.path.join(self.base_dir, 'services', 'default.%s.j2' % self.type_name) content = jinja_utils.jinja_render(app_file, jinja_vars, extra=values) app_def = yaml.load(content) self._apply_service_def(app_def) dest_file = os.path.join(temp_dir, proj, '%s.%s' % (service, self.type_name)) file_utils.mkdir_p(os.path.dirname(dest_file)) LOG.info(dest_file) with open(dest_file, 'w') as f: f.write(json.dumps(app_def, indent=2))
def snapshot(self, zk, output_dir, variables): """Produce the required files to revert the service. The files should have the same basic structure as the original service definition: <output_dir>/<project>/<service>.yml.j2 <output_dir>/<project>/templates/* <output_dir>/<project>/<service>.{marathon|chronos} <output_dir>/variables.yml """ service_name = self._conf['name'].split('/')[-1] proj = self._conf['name'].split('/')[-2] proj_dir = os.path.join(output_dir, proj) file_utils.mkdir_p(proj_dir) # variables # Note: if this exists, then the variables will be merged. var_path = os.path.join(output_dir, 'variables.yml') if os.path.exists(var_path): with open(var_path) as vfp: global_vars = yaml.load(vfp) LOG.info('global_vars: %s', yaml.dump(global_vars)) global_vars.update(variables) else: global_vars = variables with open(var_path, 'w') as vfp: vfp.write(yaml.dump(global_vars, default_flow_style=False)) # service definition and files with open(os.path.join(proj_dir, '%s.yml.j2' % service_name), 'w') as sfp: sfp.write(json.dumps(self._conf)) # get and write the files files_node = os.path.join('kolla', CONF.kolla.deployment_id, self._conf['name'], 'files') if zk.exists(files_node): file_utils.mkdir_p(os.path.join(proj_dir, 'templates')) try: files = zk.get_children(files_node) except exceptions.NoNodeError: files = [] for fn in files: with open(os.path.join(proj_dir, 'templates', fn), 'w') as fp: content, _st = zk.get(os.path.join(files_node, fn)) fp.write(content) # deployment file self.app_file = os.path.join(proj_dir, '%s.%s' % (service_name, self.type_name)) self.app_def = self.get_live_deployment_file() with open(self.app_file, 'w') as dfp: dfp.write(json.dumps(self.app_def, indent=2))
def write_config_to_zookeeper(self, zk): jinja_vars = self.get_jinja_vars() self.required_vars = jinja_vars for var in jinja_vars: if not jinja_vars[var]: LOG.info('empty %s=%s' % (var, jinja_vars[var])) if 'image' in var: LOG.info('%s=%s' % (var, jinja_vars[var])) for proj in self.get_projects(): proj_dir = os.path.join(self.config_dir, proj) if not os.path.exists(proj_dir): continue conf_path = os.path.join(self.config_dir, proj, '%s_config.yml.j2' % proj) extra = yaml.load(jinja_utils.jinja_render(conf_path, jinja_vars)) dest_node = os.path.join('kolla', 'config', proj, proj) # TODO() should be service zk.ensure_path(dest_node) zk.set(dest_node, json.dumps(extra)) for service in extra['config'][proj]: # write the config files for name, item in extra['config'][proj][service].iteritems(): dest_node = os.path.join('kolla', 'config', proj, service, name) zk.ensure_path(dest_node) if isinstance(item['source'], list): content = self.merge_ini_files(item['source']) else: src_file = item['source'] if not src_file.startswith('/'): src_file = file_utils.find_file(src_file) with open(src_file) as fp: content = fp.read() zk.set(dest_node, content) # write the commands for name, item in extra['commands'][proj][service].iteritems(): dest_node = os.path.join('kolla', 'commands', proj, service, name) zk.ensure_path(dest_node) try: zk.set(dest_node, json.dumps(item)) except Exception as te: LOG.error('%s=%s -> %s' % (dest_node, item, te)) # 3. do the service's config.json (now KOLLA_CONFIG) kc_name = os.path.join(self.config_dir, 'config.json') # override container_config_directory cont_conf_dir = 'zk://%s' % ( CONF.zookeeper.host) jinja_vars['container_config_directory'] = cont_conf_dir kolla_config = jinja_utils.jinja_render(kc_name, jinja_vars) kolla_config = kolla_config.replace('"', '\\"').replace( '\n', '') # 4. parse the marathon app file and add the KOLLA_CONFIG values = { 'kolla_config': kolla_config, 'zookeeper_hosts': CONF.zookeeper.host } for app_type in ['marathon', 'chronos']: app_file = os.path.join(self.base_dir, 'deployment_files', proj, '%s.%s.j2' % (service, app_type)) if not os.path.exists(app_file): LOG.debug('potentially missing file %s' % app_file) continue content = jinja_utils.jinja_render(app_file, jinja_vars, extra=values) dest_file = os.path.join(self.temp_dir, proj, '%s.%s' % (service, app_type)) file_utils.mkdir_p(os.path.dirname(dest_file)) with open(dest_file, 'w') as f: f.write(content)