Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bare flask + container skeleton #4

Merged
merged 1 commit into from
Jan 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: run tests
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
python: [3.8, 3.9]
steps:
- uses: actions/checkout@v2
- name: Setup Python
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python }}
- name: Install pipenv and any other packages
run: |
python -m pip install pipenv
pipenv install --dev
- name: Run pytest
run: pipenv run pytest service
- name: Lint with flake8
run: pipenv run flake8 service
53 changes: 46 additions & 7 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
# Byte-compiled / optimized / DLL files
*.pyc
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
env/
Expand All @@ -15,15 +13,16 @@ dist/
downloads/
eggs/
.eggs/
./lib/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg
.venv
Pipfile.lock
*~

# PyInstaller
Expand All @@ -44,21 +43,61 @@ htmlcov/
.cache
nosetests.xml
coverage.xml
*,cover
*.cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# pyenv python configuration file
# Jupyter Notebook
.ipynb_checkpoints

# pyenv
.python-version

# celery beat schedule file
celerybeat-schedule

# SageMath parsed files
*.sage.py

# dotenv
.env

# virtualenv
.venv
venv/
ENV/
#Pipfile.lock

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
15 changes: 10 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@
# This file will be regenerated if you run travis_pypi_setup.py
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should remove this and instead use github actions similar to https://github.com/release-depot/flask-container-scaffold/blob/main/.github/workflows/test.yml

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree, travis has not been great recently, and I think actions are now the preferred method

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with the idea as well. I tried to update this PR with a first attempt at a github action, though I wouldn't be surprised if it takes a couple iterations to get it working!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think what you have looks fine, we can always do a rev to fix CI if needed. Are you wanting to leave travis file in until we see if the new actions work correctly? If so, we could easily test the action with the next patch removing travis (the other alternative would be to temporarily not limit push action tests to be on main (well, I guess this is still using 'master'), and once you see the action work on your own branch, you will know you can limit it to just main for the patch).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds reasonable to me! And yes, the repo was created a couple of years ago so it still uses master.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok - maybe as a next step before adding more working code, we should move this to the newer 'main' standard? We need to start shifting everything anyway. I am approving this, looks good!


language: python
python:
- 3.6
- 3.8
matrix:
include:
- python: 3.8
- python: 3.9

# command to install dependencies, e.g. pip install -r requirements.txt --use-mirrors
install: pip install -U tox-travis
install:
- pip install pipenv
- pipenv install --dev

# command to run tests, e.g. python setup.py test
script: tox
script:
- pipenv run flake8 service
- pipenv run pytest service

# After you create the Github repo and add it to Travis, run the
# travis_pypi_setup.py script to finish PyPI deployment setup
Expand Down
28 changes: 15 additions & 13 deletions CONTRIBUTING.rst
Original file line number Diff line number Diff line change
Expand Up @@ -66,23 +66,26 @@ Ready to contribute? Here's how to set up `rtls` for local development.

3. Install your local copy into a virtualenv. Assuming you have virtualenvwrapper installed, this is how you set up your fork for local development::

$ mkvirtualenv rtls
$ cd rtls/
$ python setup.py develop
First, go into the pipenv virtual environment::

$ pipenv update --dev
$ pipenv shell

Running the local development server::

(pkg_syncer)[]$ cd ./service
(pkg_syncer)[]$ python main.py

4. Create a branch for local development::

$ git checkout -b name-of-your-bugfix-or-feature

Now you can make your changes locally.

5. When you're done making changes, check that your changes pass flake8 and the tests, including testing other Python versions with tox::
5. When you're done making changes, check that your changes pass flake8 and the tests::

$ flake8 rtls tests
$ python setup.py test or py.test
$ tox

To get flake8 and tox, just pip install them into your virtualenv.
(rtls)[]$ flake8 service
(rtls)[]$ pytest service

6. Commit your changes and push your branch to GitHub::

