This repository has been archived by the owner on Jul 8, 2024. It is now read-only.
forked from majkelcc/chang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chang_compose.py
executable file
·113 lines (92 loc) · 3.72 KB
/
chang_compose.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/env python
import yaml
import sys
import os
import re
import itertools
import subprocess
import pipes
CHANG_TMP_PATH = os.environ["CHANG_TMP_PATH"]
CHANG_APP_ID = os.environ["CHANG_APP_ID"]
CHANG_APP_NAME = os.environ["CHANG_APP_NAME"]
CHANG_NETWORK = os.environ["CHANG_NETWORK"]
CHANG_SET = os.environ["CHANG_SET"]
CHANG_SYNC_ENABLED = (os.environ["CHANG_SYNC_ENABLED"] == "true")
if len(sys.argv) != 2:
chang_error("USAGE...")
class VolumesMapper:
def __init__(self):
self.i = itertools.count()
self.map = {}
def __getitem__(self, volume):
if not self.map.get(volume, False):
self.map[volume] = str(next(self.i))
return self.map[volume]
def chang_service_network_alias(name):
return subprocess.check_output(["/bin/bash", "%sc" % CHANG_SET, 'chang_service_network_alias "{0}"'.format(name)])
def chang_external_volume_name(name):
return subprocess.check_output(["/bin/bash", "%sc" % CHANG_SET, "chang_external_volume_name '%s'" % name])
VOLUMES_MAPPER = VolumesMapper()
def chang_error(message):
os.system("bash -c 'chang_error {0}'".format(message))
sys.exit(1)
chang_compose_file = sys.argv[1]
chang_compose = yaml.load(file(chang_compose_file, 'r'), Loader=FullLoader)
os.system("mkdir -p {0}".format(CHANG_TMP_PATH))
env_file = open("{0}/environment".format(CHANG_TMP_PATH), "w")
for line in chang_compose["environment"]:
env_file.write("export {0}\n".format(line))
environment = chang_compose.get("environment", [])
services = {}
keys = ["command", "user", "build", "depends_on", "working_dir", "tty", "image", "environment", "extends"]
for name in chang_compose["services"]:
desc = chang_compose["services"][name]
service = {}
for key in keys:
if desc.get(key, False):
service[key] = desc[key]
for volume in desc.get("volumes", []):
match = re.search("^([^:]+):(.*)$", volume)
volume_name = match.group(1)
volume_mount = match.group(2)
if not service.get("volumes", False):
service["volumes"] = []
if CHANG_SYNC_ENABLED or volume_name != "chang":
volume_key = VOLUMES_MAPPER[volume_name]
else:
volume_key = "./"
service["volumes"].append(volume_key + ":" + volume_mount)
env = service.get("environment", [])
service["environment"] = environment + env
service["networks"] = { "chang": { "aliases": [chang_service_network_alias(name)] }}
services[name] = service
watch_file = open("{0}/watch".format(CHANG_TMP_PATH), "w")
for watch in chang_compose.get("watch", []):
watch_file.write("export {0}\n".format(line))
volumes = {}
for volume in VOLUMES_MAPPER.map:
volumes[VOLUMES_MAPPER[volume]] = {
"external": { "name": chang_external_volume_name(volume) }
}
compose_yaml = {
"version": "2",
"services": services,
"volumes": volumes,
"networks": {
"chang": { "external": { "name": CHANG_NETWORK } }
}
}
compose_file = open("{0}/compose_file".format(CHANG_TMP_PATH), "w")
yaml.dump(compose_yaml, compose_file, default_flow_style=False)
volumes_file = open("{0}/volumes".format(CHANG_TMP_PATH), "w")
for volume in VOLUMES_MAPPER.map:
if CHANG_SYNC_ENABLED or volume_name != "chang":
volumes_file.write(chang_external_volume_name(volume) + "\n")
proxy_file = open("{0}/proxy".format(CHANG_TMP_PATH), "w")
if chang_compose.get("server", {}).get("root", False):
service, port = chang_compose["server"]["root"].split(":")
proxy_file.write("$CHANG_NETWORK $CHANG_APP_NAME $CHANG_REV_PROXY_PORT %s %s\n" % (service, port))
if chang_compose.get("server", {}).get("subdomains", False):
for subdomain in chang_compose["server"]["subdomains"]:
service, port = chang_compose["server"]["subdomains"][subdomain].split(":")
proxy_file.write("$CHANG_NETWORK %s.$CHANG_APP_NAME $CHANG_REV_PROXY_PORT %s %s\n" % (subdomain, service, port))