forked from arenadata/adcm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjob_runner.py
executable file
·101 lines (80 loc) · 2.85 KB
/
job_runner.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
#!/usr/bin/env python3
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import sys
import json
import subprocess
import adcm.init_django # pylint: disable=unused-import
from cm.logger import log
import cm.config as config
import cm.job
def open_file(root, tag, job_id):
fname = '{}/{}-{}.txt'.format(root, job_id, tag)
f = open(fname, 'w')
return f
def read_config(job_id):
fd = open('{}/{}-config.json'.format(config.RUN_DIR, job_id))
conf = json.load(fd)
fd.close()
return conf
def set_job_status(job_id, ret, pid):
if ret == 0:
cm.job.set_job_status(job_id, config.Job.SUCCESS, pid)
return 0
else:
cm.job.set_job_status(job_id, config.Job.FAILED, pid)
return ret
def set_pythonpath():
cmd_env = os.environ.copy()
if "PYTHONPATH" in cmd_env:
cmd_env["PYTHONPATH"] = "./pmod:" + cmd_env["PYTHONPATH"]
else:
cmd_env["PYTHONPATH"] = "./pmod"
return cmd_env
def run_ansible(job_id):
log.debug("job_runner.py called as: %s", sys.argv)
conf = read_config(job_id)
playbook = conf['job']['playbook']
out_file = open_file(config.LOG_DIR, 'ansible-out', job_id)
err_file = open_file(config.LOG_DIR, 'ansible-err', job_id)
os.chdir(conf['env']['stack_dir'])
cmd = [
'ansible-playbook',
'-e',
'@{}/{}-config.json'.format(config.RUN_DIR, job_id),
'-i',
'{}/{}-inventory.json'.format(config.RUN_DIR, job_id),
playbook
]
if 'params' in conf['job']:
if 'ansible_tags' in conf['job']['params']:
cmd.append('--tags=' + conf['job']['params']['ansible_tags'])
proc = subprocess.Popen(cmd, env=set_pythonpath(), stdout=out_file, stderr=err_file)
log.info("job #%s run cmd: %s", job_id, ' '.join(cmd))
cm.job.set_job_status(job_id, config.Job.RUNNING, proc.pid)
log.info("run ansible job #%s, pid %s, playbook %s", job_id, proc.pid, playbook)
ret = proc.wait()
cm.job.finish_check(job_id)
res = set_job_status(job_id, ret, proc.pid)
out_file.close()
err_file.close()
log.info("finish ansible job #%s, pid %s, ret %s", job_id, proc.pid, res)
sys.exit(res)
def do():
if len(sys.argv) < 2:
print("\nUsage:\n{} job_id\n".format(os.path.basename(sys.argv[0])))
sys.exit(4)
else:
run_ansible(sys.argv[1])
if __name__ == '__main__':
do()