Skip to content

Commit

Permalink
feat: Use poetry everywhere, remove vagrant support
Browse files Browse the repository at this point in the history
- Update ansible to use poetry
- Do not generate requirements.txt files from poetry
- Upgrade poetry version to 1.3.1
  • Loading branch information
theskumar committed Jan 3, 2023
1 parent e58fae9 commit 4432046
Show file tree
Hide file tree
Showing 19 changed files with 54 additions and 213 deletions.
10 changes: 3 additions & 7 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,9 @@ jobs:
with:
python-version: "3.9"
cache: "pip"
- name: Install cookiecutter
- name: Install cookiecutter and poetry
run: |
python -m pip install --upgrade pip
pip3 install cookiecutter==1.7.3
- name: Install poetry
run: |
pip3 install poetry==1.2.0
pip3 install cookiecutter==2.1.1 poetry==1.3.1
- name: Run tests
run: |
./run_test.sh
run: bash run_test.sh
28 changes: 1 addition & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ If you opt to setup the project automatically, it will also:
- initialize a git repo and bump initial tag and version.
- create a virtualenv in the folder `venv` inside the project.
- install all the python dependencies inside it.
- create `poetry.lock` file after resolving dependencies and then generate `requirements.txt` and `requirements_dev.txt` for production and dev use respectively, for backward-compatibility.
- create `poetry.lock` file after resolving dependencies.
- create a postgres database and run the initial migration against it.

then only thing you'll need to do is:
Expand All @@ -65,32 +65,6 @@ Don't forget to carefully look at the generated README. Awesome, right?

You can also explore the [wiki] section for details on advance setup and usages.

## Managing dependencies

### Poetry

