-
Notifications
You must be signed in to change notification settings - Fork 112
Expand file tree
/
Copy pathtasks.py
More file actions
98 lines (82 loc) · 2.64 KB
/
tasks.py
File metadata and controls
98 lines (82 loc) · 2.64 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
"""
Long-running tasks for the Deis Controller API
This module orchestrates the real "heavy lifting" of Deis, and as such these
functions are decorated to run as asynchronous celery tasks.
"""
from __future__ import unicode_literals
import threading
from celery import task
@task
def create_cluster(cluster):
cluster._scheduler.setUp()
@task
def destroy_cluster(cluster):
for app in cluster.app_set.all():
app.destroy()
cluster._scheduler.tearDown()
@task
def deploy_release(app, release):
containers = app.container_set.all()
threads = []
for c in containers:
threads.append(threading.Thread(target=c.deploy, args=(release,)))
try:
[t.start() for t in threads]
[t.join() for t in threads]
except Exception:
for c in containers:
c.state = 'error'
c.save()
raise
@task
def start_containers(containers):
create_threads = []
start_threads = []
for c in containers:
create_threads.append(threading.Thread(target=c.create))
start_threads.append(threading.Thread(target=c.start))
try:
[t.start() for t in create_threads]
[t.join() for t in create_threads]
[t.start() for t in start_threads]
[t.join() for t in start_threads]
except Exception:
for c in containers:
c.state = 'error'
c.save()
raise
@task
def stop_containers(containers):
destroy_threads = []
delete_threads = []
for c in containers:
destroy_threads.append(threading.Thread(target=c.destroy))
delete_threads.append(threading.Thread(target=c.delete))
try:
[t.start() for t in destroy_threads]
[t.join() for t in destroy_threads]
[t.start() for t in delete_threads]
[t.join() for t in delete_threads]
except Exception:
for c in containers:
c.state = 'error'
c.save()
raise
@task
def run_command(c, command):
release = c.release
version = release.version
image = release.image
try:
# pull the image first
rc, pull_output = c.run("docker pull {image}".format(**locals()))
if rc != 0:
raise EnvironmentError('Could not pull image: {pull_image}'.format(**locals()))
# run the command
docker_args = ' '.join(['-a', 'stdout', '-a', 'stderr', '--rm', image])
env_args = ' '.join(["-e '{k}={v}'".format(**locals())
for k, v in release.config.values.items()])
command = "docker run {env_args} {docker_args} {command}".format(**locals())
return c.run(command)
finally:
c.delete()