Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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))
def k8s_create(context, tag='latest', namespace=None, filename=None, environment=()):
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)
ecr = ECR()
if not ecr.project_repo_exists():
raise HokusaiError("ECR repository %s does not exist... did you run `hokusai setup` for this project?" % config.project_name)
if not ecr.tag_exists(tag):
raise HokusaiError("Image tag %s does not exist... did you run `hokusai registry push`?" % tag)
if tag is 'latest' and not ecr.tag_exists(context):
ecr.retag(tag, context)
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)
def __init__(self, kubernetes_yaml):
self.kubernetes_yaml = kubernetes_yaml
self.ecr = ECR()
self.tmp_filename = None
atexit.register(self.cleanup)
def push(tag, local_tag, build, filename, force, overwrite, skip_latest=False):
if force is None and shout('git status --porcelain'):
raise HokusaiError("Working directory is not clean. Aborting.")
if force is None and shout('git status --porcelain --ignored'):
raise HokusaiError("Working directory contains ignored files and/or directories. Aborting.")
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'))
if tag is None:
tag = shout('git rev-parse HEAD').strip()
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))
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)
def __init__(self, context, namespace=None):
self.context = context
self.kctl = Kubectl(self.context, namespace=namespace)
self.ecr = ECR()
def update(self, tag, skip_tags):
print_green("Deploying %s to %s..." % (tag, self.context))
if skip_tags is None:
ecr = ECR()
if self.context != tag:
ecr.retag(tag, self.context)
print_green("Updated tag %s -> %s" % (tag, self.context))
deployment_tag = "%s--%s" % (self.context, datetime.datetime.utcnow().strftime("%Y-%m-%d--%H-%M-%S"))
ecr.retag(tag, deployment_tag)
print_green("Updated tag %s -> %s" % (tag, deployment_tag))
deployment_timestamp = datetime.datetime.utcnow().strftime("%s%f")
for deployment in self.cache:
containers = deployment['spec']['template']['spec']['containers']
container_names = [container['name'] for container in containers]
deployment_targets = [{"name": name, "image": "%s:%s" % (config.aws_ecr_registry, tag)} for name in container_names]
patch = {
"spec": {
def images(reverse_sort, limit, filter_tags, digests):
images = ECR().images
sorted_images = sorted(images, key=itemgetter('imagePushedAt'), reverse=not reverse_sort)
filtered_images = filter(lambda image: 'imageTags' in image.keys(), sorted_images)
if filter_tags:
filtered_images = filter(lambda image: filter_tags in ', '.join(image['imageTags']), filtered_images)
if digests:
print_green('Image Pushed At | Image Digest | Image Tags', newline_before=True)
print_green('--------------------------------------------------------------------------------------------------------------------------------------')
else:
print_green('Image Pushed At | Image Tags', newline_before=True)
print_green('----------------------------------------------------------')
for image in filtered_images[:limit]:
image_tags = ', '.join(image['imageTags'])
if digests:
line = "%s | %s | %s" % (image['imagePushedAt'], image['imageDigest'], image_tags)
def __init__(self, context, deployment_name=None, namespace=None):
self.context = context
self.namespace = namespace
self.kctl = Kubectl(self.context, namespace=namespace)
self.ecr = ECR()
if deployment_name:
self.cache = [self.kctl.get_object("deployment %s" % deployment_name)]
else:
self.cache = self.kctl.get_objects('deployment', selector="app=%s,layer=application" % config.project_name)
check_err('git')
return_code += 1
if os.environ.get('AWS_ACCESS_KEY_ID') is not None:
check_ok('$AWS_ACCESS_KEY_ID')
else:
check_err('$AWS_ACCESS_KEY_ID')
return_code += 1
if os.environ.get('AWS_SECRET_ACCESS_KEY') is not None:
check_ok('$AWS_SECRET_ACCESS_KEY')
else:
check_err('$AWS_SECRET_ACCESS_KEY')
return_code += 1
ecr = ECR()
if ecr.project_repo_exists():
check_ok("ECR repository '%s'" % config.project_name)
else:
check_err("ECR repository '%s'" % config.project_name)
return_code += 1
if not os.path.isfile(os.path.join(os.getcwd(), 'hokusai/build.yml')):
if os.path.isfile(os.path.join(os.getcwd(), 'hokusai/common.yml')):
check_ok('./hokusai/common.yml')
else:
check_err('./hokusai/build.yml')
else:
check_ok('./hokusai/build.yml')
return_code += 1
if os.path.isfile(os.path.join(os.getcwd(), 'hokusai/development.yml')):