def generate_output(self, app_vars): # Basic structure data = { 'apiVersion': 'v1', 'kind': 'Secret', } data['metadata'] = self.build_metadata(app_vars['APP']['metadata']) for field in ('type', ): data[underscore_to_camelcase(field)] = app_vars['APP'][field] if app_vars['APP']['string_data']: data['stringData'] = app_vars['APP']['string_data'] if app_vars['APP']['data']: tmp_data = dict() for key in app_vars['APP']['data']: # Values under 'data' should be base64-encoded # We have to jump through the hoops of encoding/decoding it because the # py3 b64encode() function expects a bytestring, and then the YAML encoder # tries to double-encode the resulting bytestring, so we convert it back # to a string tmp_value = app_vars['APP']['data'][key].encode("utf-8") tmp_data[key] = base64.b64encode(tmp_value).decode('utf-8') data['data'] = tmp_data data = self._template.render_template(data, app_vars) output = yaml_dump(data) return (output, self.get_output_filename_suffix(data))
def generate_output(self, app_vars): output = inspect.cleandoc(self.TEMPLATE) output += "\n\n" output_fmt = app_vars['APP']['format'] del app_vars['APP']['format'] if output_fmt == 'json': output += json_dump( self._template.render_template(app_vars['APP'], app_vars)) elif output_fmt == 'yaml': output += yaml_dump( self._template.render_template(app_vars['APP'], app_vars)) return output
def generate_output(self, app_vars): # Basic structure data = { 'apiVersion': 'v1', 'kind': 'ConfigMap', } data['metadata'] = self.build_metadata(app_vars['APP']['metadata']) data['data'] = app_vars['APP']['data'] data = self._template.render_template(data, app_vars) output = yaml_dump(data) return (output, self.get_output_filename_suffix(data))
def generate_output(self, app_vars): # Basic structure data = { 'apiVersion': 'apps/v1', 'kind': 'StatefulSet', 'spec': dict(), } data['metadata'] = self.build_metadata(app_vars['APP']['metadata']) self.build_spec(app_vars, data) data = self._template.render_template(data, app_vars) output = yaml_dump(data) return (output, self.get_output_filename_suffix(data))
def generate_output(self, app_vars): # Basic structure data = { 'apiVersion': 'batch/v1', 'kind': 'Job', 'spec': dict(), } data['metadata'] = self.build_metadata(app_vars['APP']['metadata']) data['spec'] = self.build_generic(app_vars['APP']['spec'], self._plugin_config['fields']['kube_jobs']['spec']['fields']) data = self._template.render_template(data, app_vars) output = yaml_dump(data) return (output, self.get_output_filename_suffix(data))
def generate_output(self, app_vars): # Basic structure data = { 'apiVersion': 'externaldns.k8s.io/v1alpha1', 'kind': 'DNSEndpoint', } data['metadata'] = self.build_metadata(app_vars['APP']['metadata']) data['spec'] = self.build_generic( app_vars['APP']['spec'], self._fields['kube_dnsendpoints']['spec']['fields']) data = self._template.render_template(data, app_vars) output = yaml_dump(data) return (output, self.get_output_filename_suffix(data))
def generate_output(self, app_vars): # Basic structure data = { 'apiVersion': 'configuration.konghq.com/v1', 'kind': 'KongPlugin', } data['metadata'] = self.build_metadata(app_vars['APP']['metadata']) for field in ('disabled', 'config', 'config_from', 'plugin'): if app_vars['APP'][field]: data.update(self.build_generic(app_vars['APP'], {field: self._fields['kong_plugins'][field]})) data = self._template.render_template(data, app_vars) output = yaml_dump(data) return (output, self.get_output_filename_suffix(data))
def generate_output(self, app_vars): # Basic structure data = { 'apiVersion': 'networking.k8s.io/v1', 'kind': 'Ingress', } data['metadata'] = self.build_metadata(app_vars['APP']['metadata']) data['spec'] = self.build_generic( app_vars['APP']['spec'], self._fields['kube_ingresses']['spec']['fields']) data = self._template.render_template(data, app_vars) output = yaml_dump(data) return (output, self.get_output_filename_suffix(data))
def generate_output(self, app_vars): # Basic structure data = { 'apiVersion': 'v1', 'kind': 'Namespace', } data['metadata'] = self.build_metadata(app_vars['APP']['metadata']) if app_vars['APP']['spec']: tmp_spec = self.build_generic( app_vars['APP']['spec'], self._plugin_config['fields'] ['kube_namespaces']['spec']['fields']) if tmp_spec: data['spec'] = tmp_spec data = self._template.render_template(data, app_vars) output = yaml_dump(data) return (output, self.get_output_filename_suffix(data))
def generate_output(self, app_vars): # Basic structure data = { 'apiVersion': 'configuration.konghq.com/v1', 'kind': 'KongConsumer', } data['metadata'] = self.build_metadata(app_vars['APP']['metadata']) for field in ('username', 'custom_id', 'credentials'): if app_vars['APP'][field]: data.update( self.build_generic( app_vars['APP'], {field: self._fields['kong_consumers'][field]}, camel_case=False)) data = self._template.render_template(data, app_vars) output = yaml_dump(data) return (output, self.get_output_filename_suffix(data))
def generate_output(self, app_vars): # Basic structure data = { 'apiVersion': 'configuration.konghq.com/v1', 'kind': 'KongIngress', } data['metadata'] = self.build_metadata(app_vars['APP']['metadata']) for field in ('upstream', 'proxy', 'route'): if app_vars['APP'][field]: data.update( self.build_generic( app_vars['APP'], {field: self._fields['kong_ingresses'][field]}, camel_case=False)) data = self._template.render_template(data, app_vars) output = yaml_dump(data) return (output, self.get_output_filename_suffix(data))
def main(): global DISPLAY, SITE_CONFIG parser = argparse.ArgumentParser() parser.add_argument( 'path', help='Path to service dir', ) parser.add_argument( '-v', '--verbose', action='count', help='Increase verbosity level', default=0, ) parser.add_argument( '-c', '--config', help='Path to config file', ) parser.add_argument( '-e', '--env', help="Environment to generate deploy configs for", default='local', ) parser.add_argument( '-o', '--output-dir', help= "Directory to output generated deploy configs to (defaults to '.')", default='.') parser.add_argument( '--dump-vars', help="Output all site config vars in shell format", action='store_true', default=False, ) args = parser.parse_args() DISPLAY = Display() DISPLAY.set_verbosity(args.verbose) DISPLAY.vv('Running with args:') DISPLAY.vv() for arg in dir(args): if arg.startswith('_'): continue DISPLAY.vv('%s: %s' % (arg, getattr(args, arg))) DISPLAY.vv() SITE_CONFIG = SiteConfig(env=args.env) if args.config is None: # Possible config locations config_paths = [ os.path.join(os.environ.get('HOME', None), '.deploy-config-generator-site.yml'), ] for path in config_paths: if os.path.exists(path): args.config = path break if args.config: try: SITE_CONFIG.load(args.config) except ConfigError as e: DISPLAY.display('Failed to load site config: %s' % str(e)) show_traceback(DISPLAY.get_verbosity()) sys.exit(1) DISPLAY.vvvv('Site config:') DISPLAY.vvvv() DISPLAY.vvvv(yaml_dump(SITE_CONFIG.get_config())) DISPLAY.vvvv() varset = Vars() varset['env'] = args.env deploy_dir = None if not args.dump_vars: deploy_dir = find_deploy_dir(args.path) try: load_vars(varset, deploy_dir, args.env, only_site_config=(True if args.dump_vars else False)) except Exception as e: DISPLAY.display('Error loading vars: %s' % str(e)) show_traceback(DISPLAY.get_verbosity()) sys.exit(1) if args.dump_vars: template = Template() templated_vars = template.render_template(dict(varset), dict(VARS=dict(varset))) for key in sorted(templated_vars.keys()): print('%s=%s' % (key, shlex.quote(templated_vars[key]))) sys.exit(0) DISPLAY.vvvv() DISPLAY.vvvv('Vars:') DISPLAY.vvvv() DISPLAY.vvvv(yaml_dump(dict(varset), indent=2)) try: deploy_config = DeployConfig( os.path.join(deploy_dir, SITE_CONFIG.deploy_config_file), varset) deploy_config.set_config( varset.replace_vars(deploy_config.get_config())) except DeployConfigError as e: DISPLAY.display('Error loading deploy config: %s' % str(e)) show_traceback(DISPLAY.get_verbosity()) sys.exit(1) except VarsReplacementError as e: DISPLAY.display( 'Error loading deploy config: variable replacement error: %s' % str(e)) show_traceback(DISPLAY.get_verbosity()) sys.exit(1) DISPLAY.vvvv('Deploy config:') DISPLAY.vvvv() DISPLAY.vvvv(yaml_dump(deploy_config.get_config(), indent=2)) deploy_config_version = deploy_config.get_version( ) or SITE_CONFIG.default_config_version output_plugins = load_output_plugins(varset, args.output_dir, deploy_config_version) DISPLAY.vvv('Available output plugins:') DISPLAY.vvv() valid_sections = [] for plugin in output_plugins: DISPLAY.vvv('- %s (%s)' % (plugin.NAME, plugin.DESCR or 'No description')) valid_sections += plugin._fields.keys() DISPLAY.vvv() try: deploy_config.apply_default_apps(SITE_CONFIG.default_apps) deploy_config.validate_sections(valid_sections) except DeployConfigError as e: DISPLAY.display('Error validating deploy config: %s' % str(e)) sys.exit(1) # TODO: do validation before running each plugin so that it doesn't complain about # values populated by wrapper plugins. This is not straightforward, since it breaks # the existing code that reports on unknown fields for section in deploy_config.get_config(): for plugin in output_plugins: plugin.set_section(section) for app_idx, app in enumerate(deploy_config.get_config()[section]): app_validate_fields(app, app_idx, output_plugins) try: for plugin in output_plugins: DISPLAY.vvv('Generating output using %s plugin' % plugin.NAME) plugin.generate(deploy_config.get_config()) except DeployConfigGenerationError as e: DISPLAY.display('Failed to generate deploy config: %s' % str(e)) sys.exit(1)