Ejemplo n.º 1
0
def deploy(
    project,
    git_ref,
    namespace,
    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_path = "{}/{}".format(tmpdirname, project)
        hr = HelmRules(cr, 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_package(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 = {
            "service.certificateArn": cr.get_certificate_arn(),
            "deploy.ecr": pr.docker_registry,
        }
        # 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_path,
                         cr.cluster_name,
                         namespace,
                         helm_args=helm_args,
                         **values)
        else:
            helm.start(hr,
                       helm_path,
                       cr.cluster_name,
                       namespace,
                       force_helm,
                       helm_args=helm_args,
                       **values)
            sync_ingress.sync_ingress(namespace)
            sync_dns.sync_dns(namespace)