-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdci-rhel-agent-ctl
executable file
·119 lines (99 loc) · 5.01 KB
/
dci-rhel-agent-ctl
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# dci-rhel-agent-ctl controls dci-rhel-agent (start, stop and kill).
import argparse
import subprocess
import signal
import sys
import yaml
import os
# dci-rhel-agent-ctl default values
default_value = {
"local_repo": "/opt/dci",
"settings_path": "/etc/dci-rhel-agent/settings.yml",
"hooks_path": "/etc/dci-rhel-agent/hooks",
"dci_rhel_agent_url":"quay.io/distributedci/dci-rhel-agent:latest-8"
}
proc = None
def sigterm_handler(signal, frame):
print('Exiting...')
proc.terminate()
sys.exit(0)
signal.signal(signal.SIGTERM, sigterm_handler)
signal.signal(signal.SIGINT, sigterm_handler)
def load_settings(settings_path):
with open(settings_path, 'r') as settings:
try:
return yaml.safe_load(settings)
except yaml.YAMLError as exc:
print(exc)
sys.exit(1)
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-s', '--start', action='store_true', help="Start dci-rhel-agent")
parser.add_argument('-c', '--config', action='store', help="Path to config file. Default is `%s`." % default_value['settings_path'], default=default_value['settings_path'])
parser.add_argument('-u', '--url', action='store', help="Url for dci-rhel-agent. Default is `%s`." % default_value['dci_rhel_agent_url'], default=default_value['dci_rhel_agent_url'])
parser.add_argument('-d', '--debug', action='store_true', help="Start a shell in dci-rhel-agent container")
parser.add_argument('-l', '--local', action='store_true', help="[dev] Mount local directory `./dci-rhel-agent` in the container. This path can be overridden with env var `LOCAL_SOURCE_PATH`.")
parser.add_argument('--build', action='store_true', help="[dev] Build the dci-rhel-agent container.")
parser.add_argument('--skip-download', action='store_true', help="Skip the download of RHEL topics.")
parser.add_argument('--hooks', action='store', help="Path to hooks folder. Default is `%s`." % default_value['hooks_path'], default=default_value['hooks_path'])
parser.add_argument('-v', '--verbosity', action='store', help="Verbosity level for Ansible log (0-4). Default is 1", type=int, choices=range(5), default=1)
parser.add_argument('--tests-only', action='store_true', help="Skip provisioning and only run enabled RH tests and user tests.")
parser.add_argument('-e', '--environment', action='append', help="Set custom environment variables from the command line, usage: -e var_name=value", nargs='+')
args = parser.parse_args()
global proc
if args.build:
myCmd = '/usr/bin/podman build -f Dockerfile -t dci-rhel-agent --no-cache'
proc = subprocess.Popen(myCmd, shell=True)
exit (proc.wait())
settings_path = args.config
dci_rhel_agent_url = args.url
hooks_path = args.hooks
entrypoint = ""
if args.debug:
entrypoint = "/bin/bash"
if args.start:
settings = load_settings(settings_path)
if 'local_repo' in settings.keys():
local_repo = settings['local_repo']
else:
print ("INFO: local_repo is NOT set in %s. Using default value." % default_value['local_repo'])
local_repo = default_value['local_repo']
# If agent is NOT in dev/debug mode, update DCI packages automatically.
if args.debug is False and args.local is False:
# Start dci package update
myCmd = 'yum upgrade -y \*dci\*'
proc = subprocess.Popen(myCmd, shell=True)
proc.wait()
myCmd = 'podman pull %s' % (dci_rhel_agent_url)
proc = subprocess.Popen(myCmd, shell=True)
proc.wait()
# Start dci-rhel-agent
volumes = {
hooks_path:'/etc/dci-rhel-agent/hooks:z',
'/etc/dci-rhel-agent/secrets':'/etc/dci-rhel-agent/secrets:z',
'/etc/dci-rhel-agent/inventory':'/etc/dci-rhel-agent/inventory:z',
settings_path:'/etc/dci-rhel-agent/settings.yml:z',
local_repo:'/data:z',
}
# if "-l" is used, we want to override container code with local code (dev mode)
if args.local:
local_source_path = os.environ.get('LOCAL_SOURCE_PATH', './dci-rhel-agent')
volumes[local_source_path] = '/usr/share/dci-rhel-agent:z'
myCmd = 'source /etc/dci-rhel-agent/dcirc.sh && '
myCmd = myCmd + 'podman run --rm -ti --network host -e DCI_CLIENT_ID -e DCI_API_SECRET -e DCI_CS_URL -e SKIP_DOWNLOAD=%s -e VERBOSITY=%s -e TESTS_ONLY=%s' % (args.skip_download, args.verbosity, args.tests_only)
if args.environment:
var_list = [item for sublist in args.environment for item in sublist]
for var in var_list:
myCmd = myCmd + ' -e ' + var
for key, value in volumes.items():
myCmd = myCmd + ' -v ' + key + ':' + value
myCmd = myCmd + ' %s %s' % (dci_rhel_agent_url, entrypoint)
proc = subprocess.Popen(myCmd, shell=True)
proc.wait()
sys.exit(proc.returncode)
else:
parser.print_help(sys.stderr)
if __name__ == "__main__":
main()