-
Notifications
You must be signed in to change notification settings - Fork 0
/
init_update.py
58 lines (47 loc) · 1.97 KB
/
init_update.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
import requests
import json
from os import environ
import sys
def generate_id(config: dict):
if config['mode'] == 'dockerfile':
return config['image'].replace('/', '-')
else:
return '-'.join(config['url'].lower().replace('//', '').split('/')[1:]).replace('.git', '')
# load service configuration
with open(environ['CONFIG_FILE']) as f:
initialization_configuration = json.load(f)
# load microservice updater configuration
host = environ['UPDATER_HOST']
api_key = environ['API_KEY']
# for each service configuration
for i, service in enumerate(initialization_configuration['services']):
service['API-KEY'] = api_key
service_id = generate_id(service)
# read all related files and add them to the payload
if 'files' in service:
for file in service['files']:
with open(f'{environ["SETUP_PATH"]}/{service["files"][file]}') as f:
service['files'][file] = f.read()
check = requests.get(f'{host}/service/{service_id}', verify=False)
# register new service
if not check.ok:
response = requests.post(f'{host}/service', json=service, headers={'Content-Type': 'application/json'},
verify=False)
# registration successful
if response.ok:
print(f'service {i} registered successfully.', response.text)
# registration failed
else:
print(f'registration of service {service_id} failed:', response.text)
sys.exit(-1)
else:
# update service
response = requests.post(f'{host}/service/{service_id}', json=service,
headers={'Content-Type': 'application/json'}, verify=False)
# update initiated successfully
if response.ok:
print(f'service {service_id} update initiated successfully.', response.text)
# update initiation failed
else:
print(f'service {service_id} update failed.', response.text)
sys.exit(-2)