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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
|
from __future__ import print_function
import os
import sys
import shutil
from distutils.dir_util import copy_tree
from distutils.file_util import copy_file
import invoke
from invoke import task
from yaksh.settings import SERVER_POOL_PORT
SCRIPT_DIR = os.path.abspath(os.path.dirname(__file__))
TARGET_CONTAINER_NAME = 'yaksh_code_server'
SRC_IMAGE_NAME = 'fossee/yaksh_codeserver'
CHECK_FILE = 'server_running.txt'
CHECK_FILE_PATH = os.path.join(SCRIPT_DIR, 'yaksh_data', CHECK_FILE)
OS_NAME = sys.platform
PYTHON_COMMAND = "python{0}".format(".".join(map(str, sys.version_info[:2])))
def create_dir(path):
if not os.path.exists(path):
os.makedirs(path)
def remove_check_file(path):
if os.path.isfile(path):
os.remove(path)
def remove_dir(path):
if os.path.isdir(path):
shutil.rmtree(path)
def run_as(os_name):
if (os_name.startswith('linux') or os_name == 'darwin' or
os_name.startswith('freebsd')):
return 'sudo'
else: # For os_name = 'Win32'
return None
def get_cmd(run_as_cmd, base_cmd):
if run_as_cmd:
return '{0} {1}'.format(run_as_cmd, base_cmd)
else:
return base_cmd
@task
def setupdb(ctx):
print("** Setting up & migrating database **")
ctx.run("{} manage.py makemigrations".format(PYTHON_COMMAND))
ctx.run("{} manage.py migrate".format(PYTHON_COMMAND))
print("** Done! Migrations complete **")
@task
def loadfixtures(ctx):
print("** Loading fixtures into database **")
ctx.run("{} manage.py loaddata demo_fixtures.json".format(PYTHON_COMMAND))
print("** Done! Loaded fixtures into database **")
@task(setupdb, loadfixtures)
def serve(ctx):
print("** Running the Django web server. Press Ctrl-C to Exit **")
ctx.run("{} manage.py runserver".format(PYTHON_COMMAND))
@task
def clean(ctx):
print("** Discarding database **")
remove_check_file(os.path.join(SCRIPT_DIR, 'db.sqlite3'))
@task
def getimage(ctx, image=SRC_IMAGE_NAME):
try:
base_cmd = "docker inspect {0}".format(image)
run_as_cmd = run_as(OS_NAME)
cmd = get_cmd(run_as_cmd, base_cmd)
ctx.run(cmd, hide=True)
except invoke.exceptions.Failure:
print("The docker image {0} does not exist locally".format(image))
print("\n** Pulling latest image <{0}> from docker hub **".format(
image)
)
base_cmd = "docker pull {0}".format(image)
run_as_cmd = run_as(OS_NAME)
cmd = get_cmd(run_as_cmd, base_cmd)
ctx.run(cmd)
print("\n** Done! Successfully pulled latest image <{0}> **".format(
image)
)
@task
def start(ctx, ports=SERVER_POOL_PORT, image=SRC_IMAGE_NAME, unsafe=False,
version=3):
if unsafe:
with ctx.cd(SCRIPT_DIR):
print("** Initializing local code server **")
base_cmd = "{} -m yaksh.code_server".format(PYTHON_COMMAND)
run_as_cmd = run_as(OS_NAME)
cmd = get_cmd(run_as_cmd, base_cmd)
ctx.run(cmd)
else:
cmd_params = {
'ports': ports, 'image': SRC_IMAGE_NAME,
'name': TARGET_CONTAINER_NAME,
'vol_mount': os.path.join(SCRIPT_DIR, 'yaksh_data'),
'command': 'sh {0}'.format(
os.path.join(
SCRIPT_DIR,
'yaksh_data', 'yaksh', 'scripts', 'yaksh_script.sh')
)
}
remove_check_file(CHECK_FILE_PATH)
getimage(ctx, image=SRC_IMAGE_NAME)
print("** Preparing code server **")
create_dir(os.path.join(SCRIPT_DIR, 'yaksh_data', 'data'))
create_dir(os.path.join(SCRIPT_DIR, 'yaksh_data', 'output'))
copy_tree(
os.path.join(SCRIPT_DIR, 'yaksh'),
os.path.join(SCRIPT_DIR, 'yaksh_data', 'yaksh')
)
copy_file(
os.path.join(SCRIPT_DIR, 'requirements',
'requirements-codeserver.txt'),
os.path.join(SCRIPT_DIR, 'yaksh_data')
)
print("** Initializing code server within docker container **")
base_cmd = "docker run \
-dp {ports}:{ports} --name={name} \
-v {vol_mount}:{vol_mount} \
-w {vol_mount} \
{image} {command}".format(**cmd_params)
run_as_cmd = run_as(OS_NAME)
cmd = get_cmd(run_as_cmd, base_cmd)
ctx.run(cmd)
while not os.path.isfile(CHECK_FILE_PATH):
print("** Checking code server status. Press Ctrl-C to exit **\r",
end="")
print("\n** Code server is up and running successfully **")
@task
def stop(ctx, container=TARGET_CONTAINER_NAME, hide=True):
base_filter_cmd = "docker ps -q --filter='name={0}'".format(container)
run_as_cmd = run_as(OS_NAME)
cmd = get_cmd(run_as_cmd, base_filter_cmd)
result = ctx.run(cmd)
remove_check_file(CHECK_FILE_PATH)
if result.stdout:
print("** Stopping the docker container <{0}> **".format(container))
base_stop_cmd = "docker stop {0}".format(container)
cmd = get_cmd(run_as_cmd, base_stop_cmd)
ctx.run(cmd)
print("** Done! Stopped the docker container <{0}> **".format(
container))
print("** Discarding the docker container <{0}> **".format(container))
base_rm_cmd = "docker rm {0}".format(container)
cmd = get_cmd(run_as_cmd, base_rm_cmd)
ctx.run(cmd)
print("** Done! Discarded the docker container <{0}> **".format(
container))
else:
print("** Docker container <{0}> not found **".format(container))
# Docker compose based deployment
@task
def build(ctx):
run_as_cmd = run_as(OS_NAME)
copy_tree(
os.path.join(SCRIPT_DIR, 'requirements'),
os.path.join(SCRIPT_DIR, 'docker', 'Files')
)
base_build_cmd = "docker-compose build --no-cache"
cmd = get_cmd(run_as_cmd, base_build_cmd)
print("** Building docker images **")
ctx.run(cmd)
print("** Done! Built the docker images **")
base_build_cmd = "docker pull mariadb:10.2 "
cmd = get_cmd(run_as_cmd, base_build_cmd)
print("** Pulling maria-db base image **")
ctx.run(cmd)
print("** Done! Pulled maria-db base image **")
@task
def begin(ctx):
print("** Initializing docker containers **")
base_cmd = "docker-compose up -d"
run_as_cmd = run_as(OS_NAME)
cmd = get_cmd(run_as_cmd, base_cmd)
ctx.run(cmd)
print("** Done! Initialized the docker containers **")
@task
def deploy(ctx, fixtures=False, static=True):
run_as_cmd = run_as(OS_NAME)
print("** Setting up & migrating database **")
base_make_migrate_cmd = "docker exec -i yaksh_django " \
" {} manage.py makemigrations".format(PYTHON_COMMAND)
cmd = get_cmd(run_as_cmd, base_make_migrate_cmd)
ctx.run(cmd)
base_migrate_cmd = "docker exec -i yaksh_django " \
"{} manage.py migrate".format(PYTHON_COMMAND)
cmd = get_cmd(run_as_cmd, base_migrate_cmd)
ctx.run(cmd)
print("** Done! Migrations complete **")
if fixtures:
base_fixture_cmd = "docker exec -i yaksh_django " \
"{} manage.py loaddata demo_fixtures.json".format(PYTHON_COMMAND)
cmd = get_cmd(run_as_cmd, base_fixture_cmd)
print("** Loading fixtures into database **")
ctx.run(cmd)
print("** Done! Loaded fixtures into database **")
if static:
base_static_cmd = "docker exec -i yaksh_django " \
"{} manage.py collectstatic".format(PYTHON_COMMAND)
cmd = get_cmd(run_as_cmd, base_static_cmd)
print("** Setting up static assets **")
ctx.run(cmd)
print("** Done! Set up static assets **")
@task
def status(ctx):
run_as_cmd = run_as(OS_NAME)
base_cmd = "docker-compose ps"
cmd = get_cmd(run_as_cmd, base_cmd)
print("** Fetching container status **")
ctx.run(cmd)
@task
def halt(ctx):
run_as_cmd = run_as(OS_NAME)
base_cmd = "docker-compose stop"
cmd = get_cmd(run_as_cmd, base_cmd)
print("** Stopping containers **")
ctx.run(cmd)
print("** Done! Stopped containers **")
@task
def restart(ctx):
run_as_cmd = run_as(OS_NAME)
base_cmd = "docker-compose restart"
cmd = get_cmd(run_as_cmd, base_cmd)
print("** Restarting containers **")
ctx.run(cmd)
print("** Done! Restarted containers **")
@task(halt)
def remove(ctx):
run_as_cmd = run_as(OS_NAME)
base_cmd = "docker-compose rm --force"
cmd = get_cmd(run_as_cmd, base_cmd)
sql_dir = os.path.join(SCRIPT_DIR, 'docker', 'mysql')
print("** Removing containers **")
remove_dir(sql_dir)
ctx.run(cmd)
print("** Done! Removed containers **")
|