From c2432107a3bff7d790eb8a69e88224088c19c8b9 Mon Sep 17 00:00:00 2001 From: Henrik Levkowetz Date: Sat, 7 Nov 2015 20:25:37 +0000 Subject: [PATCH] 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 --- docker/Dockerfile | 42 ++++++++---- docker/docker-init.sh | 93 +++++++++++++++++++++++++++ docker/run | 134 +++++++++++++++++++++++++++++++++++++++ docker/settings_local.py | 6 +- 4 files changed, 261 insertions(+), 14 deletions(-) create mode 100755 docker/docker-init.sh create mode 100755 docker/run diff --git a/docker/Dockerfile b/docker/Dockerfile index 5340fc19c..514468ccf 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -21,23 +21,37 @@ FROM debian:wheezy MAINTAINER Henrik Levkowetz +# Default django runserver port +EXPOSE 8000 + # Use backports RUN echo "deb http://http.debian.net/debian wheezy-backports main contrib non-free" >> /etc/apt/sources.list # Run apt-get noninteractive ENV DEBIAN_FRONTEND=noninteractive -# Some basic packages we'll need -RUN apt-get update && apt-get install -qy apt-utils wget less python procps ca-certificates awk +# Install needed packages +RUN apt-get update && apt-get install -qy \ + apt-utils \ + ca-certificates \ + gawk \ + less \ + libmysqlclient-dev \ + libsvn1/wheezy-backports \ + libxml2-dev \ + libxslt-dev \ + mysql-server \ + procps \ + pv \ + python \ + python-dev \ + subversion/wheezy-backports \ + wget \ + && apt-get clean \ + && rm -rf /var/lib/apt/lists/* # MySQL -RUN apt-get update && apt-get install -qy mysql-server - -# Subversion -RUN apt-get update && apt-get install -qy subversion/wheezy-backports libsvn1/wheezy-backports - -# Some packages needed to compile requirements -RUN apt-get update && apt-get install -qy libmysqlclient-dev python-dev libxml2-dev libxslt-dev +VOLUME /var/lib/mysql # Pip 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 RUN chmod +x /usr/local/bin/idnits # A default user -RUN useradd -ms /bin/bash django +RUN useradd -m -s /bin/bash -u 500 django USER django WORKDIR /home/django @@ -68,4 +82,10 @@ RUN . bin/activate; pip install -r requirements.txt COPY settings_local.py ./settings_local.py RUN . bin/activate; ietf/manage.py test --settings=settings_sqlitetest -WORKDIR /home +USER root +WORKDIR / + +COPY docker-init.sh /docker-init.sh +ENTRYPOINT ["/docker-init.sh"] + +CMD /bin/bash diff --git a/docker/docker-init.sh b/docker/docker-init.sh new file mode 100755 index 000000000..d14c3c457 --- /dev/null +++ b/docker/docker-init.sh @@ -0,0 +1,93 @@ +#!/bin/bash + +echo "Gathering info ..." +MYSQLDIR="$(mysqld --verbose --help 2>/dev/null | awk '$1 == "datadir" { print $2; exit }')" + +echo "Checking if MySQL base data exists ..." +if [ ! -d $MYSQLDIR/mysql ]; then + echo "Re-installing MySQL ..." + apt-get update && apt-get install --reinstall mysql-server +fi + + +echo "Checking if MySQL is running ..." +if ! /etc/init.d/mysql status; then + echo "Starting mysql ..." + /etc/init.d/mysql start +fi + +echo "Checking if the IETF database exists at $MYSQLDIR ..." +if [ ! -d $MYSQLDIR/ietf_utf8 ]; then + ls -l $MYSQLDIR + + echo "Creating database ..." + mysqladmin -u root --default-character-set=utf8 create ietf_utf8 + + echo "Setting up permissions ..." + mysql -u root ietf_utf8 <<< "GRANT ALL PRIVILEGES ON ietf_utf8.* TO django@localhost IDENTIFIED BY 'RkTkDPFnKpko'; FLUSH PRIVILEGES;" + + echo "Fetching database ..." + DUMPDIR=/home/$USER/$DATADIR + wget -N -P $DUMPDIR http://www.ietf.org/lib/dt/sprint/ietf_utf8.sql.gz + + echo "Loading database ..." + gunzip < $DUMPDIR/ietf_utf8.sql.gz \ + | pv --progress --bytes --rate --eta --cursor --size $(gzip --list --quiet $DUMPDIR/ietf_utf8.sql.gz | awk '{ print $2 }') \ + | sed -e 's/ENGINE=MyISAM/ENGINE=InnoDB/' \ + | mysql --user=django --password=RkTkDPFnKpko -s -f ietf_utf8 \ + && rm /tmp/ietf_utf8.sql.gz + +fi + +if ! id -u "$USER" &> /dev/null; then + echo "Creating user '$USER' ..." + useradd -ms /bin/bash $USER +fi + +if [ ! -d /opt/home/$USER ]; then + echo "Setting up python virtualenv at /opt/home/$USER ..." + mkdir -p /opt/home/$USER + chown $USER /opt/home/$USER + mkdir /opt/home/$USER/datatracker + virtualenv /opt/home/$USER/datatracker +fi + +echo "Activating virtual python environment" +cat /opt/home/$USER/datatracker/bin/activate >> /etc/bash.bashrc +. /opt/home/$USER/datatracker/bin/activate + + +if [ ! -d /opt/home/$USER/datatracker/lib/python2.7/site-packages/django ]; then + echo "Installing requirements (based on trunk)" + pip install -r /home/django/src/trunk/requirements.txt +fi + +if [ ! -f /opt/home/$USER/datatracker/lib/site-python/settings_local.py ]; then + echo "Setting up a default settings_local.py" + mkdir -p /opt/home/$USER/datatracker/lib/site-python/ + cp /home/django/src/trunk/settings_local.py /opt/home/$USER/datatracker/lib/site-python/ +fi + +echo "Done." + +FLAG1=/opt/home/$USER/.docker-init-flag-1 +if [ ! -f $FLAG1 ]; then + touch $FLAG1 + cat <<-EOT + + ****************************************************************************** + + You should now cd to your svn working directory and update the datatracker + prerequisites according to the requirements given in 'requirements.txt': + + $ pip install -r requirements.txt + + Happy coding! + + ****************************************************************************** +EOT +fi + +chown -R $USER /opt/home/$USER +cd /home/$USER +su $USER diff --git a/docker/run b/docker/run new file mode 100755 index 000000000..6e490e51a --- /dev/null +++ b/docker/run @@ -0,0 +1,134 @@ +#!/bin/bash + +version=0.10 +program=${0##*/} +progdir=${0%/*} +if [ "$progdir" = "$program" ]; then progdir="."; fi +if [ "$progdir" = "." ]; then progdir="$PWD"; fi +parent=$(dirname $progdir) + +# ---------------------------------------------------------------------- +function usage() { + cat < + +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:vVu:m: +longopts=download-data,help,ietfdb-url=,verbose,version,user=,mysqldata=, + +# Default values + +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 + -u| --user) WHO=$2; shift;; # Run container as someone else than $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 + +docker ps | grep -q levkowetz/datatracker:latest && die \ +"It seems that another docker container is already running the +image 'levkowetz/datatracker:latest' -- 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 https://www.ietf.org/lib/dt/sprint/ietf_utf8.bin.tar.bz2 && 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 + +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 +latest=$(docker ps -lq) +echo "Committing changes in the container to an image:" +docker commit $latest levkowetz/datatracker:latest diff --git a/docker/settings_local.py b/docker/settings_local.py index ed6824705..d77645d46 100644 --- a/docker/settings_local.py +++ b/docker/settings_local.py @@ -2,10 +2,10 @@ SECRET_KEY = 'jzv$o93h_lzw4a0%0oz-5t5lk+ai=3f8x@uo*9ahu8w4i300o6' DATABASES = { 'default': { - 'NAME': 'dev_ietf_utf8', + 'NAME': 'ietf_utf8', 'ENGINE': 'django.db.backends.mysql', - 'USER': 'django_dev', - 'PASSWORD': 'mJM+#@Fgec', + 'USER': 'django', + 'PASSWORD': 'RkTkDPFnKpko', }, }