def pull_config(context): config = HokusaiConfig().check() try: select_context(context) except HokusaiCommandError, e: print_red(repr(e)) return -1
def run(context, command, tag, with_config, with_secrets, env): config = HokusaiConfig().check() try: select_context(context) except HokusaiCommandError, e: print_red(repr(e)) return -1
def unset_secrets(context, secrets): config = HokusaiConfig().check() try: select_context(context) except HokusaiCommandError, e: print_red(repr(e)) return -1
def stack_up(context): HokusaiConfig().check() kubernetes_yml = os.path.join(os.getcwd(), "hokusai/%s.yml" % context) if not os.path.isfile(kubernetes_yml): print_red("Yaml file %s does not exist for given context." % kubernetes_yml) return -1 try: select_context(context) except HokusaiCommandError, e: print_red(repr(e)) return -1
def install(kubectl_version, platform, install_to, install_kubeconfig_to, bucket_name, key_name, bucket_region): try: print_green("Downloading and installing kubectl...") urllib.urlretrieve( "https://storage.googleapis.com/kubernetes-release/release/v%s/bin/%s/amd64/kubectl" % (kubectl_version, platform), os.path.join('/tmp', 'kubectl')) os.chmod(os.path.join('/tmp', 'kubectl'), 0755) shutil.move(os.path.join('/tmp', 'kubectl'), os.path.join(install_to, 'kubectl')) except Exception, e: print_red("Error installing kubectl: %s" % repr(e)) return -1
def push_config(context): HokusaiConfig().check() if not os.path.isfile( os.path.join(os.getcwd(), 'hokusai', "%s-config.yml" % context)): print_red("Secrets file hokusai/%s-config.yml does not exist" % context) return -1 try: select_context(context) except HokusaiCommandError, e: print_red(repr(e)) return -1
def development(): HokusaiConfig().check() docker_compose_yml = os.path.join(os.getcwd(), 'hokusai/development.yml') if not os.path.isfile(docker_compose_yml): print_red("Yaml file %s does not exist." % docker_compose_yml) return -1 def cleanup(*args): return 0 for sig in EXIT_SIGNALS: signal.signal(sig, cleanup) call(verbose("docker-compose -f %s up --build" % docker_compose_yml), shell=True)
def pull(): config = HokusaiConfig().check() try: login_command = check_output(verbose("aws ecr get-login --region %s" % config.aws_ecr_region), shell=True) check_call(verbose(login_command), shell=True) print_green("Pulling from %s..." % config.aws_ecr_registry) check_output(verbose("docker pull %s --all-tags" % config.aws_ecr_registry), shell=True) print_green("Pull succeeded") except CalledProcessError: print_red('Pull failed') return -1 return 0
def push(tag, test_build): config = HokusaiConfig().check() try: login_command = check_output("aws ecr get-login --region %s" % config.aws_ecr_region, shell=True) check_call(login_command, shell=True) if test_build: build = "ci_%s:latest" % config.project_name else: build = "build_%s:latest" % config.project_name check_call("docker tag %s %s:%s" % (build, config.aws_ecr_registry, tag), shell=True) check_call("docker push %s:%s" % (config.aws_ecr_registry, tag), shell=True) print_green("Pushed %s to %s:%s" % (build, config.aws_ecr_registry, tag)) except CalledProcessError: print_red('Push failed') return -1 return 0
def test(): config = HokusaiConfig().check() docker_compose_yml = os.path.join(os.getcwd(), 'hokusai/test.yml') if not os.path.isfile(docker_compose_yml): print_red("Yaml file %s does not exist." % docker_compose_yml) return -1 # stop any running containers def cleanup(*args): print_red('Tests Failed For Unexpected Reasons\n') call(verbose("docker-compose -f %s -p ci stop" % docker_compose_yml), shell=True) return -1 # catch exit, do cleanup for sig in EXIT_SIGNALS: signal.signal(sig, cleanup) # build and run the composed services if call(verbose("docker-compose -f %s -p ci up --build -d" % docker_compose_yml), shell=True) != 0: print_red("Docker Compose Failed\n") return -1 # wait for the test service to complete and grab the exit code try: test_exit_code = int(check_output(verbose("docker wait ci_%s_1" % config.project_name), shell=True)) except CalledProcessError: print_red('Docker wait failed.') call(verbose("docker-compose -f %s -p ci stop" % docker_compose_yml), shell=True) return -1 # output the logs for the test (for clarity) call(verbose("docker logs ci_%s_1" % config.project_name), shell=True) # inspect the output of the test and display respective message if test_exit_code != 0: print_red('Tests Failed - Exit Code: %s\n' % test_exit_code) else: print_green("Tests Passed") # cleanup call(verbose("docker-compose -f %s -p ci stop" % docker_compose_yml), shell=True) return test_exit_code
def check_err(check_item): print_red(u'\u2718 ' + check_item + ' not found')
bucket_name, key_name, bucket_region): try: print_green("Downloading and installing kubectl...") urllib.urlretrieve( "https://storage.googleapis.com/kubernetes-release/release/v%s/bin/%s/amd64/kubectl" % (kubectl_version, platform), os.path.join('/tmp', 'kubectl')) os.chmod(os.path.join('/tmp', 'kubectl'), 0755) shutil.move(os.path.join('/tmp', 'kubectl'), os.path.join(install_to, 'kubectl')) except Exception, e: print_red("Error installing kubectl: %s" % repr(e)) return -1 try: print_green("Configuring kubectl...") if not os.path.isdir(install_kubeconfig_to): mkpath(install_kubeconfig_to) conn = boto.s3.connect_to_region(bucket_region) bucket = conn.get_bucket(conn.lookup(bucket_name)) k = Key(bucket) k.key = key_name k.get_contents_to_filename( os.path.join(install_kubeconfig_to, 'config')) except Exception, e: print_red("Error configuring kubectl: %s" % repr(e)) return -1 return 0
def cleanup(*args): print_red('Tests Failed For Unexpected Reasons\n') call(verbose("docker-compose -f %s -p ci stop" % docker_compose_yml), shell=True) return -1
print_red(repr(e)) return -1 try: existing_configmap = check_output(verbose( "kubectl get configmap %s-config -o yaml" % config.project_name), stderr=STDOUT, shell=True) configmap_data = yaml.load(existing_configmap)['data'] except CalledProcessError, e: if 'Error from server: configmaps "%s-config" not found' % config.project_name in e.output: print("ConfigMap %s-config not found. Creating..." % config.project_name) configmap_data = {} else: print_red("Failed to pull configmap hokusai/%s-config.yml" % context) return -1 configmap_yaml = OrderedDict([('apiVersion', 'v1'), ('kind', 'ConfigMap'), ('metadata', { 'labels': { 'app': config.project_name }, 'name': "%s-config" % config.project_name }), ('data', configmap_data)]) with open(os.path.join(os.getcwd(), 'hokusai', "%s-config.yml" % context), 'w') as f: f.write(yaml.safe_dump(configmap_yaml, default_flow_style=False)) print_green("Pulled configmap hokusai/%s-config.yml" % context)
"value": x.split('=')[1] }, env) if with_config: try: existing_configmap = check_output( verbose("kubectl get configmap %s-config -o yaml" % config.project_name), stderr=STDOUT, shell=True) configmap_data = yaml.load(existing_configmap)['data'] except CalledProcessError, e: if 'Error from server: configmaps "%s-config" not found' % config.project_name in e.output: configmap_data = {} else: print_red("Error fetching config: %s" % e.output) return -1 for k, v in configmap_data.iteritems(): environment.append({"name": k, "value": v}) if with_secrets: try: existing_secrets = check_output(verbose( "kubectl get secret %s-secrets -o yaml" % config.project_name), stderr=STDOUT, shell=True) secret_data = yaml.load(existing_secrets)['data'] except CalledProcessError, e: if 'Error from server: secrets "%s-secrets" not found' % config.project_name in e.output: secret_data = {}
try: select_context(context) except HokusaiCommandError, e: print_red(repr(e)) return -1 try: existing_secrets = check_output(verbose( "kubectl get secret %s-secrets -o yaml" % config.project_name), stderr=STDOUT, shell=True) secret_data = yaml.load(existing_secrets)['data'] for k, v in secret_data.iteritems(): print("%s=%s" % (k, base64.b64decode(v))) except CalledProcessError, e: print_red("Error: %s" % e.output) return -1 return 0 def set_secrets(context, secrets): config = HokusaiConfig().check() try: select_context(context) except HokusaiCommandError, e: print_red(repr(e)) return -1 try: existing_secrets = check_output(verbose(
kubernetes_yml = os.path.join(os.getcwd(), "hokusai/%s.yml" % context) if not os.path.isfile(kubernetes_yml): print_red("Yaml file %s does not exist for given context." % kubernetes_yml) return -1 try: select_context(context) except HokusaiCommandError, e: print_red(repr(e)) return -1 try: check_call(verbose("kubectl apply -f %s" % kubernetes_yml), shell=True) except CalledProcessError, e: print_red("Stack up failed with error: %s" % e.output) return -1 print_green("Stack %s created" % kubernetes_yml) return 0 def stack_down(context): HokusaiConfig().check() kubernetes_yml = os.path.join(os.getcwd(), "hokusai/%s.yml" % context) if not os.path.isfile(kubernetes_yml): print_red("Yaml file %s does not exist for given context." % kubernetes_yml) return -1
from hokusai.common import print_red, print_green, verbose, select_context, HokusaiCommandError, kubernetes_object def promote(from_context, context): config = HokusaiConfig().check() try: select_context(from_context) except HokusaiCommandError, e: print_red(repr(e)) return -1 deployments = kubernetes_object('deployment', selector="app=%s" % config.project_name) if len(deployments['items']) != 1: print_red("Multiple deployments found for %s" % config.project_name) return -1 deployment = deployments['items'][0] containers = deployment['spec']['template']['spec']['containers'] container_names = [container['name'] for container in containers] container_images = [container['image'] for container in containers] if not all(x == container_images[0] for x in container_images): print_red( "Deployment's containers do not reference the same image tag - aborting..." ) return -1 base_image = containers[0]['image'] if config.aws_ecr_registry not in base_image:
context)) deployment_tag = "%s--%s" % (context, datetime.datetime.utcnow(). strftime("%Y-%m-%d--%H-%M-%S")) check_call(verbose("docker tag %s:%s %s:%s" % (config.aws_ecr_registry, tag, config.aws_ecr_registry, deployment_tag)), shell=True) check_call(verbose("docker push %s:%s" % (config.aws_ecr_registry, deployment_tag)), shell=True) print_green("Updated tag %s:%s -> %s:%s" % (config.aws_ecr_registry, tag, config.aws_ecr_registry, deployment_tag)) except CalledProcessError, e: print_red("Updating tags failed with error: %s" % e.output) return -1 deployments = kubernetes_object('deployment', selector="app=%s" % config.project_name) if len(deployments['items']) != 1: print_red("Multiple deployments found for %s" % config.project_name) return -1 deployment = deployments['items'][0] 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]