-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_environment.py
54 lines (41 loc) · 1.99 KB
/
build_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
import os
import sys
import argparse
import yaml
import httpx
# Parse arguments
parser = argparse.ArgumentParser(description='Build functions')
parser.add_argument('-r', '--repo', default='', nargs='?', help='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)
# Pull or clone repo
if arguments.repo and arguments.token:
# Define repo auxiliar names
full_repo_name = '.'.join(arguments.repo.split('https://github.com/')[1].split('.')[:-1])
repo_name = '.'.join(arguments.repo.split('/')[-1].split('.')[:-1])
# Clone into environments folder
if os.path.isdir(os.path.join(envs_path, repo_name)):
os.system('cd %s/%s && git pull origin main' % (envs_path, repo_name))
else:
os.system('git clone https://%s@github.com/%s %s/%s' % (arguments.token, full_repo_name, envs_path, repo_name))
# Get configuration
url = 'https://%s@raw.githubusercontent.com/%s/main/config.yaml' % (arguments.token, full_repo_name)
config = yaml.load(httpx.get(url).text, Loader=yaml.Loader)
# Install apt packages
requirements_apt = ' '.join(config.get('requirements_apt', []))
if requirements_apt:
os.system('apt-get update && apt-get install --no-install-recommends -q -y %s' % (requirements_apt))
# Install pip packages
requirements_pip = ' '.join(config.get('requirements_pip', []))
if requirements_pip:
os.system('pip3 install %s' % (requirements_pip))
# Install ops
for op in config.get('ops', []):
os.system('curl -s https://raw.githubusercontent.com/iortio/get_op/main/get_op.py | sudo python3 - -r %s -t %s' % (op, arguments.token))
# Install jobs
for job in config.get('jobs', []):
os.system('curl -s https://raw.githubusercontent.com/iortio/get_op/main/get_job.py | sudo python3 - -r %s -t %s' % (job, arguments.token))