Skip to content

Commit

Permalink
Add docker (#109)
Browse files Browse the repository at this point in the history
* Add docker
  • Loading branch information
st4lk committed Jan 12, 2020
1 parent 8401802 commit 5c51454
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 32 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Expand Up @@ -21,3 +21,9 @@ settings_local.py
.direnv/
.envrc
.mypy_cache/
.build
.install
.install-test
.install-optional
.install-python-versions
venv/
17 changes: 17 additions & 0 deletions Dockerfile
@@ -0,0 +1,17 @@
# Base image
FROM python:3.7.6

RUN set -eux; \
apt-get update; \
apt-get install -y gosu; \
rm -rf /var/lib/apt/lists/*; \
# verify that the binary works
gosu nobody true


RUN mkdir -p /opt/runtime/
ADD scripts/* /opt/runtime/

RUN useradd -ms /bin/bash appuser

ENTRYPOINT ["/opt/runtime/entrypoint.sh"]
84 changes: 84 additions & 0 deletions Makefile
@@ -0,0 +1,84 @@
.PHONY: run all test test_tox shell run-example native-test native-test-tox

PROJECT_PATH = /django_rest_social_auth
PORT ?= 8000

# ---------------------------
# Commands for running docker
# ---------------------------

all: run-example

build: .build
.build: Dockerfile scripts/*
docker build -t st4lk/django-rest-social-auth .
touch $@

rebuild:
rm .build
$(MAKE) build

run: build
docker run -it --rm --name django-rest-social-auth \
-p $(PORT):$(PORT) \
-v $(PWD):$(PROJECT_PATH)/ \
st4lk/django-rest-social-auth "$(COMMAND)"

run-example:
@COMMAND='make native-run-example' $(MAKE) run

test:
@COMMAND='make native-test' $(MAKE) run

test-tox:
@COMMAND='make native-test-tox' $(MAKE) run

shell:
@COMMAND='bash' $(MAKE) run

# ------------------------
# Commands to run natively
# ------------------------

native-install: .install
.install: requirements.txt
pip install -r requirements.txt
touch $@

native-install-test: .install-test
.install-test: requirements_test.txt
pip install -r requirements_test.txt
touch $@

native-install-optional: .install-optional
.install-optional: requirements_optional.txt
pip install -r requirements_optional.txt
touch $@

native-install-all: native-install native-install-test native-install-optional

native-migrate: native-install-all
PYTHONPATH='.' python example_project/manage.py migrate

native-run-example: native-migrate
PYTHONPATH='.' python example_project/manage.py runsslserver 0.0.0.0:$(PORT)

native-clean:
find . -path ./venv -prune | grep -E "(__pycache__|\.pyc|\.pyo$$)" | xargs rm -rf

native-test: native-install-all native-clean
PYTHONPATH='example_project/' python -m pytest -Wignore $(TEST_ARGS)

native-install-python-versions: .install-python-versions
.install-python-versions: tox.ini
curl https://pyenv.run | PYENV_ROOT=$(PROJECT_PATH)/.pyenv bash || echo '-- pyenv already setup, skipping --\n'
PYENV_ROOT=$(PROJECT_PATH)/.pyenv $(PROJECT_PATH)/.pyenv/bin/pyenv install -s 2.7.17
PYENV_ROOT=$(PROJECT_PATH)/.pyenv $(PROJECT_PATH)/.pyenv/bin/pyenv install -s 3.5.9
PYENV_ROOT=$(PROJECT_PATH)/.pyenv $(PROJECT_PATH)/.pyenv/bin/pyenv install -s 3.6.10
PYENV_ROOT=$(PROJECT_PATH)/.pyenv $(PROJECT_PATH)/.pyenv/bin/pyenv install -s 3.8.1
pyenv global system 2.7.17 3.5.9 3.6.10 3.8.1
touch $@

native-test-tox: native-install-python-versions native-clean
pip install tox tox-pyenv
tox
53 changes: 22 additions & 31 deletions README.md
Expand Up @@ -488,39 +488,19 @@ There is an [example project](https://github.com/st4lk/django-rest-social-auth/t
git clone https://github.com/st4lk/django-rest-social-auth.git
```

- make sure all dependencies are installed
- run example project

```bash
pip install -r requirements.txt
pip install -r requirements_test.txt
make
```

- step in example_project/
It is assumed, that you have:
- make
- Docker

```bash
cd django-rest-social-auth/example_project
```

- create database (sqlite3)
- open [https://127.0.0.1:8000/](https://127.0.0.1:8000/) in your browser

```bash
PYTHONPATH='../' python manage.py migrate
```

<sub><sup>Note:</sup></sub>
<sub><sup>You can avoid `PYTHONPATH='../'` if you install the package locally:</sup></sub>
<sub><sup>`pip install rest-social-auth` or `python setup.py install`.</sup></sub>
<sub><sup>
But to my mind the PYTHONPATH prefix is more useful. No need to install anything and code of rest-social-auth will be always up-to-date, even if you change source code.
</sup></sub>

- run development server

```bash
PYTHONPATH='../' python manage.py runsslserver
```

`runsslserver` is used instead of built-in `runserver` to serve the project with TLS (aka SSL) certificate.
Note: `runsslserver` is used instead of built-in `runserver` to serve the project with TLS (aka SSL) certificate.
HTTPS is required by some social providers (facebook), without it they won't work.
The certificate will not be trusted by your system - that is expected.
Just tell your browser to proceed with this untrusted certificate - it is acceptable for development purposes.
Expand All @@ -531,13 +511,24 @@ There is an [example project](https://github.com/st4lk/django-rest-social-auth/t

More details [here](https://github.com/teddziuba/django-sslserver#browser-certificate-errors).

Example project already contains facebook, google and twitter app ids and secrets.
These apps are configured to work only with 127.0.0.1:8000 domain. Google and Facebook providers support localhost:8000 as well. But Twitter only support 127.0.0.1.

So, to play with it, visit https://127.0.0.1:8000/
Facebook, Google and Twitter auth should work, all secret keys are already set.

Example project uses [satellizer](https://github.com/sahat/satellizer) angularjs module.

Development
-----------

Run tests locally

```bash
make test
```

Run tests in all enviroments (can take some time)

```bash
make test-tox
```

Contributors
------------
Expand Down
3 changes: 3 additions & 0 deletions requirements_optional.txt
@@ -0,0 +1,3 @@
django-rest-knox<4.0.0
djangorestframework-simplejwt>=4.0.0
djangorestframework-jwt==1.11.0
13 changes: 13 additions & 0 deletions scripts/entrypoint.sh
@@ -0,0 +1,13 @@
#!/usr/bin/env bash

PROJECT_PATH=/django_rest_social_auth

# create virtualenv if it doesn't exist
python -m venv /${PROJECT_PATH}/venv
. ${PROJECT_PATH}/venv/bin/activate

export PYENV_ROOT="${PROJECT_PATH}/.pyenv"
export PATH="${PROJECT_PATH}/.pyenv/bin:${PATH}"

cd ${PROJECT_PATH}
gosu appuser $1
1 change: 1 addition & 0 deletions setup.py
Expand Up @@ -65,6 +65,7 @@ def __read(fname):
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Topic :: Utilities',
],
)
1 change: 0 additions & 1 deletion test.sh

This file was deleted.

0 comments on commit 5c51454

Please sign in to comment.