Skip to content

Commit

Permalink
Merge pull request #149 from sympy/dockerize
Browse files Browse the repository at this point in the history
Dockerize the project for development
  • Loading branch information
aktech committed May 7, 2020
2 parents ce3f91d + 2431b7d commit a103bb1
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 93 deletions.
40 changes: 10 additions & 30 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,23 @@ language: python
python:
- '2.7'

cache:
directories:
- "$HOME/google-cloud-sdk/"

virtualenv:
system_site_packages: true
services:
- docker

before_install:
- npm install -g casperjs
- ls
install:
- pip install -r requirements/requirements.txt -t lib/
- pip install -r requirements/local_requirements.txt

- ls
before_script:
- gcloud version || true
- if [ ! -d "$HOME/google-cloud-sdk/bin" ]; then rm -rf "$HOME/google-cloud-sdk"; curl https://sdk.cloud.google.com | bash > /dev/null; fi
- source /home/travis/google-cloud-sdk/path.bash.inc
- gcloud version
- cd ..
- gcloud components install cloud-datastore-emulator --quiet
- gcloud beta emulators datastore start &
- wget https://storage.googleapis.com/appengine-sdks/featured/google_appengine_1.9.90.zip -nv
- unzip -q google_appengine_1.9.90.zip
- export SDK_LOCATION="$(pwd)/google_appengine"
- cd $TRAVIS_BUILD_DIR
- python $SDK_LOCATION/dev_appserver.py --skip_sdk_update_check 1 . --env_var DATASTORE_EMULATOR_HOST=localhost:8081 --env_var DATASTORE_USE_PROJECT_ID_AS_APP_ID=true &
- sleep 10
- docker-compose up -d

script:
- PYTHONPATH='.' nosetests app/test -vv
- casperjs test app/test
- sleep 2 # Wait for api to be up
- docker-compose exec app nosetests app/test -vv
- docker-compose exec app casperjs test app/test

before_deploy:
- docker cp gamma_app:/usr/src/app/lib lib
- openssl aes-256-cbc -K $encrypted_2fd045226a67_key -iv $encrypted_2fd045226a67_iv
-in client-secret.json.enc -out client-secret.json -d
- version=$(if [ ! -z "$TRAVIS_TAG" ]; then echo $(cut -d'-' -f2 <<<"$TRAVIS_TAG"); else echo "$TRAVIS_BRANCH"; fi)
Expand All @@ -51,9 +35,5 @@ deploy:
repo: sympy/sympy_gamma

after_deploy:
- pip install requests
- python bin/update_status_on_pr.py

env:
global:
# Do not prompt for user input when using any SDK methods.
- CLOUDSDK_CORE_DISABLE_PROMPTS=1
93 changes: 30 additions & 63 deletions README.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
SymPy Gamma
===========

.. image:: https://travis-ci.org/sympy/sympy_gamma.svg?branch=master
:target: https://travis-ci.org/sympy/sympy_gamma

`SymPy Gamma <https://www.sympygamma.com>`_ is a simple web application based
on Google App Engine that executes and displays the results of SymPy
expressions as well as additional related computations, in a fashion similar
Expand All @@ -20,20 +23,7 @@ information from loading.
Installation
------------

Download and unpack most recent Google App Engine SDK for Python from
https://code.google.com/appengine/downloads.html, e.g.::

$ wget https://storage.googleapis.com/appengine-sdks/featured/google_appengine_1.9.90.zip
$ unzip google_appengine_1.9.90.zip

On the Mac, it is a disk image with an application, which you should
drag to your Applications folder. Open the program and install the
symlinks (it should ask you the first time you open the application, but
if it doesn't, choose "Make Symlinks..." from the
GoogleAppEngineLauncher menu). Note that you will have to do this again
each time you update the AppEngine program.

Then clone sympy_gamma repository::
Clone sympy_gamma repository::

$ git clone git://github.com/sympy/sympy_gamma.git
$ cd sympy_gamma
Expand All @@ -46,50 +36,26 @@ We use submodules to include external libraries in sympy_gamma::
This is sufficient to clone appropriate repositories in correct versions
into sympy_gamma (see git documentation on submodules for information).

Install Dependencies
--------------------

The project depends on some third-party libraries that are not on the list
of built-in libraries (in app.yaml) bundled with the runtime, to install them
run the following command.::

pip install -r requirements/requirements.txt -t lib/

Some libraries although available on app engine runtime, but needs to be
installed locally for development.

Ref: https://cloud.google.com/appengine/docs/standard/python/tools/using-libraries-python-27#local_development ::

pip install -r requirements/local_requirements.txt

You will need to install Datastore Emulator as well, which comes from gcloud's SDK,
install the Google Cloud SDK for your OS from here: https://cloud.google.com/sdk/install
Then run the following commands to install and run the datastore emulator in the background::

gcloud components install cloud-datastore-emulator --quiet
gcloud beta emulators datastore start &


Development server
Development Server
------------------

Now you are ready to run development web server::
To setup the development environment and run the app locally, you
need ``docker`` and ``docker-compose``:

$ ../google_appengine/dev_appserver.py .
* https://docs.docker.com/get-docker/
* https://docs.docker.com/compose/install/

On the Mac, just run::
Now you are ready to run development web server::

$ dev_appserver.py .
$ docker-compose up

(make sure you installed the symlinks as described above).
This will build and run the image for app and datastore emulator.

I couldn't figure out how to make it work in the GUI (it won't find the
sympy git submodule). If you figure out how to do it, please update
this file and send a patch describing how to do it.
This will spin up a local server that runs on port ``8080``.
Open a web browser and go to http://localhost:8080.
You should see GUI of SymPy Gamma

This is a local server that runs on port 8080 (use ``--port`` option to
change this). Open a web browser and go to http://localhost:8080. You
should see GUI of SymPy Gamma.

