forked from uptick/gitops
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtasks.py
96 lines (66 loc) · 2.44 KB
/
tasks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import json
import os
from base64 import b64encode
import yaml
from invoke import run, task
@task
def test(ctx, pty=True):
run("py.test")
@task
def lint(ctx, pty=True):
run("pre-commit run --all-files")
@task
def build(ctx):
"""Build and push a Docker image to ECR.
Uses the short hash code for the Git repo to identify this build. This
allows for easier rollback.
"""
local = get_latest_image()
print(f"Building container ({local}) ... ", flush=True)
run(f"docker build -t {local} .")
@task
def push(ctx, tag=None):
local = get_latest_image()
remote = get_remote_image(tag)
password = run("aws ecr get-login-password", hide=True, warn=False).stdout.strip()
run(f"docker login -u AWS -p {password} https://{get_repo_uri()}", hide=True)
run(f"docker tag {local} {remote}", hide=False)
print(f"Pushing to ECR ({remote}) ... ", flush=True)
run(f"docker push {remote}", pty=True)
@task
def logs(ctx):
name = run(
"kubectl -n default get pods --selector=app=gitops -o jsonpath='{.items[*].metadata.name}'",
hide=True,
).stdout.strip()
run(f"kubectl -n default logs -f {name}", pty=True)
@task
def test_helm(ctx):
"""Tests / validates a dry installation of the helm chart"""
run(
"helm install --dry-run --debug --set environment.GIT_CRYPT_KEY_FILE='test' --set"
" environment.CLUSTER_NAME='hi' --set environment.CLUSTER_NAMESPACE='test' debug"
" charts/gitops/"
)
def get_commit_tag():
return run("git rev-parse --short HEAD", hide=True).stdout.strip()
def get_account_id():
caller_identity = run("aws sts get-caller-identity", hide=True).stdout.strip()
return json.loads(caller_identity)["Account"]
def get_repo_uri():
return f"{get_account_id()}.dkr.ecr.ap-southeast-2.amazonaws.com"
def get_latest_image() -> str:
return get_remote_image(tag="latest")
def get_remote_image(tag=None) -> str:
tag = tag or get_commit_tag()
return f"{get_repo_uri()}/gitops:{tag}"
def get_secret(name):
return b64encode(os.environ[name].encode()).decode()
def get_secret_file(name):
data = open(os.environ[name], "rb").read()
return b64encode(data).decode()
def get_cluster_name():
data = run("kubectl config view", hide=True).stdout.strip()
conf = yaml.safe_load(data)
contexts = {c["name"]: c["context"] for c in conf["contexts"]}
return contexts[conf["current-context"]]["cluster"].split(":cluster/")[-1]