Skip to content

Commit c243210

Browse files
committed
Updated Dockerfile and the default settings_local for the docker image. Added docker-init.sh which is used in the docker image, and docker/run which is a wrapper which runs the docker image with suitable settings.
- Legacy-Id: 10436
1 parent a17b3c6 commit c243210

4 files changed

Lines changed: 261 additions & 14 deletions

File tree

docker/Dockerfile

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,37 @@
2121
FROM debian:wheezy
2222
MAINTAINER Henrik Levkowetz <henrik@levkowetz.com>
2323

24+
# Default django runserver port
25+
EXPOSE 8000
26+
2427
# Use backports
2528
RUN echo "deb http://http.debian.net/debian wheezy-backports main contrib non-free" >> /etc/apt/sources.list
2629

2730
# Run apt-get noninteractive
2831
ENV DEBIAN_FRONTEND=noninteractive
2932

30-
# Some basic packages we'll need
31-
RUN apt-get update && apt-get install -qy apt-utils wget less python procps ca-certificates awk
33+
# Install needed packages
34+
RUN apt-get update && apt-get install -qy \
35+
apt-utils \
36+
ca-certificates \
37+
gawk \
38+
less \
39+
libmysqlclient-dev \
40+
libsvn1/wheezy-backports \
41+
libxml2-dev \
42+
libxslt-dev \
43+
mysql-server \
44+
procps \
45+
pv \
46+
python \
47+
python-dev \
48+
subversion/wheezy-backports \
49+
wget \
50+
&& apt-get clean \
51+
&& rm -rf /var/lib/apt/lists/*
3252

3353
# MySQL
34-
RUN apt-get update && apt-get install -qy mysql-server
35-
36-
# Subversion
37-
RUN apt-get update && apt-get install -qy subversion/wheezy-backports libsvn1/wheezy-backports
38-
39-
# Some packages needed to compile requirements
40-
RUN apt-get update && apt-get install -qy libmysqlclient-dev python-dev libxml2-dev libxslt-dev
54+
VOLUME /var/lib/mysql
4155

4256
# Pip
4357
ENV PYTHONWARNINGS="ignore:a true SSLContext object"
@@ -52,7 +66,7 @@ RUN wget -q -P /usr/local/bin/ https://tools.ietf.org/tools/idnits/idnits
5266
RUN chmod +x /usr/local/bin/idnits
5367

5468
# A default user
55-
RUN useradd -ms /bin/bash django
69+
RUN useradd -m -s /bin/bash -u 500 django
5670
USER django
5771
WORKDIR /home/django
5872

@@ -68,4 +82,10 @@ RUN . bin/activate; pip install -r requirements.txt
6882
COPY settings_local.py ./settings_local.py
6983
RUN . bin/activate; ietf/manage.py test --settings=settings_sqlitetest
7084

71-
WORKDIR /home
85+
USER root
86+
WORKDIR /
87+
88+
COPY docker-init.sh /docker-init.sh
89+
ENTRYPOINT ["/docker-init.sh"]
90+
91+
CMD /bin/bash

docker/docker-init.sh

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#!/bin/bash
2+
3+
echo "Gathering info ..."
4+
MYSQLDIR="$(mysqld --verbose --help 2>/dev/null | awk '$1 == "datadir" { print $2; exit }')"
5+
6+
echo "Checking if MySQL base data exists ..."
7+
if [ ! -d $MYSQLDIR/mysql ]; then
8+
echo "Re-installing MySQL ..."
9+
apt-get update && apt-get install --reinstall mysql-server
10+
fi
11+
12+
13+
echo "Checking if MySQL is running ..."
14+
if ! /etc/init.d/mysql status; then
15+
echo "Starting mysql ..."
16+
/etc/init.d/mysql start
17+
fi
18+
19+
echo "Checking if the IETF database exists at $MYSQLDIR ..."
20+
if [ ! -d $MYSQLDIR/ietf_utf8 ]; then
21+
ls -l $MYSQLDIR
22+
23+
echo "Creating database ..."
24+
mysqladmin -u root --default-character-set=utf8 create ietf_utf8
25+
26+
echo "Setting up permissions ..."
27+
mysql -u root ietf_utf8 <<< "GRANT ALL PRIVILEGES ON ietf_utf8.* TO django@localhost IDENTIFIED BY 'RkTkDPFnKpko'; FLUSH PRIVILEGES;"
28+
29+
echo "Fetching database ..."
30+
DUMPDIR=/home/$USER/$DATADIR
31+
wget -N -P $DUMPDIR http://www.ietf.org/lib/dt/sprint/ietf_utf8.sql.gz
32+
33+
echo "Loading database ..."
34+
gunzip < $DUMPDIR/ietf_utf8.sql.gz \
35+
| pv --progress --bytes --rate --eta --cursor --size $(gzip --list --quiet $DUMPDIR/ietf_utf8.sql.gz | awk '{ print $2 }') \
36+
| sed -e 's/ENGINE=MyISAM/ENGINE=InnoDB/' \
37+
| mysql --user=django --password=RkTkDPFnKpko -s -f ietf_utf8 \
38+
&& rm /tmp/ietf_utf8.sql.gz
39+
40+
fi
41+
42+
if ! id -u "$USER" &> /dev/null; then
43+
echo "Creating user '$USER' ..."
44+
useradd -ms /bin/bash $USER
45+
fi
46+
47+
if [ ! -d /opt/home/$USER ]; then
48+
echo "Setting up python virtualenv at /opt/home/$USER ..."
49+
mkdir -p /opt/home/$USER
50+
chown $USER /opt/home/$USER
51+
mkdir /opt/home/$USER/datatracker
52+
virtualenv /opt/home/$USER/datatracker
53+
fi
54+
55+
echo "Activating virtual python environment"
56+
cat /opt/home/$USER/datatracker/bin/activate >> /etc/bash.bashrc
57+
. /opt/home/$USER/datatracker/bin/activate
58+
59+
60+
if [ ! -d /opt/home/$USER/datatracker/lib/python2.7/site-packages/django ]; then
61+
echo "Installing requirements (based on trunk)"
62+
pip install -r /home/django/src/trunk/requirements.txt
63+
fi
64+
65+
if [ ! -f /opt/home/$USER/datatracker/lib/site-python/settings_local.py ]; then
66+
echo "Setting up a default settings_local.py"
67+
mkdir -p /opt/home/$USER/datatracker/lib/site-python/
68+
cp /home/django/src/trunk/settings_local.py /opt/home/$USER/datatracker/lib/site-python/
69+
fi
70+
71+
echo "Done."
72+
73+
FLAG1=/opt/home/$USER/.docker-init-flag-1
74+
if [ ! -f $FLAG1 ]; then
75+
touch $FLAG1
76+
cat <<-EOT
77+
78+
******************************************************************************
79+
80+
You should now cd to your svn working directory and update the datatracker
81+
prerequisites according to the requirements given in 'requirements.txt':
82+
83+
$ pip install -r requirements.txt
84+
85+
Happy coding!
86+
87+
******************************************************************************
88+
EOT
89+
fi
90+
91+
chown -R $USER /opt/home/$USER
92+
cd /home/$USER
93+
su $USER

docker/run

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
#!/bin/bash
2+
3+
version=0.10
4+
program=${0##*/}
5+
progdir=${0%/*}
6+
if [ "$progdir" = "$program" ]; then progdir="."; fi
7+
if [ "$progdir" = "." ]; then progdir="$PWD"; fi
8+
parent=$(dirname $progdir)
9+
10+
# ----------------------------------------------------------------------
11+
function usage() {
12+
cat <<EOF
13+
NAME
14+
$program - Run a docker datatracker container with suitable settings
15+
16+
SYNOPSIS
17+
$program [OPTIONS] ARGS
18+
19+
DESCRIPTION
20+
21+
This is a wrapper which runs docker with suitable arguments on a
22+
debian-based docker image which has been set up with the dependencies
23+
needed to easily run the IETF datatracker in development mode. By
24+
default, it expects to find MySWL database files at
25+
$parent/data/mysql, which is mapped inside the
26+
container to /var/lib/mysql, and it will set up a home directory for
27+
the current user ($USER) and map it to $HOME.
28+
29+
EOF
30+
echo -e "OPTIONS"
31+
if [ "$(uname)" = "Linux" ]; then
32+
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/'
33+
else
34+
egrep "^[ ]+[-][A-Za-z| -]+\*?\)[ ]+[A-Za-z].+#" $0 | sed 's/\|.*\$2[^#]*#/ /'| sed -E 's/\|.*\)[^#]*#/ /'
35+
fi
36+
cat <<EOF
37+
38+
FILES
39+
40+
AUTHOR
41+
Written by Henrik Levkowetz, <henrik@levkowetz.com>
42+
43+
COPYRIGHT
44+
45+
Copyright (c) 2015 IETF Trust and the persons identified as authors of
46+
the code. All rights reserved. License 'Simplified BSD', as specified
47+
in http://opensource.org/licenses/BSD-3-Clause.
48+
49+
EOF
50+
51+
}
52+
53+
# ----------------------------------------------------------------------
54+
function die() {
55+
echo -e "\n$program: error: $*" > /dev/stderr
56+
exit 1
57+
}
58+
59+
function note() {
60+
if [ -n "$VERBOSE" ]; then echo -e "$*"; fi
61+
}
62+
63+
# ----------------------------------------------------------------------
64+
function version() {
65+
echo -e "$program $version"
66+
}
67+
68+
# ----------------------------------------------------------------------
69+
trap 'echo "$program($LINENO): Command failed with error code $? ([$$] $0 $*)"; exit 1' ERR
70+
71+
72+
# ----------------------------------------------------------------------
73+
# Option parsing
74+
75+
# Options
76+
shortopts=dhi:vVu:m:
77+
longopts=download-data,help,ietfdb-url=,verbose,version,user=,mysqldata=,
78+
79+
# Default values
80+
81+
if [ "$(uname)" = "Linux" ]; then
82+
args=$(getopt -o "$shortopts" --long "$longopts" -n '$program' -- $SV "$@")
83+
if [ $? != 0 ] ; then die "Terminating..." >&2 ; exit 1 ; fi
84+
eval set -- "$args"
85+
sed="sed -r"
86+
else
87+
# Darwin, BSDs
88+
args=$(getopt -o$shortopts $SV $*)
89+
if [ $? != 0 ] ; then die "Terminating..." >&2 ; exit 1 ; fi
90+
set -- $args
91+
sed="sed -E"
92+
fi
93+
94+
while true ; do
95+
case "$1" in
96+
-d| --download-data) DOWNLOAD=1;; # Download and set up the database files
97+
-h| --help) usage; exit;; # Show this help, then exit
98+
-i| --ietfdb-url) URL=$2; shift;; # Use an alternative database tarball URL
99+
-m| --mysqldir) MYSQLDIR=$2; shift;; # Set the desired location for MySQL's database files
100+
-u| --user) WHO=$2; shift;; # Run container as someone else than $USER
101+
-v| --verbose) VERBOSE=1;; # Be more talkative
102+
-V| --version) version; exit;; # Show program version, then exit
103+
--) shift; break;;
104+
*) die "Internal error, inconsistent option specification: '$1'";;
105+
esac
106+
shift
107+
done
108+
109+
# ----------------------------------------------------------------------
110+
# The program itself
111+
112+
docker ps | grep -q levkowetz/datatracker:latest && die \
113+
"It seems that another docker container is already running the
114+
image 'levkowetz/datatracker:latest' -- but only one MySQL instance can bind
115+
to the database files at a time. Quitting."
116+
117+
[ -n "$WHO" ] || WHO=$(whoami)
118+
[ -n "$MYSQLDIR" ] || MYSQLDIR=$parent/data/mysql
119+
[ -n "$URL"] || URL=https://www.ietf.org/lib/dt/sprint/ietf_utf8.bin.tar.bz2
120+
121+
if [ -n "$DOWNLOAD" ]; then
122+
(
123+
cd $(dirname $MYSQLDIR)
124+
wget -N https://www.ietf.org/lib/dt/sprint/ietf_utf8.bin.tar.bz2 && tar xjf ietf_utf8.bin.tar.bz2 && chmod -R go+rwX mysql
125+
)
126+
[ -d "$MYSQLDIR" ] || die "The download seems to have failed; still no $MYSQLDIR. Giving up."
127+
else
128+
[ -d "$MYSQLDIR" ] || die "Expected $MYSQLDIR to exist, but it\ndidn't. Use '$program -d' to download and unpack the database."
129+
fi
130+
131+
docker run -ti -p 8000:8000 -v $HOME:/home/$WHO -v $MYSQLDIR:/var/lib/mysql -e USER=$WHO -e DATADIR=${parent#$HOME/}/data levkowetz/datatracker:latest
132+
latest=$(docker ps -lq)
133+
echo "Committing changes in the container to an image:"
134+
docker commit $latest levkowetz/datatracker:latest

docker/settings_local.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
DATABASES = {
44
'default': {
5-
'NAME': 'dev_ietf_utf8',
5+
'NAME': 'ietf_utf8',
66
'ENGINE': 'django.db.backends.mysql',
7-
'USER': 'django_dev',
8-
'PASSWORD': 'mJM+#@Fgec',
7+
'USER': 'django',
8+
'PASSWORD': 'RkTkDPFnKpko',
99
},
1010
}
1111

0 commit comments

Comments
 (0)