def publish(build_only, build_publish_ecr_only, publish_helm_only): pc = ProjectConf() pr = PublishRules() d = DockerCommands() h = Helm() # tags is a list in case we want to add other tags in the future tags = [Git.get_hash()] if not publish_helm_only: for tag in tags: env = {"HASH": tag} pc.build_docker(env) if not build_only: images = pc.get_docker_images() image_tags = [ Tag(image, tag) for image, tag in product(images, tags) ] asso = {} d.login(pr) for image_tag in image_tags: res = d.publish(pr, image_tag, login=False) asso[image_tag] = res if not build_only and not build_publish_ecr_only: hash = tags[0] for path in pc.get_helm_chart_paths(): h.publish(pc.name, pr, path, hash) logging.info(f"Ran publish on {pc.name} with git hash: {tags[0]}")
def stop(namespace, charts): pc = ProjectConf() helm = Helm() cr = ClusterRules(namespace=namespace) if charts is None: # Stop each of the project's charts charts = [ os.path.basename(chart_path) for chart_path in pc.get_helm_chart_paths() ] sync_required = False try: for chart_path in pc.get_helm_chart_paths(): chart_name = os.path.basename(chart_path) if chart_name in charts: hr = HelmRules(cr, chart_name) helm.stop(hr, namespace) sync_required = True finally: # Sync if any helm.stop() call succeeded, even if a subsequent one failed if sync_required: sync_ingress.sync_ingress(namespace) sync_dns.sync_dns(namespace)
def rollback(project, num_versions, namespace, chart=None): helm = Helm() hr = HelmRules(ClusterRules(namespace=namespace), chart or project) helm.rollback_relative(hr, num_versions, namespace) sync_ingress.sync_ingress(namespace) sync_dns.sync_dns(namespace)
def undeploy(project, namespace, chart=None): helm = Helm() cr = cluster_rules(namespace=namespace) hr = HelmRules(cr, chart or project) helm.stop(hr, namespace) sync_ingress.sync_ingress(namespace) sync_dns.sync_dns(namespace)
def namespace_init(namespace, force=False): helm = Helm() cr = cluster_rules(namespace=namespace) namespace_init_projects = cr.namespace_init for project in namespace_init_projects: project_name = project["project"] ref = project["ref"] repo = project.get("repo") or project_name if not force and helm.release_exists(f"{project_name}-{namespace}", namespace): continue deploy.deploy( project_name, ref, namespace, dry_run=False, force=True, repo=repo, set_override_values=[], )
def cluster_init(force=False): helm = Helm() cluster_init_projects = ClusterRules().cluster_init for project in cluster_init_projects: project_name = project["project"] ref = project["ref"] project_namespace = project["namespace"] repo = project.get("repo") or project_name if not force and helm.release_exists( f"{project_name}-{project_namespace}", project_namespace ): continue deploy.deploy( project_name, ref, project_namespace, dry_run=False, force=True, repo=repo, set_override_values=[], )
def start( namespace, charts=None, dry_run=False, with_mount=False, force_helm=False, set_override_values=None, ): if set_override_values is None: set_override_values = [] pc = ProjectConf() helm = Helm() cr = cluster_rules(namespace=namespace) if charts is None: # Start each of the project's charts charts = [os.path.basename(chart_path) for chart_path in pc.get_helm_chart_paths()] # Values precedence is command < cluster rules < --set-override-values # Start command values values = { "deploy.imageTag": "local", "deploy.mountPath": pc.mount_path, "deploy.namespace": namespace, "deploy.withMount": with_mount, "project.name": pc.name, "service.certificateArn": cr.service_certificate_arn, "service.certificateScope": cr.service_certificate_scope, "service.domainName": cr.service_domain_name_suffix, "service.clusterCertificateArn": cr.cluster_certificate_arn, "service.clusterCertificateScope": cr.cluster_certificate_scope, "service.clusterDomainName": cr.cluster_domain_name_suffix, } # Update with cluster rule values values.update(cr.values) # Update with --set-override-values value_overrides = {k: v for k, v in (value.split("=") for value in set_override_values)} values.update(value_overrides) sync_required = False try: for chart_path in pc.get_helm_chart_paths(): chart_name = os.path.basename(chart_path) if chart_name in charts: hr = HelmRules(cr, chart_name) if dry_run: helm.dry_run(hr, chart_path, cr.cluster_name, namespace, **values) else: helm.start(hr, chart_path, cr.cluster_name, namespace, force_helm, **values) sync_required = True finally: # Sync if any helm.start() call succeeded, even if a subsequent one failed if sync_required: sync_ingress.sync_ingress(namespace) sync_dns.sync_dns(namespace)
def publish(build_only, build_publish_ecr_only, publish_helm_only): pc = ProjectConf() pr = PublishRules() d = DockerCommands() h = Helm() git_hash = Git.get_hash() if not publish_helm_only: env = {"HASH": git_hash} pc.build_docker(env) if not build_only: images = pc.get_docker_images() image_tags = [Tag(image, git_hash) for image in images] asso = {} d.login(pr) for image_tag in image_tags: res = d.publish(pr, image_tag, login=False) asso[image_tag] = res if not build_only and not build_publish_ecr_only: for path in pc.get_helm_chart_paths(): h.publish(pc.name, pr, path, git_hash) logging.info(f"Ran publish on {pc.name} with git hash: {git_hash}")
def start( namespace, charts=None, dry_run=False, force_helm=False, set_override_values=None, values_files=None, ): if set_override_values is None: set_override_values = [] pc = ProjectConf() helm = Helm() cr = ClusterRules(namespace=namespace) if not charts: # Start each of the project's charts charts = [ os.path.basename(chart_path) for chart_path in pc.get_helm_chart_paths() ] # Values precedence is command < cluster rules < --set-override-values # Start command values values = { "deploy.imageTag": "local", "deploy.namespace": namespace, "project.name": pc.name, "service.certificateScope": cr.service_certificate_scope, "service.domainName": cr.service_domain_name_suffix, "service.clusterCertificateScope": cr.cluster_certificate_scope, "service.clusterDomainName": cr.cluster_domain_name_suffix, "service.clusterName": cr.cluster_domain_name, # aka root_dns } if cr.certificate_lookup: values.update({ "service.certificateArn": get_service_certificate_arn(), "service.clusterCertificateArn": get_cluster_certificate_arn(), }) # Update with cluster rule values values.update(cr.values) helm_args = [] # Update with --set-override-values values.update(dict(value.split("=") for value in set_override_values)) sync_required = False try: for chart_path in pc.get_helm_chart_paths(): chart_name = os.path.basename(chart_path) # Add user-specified values files if values_files: for file_path in values_files: helm_args.append( f"--values={os.path.join(chart_path, 'values', file_path)}" ) if chart_name in charts: hr = HelmRules(cr, chart_name) if dry_run: helm.dry_run(hr, chart_path, cr.cluster_name, namespace, helm_args=helm_args, **values) else: helm.start(hr, chart_path, cr.cluster_name, namespace, force=force_helm, helm_args=helm_args, **values) sync_required = True finally: # Sync if any helm.start() call succeeded, even if a subsequent one failed if sync_required: sync_ingress.sync_ingress(namespace) sync_dns.sync_dns(namespace)
def deploy( project, git_ref, namespace, chart=None, dry_run=False, force=False, force_helm=False, repo=None, set_override_values=None ): if set_override_values is None: set_override_values = [] with tempfile.TemporaryDirectory() as tmpdirname: pr = PublishRules() helm = Helm() cr = cluster_rules(namespace=namespace) helm_chart_path = "{}/{}".format(tmpdirname, chart or project) hr = HelmRules(cr, chart or project) git_account = load_git_configs()["account"] repo = repo or project git_url = f"[email protected]:{git_account}/{repo}.git" git_ref = Git.extract_hash(git_ref, git_url) if not force and cr.check_branch and Git.extract_hash(cr.check_branch, git_url) != git_ref: logging.error( f"You are deploying hash {git_ref} which does not match branch" f" {cr.check_branch} on cluster {cr.cluster_name} for project" f" {project}... exiting" ) sys.exit(1) helm.pull_packages(project, pr, git_ref, tmpdirname) # We need to use --set-string in case the git ref is all digits helm_args = ["--set-string", f"deploy.imageTag={git_ref}"] # Values precedence is command < cluster rules < --set-override-values # Deploy command values values = { "deploy.ecr": pr.docker_registry, "deploy.namespace": namespace, "project.name": project, "service.certificateArn": cr.service_certificate_arn, "service.certificateScope": cr.service_certificate_scope, "service.domainName": cr.service_domain_name_suffix, "service.clusterCertificateArn": cr.cluster_certificate_arn, "service.clusterCertificateScope": cr.cluster_certificate_scope, "service.clusterDomainName": cr.cluster_domain_name_suffix, } # Update with cluster rule values values.update(cr.values) # Update with --set-override-values value_overrides = {k: v for k, v in (value.split("=") for value in set_override_values)} values.update(value_overrides) if dry_run: helm.dry_run( hr, helm_chart_path, cr.cluster_name, namespace, helm_args=helm_args, **values ) else: helm.start( hr, helm_chart_path, cr.cluster_name, namespace, force_helm, helm_args=helm_args, **values, ) sync_ingress.sync_ingress(namespace) sync_dns.sync_dns(namespace)