-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjenkins_docker_run.py
75 lines (48 loc) · 1.53 KB
/
jenkins_docker_run.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
#!/usr/bin/python
import os
import subprocess
import sys
JENKINS_USER='jenkins'
JENKINS_HOME='/jenkins'
def build_cmd(config):
cmd = [
'/usr/bin/docker', # docker binary
'run', # run a command
'-t', # pseudo tty
'-rm', # remove afterwards
'-u', # use user jenkins
config['user'],
'-v', # mount workspace
'%s:%s' % (config['workspace'],config['workspace']),
'-v', # mount tmp_dir
'%s:%s' % (config['tmp_dir'],config['tmp_dir']),
'-w',
config['workspace'],
config['image'],
'/bin/bash',
config['cmd']
]
return cmd
def main():
config={}
try:
# TODO validate
config['workspace']=os.environ['WORKSPACE']
os.environ['JENKINS_HOME']
os.environ['HOME']
except:
print >> sys.stderr, 'I must be executed within a jenkins job'
sys.exit(1)
config['user'] = JENKINS_USER
config['tmp_dir'] = '/tmp'
#TODO validate
config['image'] = sys.argv[1]
#TODO validate, copy
config['cmd'] = sys.argv[2]
cmd = build_cmd(config)
print ("Running command: ",' '.join(cmd))
subprocess.call(cmd,stdin=sys.stdin, stdout=sys.stdout, stderr=sys.stderr)
pass
if __name__ == "__main__":
main()
#/usr/bin/docker run -t -u jenkins -v /tmp:/tmp -v $WORKSPACE:$WORKSPACE -w $WORKSPACE jenkins1.dmz:5000/former03/jenkins_slave:wheezy_ruby_test /bin/bash