forked from adamlaska/datatracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun
More file actions
executable file
·193 lines (155 loc) · 6.19 KB
/
run
File metadata and controls
executable file
·193 lines (155 loc) · 6.19 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
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
#!/bin/bash
version=0.10
program=${0##*/}
progdir=${0%/*}
if [ "$progdir" = "$program" ]; then progdir="."; fi
if [ "$progdir" = "." ]; then progdir="$PWD"; fi
parent=$(dirname $progdir)
if [ "$parent" = "." ]; then parent="$PWD"; fi
# ----------------------------------------------------------------------
function usage() {
cat <<EOF
NAME
$program - Run a docker datatracker container with suitable settings
SYNOPSIS
$program [OPTIONS] ARGS
DESCRIPTION
This is a wrapper which runs docker with suitable arguments on a
debian-based docker image which has been set up with the dependencies
needed to easily run the IETF datatracker in development mode. By
default, it expects to find MySWL database files at
$parent/data/mysql, which is mapped inside the
container to /var/lib/mysql, and it will set up a home directory for
the current user ($USER) and map it to $HOME.
EOF
echo -e "OPTIONS"
if [ "$(uname)" = "Linux" ]; then
egrep "^[ ]+[-][A-Za-z| -]+\*?\)[ ]+[A-Za-z].+#" $0 | tr -s "\t|" "\t," | sed -r -e 's/\)[ \t]+([A-Z]+)=\$2[^#]*#/=\1\t/' -e 's/\)[^#]*#/\t/'
else
egrep "^[ ]+[-][A-Za-z| -]+\*?\)[ ]+[A-Za-z].+#" $0 | sed 's/\|.*\$2[^#]*#/ /'| sed -E 's/\|.*\)[^#]*#/ /'
fi
cat <<EOF
FILES
AUTHOR
Written by Henrik Levkowetz, <henrik@levkowetz.com>
COPYRIGHT
Copyright (c) 2015 IETF Trust and the persons identified as authors of
the code. All rights reserved. License 'Simplified BSD', as specified
in http://opensource.org/licenses/BSD-3-Clause.
EOF
}
# ----------------------------------------------------------------------
function die() {
echo -e "\n$program: error: $*" > /dev/stderr
exit 1
}
function note() {
if [ -n "$VERBOSE" ]; then echo -e "$*"; fi
}
# ----------------------------------------------------------------------
function version() {
echo -e "$program $version"
}
# ----------------------------------------------------------------------
trap 'echo "$program($LINENO): Command failed with error code $? ([$$] $0 $*)"; exit 1' ERR
# ----------------------------------------------------------------------
# Option parsing
# Options
shortopts=dhi:m:Mr:t:vVu:
longopts=download-data,help,ietfdb-url=,mysqldata=,no-mysqldir,docker-repo=,tag=,verbose,version,user=,
# Default values
TAG=$(basename $(svn info $parent | grep ^URL | awk '{print $2}'))
REPO="levkowetz/datatracker"
NOMYMAP=""
if [ "$(uname)" = "Linux" ]; then
args=$(getopt -o "$shortopts" --long "$longopts" -n '$program' -- $SV "$@")
if [ $? != 0 ] ; then die "Terminating..." >&2 ; exit 1 ; fi
eval set -- "$args"
sed="sed -r"
else
# Darwin, BSDs
args=$(getopt -o$shortopts $SV $*)
if [ $? != 0 ] ; then die "Terminating..." >&2 ; exit 1 ; fi
set -- $args
sed="sed -E"
fi
while true ; do
case "$1" in
-d| --download-data) DOWNLOAD=1;; # Download and set up the database files
-h| --help) usage; exit;; # Show this help, then exit
-i| --ietfdb-url) URL=$2; shift;; # Use an alternative database tarball URL
-m| --mysqldir) MYSQLDIR=$2; shift;; # Set the desired location for MySQL's database files
-M| --no-mysqldir) NOMYMAP=1;; # Don't map the mysql dir to an external dir
-r| --docker-repo) REPO=$2; shift;; # Use the given docker repository, instead of the default
-t| --tag) TAG=$2; shift;; # Use this docker image tag, instead of the svn branch name
-u| --user) WHO=$2; shift;; # Run the container as the specified user
-v| --verbose) VERBOSE=1;; # Be more talkative
-V| --version) version; exit;; # Show program version, then exit
--) shift; break;;
*) die "Internal error, inconsistent option specification: '$1'";;
esac
shift
done
# ----------------------------------------------------------------------
# The program itself
if [ "$(uname)" != "Linux" ]; then
if [ -n "$(type -p boot2docker)" ]; then
machine=$(type -p boot2docker)
up=up
env=shellinit
elif [ -n "$(type -p docker-machine)" ]; then
machine=$(type -p docker-machine)
up=start
env="env default"
else
die "Could not find boot2docker or docker-machine -- you need to set one of those before running this script."
fi
fi
if [ $($machine status) != "running" ]; then
echo "The docker VM doesn't seem to be running; will attempt to start it by doing '$$ $machine $up':"
$machine $up || die "Failed taking up the Docker VM"
fi
if [ ! -f ~/.docker-info ]; then
$machine $env 2>/dev/null | grep DOCKER_ > ~/.docker-info
fi
. ~/.docker-info
echo ""
echo "Starting a docker container for '$TAG'."
docker ps | grep -q $REPO:$TAG && die \
"It seems that another docker container is already running the
image '$REPO:$TAG' -- but only one MySQL instance can bind
to the database files at a time. Quitting."
[ -n "$WHO" ] || WHO=$(whoami)
[ -n "$MYSQLDIR" ] || MYSQLDIR=$parent/data/mysql
[ -n "$URL"] || URL=https://www.ietf.org/lib/dt/sprint/ietf_utf8.bin.tar.bz2
if [ -n "$DOWNLOAD" ]; then
(
cd $(dirname $MYSQLDIR)
wget -N $URL && tar xjf ietf_utf8.bin.tar.bz2 && chmod -R go+rwX mysql
)
[ -d "$MYSQLDIR" ] || die "The download seems to have failed; still no $MYSQLDIR. Giving up."
else
[ -d "$MYSQLDIR" ] || die "Expected $MYSQLDIR to exist, but it\ndidn't. Use '$program -d' to download and unpack the database."
fi
if ! docker images $REPO | grep -q $TAG; then
echo "Fetching a docker image for your docker container ..."
if ! docker pull $REPO:$TAG; then
docker pull $REPO:latest || die "Failed to pull down the '$REPO:latest' docker image"
id=$(docker images $REPO | grep latest | awk '{print $3}')
echo "Tagging $REPO:latest as $REPO:$TAG for use as environment for this branch."
docker tag $id $REPO:$TAG
fi
fi
if [ -n "$NOMYMAP" ]; then
docker run -ti -p 8000:8000 -v $HOME:/home/$WHO -e USER=$WHO -e DATADIR=${parent#$HOME/}/data -e CWD=${PWD#$HOME/} -e TAG=$TAG $REPO:$TAG
else
docker run -ti -p 8000:8000 -v $HOME:/home/$WHO -v $MYSQLDIR:/var/lib/mysql -e USER=$WHO -e DATADIR=${parent#$HOME/}/data -e CWD=${PWD#$HOME/} -e TAG=$TAG $REPO:$TAG
fi
echo ""
echo "Committing changes in the container to an image:"
latest=$(docker ps -lq)
docker commit $latest $REPO:$TAG
echo ""
echo "Cleaning up containers and images"
docker rm $latest
docker images -f dangling=true -q | xargs docker rmi -f