-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathentrypoint.py
More file actions
100 lines (91 loc) · 4 KB
/
entrypoint.py
File metadata and controls
100 lines (91 loc) · 4 KB
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
97
98
99
100
import os
import sys
import json
import subprocess
import glob
if __name__ == "__main__":
project = sys.argv[1]
location = sys.argv[2]
goblet_path = sys.argv[3]
stage = sys.argv[4]
envars = sys.argv[5]
build_envars = sys.argv[6]
custom_command = sys.argv[7]
artifact_auth = sys.argv[8]
poetry = sys.argv[9]
poetry_version = sys.argv[10]
requirements_file = sys.argv[11]
apt_packages = sys.argv[12]
if apt_packages:
command = ["apt-get", "install", "-y"]
command.extend([package.strip() for package in apt_packages.split(',')])
apt = subprocess.run(command, capture_output=True)
if apt.returncode != 0:
raise Exception(apt.stderr)
# install desired version og goblet
if artifact_auth == "yes":
pip = subprocess.run(["pip", "install", "keyrings.google-artifactregistry-auth==1.1.1"], capture_output=True)
if pip.returncode != 0:
raise Exception(pip.stderr)
os.chdir(goblet_path)
if poetry != "yes":
if requirements_file == "":
requirements_file = "requirements.txt"
pip = subprocess.run(["pip", "install", "-r", requirements_file], capture_output=True)
if pip.returncode != 0:
raise Exception(pip.stderr)
elif poetry == "yes" and poetry_version != "":
pip_install_poetry = subprocess.run(["pip", "install", f"poetry=={poetry_version}"], capture_output=True)
poetry_config = subprocess.run(["poetry", "config", "virtualenvs.create", "false"], capture_output=True)
poetry_install = subprocess.run(["poetry", "install", "--no-dev", "--no-root"], capture_output=True)
if pip_install_poetry.returncode != 0 or poetry_config.returncode != 0 or poetry_install.returncode != 0:
raise Exception(
[pip_install_poetry.stderr,
poetry_config.stderr,
poetry_install.stderr
]
)
stage_sub_command = ""
config_sub_command = ""
if stage:
stage_sub_command = f"--stage {stage}"
if envars or build_envars:
config = {
"cloudfunction": {
"environmentVariables": {},
"buildEnvironmentVariables": {}
}
}
if envars:
envars_list = envars.split(',')
for envar in envars_list:
try:
key, value = envar.split(':', 1)
config["cloudfunction"]["environmentVariables"][key] = value
except ValueError:
raise "Environment variables are in the wrong format. Should be '{k1}:{v1},{k2}:{v2},...'"
if build_envars:
build_envars_list = build_envars.split(',')
for build_envar in build_envars_list:
try:
key, value = build_envar.split(':', 1)
config["cloudfunction"]["buildEnvironmentVariables"][key] = value
except ValueError:
raise "Build Environment variables are in the wrong format. Should be '{k1}:{v1},{k2}:{v2},...'"
config_sub_command = f"--config-from-json-string {json.dumps(config, separators=(',', ':'))}"
if custom_command:
command = custom_command
else:
command = f"goblet deploy --project {project} --location {location} {stage_sub_command} {config_sub_command}"
# subprocess takes in list of strings. strip to get rid of white space from undefined, optional fields
goblet = subprocess.run(command.strip().split(), capture_output=True)
# Set openapispec output
files = glob.glob(".goblet/*.yml")
if len(files) >= 1:
with open(files[0], 'r') as f:
openapi_spec = f.read().replace("%", "%25").replace("\n", "%0A").replace("\r", "%0D")
if "GITHUB_OUTPUT" in os.environ:
with open(os.environ["GITHUB_OUTPUT"], "a") as f:
print(f"openapispec={openapi_spec}", file=f)
if goblet.returncode != 0:
raise Exception(goblet.stderr)