Deploying to GAE
----------------
Expand All @@ -114,6 +80,11 @@ the google cloud console for the project::

$ gcloud init

You need to to create ``lib`` (libraries) before deploying, make sure the development
server is up and running via ``docker-compose``, as mentioned above and create
libraries folder to package with the following command::

$ docker cp gamma_app:/usr/src/app/lib lib

Assuming that sympy_gamma works properly (also across different mainstream web
browsers), you can upload your changes to Google App Engine, replacing the
Expand Down Expand Up @@ -161,6 +132,12 @@ Currently, there is no testing server set up as there is for SymPy
Live. However, you can set up your own testing server (it's free, though it
requires a cell phone to set up).

You need to to create ``lib`` (libraries) before deploying, make sure the development
server is up and running via ``docker-compose``, as mentioned above and create
libraries folder to package with the following command::

$ docker cp gamma_app:/usr/src/app/lib lib

Either way, to test, you will need to edit the Project ID in the deploy command
mentioned above with your Project ID and the version you want to deploy to::

Expand Down Expand Up @@ -206,21 +183,11 @@ versions automatically.
Running Tests
-------------

To be able to run tests, make sure you have testing libraries installed::

npm install -g casperjs
pip install nose

Install phantomjs for your system from: https://phantomjs.org/download.html

To run unit tests::

PYTHONPATH='.' nosetests app/test -vv

To run PhantomJS Tests::

casperjs test app/test
To run tests you need to spinup the container as mentioned above
via ``docker-compose`` and run the following command::

$ docker-compose exec app nosetests app/test -vv
$ docker-compose exec app casperjs test app/test

Pulling changes
---------------
Expand Down
Empty file removed __init__.py
Empty file.
27 changes: 27 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
version: '3'

services:
app:
build:
context: .
dockerfile: ./docker/app/Dockerfile
image: app
container_name: gamma_app
depends_on:
- datastore
volumes:
- ./app/:/usr/src/app/app/
ports:
- "8080:8080"
- "8082:8082"
- "8083:8083"
command: /run.sh

datastore:
build:
context: .
dockerfile: ./docker/datastore/Dockerfile
image: datastore
container_name: gamma_datastore
ports:
- "8081:8081"
45 changes: 45 additions & 0 deletions docker/app/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
FROM python:2-slim

RUN apt-get update \
# dependencies for building Python packages
&& apt-get install -y build-essential \
&& apt-get install -y python-dev \
&& apt-get install -y wget \
&& apt-get install -y zip unzip

WORKDIR /usr/src/app

COPY requirements ./requirements
COPY ./docker/app/run.sh /run.sh

RUN pip install -r requirements/requirements.txt -t lib/
RUN pip install -r requirements/local_requirements.txt

# Install App engine SDK
RUN wget https://storage.googleapis.com/appengine-sdks/featured/google_appengine_1.9.90.zip -nv -P /usr/src/
RUN unzip -q /usr/src/google_appengine_1.9.90.zip -d /usr/src/
ENV SDK_LOCATION="/usr/src/google_appengine"

# Install PhantomJs and Casperjs for Testing
ENV PHANTOM_JS="phantomjs-1.9.8-linux-x86_64"
RUN apt-get install -y chrpath libssl-dev libxft-dev \
&& apt-get install -y libfreetype6 libfreetype6-dev \
&& apt-get install -y libfontconfig1 libfontconfig1-dev \
&& apt-get install -y git

RUN cd ../ && wget https://bitbucket.org/ariya/phantomjs/downloads/$PHANTOM_JS.tar.bz2
RUN mv ../$PHANTOM_JS.tar.bz2 /usr/local/share/
RUN cd /usr/local/share/ && tar xvjf $PHANTOM_JS.tar.bz2
RUN ln -sf /usr/local/share/$PHANTOM_JS/bin/phantomjs /usr/local/share/phantomjs
RUN ln -sf /usr/local/share/$PHANTOM_JS/bin/phantomjs /usr/local/bin/phantomjs
RUN ln -sf /usr/local/share/$PHANTOM_JS/bin/phantomjs /usr/bin/phantomjs
RUN rm -rf /usr/local/share/$PHANTOM_JS.tar.bz2

RUN cd ../ && git clone git://github.com/casperjs/casperjs.git \
&& cd casperjs && ln -sf `pwd`/bin/casperjs /usr/local/bin/casperjs

ENV PYTHONPATH="/usr/src/app"

COPY . .

ENTRYPOINT [ "/run.sh" ]
15 changes: 15 additions & 0 deletions docker/app/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env bash

HOST="0.0.0.0"
API_PORT="8082"
ADMIN_PORT="8083"
DATASTORE_EMULATOR_HOST_PORT=datastore:8081

python $SDK_LOCATION/dev_appserver.py --api_host 0.0.0.0 \
--api_port "$API_PORT" \
--admin_host "$HOST" \
--admin_port "$ADMIN_PORT" \
--host "$HOST" \
--skip_sdk_update_check 1 . \
--env_var DATASTORE_EMULATOR_HOST="$DATASTORE_EMULATOR_HOST_PORT" \
--env_var DATASTORE_USE_PROJECT_ID_AS_APP_ID=true
8 changes: 8 additions & 0 deletions docker/datastore/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM google/cloud-sdk:latest

WORKDIR /usr/src/app
COPY ./docker/datastore/run.sh ./run.sh

ENV CLOUDSDK_CORE_PROJECT=sympy-live-hrd

ENTRYPOINT [ "./run.sh" ]
3 changes: 3 additions & 0 deletions docker/datastore/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash

gcloud beta emulators datastore start --host-port=0.0.0.0:8081

0 comments on commit a103bb1

Please sign in to comment.