diff --git a/.gitignore b/.gitignore index 7d04bee..8490079 100644 --- a/.gitignore +++ b/.gitignore @@ -2,7 +2,10 @@ db.sqlite3 pypolo/local_settings.py data.json db.sqlite3-journal +docker/env +docker-compose.yml +static/* .DS_Store *.pyc -static/* *.log +*.swp diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..c9d5f82 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,18 @@ +FROM python:2.7 + +RUN apt-get update && apt-get install -y \ + gcc \ + gettext \ + mysql-client libmysqlclient-dev \ + postgresql-client libpq-dev \ + sqlite3 \ + libblas-dev \ + liblapack-dev \ + libatlas-base-dev \ + gfortran \ + cron \ + --no-install-recommends && rm -rf /var/lib/apt/lists/* + +COPY requirements.txt /requirements.txt +RUN pip install --no-cache-dir -r /requirements.txt + diff --git a/docker-compose.yml.example b/docker-compose.yml.example new file mode 100644 index 0000000..7cdda64 --- /dev/null +++ b/docker-compose.yml.example @@ -0,0 +1,21 @@ +version: '2' +services: + db: + image: postgres + env_file: docker/env + volumes: + - 'db-data:/var/lib/postgresql/data' + web: + image: t0mk/pytrader-dev + build: ./ + command: /root/pytrader/docker/wait-and-run.sh db + ports: + - "8000:8000" + depends_on: + - db + env_file: docker/env + volumes: + - './:/root/pytrader' +volumes: + db-data: + diff --git a/docker/create_admin.py b/docker/create_admin.py new file mode 100755 index 0000000..221d0b5 --- /dev/null +++ b/docker/create_admin.py @@ -0,0 +1,9 @@ +#!/usr/bin/env python +import os +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "pypolo.settings") +from django.contrib.auth.models import User + +username = os.environ.get("PYTRADER_USER", 'trader') +password = os.environ.get("PYTRADER_PASSWORD", 'trader') +if not User.objects.filter(username=username).exists(): + User.objects.create_superuser(username, '', password) diff --git a/docker/env.example b/docker/env.example new file mode 100644 index 0000000..86696f8 --- /dev/null +++ b/docker/env.example @@ -0,0 +1,6 @@ +POSTGRES_USER=trader +POSTGRES_PASSWORD=trader +POSTGRES_DB=trader + +POLONIEX_API_KEY= +POLONIEX_API_SECRET= diff --git a/docker/supervisord.conf b/docker/supervisord.conf new file mode 100644 index 0000000..edc6ce5 --- /dev/null +++ b/docker/supervisord.conf @@ -0,0 +1,19 @@ +[supervisord] +logfile=/tmp/supervisord.log ; (main log file;default $CWD/supervisord.log) +logfile_maxbytes=50MB ; (max main logfile bytes b4 rotation;default 50MB) +logfile_backups=10 ; (num of main logfile rotation backups;default 10) +loglevel=info ; (log level;default info; others: debug,warn,trace) +pidfile=/tmp/supervisord.pid ; (supervisord pidfile;default supervisord.pid) +nodaemon=true ; (start in foreground if true;default false) +minfds=1024 ; (min. avail startup file descriptors;default 1024) +minprocs=200 ; (min. avail process descriptors;default 200) + +[program:django] +command = ./manage.py runserver 0.0.0.0:8000 +directory = /root/pytrader + +[program:cron] +command = /usr/sbin/cron -f +stdout_logfile = /%(program_name)s.log +stderr_logfile = /%(program_name)s.log +autorestart = true diff --git a/docker/wait-and-run.sh b/docker/wait-and-run.sh new file mode 100755 index 0000000..2a78c0b --- /dev/null +++ b/docker/wait-and-run.sh @@ -0,0 +1,24 @@ +#!/bin/bash + +set -e + +host="$1" +export PGPASSWORD=$POSTGRES_PASSWORD +until psql -h "$host" -U $POSTGRES_USER -c '\l'; do + >&2 echo "Postgres is unavailable - sleeping" + sleep 1 +done + +>&2 echo "Postgres is up - doing Django magic" +cd /root/pytrader + +# http://stackoverflow.com/questions/23564529/chartit-is-not-a-valid-tag-librarydjango +sed -i 's/from django.utils import simplejson/import simplejson/' /usr/local/lib/python2.7/site-packages/chartit/templatetags/chartit.py +./manage.py syncdb --noinput +# For some reason I can't make it run from the subdir. I dont know Django too +# well. +cp ./docker/create_admin.py ./ +./create_admin.py +./manage.py migrate --noinput +crontab /root/pytrader/scripts/crontab.txt +exec supervisord -n -c /root/pytrader/docker/supervisord.conf diff --git a/pypolo/local_settings.py.example b/pypolo/local_settings.py.example new file mode 100644 index 0000000..970e73b --- /dev/null +++ b/pypolo/local_settings.py.example @@ -0,0 +1,22 @@ +import os + +BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + +MAKE_TRADES = False + +API_KEY = os.environ['POLONIEX_API_KEY'] +API_SECRET = os.environ['POLONIEX_API_SECRET'] + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.postgresql_psycopg2', + 'NAME': os.environ['POSTGRES_DB'], + 'USER': os.environ['POSTGRES_USER'], + 'PASSWORD': os.environ['POSTGRES_PASSWORD'], + 'HOST': 'db', + 'PORT': '', + 'ATOMIC_REQUESTS': True, + }, +} + +DEBUG_APPS = [] diff --git a/readme.md b/readme.md index ce2f34b..05b3f03 100644 --- a/readme.md +++ b/readme.md @@ -224,8 +224,37 @@ Once enough `Price` objects are stored in the database, you'll be able to begin See the next document, [How to trade with pytrader](https://github.com/owocki/pytrader/blob/master/howto_trade.md). +## Docker dev setup - +Work with docker 1.10.3 and docker-compose 1.6.2 + +- initalize your environment: + +``` +cp docker-compose.yml.example docker-compose.yml +cp docker/env.example docker/env +cp pypolo/local_settings.py.example pypolo/local_settings.py +``` + +1. Add your POLONIEX_API_KEY and POLONIEX_API_SECRET to docker/env (its gitignored, dont worry) +2. Build Docker image (compiling stuff for scipy and numpy takes time): `docker-compose build` or pull the images from Docker Hub: `docker-compose pull` +4. Run the containers: `docker-compose up` +5. Get shell: `docker exec -it pytrader_web_1 /bin/bash` +6. Place sql seed in this repo dir as `prices.psql` +7. in Django container + +``` +cd /root/pytrader +export PGPASSWORD=$POSTGRES_PASSWORD +psql -h db -U trader trader < prices.psql +``` +wait for the psql load to end + +8. restart setup: (in the host): `docker-compose kill && docker-compose up -d` +9. Visit http://localhost:8000/admin and log in as `trader:trader` + + + diff --git a/requirements.txt b/requirements.txt index 006fc82..c183794 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,5 @@ django==1.8.2 +numpy==1.7.1 scipy==0.13.3 matplotlib==1.3.1 PyBrain==0.3 @@ -7,3 +8,4 @@ django-chartit==0.1 simplejson==3.8.2 Cython==0.23.5 sklearn==0.0 +supervisor==3.2.3