Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_shout(self, mocked_check_output):
with captured_output() as (out, err):
self.assertEqual(shout('whoami'), 'hokusai')
mocked_check_output.assert_called_once_with('whoami', shell=True, stderr=-2)
print_green("Updated tag 'latest' -> %s" % context)
if filename is None:
configmap = ConfigMap(context, namespace=namespace)
for s in environment:
if '=' not in s:
raise HokusaiError("Error: environment variables must be of the form 'KEY=VALUE'")
split = s.split('=', 1)
configmap.update(split[0], split[1])
configmap.create()
print_green("Created configmap %s-environment" % config.project_name)
kctl = Kubectl(context, namespace=namespace)
kubernetes_spec = KubernetesSpec(kubernetes_yml).to_file()
try:
shout(kctl.command("create --save-config -f %s" % kubernetes_spec), print_output=True)
print_green("Created Kubernetes environment %s" % kubernetes_yml)
finally:
os.unlink(kubernetes_spec)
def pull(tag, local_tag):
ecr = ECR()
if not ecr.project_repo_exists():
raise HokusaiError("ECR repo %s does not exist... did you run `hokusai setup` for this project?" % config.project_name)
shout(ecr.get_login(), mask=(r'^(docker login -u) .+ (-p) .+ (.+)$', r'\1 ****** \2 ***** \3'))
shout("docker pull %s:%s" % (ecr.project_repo, tag))
shout("docker tag %s:%s hokusai_%s:%s" % (ecr.project_repo, tag, config.project_name, local_tag))
print_green("Pulled %s:%s to hokusai_%s:%s" % (ecr.project_repo, tag, config.project_name, local_tag), newline_after=True)
if 'detached' in current_branch:
raise HokusaiError("Not on any branch! Aborting.")
if current_branch != check_branch:
raise HokusaiError("Not on %s branch! Aborting." % check_branch)
remotes = [check_remote] if check_remote else shout('git remote').splitlines()
for remote in remotes:
shout("git fetch %s" % remote)
if returncode("git diff --quiet %s/%s" % (remote, current_branch)):
raise HokusaiError("Local branch %s is divergent from %s/%s. Aborting." % (current_branch, remote, current_branch))
kctl = Kubectl(context, namespace=namespace)
kubernetes_spec = KubernetesSpec(kubernetes_yml).to_file()
try:
if dry_run:
shout(kctl.command("apply -f %s --dry-run" % kubernetes_spec), print_output=True)
print_green("Updated Kubernetes environment %s (dry run)" % kubernetes_yml)
else:
shout(kctl.command("apply -f %s" % kubernetes_spec), print_output=True)
print_green("Updated Kubernetes environment %s" % kubernetes_yml)
finally:
os.unlink(kubernetes_spec)
def k8s_update(context, namespace=None, filename=None, check_branch="master",
check_remote=None, skip_checks=False, dry_run=False):
if filename is None:
kubernetes_yml = os.path.join(CWD, HOKUSAI_CONFIG_DIR, "%s.yml" % context)
else:
kubernetes_yml = filename
if not os.path.isfile(kubernetes_yml):
raise HokusaiError("Yaml file %s does not exist." % kubernetes_yml)
if not skip_checks:
current_branch = None
for branchname in shout('git branch').splitlines():
if '* ' in branchname:
current_branch = branchname.replace('* ', '')
break
if 'detached' in current_branch:
raise HokusaiError("Not on any branch! Aborting.")
if current_branch != check_branch:
raise HokusaiError("Not on %s branch! Aborting." % check_branch)
remotes = [check_remote] if check_remote else shout('git remote').splitlines()
for remote in remotes:
shout("git fetch %s" % remote)
if returncode("git diff --quiet %s/%s" % (remote, current_branch)):
raise HokusaiError("Local branch %s is divergent from %s/%s. Aborting." % (current_branch, remote, current_branch))
kctl = Kubectl(context, namespace=namespace)
def gitlog():
ecr = ECR()
staging_tag = ecr.find_git_sha1_image_tag('staging')
if staging_tag is None:
raise HokusaiError("Could not find a tag for staging. Aborting.")
production_tag = ecr.find_git_sha1_image_tag('production')
if production_tag is None:
raise HokusaiError("Could not find a git SHA1 tag for production. Aborting.")
print_green("Comparing %s to %s" % (production_tag, staging_tag))
for remote in shout('git remote').splitlines():
shout("git fetch %s" % remote)
shout("git log --right-only %s..%s" % (production_tag, staging_tag), print_output=True)
if not os.path.isfile(kubernetes_yml):
raise HokusaiError("Yaml file %s does not exist." % kubernetes_yml)
if not skip_checks:
current_branch = None
for branchname in shout('git branch').splitlines():
if '* ' in branchname:
current_branch = branchname.replace('* ', '')
break
if 'detached' in current_branch:
raise HokusaiError("Not on any branch! Aborting.")
if current_branch != check_branch:
raise HokusaiError("Not on %s branch! Aborting." % check_branch)
remotes = [check_remote] if check_remote else shout('git remote').splitlines()
for remote in remotes:
shout("git fetch %s" % remote)
if returncode("git diff --quiet %s/%s" % (remote, current_branch)):
raise HokusaiError("Local branch %s is divergent from %s/%s. Aborting." % (current_branch, remote, current_branch))
kctl = Kubectl(context, namespace=namespace)
kubernetes_spec = KubernetesSpec(kubernetes_yml).to_file()
try:
if dry_run:
shout(kctl.command("apply -f %s --dry-run" % kubernetes_spec), print_output=True)
print_green("Updated Kubernetes environment %s (dry run)" % kubernetes_yml)
else:
shout(kctl.command("apply -f %s" % kubernetes_spec), print_output=True)
print_green("Updated Kubernetes environment %s" % kubernetes_yml)
finally:
os.unlink(kubernetes_spec)
def contexts(self):
return [context['name'] for context in yaml.load(shout('kubectl config view'))['contexts']]
if overwrite is None and ecr.tag_exists(tag):
raise HokusaiError("Tag %s already exists in registry. Aborting." % tag)
if build:
Docker().build(filename)
build_tag = "hokusai_%s:%s" % (config.project_name, local_tag)
shout("docker tag %s %s:%s" % (build_tag, ecr.project_repo, tag))
shout("docker push %s:%s" % (ecr.project_repo, tag), print_output=True)
print_green("Pushed %s to %s:%s" % (build_tag, ecr.project_repo, tag), newline_after=True)
if skip_latest: return
shout("docker tag %s %s:%s" % (build_tag, ecr.project_repo, 'latest'))
shout("docker push %s:%s" % (ecr.project_repo, 'latest'), print_output=True)
print_green("Pushed %s to %s:%s" % (build_tag, ecr.project_repo, 'latest'), newline_after=True)
raise HokusaiError("Yaml files %s / %s do not exist." % (docker_compose_yml, legacy_docker_compose_yml))
if os.path.isfile(docker_compose_yml):
build_command = "docker-compose -f %s -p hokusai build" % docker_compose_yml
if os.path.isfile(legacy_docker_compose_yml):
build_command = "docker-compose -f %s -p hokusai build" % legacy_docker_compose_yml
else:
build_command = "docker-compose -f %s -p hokusai build" % filename
if config.pre_build:
build_command = "%s && %s" % (config.pre_build, build_command)
if config.post_build:
build_command = "%s && %s" % (build_command, config.post_build)
shout(build_command, print_output=True)