def build(kubeb, message): """ Build current application Build Dockerfile image Add release note, tag to config file """ if not file_util.config_file_exist(): kubeb.log('Kubeb config file not found in %s', file_util.kubeb_directory) exit(1) if not message: marker = '# Add release note:' hint = ['', '', marker] message = click.edit('\n'.join(hint)) msg = message.split(marker)[0].rstrip() if not msg: msg = "" else: msg = '\n'.join(message) if config.get_local(): image = config.get_image() tag = 'v' + str(int(round(time.time() * 1000))) status, output, err = command.run(command.build_command(image, tag)) if status != 0: kubeb.log('Docker image build failed', err) return kubeb.log(output) kubeb.log('Docker image build succeed.') config.add_version(tag, msg)
def init(kubeb, name, user, template, local, image, env, force): """ Init kubeb configuration Generate config, script files Generate Docker stuff if use --local option """ if file_util.config_file_exist() and force is False: kubeb.log( 'Kubeb config found. Please update config file or use --force option' ) return if (not file_util.template_exist(template)): kubeb.log('Kubeb template not found. Please check template name') return ext_template = file_util.is_ext_template(template) file_util.generate_config_file(name, user, template, ext_template, image, local, env) file_util.generate_script_file(name, template) file_util.generate_environment_file(env, template) if local: file_util.generate_docker_file(template) kubeb.log('Kubeb config file generated in %s', click.format_filename(file_util.config_file))
def initiate(self, name, user, template, image, env, force): if file_util.config_file_exist() and force is False: self.log( 'Kubeb config found. Please update config file or use --force option' ) return if not file_util.template_exist(template): self.log('Kubeb template not found. Please check template name') return ext_template = file_util.is_ext_template(template) generator = self._get_generator(template) if generator is None: self.log('Kubeb generator not found. Please check template name') return generator(data=dict(name=name, user=user, template=template, ext_template=ext_template, image=image, env=env, force=force)).execute() self.log('Kubeb config file generated in %s', click.format_filename(file_util.config_file))
def install(kubeb, version): """ Install current application to Kubernetes Generate Helm chart value file with docker image version If version is not specified, will get the latest version """ if not file_util.config_file_exist(): kubeb.log('Kubeb config file not found') return if config.get_local(): deploy_version = config.get_version(version) if not deploy_version: kubeb.log('No deployable version found') return kubeb.log('Deploying version: %s', deploy_version["tag"]) file_util.generate_helm_file(config.get_template(), config.get_image(), config.get_ext_template(), deploy_version["tag"], config.get_current_environment()) else: file_util.generate_helm_file(config.get_template(), config.get_image(), config.get_ext_template(), "latest", config.get_current_environment()) status, output, err = command.run(command.install_command()) if status != 0: kubeb.log('Install application failed', err) return kubeb.log(output) kubeb.log('Install application succeed.')
def setenv(self, env_vars): if not file_util.config_file_exist(): self.log('Kubeb config file not found in %s', file_util.kubeb_directory) return env = config.get_current_environment() config.set_environment_variable(env, env_vars)
def info(self): if not file_util.config_file_exist(): self.log('Kubeb config file not found in %s', file_util.kubeb_directory) return config_data = yaml.dump(config.load_config(), default_flow_style=False) print(config_data)
def info(kubeb): """ Show current configuration """ if not file_util.config_file_exist(): kubeb.log('Kubeb config file not found in %s', file_util.kubeb_directory) return config_data = config.load_config() print(config_data)
def delete(self): if not file_util.config_file_exist(): self.log('Kubeb config file not found') return status = util.run_helm_uninstall(config.get_name()) if status != 0: self.log('Delete application failed') return self.log('Uninstall application succeed.')
def uninstall(kubeb): """Uninstall current application from Kubernetes """ if not file_util.config_file_exist(): kubeb.log('Kubeb config file not found') return status, output, err = command.run(command.uninstall_command()) if status != 0: kubeb.log('Uninstall application failed', err) return kubeb.log(output) kubeb.log('Uninstall application succeed.')
def rollback(self, revision): if not file_util.config_file_exist(): self.log('Kubeb config file not found in %s', file_util.kubeb_directory) return self.log('Rollback application to revision {} ...'.format(revision)) spinner.start() status = util.run_helm_rollback(config.get_name(), revision) spinner.stop() if status != 0: self.log('Rollback application to revision failed.') else: self.log('Rollback application to revision succeed.')
def history(self): if not file_util.config_file_exist(): self.log('Kubeb config file not found in %s', file_util.kubeb_directory) return self.log('Get application deploy history ...') spinner.start() status = util.run_helm_history(config.get_name()) spinner.stop() if status != 0: self.log('Get application deploy history failed.') else: self.log('Get application deploy history succeed.')
def version(self): """Show current application versions """ if not file_util.config_file_exist(): self.log('Kubeb config file not found in %s', file_util.kubeb_directory) return versions = config.get_versions() if not versions or len(versions) == 0: self.log('No version found in %s', file_util.kubeb_directory) return for version in versions: self.log('- %s: %s', version['tag'], version['message'])
def env(self, env): if not file_util.config_file_exist(): self.log('Kubeb config file not found in %s', file_util.kubeb_directory) return environment = config.get_env(env) if not environment: self.log('Environment not found') self.log('Initiate environment %s in %s', env, file_util.kubeb_directory) file_util.generate_environment_file(env, config.get_template()) config.set_current_environement(env) self.log('Now use %s', env)
def env(kubeb, env): """Use environment Example: kubeb env develop to use environment develop """ if not file_util.config_file_exist(): kubeb.log('Kubeb config file not found in %s', file_util.kubeb_directory) return environment = config.get_env(env) if not environment: kubeb.log('Environment not found') kubeb.log('Initiate environment %s in %s', env, file_util.kubeb_directory) file_util.generate_environment_file(env, config.get_template()) config.set_current_environement(env) kubeb.log('Now use %s', env)
def build(self, message, push): if not file_util.config_file_exist(): self.log('Kubeb config file not found in %s', file_util.kubeb_directory) exit(1) if not message: marker = '# Add release note:' hint = ['', '', marker] message = click.edit('\n'.join(hint)) msg = message.split(marker)[0].rstrip() if not msg: msg = "" else: msg = '\n'.join(message) image = config.get_image() tag = 'v' + str(int(round(time.time() * 1000))) self.log('Building docker image {}:{}...'.format(image, tag)) spinner.start() status = util.run_docker_build(image, tag, os.getcwd()) spinner.stop() if status != 0: self.log('Docker image build failed') return else: self.log('Docker image build succeed.') if push: spinner.start() status = util.run_docker_push(image, tag) spinner.stop() if status != 0: self.log('Docker image push failed') return else: self.log('Docker image push succeed.') config.add_version(tag, msg)
def deploy(self, version, options, dry_run, rollback=True): if not file_util.config_file_exist(): self.log('Kubeb config file not found') return deploy_version = config.get_version(version) if not deploy_version: self.log('No deployable version found') return self.log('Deploying version: %s', deploy_version["tag"]) file_util.generate_helm_file(config.get_template(), config.get_ext_template(), config.get_image(), deploy_version["tag"], config.get_current_environment()) if options and not dry_run: self.log('Saving deploy options ...') file_util.save_deploy_options(options) self.log('Installing application ...') spinner.start() status = util.run_helm_install(config.get_name(), config.get_template(), dry_run, options) spinner.stop() if status != 0: self.log('Install application failed.') if dry_run is False and rollback: last_working_revision = util.get_last_working_revision( config.get_name()) if not last_working_revision: self.log('Last working revision not found. Skip rollback') return self.log( 'Rollback application to last working revision {}'.format( last_working_revision)) self.rollback(last_working_revision) else: self.log('Install application succeed.')
def push(self, version=None): if not file_util.config_file_exist(): self.log('Kubeb config file not found') return deploy_version = config.get_version(version) if not deploy_version: self.log('No deploy version found') return image = config.get_image() self.log('docker push {}:{}'.format(image, deploy_version["tag"])) spinner.start() status = util.run_docker_push(image, deploy_version["tag"]) spinner.stop() if status != 0: self.log('Docker image push failed') else: self.log('Docker image push succeed.')