forked from michaelmcandrew/civicrm-docker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate.py
executable file
·172 lines (145 loc) · 4.51 KB
/
generate.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/env python
import os
from subprocess import run
import jinja2
import itertools
import json
# Define releases and variants
latest_civicrm_release = os.popen(
"curl -s https://latest.civicrm.org/stable.php"
).read()
civi_releases = [latest_civicrm_release]
cms_variants = ["drupal", "wordpress", "backdrop"]
php_releases = ["7.3", "7.2", "7.1", "7.0", "5.6"]
defaults = {"civi": latest_civicrm_release, "cms": "drupal", "php": "7.2"}
# Generate combinations
combos = {}
for civi, cms, php in itertools.product(civi_releases, cms_variants, php_releases):
major = civi.split(".")[0]
key = f"{civi}/{cms}/{php}"
combos[key] = {
"tags": [],
"variables": {"civi": civi, "cms": cms, "php": php},
"dir": f"{major}/{cms}/php{php}",
}
# Generate tags
tags = []
for civi, cms, php in itertools.product(
civi_releases + [False], cms_variants + [False], php_releases + [False]
):
tags.append({"civi": civi, "cms": cms, "php": php})
# Attach tags to combinations
for tag in tags:
# The civi tag has major, minor and rev aliases, e.g. 5-drupal and 5.10-drupal.
# We generate combinations for these using itertools.product()
tag_combos = []
if tag["civi"]:
civi_parts = tag["civi"].split(".")
tag_combos.append(
[tag["civi"], civi_parts[0] + "." + civi_parts[1], civi_parts[0]]
)
if tag["cms"]:
tag_combos.append([tag["cms"]])
if tag["php"]:
tag_combos.append(["php" + tag["php"]])
# Subsitiute
key_parts = []
for tag_key, tag_value in tag.items():
if tag_value:
key_parts.append(tag_value)
else:
key_parts.append(defaults[tag_key])
key = "/".join(key_parts)
for t in itertools.product(*tag_combos):
t = "-".join(t)
if t == "":
t = "latest"
combos[key]["tags"].append(t)
# Populate directories
root_dir = os.path.dirname(__file__)
run(["rm", "-r", root_dir + "/5"])
templates = jinja2.Environment(loader=jinja2.FileSystemLoader("templates"))
for combo in combos.values():
cms = combo["variables"]["cms"]
combo_dir = root_dir + "/" + combo["dir"]
run(["mkdir", "-p", combo_dir])
# Dockerfile
docker_file = templates.get_template("Dockerfile")
docker_file.stream(**combo["variables"]).dump(f"{combo_dir}/Dockerfile")
# civicrm.settings.php
run(
[
"cp",
f"templates/{cms}.civicrm.settings.php",
f"{combo_dir}/civicrm.settings.php",
]
)
# cms.settings.php
cms_settings_file_lookup = {
"backdrop": "settings.php",
"drupal": "settings.php",
"wordpress": "wp-config.php",
}
cms_settings_file = cms_settings_file_lookup[cms]
run(
[
"cp",
f"templates/{cms}.{cms_settings_file}",
f"{combo_dir}/{cms_settings_file}",
]
)
# init mysql, ready for load or install
run(
[
"cp",
f"templates/{cms}.civicrm-docker-init",
f"{combo_dir}/civicrm-docker-init",
]
)
# dump
run(
[
"cp",
f"templates/{cms}.civicrm-docker-dump",
f"{combo_dir}/civicrm-docker-dump",
]
)
# load
run(["cp", "templates/civicrm-docker-load", combo_dir])
# install (todo: split from init)
run(
[
"cp",
f"templates/{cms}.civicrm-docker-install",
f"{combo_dir}/civicrm-docker-install",
]
)
# common files
run(
[
"cp",
"templates/apache.conf",
"templates/civicrm_dump.php",
"templates/civicrm-docker-entrypoint",
combo_dir,
]
)
# CMS specific
if cms == "wordpress":
run(["cp", "templates/wordpress..htaccess", f"{combo_dir}/.htaccess"])
# Update tags section of the README.md
tag_text = []
for combo in combos.values():
tag_list = " ".join([f"`{tag}`" for tag in combo["tags"]])
combo_dir = combo["dir"]
tag_text.append(f"* {tag_list} [({combo_dir})]({combo_dir})\n")
# print("* " + combo["dir"] + str(combo["tags"]))
readme = list(open("README.md", "r"))
start = readme.index("<!---START_TAGS-->\n")
end = readme.index("<!---END_TAGS-->\n")
readme = readme[: start + 1] + tag_text + readme[end:]
writeme = open("README.md", "w")
writeme.write("".join(readme))
# Dump combos to a json file for other scripts
with open("combos.json", "w") as combos_file:
json.dump(combos, combos_file, sort_keys=True, indent=4)