forked from mendix/cf-mendix-buildpack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stage.py
executable file
·227 lines (193 loc) · 6.77 KB
/
stage.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#!/usr/bin/env python3
import logging
import os
import shutil
import sys
from buildpack import databroker, util
from buildpack.core import java, mxbuild, nginx, runtime
from buildpack.infrastructure import database
from buildpack.telemetry import (
appdynamics,
datadog,
dynatrace,
fluentbit,
splunk,
logs,
metering,
mx_java_agent,
newrelic,
telegraf,
)
BUILDPACK_DIR = os.path.abspath(
os.path.dirname(
os.path.dirname(os.path.join(os.path.dirname(__file__), ".."))
)
)
BUILD_DIR = os.path.abspath(sys.argv[1])
CACHE_DIR = os.path.abspath(os.path.join(sys.argv[2], "bust"))
DOT_LOCAL_LOCATION = os.path.abspath(os.path.join(BUILD_DIR, ".local"))
if len(sys.argv) >= 5:
DEPS_DIR = os.path.abspath(sys.argv[3])
DEPS_IDX = os.path.abspath(sys.argv[4])
if len(sys.argv) >= 6 and sys.argv[5] != "":
PROFILE_DIR = os.path.abspath(sys.argv[5])
else:
PROFILE_DIR = os.path.abspath(os.path.join(BUILD_DIR, ".profile.d"))
SUPPORTED_STACKS = [
"cflinuxfs3",
None,
] # None is allowed, but not supported
def check_database_environment():
try:
database.get_config()
return True
except RuntimeError as ex:
logging.error(
"You should provide a DATABASE_URL by adding a database service "
"to this application, it can be either MySQL or Postgres "
"If this is the first push of a new app, "
"set up a database service "
"and push again afterwards: %s",
ex,
)
return False
def preflight_check(version):
if not check_database_environment():
raise ValueError("Missing database configuration")
stack = os.getenv("CF_STACK")
logging.info(
"Preflight check on Mendix version [%s] and stack [%s]...",
version,
stack,
)
if not stack in SUPPORTED_STACKS:
raise NotImplementedError(
"Stack [{}] is not supported by this buildpack".format(stack)
)
if not runtime.is_version_implemented(version):
raise NotImplementedError(
"Mendix [{}] is not supported by this buildpack".format(
version.major
)
)
if not runtime.is_version_supported(version):
logging.warning(
"Mendix [{}] is end-of-support. Please use a supported Mendix version (https://docs.mendix.com/releasenotes/studio-pro/lts-mts).".format(
version.major
)
)
elif not runtime.is_version_maintained(version):
logging.info(
"Mendix [{}.{}] is not maintained. Please use a medium- or long-term supported Mendix version to easily receive fixes (https://docs.mendix.com/releasenotes/studio-pro/lts-mts).".format(
version.major, version.minor
)
)
logging.info("Preflight check completed")
def set_up_directory_structure():
logging.debug("Creating buildpack directory structure...")
util.mkdir_p(DOT_LOCAL_LOCATION)
def set_up_launch_environment():
logging.debug("Creating buildpack launch environment...")
util.mkdir_p(PROFILE_DIR)
util.set_up_launch_environment(DEPS_DIR, PROFILE_DIR)
def copy_buildpack_resources():
shutil.copytree(
os.path.join(BUILDPACK_DIR, "buildpack"),
os.path.join(BUILD_DIR, "buildpack"),
)
shutil.copytree(
os.path.join(BUILDPACK_DIR, "lib"), os.path.join(BUILD_DIR, "lib")
)
commit_file_path = os.path.join(BUILDPACK_DIR, ".commit")
if os.path.isfile(commit_file_path):
shutil.copy(
commit_file_path,
os.path.join(BUILD_DIR, ".commit"),
)
version_file_path = os.path.join(BUILDPACK_DIR, "VERSION")
if os.path.isfile(version_file_path):
shutil.copy(
version_file_path,
os.path.join(BUILD_DIR, "VERSION"),
)
def copy_dependency_file():
shutil.copy(
os.path.join(BUILDPACK_DIR, util.DEPENDENCY_FILE),
os.path.join(BUILD_DIR, util.DEPENDENCY_FILE),
)
def get_mpr_file():
return util.get_mpr_file_from_dir(BUILD_DIR)
def is_source_push():
if get_mpr_file() is not None:
return True
else:
return False
def cleanup_dependency_cache(cached_dir, dependency_list):
# get directory structure
for root, dirs, files in os.walk(cached_dir):
for file in files:
file_full_path = os.path.join(root, file)
logging.debug("dependency in cache folder: [{}]".format(file_full_path))
if file_full_path not in dependency_list:
# delete from cache
os.remove(file_full_path)
logging.debug(
"deleted unused dependency [{}] from [{}]...".format(file_full_path, root)
)
if __name__ == "__main__":
util.initialize_globals()
logging.basicConfig(
level=util.get_buildpack_loglevel(),
stream=sys.stdout,
format="%(levelname)s: %(message)s",
)
runtime_version = runtime.get_runtime_version(BUILD_DIR)
java_version = java.get_java_major_version(runtime_version)
try:
preflight_check(runtime_version)
except (ValueError, NotImplementedError) as error:
logging.error(error)
exit(1)
copy_dependency_file()
if is_source_push():
try:
mxbuild.build_from_source(
BUILDPACK_DIR,
BUILD_DIR,
CACHE_DIR,
DOT_LOCAL_LOCATION,
runtime_version,
java_version,
)
except RuntimeError as error:
logging.error(error)
exit(1)
set_up_directory_structure()
copy_buildpack_resources()
set_up_launch_environment()
java.stage(
BUILDPACK_DIR,
CACHE_DIR,
DOT_LOCAL_LOCATION,
java_version,
)
appdynamics.stage(BUILDPACK_DIR, DOT_LOCAL_LOCATION, CACHE_DIR)
dynatrace.stage(BUILDPACK_DIR, DOT_LOCAL_LOCATION, CACHE_DIR)
splunk.stage()
fluentbit.stage(BUILDPACK_DIR, DOT_LOCAL_LOCATION, CACHE_DIR)
newrelic.stage(BUILDPACK_DIR, DOT_LOCAL_LOCATION, CACHE_DIR)
mx_java_agent.stage(
BUILDPACK_DIR, DOT_LOCAL_LOCATION, CACHE_DIR, runtime_version
)
telegraf.stage(
BUILDPACK_DIR, DOT_LOCAL_LOCATION, CACHE_DIR, runtime_version
)
datadog.stage(BUILDPACK_DIR, DOT_LOCAL_LOCATION, CACHE_DIR)
metering.stage(BUILDPACK_DIR, BUILD_DIR, CACHE_DIR)
database.stage(BUILDPACK_DIR, BUILD_DIR)
runtime.stage(BUILDPACK_DIR, BUILD_DIR, CACHE_DIR)
logs.stage(BUILDPACK_DIR, BUILD_DIR, CACHE_DIR)
databroker.stage(BUILDPACK_DIR, DOT_LOCAL_LOCATION, CACHE_DIR)
nginx.stage(BUILDPACK_DIR, BUILD_DIR, CACHE_DIR)
logging.info("Mendix Cloud Foundry Buildpack staging completed")
cleanup_dependency_cache(CACHE_DIR, util.CACHED_DEPENDENCIES)