-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfabfile.py
110 lines (67 loc) · 2.57 KB
/
fabfile.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
from fabric.api import local, env
from dockerfabric.api import docker_fabric
env.docker_tunnel_local_port = 22024 # or any other available port above 1024 of your choice
docker_client = docker_fabric()
def get_image_name():
image_name = 'ofensivaria-bot-3'
if env.docker_username:
image_name = '%s/%s' % (env.docker_username, image_name)
return image_name
def poll():
local('docker-compose run --name bot --service-ports --rm poll')
def web():
local('docker-compose run --name bot --service-ports --rm app')
def build():
local('docker build . -t %s' % get_image_name())
def push():
if getattr(env, 'docker_username', None):
local('docker push %(docker_username)s/ofensivaria-bot-3' % env)
else:
print('No docker_username set. Not pushing.')
def pull():
docker_client.pull(get_image_name())
def stop():
docker_client.stop('ofensivaria')
def logs():
docker_client.logs('ofensivaria')
def start():
docker_client.start('ofensivaria')
def remove():
docker_client.remove_container('ofensivaria')
def start_from_scratch():
docker_env = {
'TOKEN': env.telegram_token,
'REDIS_HOST': 'redis',
'REDIS_PORT': '6379',
'DEBUG': ''
}
config = docker_client.create_host_config(
port_bindings={8000: 8000},
links={'redis': 'redis'},
binds=['/markov:/markov']
)
docker_client.create_container(get_image_name(), environment=docker_env,
name='ofensivaria', host_config=config, detach=True)
def start_redis():
status = get_container_status('redis')
if status == 'deleted':
config = docker_client.create_host_config(binds=['/data:/data'])
docker_client.create_container('redis', command='--appendonly yes', host_config=config,
name='redis', detach=True)
docker_client.start('redis')
STEPS = {
'running': (build, push, stop, remove, pull, start_redis, start_from_scratch, start),
'exited': (build, push, remove, pull, start_redis, start_from_scratch, start),
'deleted': (build, push, pull, start_redis, start_from_scratch, start),
'created': (build, push, pull, start_redis, start_from_scratch, start)
}
def deploy():
status = get_container_status('ofensivaria')
for step in STEPS[status]:
step()
def get_container_status(container_name):
try:
container = docker_client.containers(filters={'name': container_name}, all=True)[0]
return container['State']
except IndexError:
return 'deleted'