-
Notifications
You must be signed in to change notification settings - Fork 2
/
fabfile.py
125 lines (114 loc) · 3.87 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
from fabric.context_managers import settings, prefix, cd, lcd, shell_env
from fabric.contrib.project import rsync_project
from fabric.decorators import task, roles
from fabric.operations import local, prompt, run, put
from fabric.state import env
from fabric.tasks import execute
import time
from dockerfabric import tasks as docker
# see https://github.com/merll/docker-fabric
env.docker_tunnel_local_port = 22024 # or any other available port above 1024 of your choice
# see http://stackoverflow.com/a/28382014
env.use_ssh_config = True
running_locally = lambda : not env.hosts or len(env.hosts) == 0
def xrun(cmd, *args, **kwargs):
if running_locally():
local(cmd, *args, **kwargs)
else:
run(cmd, *args, **kwargs)
@task
def build_builder():
"""
build the builder
"""
xrun('docker build -t opentripplanner:builder builder')
xrun('docker images | grep opentripplanner')
@task
def build_server():
"""
build the server
"""
xrun('docker build -t opentripplanner:server server')
xrun('docker images | grep opentripplanner')
@task
def build_nginx():
"""
build the nginx image
"""
xrun('docker build -t opentripplanner:nginx nginx ')
xrun('docker images | grep opentripplanner')
@task
def go(name=None, port=None, urls=None, params=None, build=False, router=False):
"""
run the server, build the graph, restart the server
:param name: the name of the image, defaults to 'otpserver'
:param port: the host port to serve at, defaults to 80
:param urls: list of URL files to download (gtfs, pdx)
:param params: list of parameters to run the server
"""
params = params or ''
assert urls, "need at least one gtfs file (checkout http://www.gtfs-data-exchange.com/agencies)"
opts = {
'name' : name or 'otpserver',
'urls' : ' '.join(urls.split(',')),
'params' : ' '.join(params.split(',')),
'port' : port or 80,
'router' : router or 'default'
}
if build:
if not running_locally():
target = './otp-dockerdeploy'
tmptar = '/tmp/otp-dockerdeploy.tgz'
run('mkdir -p %s' % target)
local('tar -czf %s --exclude .git .' % tmptar)
put(tmptar, tmptar)
run('tar -C %s -xzf %s' % (target, tmptar))
run('docker ps')
with cd(target):
execute(build_builder)
execute(build_server)
else:
execute(build_builder)
execute(build_server)
cmd_rmserver = (
'docker rm -f {name} 2>&1 /dev/null'
).format(**opts)
cmd_server = (
'docker run '
' -p {port}:8080 -d'
' --name {name}'
' opentripplanner:server '
' --router {router} --server '
).format(**opts)
cmd_builder = (
'docker run --volumes-from {name}'
' opentripplanner:builder '
' -u "{urls}" -e "{params}"'
).format(**opts)
cmd_restart = (
'docker restart {name}'
).format(**opts)
# be nice and tell what we're doing
print "[INFO] Running with options %s" % opts
# run the server (delete existing server first)
with settings(warn_only=True):
xrun(cmd_rmserver)
xrun(cmd_server)
# build the graph
time.sleep(10)
xrun(cmd_builder)
# restart the server
xrun(cmd_restart)
# report success
print "[INFO] server {name} running at http://localhost:{port}".format(**opts)
@task
def dockerrm(images=None, containers=None):
"""
mass remove images and containers
"""
prompt('Are you sure? Press Ctrl-C otherwise.')
xrun('docker ps --all | grep Exited | cut -c1-19 | xargs -L1 docker rm')
xrun('docker images | grep "6 months" | cut -c 35-65 | xargs -L1 docker rmi')
@task
def enter(name=None):
xrun('PID=$(docker inspect --format {{.State.Pid}} %s) && nsenter --target $PID --mount' % name)