-
Notifications
You must be signed in to change notification settings - Fork 0
/
entrypoint.py
executable file
·74 lines (54 loc) · 2.21 KB
/
entrypoint.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
#!/usr/bin/env python3
import json
import os
import sys
import boto3
def debug(msg):
print(f"::debug::{msg}")
def fail(msg):
print(f"::error ::{msg}")
sys.exit(1)
def set_output(key, val):
print(f"Set output {key}={val}")
print(f"::set-output name={key}::{val}")
def load_config():
config = {
"app_name": os.environ.get("INPUT_APPLICATION_NAME"),
"env_id": os.environ.get("INPUT_ENVIRONMENT_ID"),
"env_name": os.environ.get("INPUT_ENVIRONMENT_NAME"),
}
debug(f"Loaded config: {config}")
return config
def validate_config(config):
if not config["app_name"] and not config["env_id"] and not config["env_name"]:
fail("You must specify at least environment_id or environment_name.")
if config["env_id"] and (config["env_name"] or config["app_name"]):
fail(
"If you specify environment_id, don't specify environment_name or application_name."
)
if config["app_name"] and not config["env_name"]:
fail("If you specify application_name, environment_name is required.")
if __name__ == "__main__":
CONFIG = load_config()
validate_config(CONFIG)
client = boto3.client("elasticbeanstalk")
describe_environments_params = {}
if CONFIG["app_name"]:
describe_environments_params["ApplicationName"] = CONFIG["app_name"]
if CONFIG["env_id"]:
describe_environments_params["EnvironmentIds"] = [CONFIG["env_id"]]
if CONFIG["env_name"]:
describe_environments_params["EnvironmentNames"] = [CONFIG["env_name"]]
debug(f"Request params: {describe_environments_params}")
response = client.describe_environments(**describe_environments_params)
debug(f"Response: {response}")
if len(response["Environments"]) > 1:
apps = ",".join(e["ApplicationName"] for e in response["environments"])
fail(f"Found multiple matching environments in applications: {apps}")
if not response["Environments"]:
fail("Found no matching environment.")
env = response["Environments"][0]
set_output("name", env["EnvironmentName"])
set_output("id", env["EnvironmentId"])
set_output("application", env["ApplicationName"])
set_output("version", env["VersionLabel"])