-
Notifications
You must be signed in to change notification settings - Fork 38
/
install.py
executable file
·229 lines (192 loc) · 7.94 KB
/
install.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#!/usr/bin/python
"""
The MIT License (MIT)
Copyright (c) 2016 Guillaume Pellerin @yomguy
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
import os
import argparse
import platform
from pwd import getpwnam
from grp import getgrnam
sysvinit_script = """#!/bin/sh
### BEGIN INIT INFO
# Provides: %s
# Required-Start: docker
# Required-Stop: docker
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Docker Services
### END INIT INFO
set -e
PROJECT_NAME=%s
YAMLFILE=%s
OPTS="-f $YAMLFILE -p $PROJECT_NAME"
UPOPTS="-d --no-build --no-deps"
. /lib/lsb/init-functions
case "$1" in
start)
log_daemon_msg "Starting $PROJECT_NAME composition" "$PROJECT_NAME" || true
if su -c "docker-compose $OPTS up $UPOPTS > /dev/null 2>&1" root ; then
log_end_msg 0 || true
else
log_end_msg 1 || true
fi
;;
stop)
log_daemon_msg "Stopping $PROJECT_NAME composition" "$PROJECT_NAME" || true
if su -c "docker-compose $OPTS stop > /dev/null 2>&1" root; then
log_end_msg 0 || true
else
log_end_msg 1 || true
fi
;;
reload|force-reload)
log_daemon_msg "Reloading $PROJECT_NAME composition" "$PROJECT_NAME" || true
if docker-compose $OPTS up $UPOPTS > /dev/null 2>&1 ; then
log_end_msg 0 || true
else
log_end_msg 1 || true
fi
;;
restart|try-restart)
log_daemon_msg "Restarting $PROJECT_NAME composition" "$PROJECT_NAME" || true
if docker-compose $OPTS stop > /dev/null 2>&1; docker-compose $OPTS up $UPOPTS > /dev/null 2>&1 ; then
log_end_msg 0 || true
else
log_end_msg 1 || true
fi
;;
status)
docker-compose $OPTS ps && exit 0 || exit $?
;;
*)
log_action_msg "Usage: /etc/init.d/$PROJECT_NAME {start|stop|reload|force-reload|restart|try-restart|status}" || true
exit 1
;;
esac
exit 0
"""
systemd_service = """
[Unit]
Description=%s composition
Requires=docker.service
After=docker.service
ConditionPathExists=%s
[Service]
ExecStart=%s -f %s up -d
ExecStop=%s -f %s stop
[Install]
WantedBy=local.target
"""
class DockerCompositionInstaller(object):
docker = '/etc/init.d/docker'
docker_compose = '/usr/local/bin/docker-compose'
cron_rule = "0 */6 * * * %s %s"
def __init__(self, config='docker-compose.yml', init_type='sysvinit', cron=False):
self.init_type = init_type
self.path = os.path.dirname(os.path.realpath(__file__))
self.config = config
self.root = self.get_root()
self.config = os.path.abspath(self.root + os.sep + self.config)
self.name = self.config.split(os.sep)[-2].lower()
self.cron = cron
def get_root(self):
path = self.path
while not self.config in os.listdir(path):
path = os.sep.join(path.split(os.sep)[:-1])
if not path:
raise ValueError('The YAML docker composition was not found, please type "install.py -h" for more infos.')
return path
def install_docker(self):
if not os.path.exists(self.docker):
print 'Installing docker first...'
os.system('wget -qO- https://get.docker.com/ | sh')
if not os.path.exists(self.docker_compose):
print 'Installing docker-compose...'
os.system('pip install docker-compose')
def install_daemon_sysvinit(self):
script = '/etc/init.d/' + self.name
print 'Writing sysvinit script in ' + script
data = sysvinit_script % (self.name, self.name, self.config)
f = open(script, 'w')
f.write(data)
f.close()
os.system('chmod 755 ' + script)
os.system('update-rc.d ' + self.name + ' defaults')
def install_daemon_systemd(self):
service = '/lib/systemd/system/' + self.name + '.service'
print 'Writing systemd service in ' + service
data = systemd_service % (self.name, self.config, self.docker_compose,
self.config, self.docker_compose, self.config)
f = open(service, 'w')
f.write(data)
f.close()
os.system('systemctl enable ' + service)
os.system('systemctl daemon-reload')
def install_cron(self):
cron_path = os.sep.join([self.root, 'etc', 'cron.d', 'app'])
log_path = os.sep.join([self.root, 'var', 'log', 'cron'])
if not os.path.exists(log_path) :
os.makedirs(log_path, 0o755)
os.symlink(cron_path, '/etc/cron.d/' + self.name)
uid = int(getpwnam('root').pw_uid)
guid = int(getgrnam('root').gr_gid)
fd_cron_path = os.open(cron_path, os.O_RDONLY)
os.fchown(fd_cron_path, 0, 0)
def uninstall_daemon_sysvinit(self):
script = '/etc/init.d/' + self.name
os.system('update-rc.d -f ' + self.name + ' remove')
os.system('rm ' + script)
def uninstall_daemon_systemd(self):
service = '/lib/systemd/system/' + self.name + '.service'
os.system('systemctl disable ' + service)
os.system('systemctl daemon-reload')
os.system('rm ' + service)
def uninstall_cron(self):
os.system('rm /etc/cron.d/' + self.name)
def uninstall(self):
print 'Uninstalling ' + self.name + ' composition as a daemon...'
if self.init_type == 'sysvinit':
self.uninstall_daemon_sysvinit()
elif self.init_type == 'systemd':
self.uninstall_daemon_systemd()
if self.cron:
self.uninstall_cron()
print 'Done'
def install(self):
print 'Installing ' + self.name + ' composition as a daemon...'
self.install_docker()
if self.init_type == 'sysvinit':
self.install_daemon_sysvinit()
elif self.init_type == 'systemd':
self.install_daemon_systemd()
if self.cron:
self.install_cron()
print 'Done'
def main():
description ="""Install this docker composition program as a daemon with boot init (sysvinit by default)."""
parser = argparse.ArgumentParser(description=description)
parser.add_argument('--uninstall', help='uninstall the daemon', action='store_true')
parser.add_argument('--cron', help='install cron backup rule', action='store_true')
parser.add_argument('--systemd', help='use systemd', action='store_true')
parser.add_argument('composition_file', nargs='?', help='the path of the YAML composition file to use (optional)')
config = 'docker-compose.yml'
init_type = 'sysvinit'
args = vars(parser.parse_args())
if args['systemd']:
init_type = 'systemd'
if args['composition_file']:
config = args['composition_file']
installer = DockerCompositionInstaller(config, init_type, args['cron'])
if args['uninstall']:
installer.uninstall()
else:
installer.install()
if __name__ == '__main__':
if not 'Linux' in platform.system():
print 'Sorry, this script in only compatible with Linux for the moment...\n'
else:
main()