Skip to content

Commit

Permalink
Merge pull request #8 from miraculixx/fix-otpversion
Browse files Browse the repository at this point in the history
Fix otpversion
  • Loading branch information
miraculixx committed Mar 31, 2015
2 parents 1ebc839 + 736ea30 commit bdc5a9f
Show file tree
Hide file tree
Showing 8 changed files with 198 additions and 18 deletions.
39 changes: 38 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,33 @@ otp-dockerdeploy

This project has a Dockerfile to deploy an opentripplanner instance, with the GTFS for Switzerland built in. There is also a unittest script to run the instance and check if everything is working.

## Running the containers
## Install

```
chmod +x install.sh
sudo ./install.sh
```

## Quick start

```
fab go:urls=http://url/to/gtfs.zip,port=80,build=y,name=otpserver
```

Make sure to replace the URL to the actual GTFS feed you want to use. This will build the necessary docker images, build the OTP graph and
start otp server. Access the server at http://localhost:80. You may start as many servers as you want, they don't interfere with each other as
long as you use different ports and a different name for each server.

To download a new GTFS feed and build again:

```
fab go:urls=http://url/to/new-gtfs.zip,port=80,name=otpserver
```

This will stop (actually, kill and remove) the server, download the GTFS file and restart the server again.


## Running the containers manually

First pull the Docker container from the repo (the repo might change):

Expand Down Expand Up @@ -105,3 +131,14 @@ This is an example output:
```

As you can see, it creates the container, starts it, and tests a little search.

### Building the images

Other then the unit test above, we provide a simple Python Fabric script:

```
# build the images
fab build_server build_builder build_nginx
docker images | grep opentripplanner
```

8 changes: 6 additions & 2 deletions builder/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@ MAINTAINER Pablo Fredrikson <[email protected]>
RUN \
apt-get update && \
apt-get install -y openjdk-7-jre wget && \
rm -rf /var/lib/apt/lists/*

rm -rf /var/lib/apt/lists/* && \
mkdir -p /var/otp/pdx && \
echo "downloading otp" && \
cd /var/otp && \
wget -S -nv -O /var/otp/otp.jar http://dev.opentripplanner.org/jars/otp-0.14.0.jar

ADD build.sh /root/
RUN chmod +x /root/build.sh

Expand Down
26 changes: 17 additions & 9 deletions builder/build.sh
Original file line number Diff line number Diff line change
@@ -1,27 +1,35 @@
#!/bin/bash


# Getting the urls and environment variables for otp
while getopts ":u:e:" opt
do
case $opt in
u ) urls=$OPTARG;;
e ) envs=$OPTARG;;
u ) urls=$OPTARG;;
e ) envs=$OPTARG;;
r ) router=$OPTARG;;
esac
done

OTP_BASE=/var/otp
OTP_GRAPHS=$OTP_BASE/graphs
OTP_ROUTER=${router:-default}

mkdir -p $OTP_GRAPHS

for url in $urls; do
wget -q -P /var/otp/pdx/ $url
echo "downloading $url"
wget -S -nv -P $OTP_GRAPHS/$OTP_ROUTER $url || echo "-- error"
done

# Defining the env variables
export JAVA_HOME="/usr/lib/jvm/java-7-openjdk-amd64"
mkdir -p /var/otp/pdx && \
#mkdir -p /var/otp/pdx && \

# Let's build the maps
cd /var/otp && \
wget -q -O /var/otp/otp.jar http://dev.opentripplanner.org/jars/otp-latest-master.jar && \
java -Xmx8G -jar /var/otp/otp.jar --build /var/otp/pdx $envs
echo "generating graph"
java -Xmx8G -jar /var/otp/otp.jar --build $OTP_GRAPHS/$OTP_ROUTER $envs

# Moving the generated graph to the shared dir
mkdir -p /var/otp/graphs && \
mv /var/otp/pdx/Graph.obj /var/otp/graphs/
echo "finalizing"
find $OTP_GRAPHS
125 changes: 125 additions & 0 deletions fabfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,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)
8 changes: 8 additions & 0 deletions install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash
# run this to install required software on the server
#
# sudo ./install.ch
#
apt-get install python-pip && pip -q install docker-py
apt-get install docker
pip install -r requirements.txt
1 change: 1 addition & 0 deletions nginx/build.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@

# Starting nginx on the foreground
system("/usr/sbin/nginx -g \"daemon off\;\"")

7 changes: 2 additions & 5 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
== SERVER

Docker > 1.3

apt-get install python-pip && pip -q install docker-py
fabric
docker-fabric==0.3.2
2 changes: 1 addition & 1 deletion server/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ ENV JAVA_HOME /usr/lib/jvm/java-7-openjdk-amd64
RUN \
mkdir /var/otp && \
cd /var/otp && \
wget -q -O /var/otp/otp.jar http://dev.opentripplanner.org/jars/otp-latest-master.jar
wget -q -O /var/otp/otp.jar http://dev.opentripplanner.org/jars/otp-0.14.0.jar

# Expose ports for other containers
EXPOSE 8080
Expand Down

0 comments on commit bdc5a9f

Please sign in to comment.