Skip to content

Commit

Permalink
Basic setup works (although not postgres roles)
Browse files Browse the repository at this point in the history
  • Loading branch information
philipbelesky committed Feb 4, 2017
1 parent 573891c commit cb011ac
Show file tree
Hide file tree
Showing 7 changed files with 212 additions and 26 deletions.
10 changes: 10 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copying this over will mess up database settings
*/*/local_settings.py

# Dependencies
node_modules/
venv/

# Compilations
tabbycat/staticfiles/
docs/
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ tabbycat/static/fonts/

# Dependencies
node_modules
bower_components

# Data to ignore
data/*
Expand Down
23 changes: 6 additions & 17 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,21 @@
# Grab a python image
FROM python:3.6

# Just needed for all things python
# Just needed for all things python (note this is setting an env variable)
ENV PYTHONUNBUFFERED 1

# Setup Node/NPM
RUN apt-get update && apt-get install curl
RUN curl -sL https://deb.nodesource.com/setup_6.x | bash
RUN apt-get install -y nodejs
RUN curl -sL https://deb.nodesource.com/setup_6.x | bash && apt-get install -y nodejs

# Copy all our files into the baseimage and cd to that directory
RUN mkdir /tcd
WORKDIR /tcd
ADD . /tcd/ # Can this be skipped? Takes ages
COPY ./tabbycat/local_settings.example /tcd/tabbycat/local_settings.py
# Can this be skipped? Takes ages
ADD . /tcd/

# Install our python requirements
RUN pip install -r ./requirements_common.txt

# Install our node requirements
RUN npm install
# Install our node/python requirements
RUN pip install -r ./requirements_common.txt && npm install

# This needs to happen else sass gets angry
RUN npm rebuild node-sass

# Migrate our database
# RUN python ./tabbycat/manage.py migrate --noinput

# Run server
EXPOSE 8000
# RUN python ./tabbycat/manage.py runserver
5 changes: 5 additions & 0 deletions bin/docker-run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash
# Shorthand to migrate and runserver in docker

python ./tabbycat/manage.py migrate --no-input
python ./tabbycat/manage.py runserver 0.0.0.0:8000
155 changes: 155 additions & 0 deletions bin/docker-wait.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
#!/usr/bin/env bash
# Use this script to test if a given TCP host/port are available
# From https://github.com/vishnubob/wait-for-it/blob/master/wait-for-it.sh

cmdname=$(basename $0)

echoerr() { if [[ $QUIET -ne 1 ]]; then echo "$@" 1>&2; fi }

usage()
{
cat << USAGE >&2
Usage:
$cmdname host:port [-s] [-t timeout] [-- command args]
-h HOST | --host=HOST Host or IP under test
-p PORT | --port=PORT TCP port under test
Alternatively, you specify the host and port as host:port
-s | --strict Only execute subcommand if the test succeeds
-q | --quiet Don't output any status messages
-t TIMEOUT | --timeout=TIMEOUT
Timeout in seconds, zero for no timeout
-- COMMAND ARGS Execute command with args after the test finishes
USAGE
exit 1
}
wait_for()
{
if [[ $TIMEOUT -gt 0 ]]; then
echoerr "$cmdname: waiting $TIMEOUT seconds for $HOST:$PORT"
else
echoerr "$cmdname: waiting for $HOST:$PORT without a timeout"
fi
start_ts=$(date +%s)
while :
do
(echo > /dev/tcp/$HOST/$PORT) >/dev/null 2>&1
result=$?
if [[ $result -eq 0 ]]; then
end_ts=$(date +%s)
echoerr "$cmdname: $HOST:$PORT is available after $((end_ts - start_ts)) seconds"
break
fi
sleep 1
done
return $result
}
wait_for_wrapper()
{
# In order to support SIGINT during timeout: http://unix.stackexchange.com/a/57692
if [[ $QUIET -eq 1 ]]; then
timeout $TIMEOUT $0 --quiet --child --host=$HOST --port=$PORT --timeout=$TIMEOUT &
else
timeout $TIMEOUT $0 --child --host=$HOST --port=$PORT --timeout=$TIMEOUT &
fi
PID=$!
trap "kill -INT -$PID" INT
wait $PID
RESULT=$?
if [[ $RESULT -ne 0 ]]; then
echoerr "$cmdname: timeout occurred after waiting $TIMEOUT seconds for $HOST:$PORT"
fi
return $RESULT
}
# process arguments
while [[ $# -gt 0 ]]
do
case "$1" in
*:* )
hostport=(${1//:/ })
HOST=${hostport[0]}
PORT=${hostport[1]}
shift 1
;;
--child)
CHILD=1
shift 1
;;
-q | --quiet)
QUIET=1
shift 1
;;
-s | --strict)
STRICT=1
shift 1
;;
-h)
HOST="$2"
if [[ $HOST == "" ]]; then break; fi
shift 2
;;
--host=*)
HOST="${1#*=}"
shift 1
;;
-p)
PORT="$2"
if [[ $PORT == "" ]]; then break; fi
shift 2
;;
--port=*)
PORT="${1#*=}"
shift 1
;;
-t)
TIMEOUT="$2"
if [[ $TIMEOUT == "" ]]; then break; fi
shift 2
;;
--timeout=*)
TIMEOUT="${1#*=}"
shift 1
;;
--)
shift
CLI="$@"
break
;;
--help)
usage
;;
*)
echoerr "Unknown argument: $1"
usage
;;
esac
done
if [[ "$HOST" == "" || "$PORT" == "" ]]; then
echoerr "Error: you need to provide a host and port to test."
usage
fi
TIMEOUT=${TIMEOUT:-15}
STRICT=${STRICT:-0}
CHILD=${CHILD:-0}
QUIET=${QUIET:-0}
if [[ $CHILD -gt 0 ]]; then
wait_for
RESULT=$?
exit $RESULT
else
if [[ $TIMEOUT -gt 0 ]]; then
wait_for_wrapper
RESULT=$?
else
wait_for
RESULT=$?
fi
fi
if [[ $CLI != "" ]]; then
if [[ $RESULT -ne 0 && $STRICT -eq 1 ]]; then
echoerr "$cmdname: strict mode, refusing to execute subprocess"
exit $RESULT
fi
exec $CLI
else
exit $RESULT
fi
30 changes: 22 additions & 8 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,39 @@

# Initial setup with
# $ docker-compose up

# Can run management commands with
# $ docker-compose run web /code/manage.py whatever

# Rebuild images with
# $ docker build --no-cache .

version: '2'
services:
db:
image: postgres
environment:
POSTGRES_PASSWORD: password
image: postgres:9.6
restart: always
volumes:
- ./dbdata:/var/lib/postgresql/data
environment:
- POSTGRES_PASSWORD=tabbycat
- POSTGRES_USER=tabbycat
- POSTGRES_DB=tabbycat
web:
build: .
# Hack to wait until Postgres is up before running things
command: bash -c "while ! nc -w 1 -z db 5432; do sleep 0.1; done; ./tabbycat/manage.py migrate; while :; do ./tabbycat/manage.py runserver_plus 0.0.0.0:8000; sleep 1; done"
command: ["./bin/docker-wait.sh", "db:5432", "--", "./bin/docker-run.sh"]
environment:
- DEBUG=1
- IN_DOCKER=1
image: django
build: .
volumes:
- .:/code
- .:/tcd
ports:
- "8000:8000"
expose:
- "8000"
depends_on:
- db
environment:
IN_DOCKER: 1
restart: always

14 changes: 14 additions & 0 deletions tabbycat/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,20 @@
}
}


if bool(int(os.environ['IN_DOCKER'])):
ALLOWED_HOSTS = ["0.0.0.0"]
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'tabbycat',
'USER': 'tabbycat',
'PASSWORD': 'tabbycat',
'HOST': 'db',
'PORT': 5432,
}
}

# ==============================================================================
# Local Overrides
# ==============================================================================
Expand Down

0 comments on commit cb011ac

Please sign in to comment.