-
Notifications
You must be signed in to change notification settings - Fork 0
/
pgd.py
executable file
·157 lines (141 loc) · 5.45 KB
/
pgd.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/env python
# Coding:UTF8
#
# Git-Docker env tool
#
# (c)2015 - Philippe ENTZMANN - [email protected]
#
import os
import subprocess
import sys
import argparse
print 'pgd'
parser = argparse.ArgumentParser(prog='pgd',
description='Python Git Docker simple integrator',
formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument("command",
choices=['run', 'build', 'info', 'bootstrap'],
default='run',
help='''Action to perform :
%(prog)s run : run the container for the current project/branch
%(prog)s build : build the container for the current project/branch
%(prog)s bootstrap : create a basic Dockerfile
%(prog)s info : output info only
''')
parser.add_argument('-d', '--dry',
action='store_true',
help="No action. Show the actions that would occur but do not actually start or build a container.")
parser.add_argument('-D', '--DockerfilePath',
default=os.path.join('container', 'dev'),
help="Set the path to the Dockerfile relative to the project root.")
parser.add_argument('-x', '--autoexec',
default=os.path.join(
'container', 'dev', 'autoexec_docker'),
help="Relative path to the executable file to run inside the container.")
parser.add_argument('-u', '--user',
default='alan',
help="User who run the main container process.")
args = parser.parse_args()
#command = sys.argv[1] if len(sys.argv) > 1 else 'run'
# print __file__
FNULL = open(os.devnull, 'w')
default_image = 'phentz/devpy' # :latest ?
current_dir = os.getcwd()
app_name = None
git_branch = None
app_path = None
tmp_path = os.getcwd()
try:
while tmp_path <> '':
# TODO: disable output
git_branch = subprocess.check_output(
'cd %s && git rev-parse --abbrev-ref HEAD' % tmp_path,
stderr=FNULL, shell=True).strip().lower()
app_path = tmp_path
tmp_path, app_name = os.path.split(tmp_path)
except subprocess.CalledProcessError:
pass
if app_name is None:
app_path = current_dir
# output = git_branch = subprocess.check_output('git rev-parse --is-inside-work-tree',
# stderr=FNULL, shell=True).strip().lower())
# print output
if app_name:
docker_image = '%s-%s' % (app_name.lower(), git_branch)
else:
docker_image = default_image
# TODO: guess DockerFile
docker_file_path = os.path.join(app_path, args.DockerfilePath)
if not os.path.exists(os.path.join(docker_file_path, 'Dockerfile')):
docker_file_path = current_dir # app_path
if not os.path.exists(os.path.join(docker_file_path, 'Dockerfile')):
docker_file_path = None
#extra_args = ' '.join(sys.argv[2:]) if len(sys.argv) > 2 else ''
print 'App name : %s' % app_name
print 'Git branch : %s' % git_branch
print 'Docker image : %s' % docker_image
print 'DockerFile path : %s' % docker_file_path
# if extra_args <> '':
# print 'extra_args : %s' % extra_args
try:
subprocess.check_output('docker images | grep %s' %
docker_image, shell=True).strip()
except:
if args.command == 'run':
print 'Docker image %(docker_image)s not found, use default image %(default_image)s' % locals()
docker_image = default_image
# TODO: add $1 $2 $3 $4 $5
cmd_build = 'docker build -t %(docker_image)s %(docker_file_path)s' % locals()
# thanks to http://fabiorehm.com/blog/2014/09/11/running-gui-apps-with-docker/
# TODO: add $1 $2 $3 $4 $5
cmd_run = '''docker run -it --rm \
-v %(app_path)s:/var/myapp \
-v %(app_path)s:/var/%(app_name)s \
-v %(current_dir)s:/current_dir \
-v $HOME/.Xauthority:/root/.Xauthority \
-e DISPLAY \
--workdir="/current_dir" \
--net=host \
%(docker_image)s''' % locals()
if args.command == 'build':
if docker_file_path is None:
print 'Dockerfile not found !'
sys.exit(0)
print 'exec :', cmd_build
if not args.dry:
subprocess.call(cmd_build, shell=True)
elif args.command == 'run':
#--hostname="%(docker_image)s" \
print 'exec :', cmd_run
autoexec_file = os.path.join(app_path, args.autoexec)
if os.path.exists(autoexec_file) and len(args.autoexec) > 0:
if args.user not in (None, 'root'):
cmd_run = cmd_run + " sh -c '/sbin/setuser %s /current_dir/%s'" % (args.user, args.autoexec)
else:
cmd_run = cmd_run + " /current_dir/%s" % args.autoexec
print 'exec autoexec_docker :', cmd_run
if not args.dry:
subprocess.call(cmd_run, shell=True)
else:
print 'exec default :', cmd_run
if not args.dry:
subprocess.call(cmd_run, shell=True)
elif args.command == 'bootstrap':
# Create the default Dockerfile path
if not os.path.exists(docker_file_path):
print 'Creating folder %s ...' % docker_file_path
os.makedirs(docker_file_path)
# Create the default Dockerfile
docker_file = os.path.join(docker_file_path, 'Dockerfile')
if not os.path.exists(docker_file):
print 'Creating file %s ...' % docker_file
with open(docker_file, 'w') as f:
f.write('''FROM %s:latest\n''' % default_image)
else:
print 'A Dockerfile already exists in %s !' % docker_file
elif args.command == 'info':
print 'App path : %s' % app_path
print 'current_dir : %s' % current_dir
print 'Docker commands :\n'
print cmd_build
print cmd_run