-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_environment.py
79 lines (65 loc) · 2.94 KB
/
update_environment.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
import os
import sys
import argparse
import yaml
import httpx
# Parse arguments
parser = argparse.ArgumentParser(description='Build functions')
parser.add_argument('-o', '--op', default='', nargs='?', help='Op repo')
parser.add_argument('-j', '--job', default='', nargs='?', help='Job repo')
parser.add_argument('-t', '--token', default='', nargs='?', help='Token')
arguments = parser.parse_args()
# Create environments folder
envs_path = '/opt/environments'
if not os.path.isdir(envs_path):
os.mkdir(envs_path)
repo_path = '/opt/repos'
if not os.path.isdir(repo_path):
os.mkdir(repo_path)
# Get name of repo
if arguments.op:
repo = arguments.op
elif arguments.job:
repo = arguments.job
else:
repo = None
# Pull or clone repo
if arguments.token and repo:
# Define repo auxiliar names
full_repo_name = '.'.join(repo.split('https://github.com/')[1].split('.')[:-1])
repo_name = '.'.join(repo.split('/')[-1].split('.')[:-1])
# Clone into repos folder
if os.path.isdir(os.path.join(repo_path, repo_name)):
os.system('cd %s/%s && git pull origin main' % (repo_path, repo_name))
else:
os.system('git clone https://%[email protected]/%s %s/%s' % (arguments.token, full_repo_name, repo_path, repo_name))
# Read configuration
config = yaml.load(open('%s/%s/config.yaml' % (repo_path, repo_name)), Loader=yaml.Loader)
# Iterate over environments
for environment in config[0].get('environments', []):
# Define repo auxiliar names
full_env_name = '.'.join(environment.split('https://github.com/')[1].split('.')[:-1])
env_name = '.'.join(environment.split('/')[-1].split('.')[:-1])
# Clone environment
if os.path.isdir(os.path.join(envs_path, env_name)):
os.system('cd %s/%s && git pull origin main' % (envs_path, env_name))
else:
os.system('git clone https://%[email protected]/%s %s/%s' % (arguments.token, full_env_name, envs_path, env_name))
# Read env configuration
env_config = yaml.load(open('%s/%s/config.yaml' % (envs_path, env_name)), Loader=yaml.Loader)
update_environment = False
# Add op to config if required
if arguments.op and arguments.op not in env_config.get('ops', []):
if not 'ops' in env_config: env_config['ops'] = []
env_config['ops'].append(arguments.op)
update_environment = True
# Add job to config if required
if arguments.job and arguments.job not in env_config.get('jobs', []):
if not 'jobs' in env_config: env_config['jobs'] = []
env_config['ops'].append(arguments.job)
update_environment = True
if update_environment:
# Save config
open('%s/%s/config.yaml' % (envs_path, env_name), 'w').write(yaml.dump(env_config))
# Update repo
os.system('cd %s/%s && git add . && git commit -m "Environment updated" && git push origin main' % (envs_path, env_name))