From d8757a30164ea30c5f099bf9a01c57e87b6eff96 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Thu, 11 Feb 2016 17:51:35 -0500 Subject: [PATCH] initial commit, it works! --- .dockerignore | 1 + .gitignore | 5 +++++ Dockerfile | 14 ++++++++++++ README.md | 55 ++++++++++++++++++++++++++++++++++++++++++++++ add_users | 3 +++ create_pw | 9 ++++++++ docker-build | 24 ++++++++++++++++++++ docker-compose.yml | 14 ++++++++++++ playbook.yml | 51 ++++++++++++++++++++++++++++++++++++++++++ run | 50 +++++++++++++++++++++++++++++++++++++++++ 10 files changed, 226 insertions(+) create mode 100644 .dockerignore create mode 100644 .gitignore create mode 100644 Dockerfile create mode 100644 README.md create mode 100755 add_users create mode 100755 create_pw create mode 100755 docker-build create mode 100644 docker-compose.yml create mode 100644 playbook.yml create mode 100755 run diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..8076020 --- /dev/null +++ b/.dockerignore @@ -0,0 +1 @@ +chantry-perl.tar.bz2 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6dfd2cd --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +/chantry-website +/chantry-ts +/hosts +/chantry-perl.tar.bz2 +/users.sql diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..6872796 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,14 @@ +FROM ubuntu:15.10 + +RUN apt-get update && apt-get -y install runit postgresql libpq-dev curl redis-server +RUN apt-get update && apt-get -y build-dep perl + +RUN curl -sL https://deb.nodesource.com/setup_5.x | bash - +RUN apt-get install -y nodejs + +RUN npm install -g coffee-script + +WORKDIR /app +COPY . /app + +CMD /app/run diff --git a/README.md b/README.md new file mode 100644 index 0000000..3568c38 --- /dev/null +++ b/README.md @@ -0,0 +1,55 @@ +Run chantry's Twilight Struggle server in Docker. Very nice! + +# Setup + +Clone the `chantry-website` and `chantry-ts` repos from https://bitbucket.org/arry/chantry-website. + +## Bugfix + +You may have to make a small modification to `chantry-website/main/lib/Chantry/WebApp.pm` +and `chantry-website/main/templates/games/play.html.tt` if you're unable to +load the `ts` module in the browser. Find these lines and change `${game_code}.js` +to `${game_code}_bundle.js`: + +``` +# chantry-website/main/lib/Chantry/WebApp.pm +$self->asset("${game_code}.js" => ( + +# chantry-website/main/templates/games/play.html.tt +[% additional_page_js = "${game_code}.js" %] +``` + +# Local hacking + +`docker-compose build && docker-compose up`. + +# Deploying + +`REPO=your.docker.registry ./docker-build` will build the app and Perl/PostgreSQL +container, push the app container to the registry, and export the data container +to a `.tar.bz2` file. You can then use the Ansible playbook to do the deploy to +a remote Docker host: + +``` +REPO=your.docker.registry \ +EXTERNAL_HOST=www.deployed.location \ +EXTERNAL_PORT=45678 \ +ansible-playbook playbook.yml -e with_perl=1 -i hosts +``` + +Set `with_perl=0` to not push a new Perl/PostgreSQL data container. + +## Adding users + +Generate passwords with `docker exec -i chantry create_pw $password`. +Add them to a SQL file (let's say `users.sql`) with this format: + +``` sql +INSERT INTO chantry.users (username, password_hash, email) +VALUES ('john', 'password-from-create_pw', 'john@example.com'); +``` + +Pipe that file into PostgreSQL: + +`docker exec -i chantry add_users < ./users.sql` + diff --git a/add_users b/add_users new file mode 100755 index 0000000..6f94c20 --- /dev/null +++ b/add_users @@ -0,0 +1,3 @@ +#!/bin/bash + +cat | chpst -u postgres:postgres psql -d chantry -a diff --git a/create_pw b/create_pw new file mode 100755 index 0000000..71981ce --- /dev/null +++ b/create_pw @@ -0,0 +1,9 @@ +#!/bin/bash + +PERL_VERSION=5.22.1 + +export PERLBREW_ROOT=/usr/local/perlbrew +export PATH=$PERLBREW_ROOT/bin:$PATH + +cd /app/chantry-website/main +perlbrew exec --with perl-$PERL_VERSION perl script/bcrypt.pl $1 diff --git a/docker-build b/docker-build new file mode 100755 index 0000000..7e33373 --- /dev/null +++ b/docker-build @@ -0,0 +1,24 @@ +#!/bin/bash + +set -x +set -e + +if [ $(docker ps -a | grep chantry-perl | wc -l) -eq 0 ]; then + docker run -v /usr/local/perlbrew -v /var/lib/postgresql/9.4/main --name chantry-perl busybox true +else + echo "You've got a chantry-perl container already. If you want to rebuild it," + echo "docker rm chantry-perl, then run this script again." +fi + +docker build -t $REPO/chantry/app . + +docker run --rm -it --volumes-from chantry-perl busybox rm -f /usr/local/perlbrew/done +docker run --rm -it --volumes-from chantry-perl $REPO/chantry/app + +docker push $REPO/chantry/app + +if [ -z $SKIP_PERL ]; then + docker run --rm -it --volumes-from chantry-perl \ + -v $PWD:/backup \ + busybox tar jcvf /backup/chantry-perl.tar.bz2 /usr/local/perlbrew /var/lib/postgresql/9.4/main +fi diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..aca3dd6 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,14 @@ +app: + build: . + container_name: chantry + volumes_from: + - perl + ports: + - "3000:3010" + environment: + EXTERNAL_HOST: '192.168.99.100' +perl: + image: busybox + volumes: + - /usr/local/perlbrew + - /var/lib/postgresql/9.4/main diff --git a/playbook.yml b/playbook.yml new file mode 100644 index 0000000..5a993ce --- /dev/null +++ b/playbook.yml @@ -0,0 +1,51 @@ +- hosts: all + become: true + become_method: sudo + vars: + with_perl: "0" + tasks: + - name: copy up chantry-perl.tar.bz2 + when: with_perl == "1" + copy: + src: ./chantry-perl.tar.bz2 + dest: /tmp/chantry-perl.tar.bz2 + + - name: remove existing perl container + when: with_perl == "1" + command: bash -c 'docker rm chantry-perl ; docker rmi chantry/perl:latest ; true' + + - name: use new perl container + when: with_perl == "1" + docker: + name: chantry-perl + image: busybox + command: "true" + state: present + volumes: + - /usr/local/perlbrew + - /var/lib/postgresql/9.4/main + + - name: import new perl container + when: with_perl == "1" + command: bash -c 'docker run --rm --volumes-from chantry-perl -v /tmp:/backup busybox tar jxvf /backup/chantry-perl.tar.bz2' + + - name: remove tmp file + when: with_perl == "1" + file: + path: /tmp/chantry-perl.tar.bz2 + state: absent + + - name: run chantry + docker: + name: chantry-app + image: "{{ lookup('env','REPO') }}/chantry/app:latest" + pull: always + net: 'host' + volumes_from: + - 'chantry-perl' + ports: + - "0.0.0.0:{{ lookup('env','EXTERNAL_PORT') }}:3010" + env: + EXTERNAL_HOST: "{{ lookup('env','EXTERNAL_HOST') }}:{{ lookup('env','EXTERNAL_PORT')}}" + restart_policy: always + state: reloaded diff --git a/run b/run new file mode 100755 index 0000000..e041324 --- /dev/null +++ b/run @@ -0,0 +1,50 @@ +#!/bin/bash + +set -x +set -e + +PERL_VERSION=5.22.1 + +export PERLBREW_ROOT=/usr/local/perlbrew +export PATH=$PERLBREW_ROOT/bin:$PATH +export CHANTRY_GAME_DIRS=/app/chantry-ts +export CHANTRY_WEBSITE_DIRS=/app/chantry-ts + +function as_postgres() { + chpst -u postgres:postgres $* +} + +chown -R postgres:postgres /app + +#su -c "/usr/lib/postgresql/9.4/bin/postgres -d 3 -D /var/lib/postgresql/9.4/main -c config_file=/etc/postgresql/9.4/main/postgresql.conf 2>&1" postgres +if [ ! -f /usr/local/perlbrew/done ]; then + curl -L http://install.perlbrew.pl | bash + + perlbrew install perl-$PERL_VERSION || true + perlbrew install-cpanm + perlbrew exec --with perl-$PERL_VERSION cpanm -f --notest --skip-installed < chantry-website/main/deplist.txt + + chown -R postgres:postgres /var/lib/postgresql/9.4/main + as_postgres '/usr/lib/postgresql/9.4/bin/initdb -D /var/lib/postgresql/9.4/main' || true + + /etc/init.d/postgresql stop + /etc/init.d/postgresql start + + as_postgres "createdb -O postgres chantry" || true + cd chantry-website/main/sqitch + as_postgres "/usr/local/perlbrew/bin/perlbrew exec --with perl-$PERL_VERSION sqitch deploy" + + touch /usr/local/perlbrew/done +else + /etc/init.d/postgresql stop + /etc/init.d/postgresql start + + sed -e "s#ws://localhost:3000#ws://${EXTERNAL_HOST}#" < /app/chantry-website/main/chantry-web_app.conf.example > /app/chantry-website/main/chantry-web_app.conf + + cd chantry-website/main + + redis-server & + + chpst -u postgres:postgres /usr/local/perlbrew/bin/perlbrew exec --with perl-$PERL_VERSION .local/start_server & + chpst -u postgres:postgres /usr/local/perlbrew/bin/perlbrew exec --with perl-$PERL_VERSION .local/start_worker +fi