-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart.py
1009 lines (854 loc) · 33.1 KB
/
start.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
import json
import os
import re
import signal
import subprocess
import time
import sys
import base64
import uuid
import atexit
import traceback
sys.path.insert(0, 'lib')
import requests
import buildpackutil
import logging
import instadeploy
import metrics
from m2ee import M2EE, logger
from nginx import get_path_config, gen_htpasswd
from buildpackutil import i_am_primary_instance
logger.setLevel(buildpackutil.get_buildpack_loglevel())
logger.info('Started Mendix Cloud Foundry Buildpack v1.8.2')
logging.getLogger('m2ee').propagate = False
app_is_restarting = False
default_m2ee_password = str(uuid.uuid4()).replace('-', '@') + 'A1'
nginx_process = None
def get_nginx_port():
return int(os.environ['PORT'])
def get_runtime_port():
return get_nginx_port() + 1
def get_admin_port():
return get_nginx_port() + 2
def get_deploy_port():
return get_nginx_port() + 3
def pre_process_m2ee_yaml():
subprocess.check_call([
'sed',
'-i',
's|BUILD_PATH|%s|g; s|RUNTIME_PORT|%d|; s|ADMIN_PORT|%d|'
% (os.getcwd(), get_runtime_port(), get_admin_port()),
'.local/m2ee.yaml'
])
def use_instadeploy(mx_version):
return mx_version >= 6.7
def get_admin_password():
return os.getenv('ADMIN_PASSWORD')
def get_m2ee_password():
m2ee_password = os.getenv('M2EE_PASSWORD', get_admin_password())
if not m2ee_password:
logger.warning('No M2EE_PASSWORD set, generating a random password for protection')
m2ee_password = default_m2ee_password
return m2ee_password
def set_up_nginx_files(m2ee):
lines = ''
x_frame_options = os.environ.get('X_FRAME_OPTIONS', 'ALLOW')
if x_frame_options == 'ALLOW':
x_frame_options = ''
else:
x_frame_options = "add_header X-Frame-Options '%s';" % x_frame_options
if use_instadeploy(m2ee.config.get_runtime_version()):
mxbuild_upstream = 'proxy_pass http://mendix_mxbuild'
else:
mxbuild_upstream = 'return 501'
with open('nginx/conf/nginx.conf') as fh:
lines = ''.join(fh.readlines())
lines = lines.replace(
'CONFIG', get_path_config()
).replace(
'NGINX_PORT', str(get_nginx_port())
).replace(
'RUNTIME_PORT', str(get_runtime_port())
).replace(
'ADMIN_PORT', str(get_admin_port())
).replace(
'DEPLOY_PORT', str(get_deploy_port())
).replace(
'ROOT', os.getcwd()
).replace(
'XFRAMEOPTIONS', x_frame_options
).replace(
'MXBUILD_UPSTREAM', mxbuild_upstream
)
for line in lines.split('\n'):
logger.debug(line)
with open('nginx/conf/nginx.conf', 'w') as fh:
fh.write(lines)
gen_htpasswd({'MxAdmin': get_m2ee_password()})
gen_htpasswd(
{'deploy': os.getenv('DEPLOY_PASSWORD')},
file_name_suffix='-mxbuild'
)
def start_nginx():
global nginx_process
nginx_process = subprocess.Popen([
'nginx/sbin/nginx', '-p', 'nginx', '-c', 'conf/nginx.conf'
])
def get_vcap_data():
if os.environ.get('VCAP_APPLICATION'):
return json.loads(os.environ.get('VCAP_APPLICATION'))
else:
return {
'application_uris': ['example.com'],
'application_name': 'My App',
}
def activate_license():
prefs_dir = os.path.expanduser('~/../.java/.userPrefs/com/mendix/core')
buildpackutil.mkdir_p(prefs_dir)
prefs_template = """<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE map SYSTEM "http://java.sun.com/dtd/preferences.dtd">
<map MAP_XML_VERSION="1.0">
<entry key="id" value="{{LICENSE_ID}}"/>
<entry key="license_key" value="{{LICENSE_KEY}}"/>
</map>"""
license_key = os.environ.get(
'FORCED_LICENSE_KEY',
os.environ.get('LICENSE_KEY', None)
)
server_id = os.environ.get(
'FORCED_SERVER_ID',
os.environ.get('SERVER_ID', None)
)
license_id = os.environ.get(
'FORCED_LICENSE_ID',
os.environ.get('LICENSE_ID', None)
)
if server_id:
logger.warning('SERVER_ID is deprecated, please use LICENSE_ID instead')
if not license_id:
license_id = server_id
if license_key is not None and license_id is not None:
logger.debug('A license was supplied so going to activate it')
prefs_body = prefs_template.replace(
'{{LICENSE_ID}}', license_id
).replace(
'{{LICENSE_KEY}}', license_key
)
with open(os.path.join(prefs_dir, 'prefs.xml'), 'w') as prefs_file:
prefs_file.write(prefs_body)
def get_scheduled_events(metadata):
scheduled_events = os.getenv('SCHEDULED_EVENTS', None)
if not i_am_primary_instance():
logger.debug(
'Disabling all scheduled events because I am not the primary '
'instance'
)
return ('NONE', None)
elif scheduled_events is None or scheduled_events == 'ALL':
logger.debug('Enabling all scheduled events')
return ('ALL', None)
elif scheduled_events == 'NONE':
logger.debug('Disabling all scheduled events')
return ('NONE', None)
else:
parsed_scheduled_events = scheduled_events.split(',')
metadata_scheduled_events = [
scheduled_event['Name']
for scheduled_event
in metadata['ScheduledEvents']
]
result = []
for scheduled_event in parsed_scheduled_events:
if scheduled_event not in metadata_scheduled_events:
logger.warning(
'Scheduled event defined but not detected in model: "%s"'
% scheduled_event
)
else:
result.append(scheduled_events)
logger.debug('Enabling scheduled events %s' % ','.join(result))
return ('SPECIFIED', result)
def get_constants(metadata):
constants = {}
constants_from_json = {}
constants_json = os.environ.get(
'CONSTANTS',
json.dumps(constants_from_json)
)
try:
constants_from_json = json.loads(constants_json)
except Exception as e:
logger.warning('Failed to parse CONSTANTS: ' + str(e))
for constant in metadata['Constants']:
constant_name = constant['Name']
env_name = 'MX_%s' % constant_name.replace('.', '_')
value = os.environ.get(
env_name,
constants_from_json.get(constant_name)
)
if value is None:
value = constant['DefaultValue']
logger.debug(
'Constant not found in environment, taking default '
'value %s' % constant_name
)
if constant['Type'] == 'Integer':
value = int(value)
constants[constant_name] = value
return constants
def set_jvm_memory(javaopts, vcap_max_mem, java_version):
max_memory = os.environ.get('MEMORY_LIMIT')
env_heap_size = os.environ.get('HEAP_SIZE')
if max_memory:
match = re.search('([0-9]+)M', max_memory.upper())
limit = int(match.group(1))
else:
limit = int(vcap_max_mem)
if limit >= 8192:
heap_size = limit - 2048
elif limit >= 4096:
heap_size = limit - 1536
elif limit >= 2048:
heap_size = limit - 1024
else:
heap_size = int(limit / 2)
heap_size = str(heap_size) + 'M'
if env_heap_size:
max_memory = max_memory[:-1] if max_memory else vcap_max_mem
heap_size = env_heap_size if int(env_heap_size[:-1]) < int(max_memory) else heap_size
javaopts.append('-Xmx%s' % heap_size)
javaopts.append('-Xms%s' % heap_size)
if java_version.startswith('7'):
javaopts.append('-XX:MaxPermSize=256M')
elif java_version.startswith('8'):
javaopts.append('-XX:MaxMetaspaceSize=256M')
logger.debug('Java heap size set to %s' % heap_size)
def _get_s3_specific_config(vcap_services, m2ee):
access_key = secret = bucket = encryption_keys = key_suffix = None
endpoint = None
v2_auth = ''
amazon_s3 = None
for key in vcap_services:
if key.startswith("amazon-s3") or key == "objectstore":
amazon_s3 = key
if amazon_s3:
_conf = vcap_services[amazon_s3][0]['credentials']
access_key = _conf['access_key_id']
secret = _conf['secret_access_key']
bucket = _conf['bucket'] # see below at hacky for actual conf
if 'encryption_keys' in _conf:
encryption_keys = _conf['encryption_keys']
if 'key_suffix' in _conf:
key_suffix = _conf['key_suffix']
if 'host' in _conf:
endpoint = _conf['host']
if 'endpoint' in _conf:
endpoint = _conf['endpoint']
# hacky way to switch from suffix to prefix configuration
if 'key_prefix' in _conf and 'endpoint' in _conf:
bucket = _conf['key_prefix'].replace('/', '')
endpoint = _conf['endpoint'] + '/' + _conf['bucket']
key_suffix = None
elif 'p-riakcs' in vcap_services:
_conf = vcap_services['p-riakcs'][0]['credentials']
access_key = _conf['access_key_id']
secret = _conf['secret_access_key']
pattern = r'https://(([^:]+):([^@]+)@)?([^/]+)/(.*)'
match = re.search(pattern, _conf['uri'])
endpoint = 'https://' + match.group(4)
bucket = match.group(5)
v2_auth = 'true'
access_key = os.getenv('S3_ACCESS_KEY_ID', access_key)
secret = os.getenv('S3_SECRET_ACCESS_KEY', secret)
bucket = os.getenv('S3_BUCKET_NAME', bucket)
if 'S3_ENCRYPTION_KEYS' in os.environ:
encryption_keys = json.loads(os.getenv('S3_ENCRYPTION_KEYS'))
dont_perform_deletes = os.getenv('S3_PERFORM_DELETES', 'true').lower() == 'false'
key_suffix = os.getenv('S3_KEY_SUFFIX', key_suffix)
endpoint = os.getenv('S3_ENDPOINT', endpoint)
v2_auth = os.getenv('S3_USE_V2_AUTH', v2_auth).lower() == 'true'
sse = os.getenv('S3_USE_SSE', '').lower() == 'true'
if not (access_key and secret and bucket):
return None
logger.info(
'S3 config detected, activating external file store'
)
config = {
'com.mendix.core.StorageService': 'com.mendix.storage.s3',
'com.mendix.storage.s3.AccessKeyId': access_key,
'com.mendix.storage.s3.SecretAccessKey': secret,
'com.mendix.storage.s3.BucketName': bucket,
}
if dont_perform_deletes:
logger.debug('disabling perform deletes for runtime')
config['com.mendix.storage.s3.PerformDeleteFromStorage'] = False
if key_suffix:
config['com.mendix.storage.s3.ResourceNameSuffix'] = key_suffix
if v2_auth:
config['com.mendix.storage.s3.UseV2Auth'] = v2_auth
if endpoint:
config['com.mendix.storage.s3.EndPoint'] = endpoint
if m2ee.config.get_runtime_version() >= 5.17 and encryption_keys:
config['com.mendix.storage.s3.EncryptionKeys'] = encryption_keys
if m2ee.config.get_runtime_version() >= 6 and sse:
config['com.mendix.storage.s3.UseSSE'] = sse
return config
def _get_swift_specific_config(vcap_services, m2ee):
if 'Object-Storage' not in vcap_services:
return None
if m2ee.config.get_runtime_version() < 6.7:
logger.warning('Can not configure Object Storage with Mendix < 6.7')
return None
creds = vcap_services['Object-Storage'][0]['credentials']
container_name = os.getenv('SWIFT_CONTAINER_NAME', 'mendix')
return {
'com.mendix.core.StorageService': 'com.mendix.storage.swift',
'com.mendix.storage.swift.Container': container_name,
'com.mendix.storage.swift.Container.AutoCreate': True,
'com.mendix.storage.swift.credentials.DomainId': creds['domainId'],
'com.mendix.storage.swift.credentials.Authurl': creds['auth_url'],
'com.mendix.storage.swift.credentials.Username': creds['username'],
'com.mendix.storage.swift.credentials.Password': creds['password'],
'com.mendix.storage.swift.credentials.Region': creds['region'],
}
def _get_azure_storage_specific_config(vcap_services, m2ee):
if 'azure-storage' not in vcap_services:
return None
if m2ee.config.get_runtime_version() < 6.7:
logger.warning('Can not configure Azure Storage with Mendix < 6.7')
return None
creds = vcap_services['azure-storage'][0]['credentials']
container_name = os.getenv('AZURE_CONTAINER_NAME', 'mendix')
return {
'com.mendix.core.StorageService': 'com.mendix.storage.azure',
'com.mendix.storage.azure.Container': container_name,
'com.mendix.storage.azure.AccountName': creds['storage_account_name'],
'com.mendix.storage.azure.AccountKey': creds['primary_access_key'],
}
def get_filestore_config(m2ee):
vcap_services = buildpackutil.get_vcap_services_data()
config = _get_s3_specific_config(vcap_services, m2ee)
if config is None:
config = _get_swift_specific_config(vcap_services, m2ee)
if config is None:
config = _get_azure_storage_specific_config(vcap_services, m2ee)
if config is None:
logger.warning(
'External file store not configured, uploaded files in the app '
'will not persist across restarts. See https://github.com/mendix/'
'cf-mendix-buildpack for file store configuration details.'
)
return {}
else:
return config
def get_certificate_authorities():
config = {}
cas = os.getenv('CERTIFICATE_AUTHORITIES', None)
if cas:
ca_list = cas.split('-----BEGIN CERTIFICATE-----')
n = 0
files = []
for ca in ca_list:
if '-----END CERTIFICATE-----' in ca:
ca = '-----BEGIN CERTIFICATE-----' + ca
location = os.path.abspath(
'.local/certificate_authorities.%d.crt' % n
)
with open(location, 'w') as output_file:
output_file.write(ca)
files.append(location)
n += 1
config['CACertificates'] = ','.join(files)
return config
def get_client_certificates():
config = {}
client_certificates_json = os.getenv('CLIENT_CERTIFICATES', '[]')
'''
[
{
'pfx': 'base64...', # required
'password': '',
'pin_to': ['Module.WS1', 'Module2.WS2'] # optional
},
{...}
]
'''
client_certificates = json.loads(client_certificates_json)
num = 0
files = []
passwords = []
pins = {}
for client_certificate in client_certificates:
pfx = base64.b64decode(client_certificate['pfx'])
location = os.path.abspath(
'.local/client_certificate.%d.crt' % num
)
with open(location, 'w') as f:
f.write(pfx)
passwords.append(client_certificate['password'])
files.append(location)
if 'pin_to' in client_certificate:
for ws in client_certificate['pin_to']:
pins[ws] = location
num += 1
if len(files) > 0:
config['ClientCertificates'] = ','.join(files)
config['ClientCertificatePasswords'] = ','.join(passwords)
config['WebServiceClientCertificates'] = pins
return config
def get_custom_settings(metadata, existing_config):
if os.getenv('USE_DATA_SNAPSHOT', 'false').lower() == 'true':
custom_settings_key = 'Configuration'
if custom_settings_key in metadata:
config = {}
for k, v in metadata[custom_settings_key].iteritems():
if k not in existing_config:
config[k] = v
return config
return {}
def get_custom_runtime_settings():
custom_runtime_settings = {}
custom_runtime_settings_json = os.environ.get(
'CUSTOM_RUNTIME_SETTINGS',
json.dumps(custom_runtime_settings)
)
try:
custom_runtime_settings = json.loads(custom_runtime_settings_json)
except Exception as e:
logger.warning('Failed to parse CUSTOM_RUNTIME_SETTINGS: ' + str(e))
for k, v in os.environ.iteritems():
if k.startswith('MXRUNTIME_'):
custom_runtime_settings[
k.replace('MXRUNTIME_', '', 1).replace('_', '.')
] = v
return custom_runtime_settings
def is_development_mode():
return os.getenv('DEVELOPMENT_MODE', '').lower() == 'true'
def set_runtime_config(metadata, mxruntime_config, vcap_data, m2ee):
scheduled_event_execution, my_scheduled_events = (
get_scheduled_events(metadata)
)
app_config = {
'ApplicationRootUrl': 'https://%s' % vcap_data['application_uris'][0],
'MicroflowConstants': get_constants(metadata),
'ScheduledEventExecution': scheduled_event_execution,
}
if my_scheduled_events is not None:
app_config['MyScheduledEvents'] = my_scheduled_events
if is_development_mode():
logger.warning(
'Runtime is being started in Development Mode. Set '
'DEVELOPMENT_MODE to "false" (currently "true") to '
'set it to production.'
)
app_config['DTAPMode'] = 'D'
if (m2ee.config.get_runtime_version() >= 7 and
not i_am_primary_instance()):
app_config['com.mendix.core.isClusterSlave'] = 'true'
elif (m2ee.config.get_runtime_version() >= 5.15 and
os.getenv('ENABLE_STICKY_SESSIONS', 'false').lower() == 'true'):
logger.info('Enabling sticky sessions')
app_config['com.mendix.core.SessionIdCookieName'] = 'JSESSIONID'
buildpackutil.mkdir_p(os.path.join(os.getcwd(), 'model', 'resources'))
mxruntime_config.update(app_config)
mxruntime_config.update(buildpackutil.get_database_config(
development_mode=is_development_mode(),
))
mxruntime_config.update(get_filestore_config(m2ee))
mxruntime_config.update(get_certificate_authorities())
mxruntime_config.update(get_client_certificates())
mxruntime_config.update(get_custom_settings(metadata, mxruntime_config))
mxruntime_config.update(get_custom_runtime_settings())
def set_application_name(m2ee, name):
logger.debug('Application name is %s' % name)
m2ee.config._conf['m2ee']['app_name'] = name
def activate_appdynamics(m2ee, app_name):
if not buildpackutil.appdynamics_used():
return
logger.info('Adding app dynamics')
m2ee.config._conf['m2ee']['javaopts'].append(
'-javaagent:{path}'.format(
path=os.path.abspath('.local/ver4.3.5.7/javaagent.jar')
)
)
APPDYNAMICS_AGENT_NODE_NAME = 'APPDYNAMICS_AGENT_NODE_NAME'
if os.getenv(APPDYNAMICS_AGENT_NODE_NAME):
m2ee.config._conf['m2ee']['custom_environment'][
APPDYNAMICS_AGENT_NODE_NAME
] = (
'%s-%s' % (
os.getenv(APPDYNAMICS_AGENT_NODE_NAME),
os.getenv('CF_INSTANCE_INDEX', '0'),
)
)
def activate_new_relic(m2ee, app_name):
if buildpackutil.get_new_relic_license_key() is None:
logger.debug(
'Skipping New Relic setup, no license key found in environment'
)
return
logger.info('Adding new relic')
m2ee_section = m2ee.config._conf['m2ee']
if 'custom_environment' not in m2ee_section:
m2ee_section['custom_environment'] = {}
m2ee_section['custom_environment']['NEW_RELIC_LICENSE_KEY'] = (
buildpackutil.get_new_relic_license_key()
)
m2ee_section['custom_environment']['NEW_RELIC_APP_NAME'] = app_name
m2ee_section['custom_environment']['NEW_RELIC_LOG'] = (
os.path.abspath('newrelic/agent.log')
)
m2ee.config._conf['m2ee']['javaopts'].append(
'-javaagent:{path}'.format(
path=os.path.abspath('newrelic/newrelic.jar')
)
)
def set_up_m2ee_client(vcap_data):
m2ee = M2EE(yamlfiles=['.local/m2ee.yaml'], load_default_files=False, config={
'm2ee': {
# this is named admin_pass, but it's the verification http header
# to communicate with the internal management port of the runtime
'admin_pass': get_m2ee_password(),
}
})
version = m2ee.config.get_runtime_version()
mendix_runtimes_path = '/usr/local/share/mendix-runtimes.git'
mendix_runtime_version_path = os.path.join(os.getcwd(), 'runtimes', str(version))
if os.path.isdir(mendix_runtimes_path) and not os.path.isdir(mendix_runtime_version_path):
buildpackutil.mkdir_p(mendix_runtime_version_path)
env = dict(os.environ)
env['GIT_WORK_TREE'] = mendix_runtime_version_path
# checkout the runtime version
process = subprocess.Popen(['git', 'checkout', str(version), '-f'],
cwd=mendix_runtimes_path, env=env,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
process.communicate()
if process.returncode != 0:
logger.info('Mendix {} is not available in the rootfs'.format(version))
logger.info('Fallback (1): trying to fetch Mendix {} using git'.format(version))
process = subprocess.Popen(['git', 'fetch', 'origin', 'refs/tags/{0}:refs/tags/{0}'.format(str(version)),
'&&', 'git', 'checkout', str(version), '-f'],
cwd=mendix_runtimes_path, env=env, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
process.communicate()
if process.returncode != 0:
logger.info('Unable to fetch Mendix {} using git'.format(version))
url = buildpackutil.get_blobstore_url('/runtime/mendix-%s.tar.gz' % str(version))
logger.info('Fallback (2): downloading Mendix {} from {}'.format(version, url))
buildpackutil.download_and_unpack(url, os.path.join(os.getcwd(), 'runtimes'))
m2ee.reload_config()
set_runtime_config(
m2ee.config._model_metadata,
m2ee.config._conf['mxruntime'],
vcap_data,
m2ee,
)
java_version = buildpackutil.get_java_version(
m2ee.config.get_runtime_version()
)
set_jvm_memory(
m2ee.config._conf['m2ee']['javaopts'],
vcap_data['limits']['mem'],
java_version,
)
activate_new_relic(m2ee, vcap_data['application_name'])
activate_appdynamics(m2ee, vcap_data['application_name'])
set_application_name(m2ee, vcap_data['application_name'])
return m2ee
def set_up_logging_file():
buildpackutil.lazy_remove_file('log/out.log')
os.mkfifo('log/out.log')
log_ratelimit = os.getenv('LOG_RATELIMIT', None)
if log_ratelimit is None:
subprocess.Popen([
'sed',
'--unbuffered',
's|^[0-9\-]\+\s[0-9:\.]\+\s||',
'log/out.log',
])
else:
subprocess.Popen(['./bin/mendix-logfilter', '-r', log_ratelimit, '-f', 'log/out.log'])
def service_backups():
vcap_services = buildpackutil.get_vcap_services_data()
schnapps = None
amazon_s3 = None
for key in vcap_services:
if key.startswith("amazon-s3"):
amazon_s3 = key
if key.startswith("schnapps"):
schnapps = key
if not vcap_services or schnapps not in vcap_services:
logger.debug("No backup service detected")
return
backup_service = {}
if amazon_s3 in vcap_services:
s3_credentials = vcap_services[amazon_s3][0]['credentials']
backup_service['filesCredentials'] = {
'accessKey': s3_credentials['access_key_id'],
'secretKey': s3_credentials['secret_access_key'],
'bucketName': s3_credentials['bucket'],
}
if 'key_suffix' in s3_credentials: # Not all s3 plans have this field
backup_service['filesCredentials']['keySuffix'] = s3_credentials['key_suffix']
try:
db_config = buildpackutil.get_database_config()
if db_config['DatabaseType'] != 'PostgreSQL':
raise Exception(
'Schnapps only supports postgresql, not %s'
% db_config['DatabaseType']
)
host_and_port = db_config['DatabaseHost'].split(':')
backup_service['databaseCredentials'] = {
'host': host_and_port[0],
'username': db_config['DatabaseUserName'],
'password': db_config['DatabasePassword'],
'dbname': db_config['DatabaseName'],
'port': int(host_and_port[1]) if len(host_and_port) > 1 else 5432,
}
except Exception as e:
logger.exception(
'Schnapps will not be activated because error occurred with '
'parsing the database credentials'
)
return
schnapps_url = vcap_services[schnapps][0]['credentials']['url']
schnapps_api_key = vcap_services[schnapps][0]['credentials']['apiKey']
try:
result = requests.put(
schnapps_url,
headers={
'Content-Type': 'application/json',
'apiKey': schnapps_api_key
},
data=json.dumps(backup_service),
)
except requests.exceptions.SSLError as e:
logger.warning('Failed to contact backup service. SSLError: ' + str(e))
return
except Exception as e:
logger.warning('Failed to contact backup service: ' + e)
return
if result.status_code == 200:
logger.info("Successfully updated backup service")
else:
logger.warning("Failed to update backup service: " + result.text)
def start_app(m2ee):
m2ee.start_appcontainer()
if not m2ee.send_runtime_config():
sys.exit(1)
logger.debug('Appcontainer has been started')
abort = False
success = False
while not (success or abort):
startresponse = m2ee.client.start({'autocreatedb': True})
logger.debug('startresponse received')
result = startresponse.get_result()
if result == 0:
success = True
logger.info('The MxRuntime is fully started now.')
else:
startresponse.display_error()
if result == 2:
logger.warning('DB does not exists')
abort = True
elif result == 3:
if i_am_primary_instance():
if os.getenv('SHOW_DDL_COMMANDS', '').lower() == 'true':
for line in m2ee.client.get_ddl_commands({
"verbose": True
}).get_feedback()['ddl_commands']:
logger.info(line)
m2eeresponse = m2ee.client.execute_ddl_commands()
if m2eeresponse.has_error():
m2eeresponse.display_error()
abort = True
else:
logger.info(
'waiting 10 seconds before primary instance '
'synchronizes database'
)
time.sleep(10)
elif result == 4:
logger.warning('Not enough constants!')
abort = True
elif result == 5:
logger.warning('Unsafe password!')
abort = True
elif result == 6:
logger.warning('Invalid state!')
abort = True
elif result == 7 or result == 8 or result == 9:
logger.warning(
"You'll have to fix the configuration and run start "
"again... (or ask for help..)"
)
abort = True
else:
abort = True
if abort:
logger.warning('start failed, stopping')
sys.exit(1)
@atexit.register
def terminate_process():
logger.info('stopping app...')
if not m2ee.stop():
if not m2ee.terminate():
m2ee.kill()
try:
this_process = os.getpgid(0)
logger.debug(
'Terminating process group with pgid={}'.format(this_process)
)
os.killpg(this_process, signal.SIGTERM)
time.sleep(3)
os.killpg(this_process, signal.SIGKILL)
except Exception:
logger.exception('Failed to terminate all child processes')
def create_admin_user(m2ee):
logger.info('Ensuring admin user credentials')
app_admin_password = get_admin_password()
if os.getenv('M2EE_PASSWORD'):
logger.debug('M2EE_PASSWORD is set so skipping creation of application admin password')
return
if not app_admin_password:
logger.warning('ADMIN_PASSWORD not set, so skipping creation of application admin password')
return
logger.debug('Creating admin user')
m2eeresponse = m2ee.client.create_admin_user({
'password': app_admin_password
})
if m2eeresponse.has_error():
m2eeresponse.display_error()
if not is_development_mode():
sys.exit(1)
logger.debug('Setting admin user password')
m2eeresponse = m2ee.client.create_admin_user({
'username': m2ee.config._model_metadata['AdminUser'],
'password': app_admin_password
})
if m2eeresponse.has_error():
m2eeresponse.display_error()
if not is_development_mode():
sys.exit(1)
def configure_debugger(m2ee):
debugger_password = os.environ.get('DEBUGGER_PASSWORD')
if debugger_password is None:
logger.debug('Not configuring debugger, as environment variable '
'was not found')
return
response = m2ee.client.enable_debugger({
'password': debugger_password
})
response.display_error()
if not response.has_error():
logger.info(
'The remote debugger is now enabled, the password to '
'use is %s' % debugger_password
)
logger.info(
'You can use the remote debugger option in the Mendix '
'Business Modeler to connect to the /debugger/ sub '
'url on your application (e.g. '
'https://app.example.com/debugger/). '
)
def _transform_logging(nodes):
res = []
for k, v in nodes.iteritems():
res.append({
"name": k,
"level": v
})
return res
def configure_logging(m2ee):
for k, v in os.environ.iteritems():
if k.startswith('LOGGING_CONFIG'):
m2ee.set_log_levels(
'*',
nodes=_transform_logging(
json.loads(v)
),
force=True,
)
def display_running_version(m2ee):
if m2ee.config.get_runtime_version() >= 4.4:
feedback = m2ee.client.about().get_feedback()
if 'model_version' in feedback:
logger.info('Model version: %s' % feedback['model_version'])
def loop_until_process_dies(m2ee):
while True:
if app_is_restarting or m2ee.runner.check_pid():
time.sleep(10)
else:
break
logger.info('process died, stopping')
sys.exit(1)
def set_up_instadeploy_if_deploy_password_is_set(m2ee):
if os.getenv('DEPLOY_PASSWORD'):
mx_version = m2ee.config.get_runtime_version()
if use_instadeploy(mx_version):
def reload_callback():
m2ee.client.request('reload_model')
def restart_callback():
global app_is_restarting
app_is_restarting = True
if not m2ee.stop():
m2ee.terminate()
complete_start_procedure_safe_to_use_for_restart(m2ee)
app_is_restarting = False
thread = instadeploy.InstaDeployThread(
get_deploy_port(),
restart_callback,
reload_callback,
mx_version,
)
thread.setDaemon(True)
thread.start()
else:
logger.warning(
'Not setting up InstaDeploy because this mendix '
'runtime version %s does not support it' % mx_version
)
def start_metrics(m2ee):
metrics_interval = os.getenv('METRICS_INTERVAL')
if metrics_interval:
thread = metrics.MetricsEmitterThread(int(metrics_interval), m2ee)
thread.setDaemon(True)
thread.start()
def complete_start_procedure_safe_to_use_for_restart(m2ee):
buildpackutil.mkdir_p('model/lib/userlib')
set_up_logging_file()
start_app(m2ee)
create_admin_user(m2ee)
configure_logging(m2ee)
display_running_version(m2ee)
configure_debugger(m2ee)
if __name__ == '__main__':
if os.getenv('CF_INSTANCE_INDEX') is None:
logger.warning(
'CF_INSTANCE_INDEX environment variable not found. Assuming '
'responsibility for scheduled events execution and database '
'synchronization commands.'
)
pre_process_m2ee_yaml()
activate_license()
m2ee = set_up_m2ee_client(get_vcap_data())
def sigterm_handler(_signo, _stack_frame):
m2ee.stop()
sys.exit(0)
signal.signal(signal.SIGTERM, sigterm_handler)
try:
service_backups()
set_up_nginx_files(m2ee)
complete_start_procedure_safe_to_use_for_restart(m2ee)
set_up_instadeploy_if_deploy_password_is_set(m2ee)
start_metrics(m2ee)