forked from ownrecipes/OwnRecipes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquick-start.py
More file actions
246 lines (213 loc) · 6.88 KB
/
quick-start.py
File metadata and controls
246 lines (213 loc) · 6.88 KB
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#!/usr/bin/env python
# encoding: utf-8
import argparse
from time import sleep
from os import getcwd, name as os_name
from subprocess import call, Popen, DEVNULL, PIPE, STDOUT
app_version='3.4.0'
def parse_args():
parser = argparse.ArgumentParser(
description='OwnRecipes quick setup script. '
'This script will restart your OwnRecipes server and '
'take a database and recipe image backup.'
)
parser.add_argument(
'-t',
'--tag',
type=str,
help='The git tag of OwnRecipes you want to run. '
'If not included, then the master branch will be used.'
)
parser.add_argument(
'stop',
nargs='?',
help='Stops all OwnRecipes containers for the specified tag (or master).'
)
args = parser.parse_args()
args.tag = args.tag if args.tag is not None else app_version
return args
def update_image_tags(version):
"""
Update the version.yml-file with the required OwnRecipes version(s).
:param version: The version of OwnRecipes the users wants to run.
This is a docker tag.
"""
print("")
print("Configure app")
print("=============")
print("Version is " + version + ".")
version = '''version: '3.1'
services:
api:
image: ownrecipes/ownrecipes-api:%s
web:
image: ownrecipes/ownrecipes-web:%s
nginx:
image: ownrecipes/ownrecipes-nginx:%s
''' % (version, version, version)
with open('docker-prod.version.yml', 'w') as f:
f.write(version)
def download_images(version):
"""
Download the required images.
:param version: The version of OwnRecipes the users wants to run.
This is a docker tag.
"""
print("")
print("")
print("Downloading Images")
print("==================")
getDockerCompose()
call(['docker', 'pull', 'ownrecipes/ownrecipes-api:' + version])
call(['docker', 'pull', 'ownrecipes/ownrecipes-web:' + version])
call(['docker', 'pull', 'ownrecipes/ownrecipes-nginx:' + version])
def getDockerCompose():
""" Check if docker-compose V1 or V2 is available. """
try:
Popen(['docker', 'compose', 'version'], stdin=DEVNULL, stdout=DEVNULL, stderr=STDOUT)
return 'docker compose'
except OSError:
try:
Popen(['docker-compose', '--version'], stdin=DEVNULL, stdout=DEVNULL, stderr=STDOUT)
return 'docker-compose'
except OSError:
raise RuntimeError("docker-compose not found. Please install the requirements and try again.")
def start_containers():
"""
Takes a back up of the Recipe images and DB.
Restarts OwnRecipes with a new (or the same) version.
"""
print("")
print("")
print("Starting OwnRecipes")
print("===================")
dockerCompose = getDockerCompose()
dockerComposeArr = dockerCompose.split(' ')
# Check if the DB is up and running locally.
# If it is then take a backup.
# If the user is using a remote DB, do nothing.
# If no DB is found, Start the docker DB and wait 45s to start.
p = Popen(
['docker', 'ps', '-q', '-f', 'name=ownrecipes_db_1'],
stdin=PIPE,
stdout=PIPE,
stderr=PIPE
)
output, err = p.communicate(b"input data that is passed to subprocess' stdin")
if output and not err:
print("Taking a database backup (saving as ownrecipes.sql)...")
if os_name == 'nt':
call(
'docker exec ownrecipes_db_1 sh -c ' +
'"exec mysqldump ownrecipes -u root -p"$MYSQL_ROOT_PASSWORD""' +
' > ownrecipes.sql',
shell=True
)
else:
call(
'docker exec ownrecipes_db_1 sh -c ' +
'\'exec mysqldump ownrecipes -u root -p"$MYSQL_ROOT_PASSWORD"\'' +
' > ownrecipes.sql',
shell=True
)
elif 'MYSQL_HOST' in open('.env.docker.production.api').read():
print("Using remote DB...")
else:
print("Creating the DB. This may take a minute...")
call([*dockerComposeArr, '-f', 'docker-prod.yml', 'up', '-d', 'db'])
sleep(45)
# Check if the API is up.
# If it is then take a backup of the Recipe images folder.
# The backup folder is called `site-media`.
p = Popen(
['docker', 'ps', '-q', '-f', 'name=ownrecipes_api_1'],
stdin=PIPE,
stdout=PIPE,
stderr=PIPE
)
output, err = p.communicate(b"input data that is passed to subprocess' stdin")
if output and not err:
print("Taking a image backup save to 'site-media'...")
call(
'docker cp ownrecipes_api_1:/code/site-media/ ' + getcwd(),
shell=True
)
# Stop each container that needs to be updated.
# Don't stop the DB! There is no reason to.
print("Stopping the containers...")
call([
*dockerComposeArr,
'-f', 'docker-prod.yml',
'-f', 'docker-prod.version.yml',
'-f', 'docker-prod.override.yml',
'stop', 'nginx'
])
call([
*dockerComposeArr,
'-f', 'docker-prod.yml',
'-f', 'docker-prod.version.yml',
'-f', 'docker-prod.override.yml',
'stop', 'web'
])
call([
*dockerComposeArr,
'-f', 'docker-prod.yml',
'-f', 'docker-prod.version.yml',
'-f', 'docker-prod.override.yml',
'stop', 'api'
])
# Start all the containers
print("Starting the containers...")
call([
*dockerComposeArr,
'-f', 'docker-prod.yml',
'-f', 'docker-prod.version.yml',
'-f', 'docker-prod.override.yml',
'up', '-d'
])
print("App started. Please wait ~30 seconds for the containers to come online.")
def stop_containers():
print("")
print("")
print("Stopping OwnRecipes")
print("===================")
dockerCompose = getDockerCompose()
dockerComposeArr = dockerCompose.split(' ')
# Stop each container.
call([
*dockerComposeArr,
'-f', 'docker-prod.yml',
'-f', 'docker-prod.version.yml',
'-f', 'docker-prod.override.yml',
'stop', 'nginx'
])
call([
*dockerComposeArr,
'-f', 'docker-prod.yml',
'-f', 'docker-prod.version.yml',
'-f', 'docker-prod.override.yml',
'stop', 'web'
])
call([
*dockerComposeArr,
'-f', 'docker-prod.yml',
'-f', 'docker-prod.version.yml',
'-f', 'docker-prod.override.yml',
'stop', 'api'
])
call([
*dockerComposeArr,
'-f', 'docker-prod.yml',
'-f', 'docker-prod.version.yml',
'-f', 'docker-prod.override.yml',
'stop', 'db'
])
print("App stopped.")
if __name__ == '__main__':
args = parse_args()
update_image_tags(args.tag)
if args.stop:
stop_containers()
else:
download_images(args.tag)
start_containers()