To guarantee repeatable installations, all project dependencies are managed using [Poetry](https://python-poetry.org/). The project’s direct dependencies are listed in `pyproject.toml`.
Running `poetry lock` generates `poetry.lock` which has all versions pinned.

You can install Poetry by using `pip install --pre poetry` or by following the official installation guide [here](https://github.com/python-poetry/poetry#installation).

*Tip:* We recommend that you use this workflow and keep `pyproject.toml` as well as `poetry.lock` under version control to make sure all computers and environments run exactly the same code.

### Other tools

For compatibility, `requirements.txt` and `requirements_dev.txt` can be updated by running

```bash
poetry export -f requirements.txt -o requirements.txt
poetry export -f requirements.txt -o requirements_dev.txt --dev
```

or

```bash
make generate_requirements
```

## Articles

- [Setting up Django projects in a breeze](https://medium.com/fueled-engineering/setting-up-django-projects-in-a-breeze-36c715cc9a6f)
Expand Down
6 changes: 3 additions & 3 deletions hooks/post_gen_project.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ echo "${green}[Finished]${reset}"

echo "==> Setup project dependencies? It will:"
echo " - Create virtualenv at './{{ cookiecutter.github_repository }}/venv/'."
echo " - Install development requirements inside virtualenv."
echo " - Install development requirements using poetry"
echo " - Create a postgres database named '{{ cookiecutter.main_module }}'."
echo " - Run './manage.py migrate'."
echo " - Initialize git."
Expand All @@ -26,11 +26,11 @@ else
fi

if echo "{{ cookiecutter.add_heroku }}" | grep -iq "^n"; then
rm -rf uwsgi.ini Procfile runtime.txt bin/post_compile
rm -rf uwsgi.ini Procfile bin/post_compile
fi

if echo "{{ cookiecutter.add_ansible }}" | grep -iq "^n"; then
rm -rf provisioner Vagrantfile ansible.cfg
rm -rf provisioner ansible.cfg
fi

if echo "{{ cookiecutter.add_celery }}" | grep -iq "^n"; then
Expand Down
23 changes: 12 additions & 11 deletions {{cookiecutter.github_repository}}/.github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,22 @@ jobs:
sudo apt-get update
sudo apt-get install postgresql-13-postgis-3-scripts
{%- endif %}

- name: Set up Python 3.9
uses: actions/setup-python@v2
with:
python-version: '3.9'
cache: 'pip'
- name: Install poetry
run: |
pip3 install poetry==1.2.0
- name: Install requirements

- name: Install python dependencies
run: |
python -m pip install --upgrade pip
python -m pip install --upgrade pip poetry wheel
poetry install --with dev
- name: Run tests
run: |
poetry run pytest --cov -v --tb=native
- name: Linting
run: |
make lint
- name: Run Linting
run: make lint

- name: Run Python tests
run: poetry run pytest --cov -v --tb=native


28 changes: 7 additions & 21 deletions {{cookiecutter.github_repository}}/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ SHELL := bash

PROJECT_NAME={{cookiecutter.main_module}}
DB_NAME=$(PROJECT_NAME)
{% if cookiecutter.add_ansible.lower() == 'y' -%}
INVENTORY=provisioner/hosts
PLAYBOOK=provisioner/site.yml
{%- endif %}
ENV_PREFIX=$(shell echo 'poetry run ')


Expand Down Expand Up @@ -36,32 +38,14 @@ run_all: ## Run all the servers in parallel, requires GNU Make
make -j django docs{% if cookiecutter.add_celery == 'y' %} celery{% endif %} redis
.PHONY: run_all

virtualenv: ## Create a virtual environment.
@echo "creating virtualenv using poetry..."
@pip install -U pip poetry
@poetry env use python3
@echo
@echo "!!! Please run 'poetry shell' to enable the environment !!!"


regenerate: ## Delete and create new database.
-dropdb $(DB_NAME)
createdb $(DB_NAME)
${ENV_PREFIX}python manage.py migrate
.PHONY: regenerate

generate_requirements:
poetry export -f requirements.txt -o requirements_dev.txt --dev
poetry export -f requirements.txt -o requirements.txt

update_libs: ## update libs + generate new lockfile & requirements
poetry update
make generate_requirements
.PHONY: update-libs

install: virtualenv ## Install and setup project dependencies
python3 -m pip install --upgrade pip wheel
make generate_requirements
install: ## Install dependencies, create db and migrate
python3 -m pip install --upgrade pip wheel poetry
poetry install
${ENV_PREFIX}pre-commit install
ifneq ($(CI),True)
Expand Down Expand Up @@ -99,7 +83,7 @@ djurls: ## Displays all the django urls
shell: ## Enter the django shell
${ENV_PREFIX}python manage.py shell_plus

docs: virtualenv ## Start documentation server locally
docs: ## Start documentation server locally
${ENV_PREFIX}mkdocs serve

{%- if cookiecutter.add_celery == 'y' %}
Expand All @@ -110,6 +94,7 @@ celery: install ## Start celery worker
redis: ## Start redis server
redis-server

{% if cookiecutter.add_ansible.lower() == 'y' %}
# Ansible related things
# ------------------------------------------------------
# Usages:
Expand Down Expand Up @@ -138,3 +123,4 @@ deploy_qa: deploy

deploy_prod: ENV=prod ## Deploy to production server
deploy_prod: deploy
{%- endif %}
16 changes: 0 additions & 16 deletions {{cookiecutter.github_repository}}/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,6 @@ You can install Poetry by using `pip install --pre poetry` or by following the o

*Tip:* We recommend that you use this workflow and keep `pyproject.toml` as well as `poetry.lock` under version control to make sure all computers and environments run exactly the same code.

### Other tools

For compatibility, `requirements.txt` and `requirements_dev.txt` can be updated by running

```bash
poetry export --without-hashes -f requirements.txt -o requirements.txt
```

and

```bash
poetry export --without-hashes -f requirements.txt -o requirements_dev.txt --with dev
```

, respectively.


## Deploying Project

Expand Down
48 changes: 0 additions & 48 deletions {{cookiecutter.github_repository}}/Vagrantfile

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ ARG PYTHON_VERSION=3.9-slim-buster
# define an alias for the specfic python version used in this file.
FROM python:${PYTHON_VERSION} as python

ENV POETRY_VERSION=1.2.0
ENV POETRY_VERSION=1.3.1

ARG BUILD_ENVIRONMENT=dev
ARG APP_HOME=/app
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ FROM python:${PYTHON_VERSION} as python
ARG BUILD_ENVIRONMENT=local
ARG APP_HOME=/app

ENV POETRY_VERSION=1.2.0
ENV POETRY_VERSION=1.3.1
ENV PYTHONUNBUFFERED 1
ENV PYTHONDONTWRITEBYTECODE 1
ENV BUILD_ENV ${BUILD_ENVIRONMENT}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ Run these commands to deploy this project on Heroku (substitue all references of
```
heroku create --ssh-git <heroku-app-name>
heroku buildpacks:add https://github.com/moneymeets/python-poetry-buildpack.git --app=<heroku-app-name>
heroku buildpacks:set heroku/python --app=<heroku-app-name>
heroku addons:create heroku-postgresql{% if cookiecutter.add_postgis == 'y' %}:standard-0{% endif %} --app=<heroku-app-name>
Expand Down
19 changes: 0 additions & 19 deletions {{cookiecutter.github_repository}}/provisioner/hosts
Original file line number Diff line number Diff line change
@@ -1,31 +1,12 @@
[all:vars]
vm=0
user=ubuntu
project_namespace={% raw %}{{ project_name }}-{{ deploy_env }}{% endraw %}
project_path=/home/ubuntu/{% raw %}{{ deploy_env }}{% endraw %}/{{ cookiecutter.github_repository }}
venv_path={% raw %}{{ project_path }}/venv{% endraw %}
use_letsencrypt={{ 'True' if cookiecutter.letsencrypt.lower() == 'y' else 'False' }}
letsencrypt_email={{ cookiecutter.letsencrypt_email }}
django_requirements_file=requirements.txt
django_settings="settings.production"

[vagrant]
192.168.33.12

[vagrant:vars]
vm=1
deploy_env=vagrant
user=vagrant
project_path=/home/vagrant/{{ cookiecutter.github_repository }}
venv_path=/home/vagrant/venv
django_requirements_file=requirements_dev.txt
django_settings="settings.development"
use_letsencrypt=False
pg_db={{ cookiecutter.main_module }}
pg_user=vagrant
pg_password=vagrant
domain_name=vagrant.{{ cookiecutter.main_module }}.com

[dev]
dev.{{ cookiecutter.main_module }}.com

Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,6 @@
{% raw %}---

# Uncomment following if ipv6 is not available or not tunelled through ipv4 properly
# Disable ipv6 when running in VM(vagrant)
#- name: Disable ipv6 for all interfaces
# sysctl: name="net.ipv6.conf.all.disable_ipv6" value=1 state=present
# when: vm == 1
#
#- name: Disable ipv6 for default interface
# sysctl: name="net.ipv6.conf.default.disable_ipv6" value=1 state=present
# when: vm == 1
#
#- name: Disable ipv6 for local interface
# sysctl: name="net.ipv6.conf.lo.disable_ipv6" value=1 state=present
# when: vm == 1
- name: Set hostname
action: shell hostnamectl set-hostname {{ domain_name }}
when: vm == 0

- name: set system locale
command: update-locale LC_ALL={{ lc_all }} LANG={{ lc_lang }} LC_CTYPE={{ lc_ctype }} LC_COLLATE={{ lc_collate }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

- name: make sure ssl directory exists
file: path={{ ssl_cert_dir }} state=directory
when: vm == 0 and use_letsencrypt
when: use_letsencrypt

- name: check {{ letsencrypt_ssl_cert_dir }} exists
stat: path={{ letsencrypt_ssl_cert_dir }}
Expand All @@ -18,7 +18,7 @@
- import_tasks: htpasswd.yml

- import_tasks: letsencrypt.yml
when: vm == 0 and use_letsencrypt and letsencrypt_dir.stat.exists == false
when: use_letsencrypt and letsencrypt_dir.stat.exists == false

- name: check ssl/nginx.crt exists
stat: path={{ ssl_certificate }}
Expand All @@ -29,11 +29,10 @@
register: nginx_key

- fail: msg="Whoops! ssl certificate doesn't exist"
when: (vm == 0 and use_letsencrypt) == true and (nginx_cert.stat.exists == false or nginx_key.stat.exists == false)
when: use_letsencrypt and (nginx_cert.stat.exists == false or nginx_key.stat.exists == false)

- name: generate ssl forward secrecy key
command: openssl dhparam -out {{ ssl_forward_secrecy_key_path }} {{ ssl_forward_secrecy_key_length }} creates={{ ssl_forward_secrecy_key_path }}
when: vm == 0

- name: copy base nginx configuration.
template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
server {
listen 80;
listen [::]:80;
server_name {% if vm %}_{% else %}{{ domain_name }}{% endif %};
server_name {{ domain_name }};

{% if use_letsencrypt %}
location /.well-known/acme-challenge/ {
Expand All @@ -11,7 +11,7 @@ server {
}
{% endif %}

{% if vm and (nginx_cert.stat.exists == false or nginx_key.stat.exists == false) %}
{% if nginx_cert.stat.exists == false or nginx_key.stat.exists == false %}
location / {
uwsgi_pass unix:///tmp/uwsgi-{{ project_namespace }}.sock;
include /etc/nginx/uwsgi_params;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ pg_hstore: False
pg_db: "{{ project_namespace }}"
pg_user: dev
pg_password: password
django_requirements_file: requirements.txt

# uwsgi related variables
uwsgi_user: www-data
Expand Down
Loading

0 comments on commit 4432046

Please sign in to comment.