def run(app_name, command, tty, tag, env, constraint, verbose): """Launch a new container and run a command""" set_verbosity(verbose) if tag is None: tag = clean_string(app_name) hokusai.run(KUBE_CONTEXT, command, tty, tag, env, constraint, namespace=clean_string(app_name))
def create(app_name, verbose): """Creates the Kubernetes based resources defined in ./hokusai/{APP_NAME}.yml""" hokusai.k8s_create(KUBE_CONTEXT, tag=app_name, namespace=clean_string(app_name), filename=os.path.join(CWD, HOKUSAI_CONFIG_DIR, "%s.yml" % app_name))
def delete(app_name, verbose): """Deletes the Kubernetes based resources defined in ./hokusai/{APP_NAME}.yml""" set_verbosity(verbose) hokusai.k8s_delete(KUBE_CONTEXT, namespace=clean_string(app_name), filename=os.path.join(CWD, HOKUSAI_CONFIG_DIR, "%s.yml" % app_name))
def logs(app_name, timestamps, follow, tail, verbose): """Get container logs""" set_verbosity(verbose) hokusai.logs(KUBE_CONTEXT, timestamps, follow, tail, namespace=clean_string(app_name))
def deploy(app_name, tag, migration, constraint, git_remote, verbose): """Update the project's deployment(s) to reference the given image tag""" set_verbosity(verbose) hokusai.update(KUBE_CONTEXT, tag, migration, constraint, git_remote, namespace=clean_string(app_name), resolve_tag_sha1=False)
def status(app_name, resources, pods, describe, top, verbose): """Print the Kubernetes resources status defined in the staging context / {APP_NAME} namespace""" set_verbosity(verbose) hokusai.k8s_status(KUBE_CONTEXT, resources, pods, describe, top, namespace=clean_string(app_name), yaml_file_name=app_name)
def status(app_name, resources, pods, describe, top, verbose): """Print the Kubernetes resources status defined in the staging context / {APP_NAME} namespace""" set_verbosity(verbose) hokusai.k8s_status(KUBE_CONTEXT, resources, pods, describe, top, namespace=clean_string(app_name), filename=os.path.join(CWD, HOKUSAI_CONFIG_DIR, "%s.yml" % app_name))
def create_new_app_yaml(source_file, app_name): with open(source_file, 'r') as stream: try: yaml_content = list(yaml.load_all(stream)) except yaml.YAMLError as exc: raise HokusaiError("Cannot read source yaml file %s." % source_file) for c in yaml_content: update_namespace(c, clean_string(app_name)) new_namespace = OrderedDict([('apiVersion', 'v1'), ('kind', 'Namespace'), ('metadata', { 'name': clean_string(app_name) })]) yaml_content = [new_namespace] + yaml_content with open("hokusai/%s.yml" % app_name, 'w') as output: output.write(YAML_HEADER) yaml.safe_dump_all(yaml_content, output, default_flow_style=False) print_green("Created hokusai/%s.yml" % app_name)
def deploy(app_name, tag, migration, constraint, git_remote, timeout, update_config, verbose): """Update the project's deployment(s) to reference the given image tag""" set_verbosity(verbose) hokusai.update(KUBE_CONTEXT, tag, migration, constraint, git_remote, timeout, namespace=clean_string(app_name), update_config=update_config, filename=os.path.join(CWD, HOKUSAI_CONFIG_DIR, "%s.yml" % app_name))
def create_new_app_yaml(source_file, app_name): yaml_spec = YamlSpec(source_file).to_file() with open(yaml_spec, 'r') as stream: try: yaml_content = list(yaml.load_all(stream, Loader=yaml.FullLoader)) except yaml.YAMLError as exc: raise HokusaiError("Cannot read source yaml file %s." % source_file) for c in yaml_content: update_namespace(c, clean_string(app_name)) new_namespace = OrderedDict([('apiVersion', 'v1'), ('kind', 'Namespace'), ('metadata', { 'name': clean_string(app_name) })]) yaml_content = [new_namespace] + yaml_content with open(os.path.join(CWD, HOKUSAI_CONFIG_DIR, "%s.yml" % app_name), 'w') as output: output.write(YAML_HEADER) yaml.safe_dump_all(yaml_content, output, default_flow_style=False) print_green("Created %s/%s.yml" % (HOKUSAI_CONFIG_DIR, app_name))
def create(app_name, verbose): """Create the Kubernetes configmap object `{project_name}-environment` in the {APP_NAME} namespace""" set_verbosity(verbose) hokusai.create_env(KUBE_CONTEXT, namespace=clean_string(app_name))
def delete(app_name, verbose): """Delete the Kubernetes configmap object `{project_name}-environment`""" set_verbosity(verbose) hokusai.delete_env(KUBE_CONTEXT, namespace=clean_string(app_name))
def unset(app_name, env_vars, verbose): """Unset environment variables - each of {ENV_VARS} must be of the form 'KEY'""" set_verbosity(verbose) hokusai.unset_env(KUBE_CONTEXT, env_vars, namespace=clean_string(app_name))
def set(app_name, env_vars, verbose): """Set environment variables - each of {ENV_VARS} must be in of form 'KEY=VALUE'""" set_verbosity(verbose) hokusai.set_env(KUBE_CONTEXT, env_vars, namespace=clean_string(app_name))
def get(app_name, env_vars, verbose): """Print environment variables stored on the Kubernetes server""" set_verbosity(verbose) hokusai.get_env(KUBE_CONTEXT, env_vars, namespace=clean_string(app_name))
def copy(app_name, configmap, verbose): """Copies the app's environment config map to the namespace {APP_NAME}""" set_verbosity(verbose) hokusai.k8s_copy_config(KUBE_CONTEXT, clean_string(app_name), name=configmap)
def restart(app_name, deployment, verbose): """Alias for 'refresh'""" set_verbosity(verbose) hokusai.refresh(KUBE_CONTEXT, deployment, namespace=clean_string(app_name))
def refresh(app_name, deployment, verbose): """Refresh the project's deployment(s) by recreating the currently running containers""" set_verbosity(verbose) hokusai.refresh(KUBE_CONTEXT, deployment, namespace=clean_string(app_name))
def create(app_name, verbose): """Creates the Kubernetes based resources defined in ./hokusai/{APP_NAME}.yml""" hokusai.k8s_create(KUBE_CONTEXT, tag=app_name, namespace=clean_string(app_name), yaml_file_name=app_name)
def setup(project_name, template_remote, template_dir, template_vars, allow_missing_vars): mkpath(os.path.join(CWD, HOKUSAI_CONFIG_DIR)) config.create(clean_string(project_name)) ecr = ECR() if ecr.project_repo_exists(): print_green("Project repo %s already exists. Skipping create." % ecr.project_repo) else: ecr.create_project_repo() print_green("Created project repo %s" % ecr.project_repo) scratch_dir = None if template_remote: scratch_dir = tempfile.mkdtemp() git_repo_and_branch = template_remote.split('#', 1) git_repo = git_repo_and_branch[0] if len(git_repo_and_branch) == 2: git_branch = git_repo_and_branch[1] else: git_branch = "master" shout("git clone -b %s --single-branch %s %s" % (git_branch, git_repo, scratch_dir)) custom_template_dir = None if allow_missing_vars: environment_kwargs = {} else: environment_kwargs = {"undefined": StrictUndefined} if scratch_dir and template_dir: custom_template_dir = os.path.join(scratch_dir, os.path.basename(template_dir)) env = Environment(loader=FileSystemLoader( os.path.join(scratch_dir, os.path.basename(template_dir))), **environment_kwargs) elif scratch_dir: custom_template_dir = scratch_dir env = Environment(loader=FileSystemLoader(scratch_dir), **environment_kwargs) elif template_dir: custom_template_dir = os.path.abspath(template_dir) env = Environment(loader=FileSystemLoader( os.path.abspath(template_dir)), **environment_kwargs) else: try: base_path = sys._MEIPASS env = Environment(loader=FileSystemLoader( os.path.join(base_path, 'hokusai', 'templates'))) except: env = Environment(loader=PackageLoader('hokusai', 'templates')) required_templates = [ 'Dockerfile.j2', '.dockerignore.j2', 'hokusai/build.yml.j2', 'hokusai/development.yml.j2', 'hokusai/test.yml.j2', 'hokusai/staging.yml.j2', 'hokusai/production.yml.j2' ] template_context = { "project_name": config.project_name, "project_repo": ecr.project_repo } for s in template_vars: if '=' not in s: raise HokusaiError( "Error: template variables must be of the form 'key=value'") split = s.split('=', 1) template_context[split[0]] = split[1] try: for template in required_templates: if custom_template_dir and not os.path.isfile( os.path.join(custom_template_dir, template)): raise HokusaiError("Could not find required template file %s" % template) with open(os.path.join(CWD, template.rstrip('.j2')), 'w') as f: f.write(env.get_template(template).render(**template_context)) print_green("Created %s" % template.rstrip('.j2')) if custom_template_dir: for root, _, files in os.walk(custom_template_dir): subpath = os.path.relpath(root, custom_template_dir) if subpath is not '.': mkpath(os.path.join(CWD, subpath)) for file in files: if subpath is not '.': file_path = os.path.join(subpath, file) else: file_path = file if file_path in required_templates: continue if file_path.endswith('.j2'): with open(os.path.join(CWD, file_path.rstrip('.j2')), 'w') as f: f.write( env.get_template(file_path).render( **template_context)) else: copyfile(os.path.join(custom_template_dir, file_path), os.path.join(CWD, file_path)) print_green("Created %s" % file_path.rstrip('.j2')) finally: if scratch_dir: rmtree(scratch_dir)
def update(app_name, verbose): """Updates the Kubernetes based resources defined in ./hokusai/{APP_NAME}.yml""" set_verbosity(verbose) hokusai.k8s_update(KUBE_CONTEXT, namespace=clean_string(app_name), yaml_file_name=app_name)