Expand All @@ -101,14 +104,13 @@ Before you submit a pull request, check that it meets these guidelines:
2. If the pull request adds functionality, the docs should be updated. Put
your new functionality into a function with a docstring, and add the
feature to the list in README.rst.
3. The pull request should work for Python 3.6 and 3.7. Check
3. The pull request should work for Python 3.8. Check
https://travis-ci.org/release-depot/rtls/pull_requests
and make sure that the tests pass for all supported Python versions.

Tips
----

To run a subset of tests::

To run a single test::

$ python -m unittest tests.test_rtls
(rtls)[]$ service/tests/test_rtls.py::test_rtls
15 changes: 15 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true

[dev-packages]
ansible-bender = "*"
autopep8 = "*"
flake8 = "*"
pylint = "*"
pytest = "*"
selinux = "*"

[packages]
rtls = {editable = true, path = "./service"}
34 changes: 31 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,39 @@ RTLS, or Real Time Locator System, is an abstraction layer for tracking changes
* Free software: MIT license
* Documentation: https://rtls.readthedocs.io.

API
---

Features
--------
Test call - /rtls/[package]/[changeId] (GET)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Test call::

$ curl localhost:8080/rtls/mypackage/I123456
{"msg": "Test with mypackage"}

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

See **CONTRIBUTING.rst**.

Container image creation
~~~~~~~~~~~~~~~~~~~~~~~~

Building the container image::

$ pipenv shell
$ ansible-bender build ./build.yml

Running the container image::

$ podman run -d --name rtls -p 8080:80 localhost/rtls:latest

Reading the container logs::

$ podman logs rtls
$ podman exec -it rtls cat /var/log/nginx/error.log | less

* TODO

Credits
-------
Expand Down
23 changes: 23 additions & 0 deletions build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
- name: rtls build
hosts: all
vars:
www_root: '/var/www/rtls/service'
ansible_bender:
base_image: 'registry.fedoraproject.org/fedora:latest'
cache_tasks: False
layering: False
target_image:
name: rtls
environment:
PIPENV_VENV_IN_PROJECT: 'True'
entrypoint: "{{ www_root }}/service_run.sh"
working_dir: "{{ www_root }}"
tasks:
- name: Setup the base
include_role:
name: base

- name: Setup the app
include_role:
name: app
12 changes: 0 additions & 12 deletions requirements_dev.txt

This file was deleted.

38 changes: 38 additions & 0 deletions roles/app/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---

- name: Setting up the app directory
file:
path: "{{ www_root }}"
state: directory

- name: Copy app code
copy:
src: "{{ playbook_dir }}/service"
dest: "{{ app_root }}"

- name: Copy python packaging files
copy:
src: "{{ item }}"
dest: "{{ app_root }}"
loop:
- "{{ playbook_dir }}/service/setup.py"
- "{{ playbook_dir }}/service/requirements.txt"

- name: Copy Pipenv files
copy:
src: "{{ item }}"
dest: "{{ www_root }}"
mode: preserve
loop:
- "{{ playbook_dir }}/Pipfile"

- name: Setup venv
shell:
cmd: "pipenv update"
chdir: "{{ app_root }}"

- name: Copy files
template:
src: templates/service_run.j2
dest: "{{ app_root }}/service_run.sh"
mode: 'o+x'
9 changes: 9 additions & 0 deletions roles/app/templates/service_run.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#! /bin/bash

source {{ www_root }}/.venv/bin/activate
cd {{ www_root }}
nginx
# Timeout value is in seconds
gunicorn wsgi:app \
--bind 127.0.0.1:8000 \
--timeout 300
3 changes: 3 additions & 0 deletions roles/app/vars/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---

app_root: '{{ www_root }}'
4 changes: 4 additions & 0 deletions roles/base/files/proxy_params
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
9 changes: 9 additions & 0 deletions roles/base/files/rtls.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
server {
listen 80;
server_name server_domain_or_IP;

location / {
include proxy_params;
proxy_pass http://127.0.0.1:8000;
}
}
Loading