Skip to content

Commit

Permalink
Djangogoboot template v1
Browse files Browse the repository at this point in the history
  • Loading branch information
Apreche committed Jul 18, 2021
0 parents commit bbe7f3b
Show file tree
Hide file tree
Showing 89 changed files with 2,573 additions and 0 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
name: Lint Code Base

on:
push:
branches: [main]
pull_request:
branches: [main]

jobs:
build:
name: Lint Code Base
runs-on: ubuntu-20.04

steps:
- name: Checkout Code
uses: actions/checkout@v2
with:
fetch-depth: 0

- name: Lint Code Base
uses: github/super-linter@v3
env:
VALIDATE_ALL_CODEBASE: false
IGNORE_GITIGNORED_FILES: true
DEFAULT_BRANCH: main
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
...
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Scott Rubin

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Djangogoboot-template

This is a special Django project template that is used by Djangogoboot. Djangogoboot a small Python CLI program that will use this template to start a new Django project from scratch with a complete single-instance CI/CD production stack powered by GitHub Actions.

See the Djangogoboot project at [https://github.com/Apreche/djangogoboot/](https://github.com/Apreche/djangogoboot/) for more information.

## Using without Djangogoboot

It is possible to use this template on its own without Djangogoboot, but it will be quiet tedious. If that's really a road you would liek to go down, the source code of Djangogoboot is effectively the documentation for this process.
3 changes: 3 additions & 0 deletions README.md-tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# {{ project_name }}

[![GitHub Super-Linter](https://github.com/{% templatetag openvariable %} full_repo_name {% templatetag closevariable %}/workflows/Lint%20Code%20Base/badge.svg)](https://github.com/marketplace/actions/super-linter)
7 changes: 7 additions & 0 deletions ansible/ansible.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[defaults]
remote_tmp = /tmp/ansible
deprecation_warnings = False
interpreter_python = /usr/bin/python3

[ssh_connection]
pipelining = True
5 changes: 5 additions & 0 deletions ansible/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
- hosts: all
roles:
- role: full_stack
...
1 change: 1 addition & 0 deletions ansible/gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.retry
Empty file.
5 changes: 5 additions & 0 deletions ansible/requirements.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
collections:
- name: community.general
version: 3.0.0
...
8 changes: 8 additions & 0 deletions ansible/roles/cache/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
memcached_runtime_dir: "{{ runtime_dir }}/memcached"
memcached_socket: "{{ memcached_runtime_dir }}/memcached.sock"
memcached_pid_file: "{{ memcached_runtime_dir }}/memcached.pid"
memcached_config_file: "{{ config_dir }}/memcached.conf"
memcached_log_file: "{{ log_dir }}/memcached.log"
memcached_memory: 64
...
4 changes: 4 additions & 0 deletions ansible/roles/cache/meta/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
dependencies:
- role: common
...
23 changes: 23 additions & 0 deletions ansible/roles/cache/tasks/install_memcached.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
- name: Install memcached
ansible.builtin.apt:
name: memcached
state: present
update_cache: yes
become: yes

- name: Install memcached configuration file
ansible.builtin.template:
src: templates/memcached.conf.j2
dest: "{{ memcached_config_file }}"
owner: root
group: root
mode: '0644'
become: yes

- name: Ensure memcached is started
ansible.builtin.service:
name: memcached
state: started
become: yes
...
3 changes: 3 additions & 0 deletions ansible/roles/cache/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
- include_tasks: install_memcached.yml
...
13 changes: 13 additions & 0 deletions ansible/roles/cache/templates/memcached.conf.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
-d

-m {{ memcached_memory }}

-u memcache

-a 777

-s {{ memcached_socket | default('/run/memcached/memcached.sock') }}

-P {{ memcached_pid_file | default('/run/memcached/memcached.pid') }}

logfile {{ memcached_log_file | default('/var/log/memcached.log') }}
34 changes: 34 additions & 0 deletions ansible/roles/common/defaults/main/project.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
project_name: "{{ lookup('env', 'GITHUB_REPOSITORY').split('/')[1] | lower }}"
owner: "{{ lookup('env', 'GITHUB_REPOSITORY').split('/')[0] | lower }}"
email_address: "{{ lookup('env', 'EMAIL_ADDRESS') }}"
web_domain: "{{ lookup('env', 'WEB_DOMAIN') | default(inventory_hostname, true) }}"

git_repo: "[email protected]:{{ lookup('env', 'GITHUB_REPOSITORY') }}.git"
git_version: "{{ lookup('env', 'GITHUB_REF') | default(lookup('env', 'GITHUB_SHA'), True) }}"


base_dir: "{{ ansible_env.HOME }}/projects"
config_dir: "/etc"
runtime_dir: "/run"
temp_dir: "/tmp"
log_dir: "/var/log"
state_dir: "/var/lib"
www_dir: "/var/www"

ansible_temp_dir: "{{ temp_dir }}/ansible"

project_source_dir: "{{ base_dir }}/{{ project_name }}"
project_config_dir: "{{ config_dir }}/{{ project_name }}"
project_runtime_dir: "{{ runtime_dir }}/{{ project_name }}"
project_temp_dir: "{{ temp_dir }}/{{ project_name }}"
project_log_dir: "{{ log_dir }}/{{ project_name }}"
project_state_dir: "{{ state_dir }}/{{ project_name }}"
project_www_dir: "{{ www_dir }}/{{ project_name }}"

ssh_dir: "{{ ansible_env.HOME }}/.ssh"
deploy_key: "{{ lookup('env', 'DEPLOY_SSH_PRIVATE_KEY') }}"
deploy_key_path: "{{ ssh_dir }}/{{ project_name }}_deploykey"

systemd_config_dir: "{{ config_dir }}/systemd/system"
...
2 changes: 2 additions & 0 deletions ansible/roles/common/meta/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
...
8 changes: 8 additions & 0 deletions ansible/roles/common/tasks/deploy_key.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
- name: Install Deploy Key
ansible.builtin.template:
src: deploy_key.j2
force: yes
dest: "{{ deploy_key_path }}"
mode: '0600'
...
5 changes: 5 additions & 0 deletions ansible/roles/common/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
- include_tasks: register_vars.yml
- include_tasks: make_dirs.yml
- include_tasks: deploy_key.yml
...
63 changes: 63 additions & 0 deletions ansible/roles/common/tasks/make_dirs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
---
- name: Create ansible remote temp dir
ansible.builtin.file:
path: "{{ ansible_temp_dir }}"
state: directory
mode: '0777'

- name: Ensure project source directory exists
ansible.builtin.file:
path: "{{ project_source_dir }}"
state: directory
mode: '0755'

- name: Ensure project config directory exists
ansible.builtin.file:
path: "{{ project_config_dir }}"
state: directory
mode: '0755'
become: yes

- name: Ensure project runtime directory exists
ansible.builtin.file:
path: "{{ project_runtime_dir }}"
state: directory
mode: '0755'
become: yes

- name: Ensure project temp directory exists
ansible.builtin.file:
path: "{{ project_temp_dir }}"
state: directory
mode: '0755'
become: yes

- name: Ensure project log directory exists
ansible.builtin.file:
path: "{{ project_log_dir }}"
state: directory
mode: '0775'
become: yes

- name: Ensure project state directory exists
ansible.builtin.file:
path: "{{ project_state_dir }}"
state: directory
mode: '0775'
become: yes

- name: Ensure project www directory exists
ansible.builtin.file:
path: "{{ project_www_dir }}"
state: directory
owner: root
group: www-data
mode: '0755'
become: yes

- name: Ensure SSH directory exists
ansible.builtin.file:
path: "{{ ssh_dir }}"
state: directory
mode: '0755'
...
11 changes: 11 additions & 0 deletions ansible/roles/common/tasks/register_vars.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
- name: Get Ubuntu release name
ansible.builtin.command: lsb_release -cs
register: ubuntu_release_string
changed_when: false
check_mode: false

- name: Register common vars
ansible.builtin.set_fact:
ubuntu_release: "{{ ubuntu_release_string.stdout }}"
...
1 change: 1 addition & 0 deletions ansible/roles/common/templates/deploy_key.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ deploy_key }}
7 changes: 7 additions & 0 deletions ansible/roles/db/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
postgresql_db_name: "{{ project_name }}"
postgresql_user: "{{ project_name }}"
postgresql_password: "{{ project_name }}"
postgresql_host: "localhost"
postgresql_port: "5432"
...
4 changes: 4 additions & 0 deletions ansible/roles/db/meta/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
dependencies:
- role: common
...
32 changes: 32 additions & 0 deletions ansible/roles/db/tasks/configure_postgres.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
- name: Install Ansible PostgreSQL dependencies
ansible.builtin.apt:
pkg:
- libpq-dev
- python3-psycopg2
become: yes

- name: Create PostgreSQL database
community.general.postgresql_db:
name: "{{ postgresql_db_name }}"
become: yes
become_user: postgres

- name: Create PostgreSQL database user
community.general.postgresql_user:
db: "{{ postgresql_db_name }}"
name: "{{ postgresql_user }}"
password: "{{ postgresql_password }}"
priv: ALL
role_attr_flags: SUPERUSER,CREATEDB
expires: infinity
become: yes
become_user: postgres

- name: Set PostgreSQL database owner
community.general.postgresql_db:
name: "{{ postgresql_db_name }}"
owner: "{{ postgresql_password }}"
become: yes
become_user: postgres
...
27 changes: 27 additions & 0 deletions ansible/roles/db/tasks/install_postgres.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
# Thank you to this URL
# http://postgresql.freeideas.cz/ansible-simple-playbook-installing-postgresql-ubuntu-debian/
- name: Add PostgreSQL repository key
ansible.builtin.apt_key:
url: https://www.postgresql.org/media/keys/ACCC4CF8.asc
state: present
become: yes

- name: Add PostgreSQL repository
ansible.builtin.apt_repository:
repo: "deb [arch=amd64] http://apt.postgresql.org/pub/repos/apt {{ ubuntu_release }}-pgdg main"
state: present
become: yes

- name: Install PostgreSQL
ansible.builtin.apt:
name: postgresql
update_cache: yes
become: yes

- name: Ensure PostgreSQL is started
ansible.builtin.service:
name: postgresql
state: started
become: yes
...
4 changes: 4 additions & 0 deletions ansible/roles/db/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
- include_tasks: install_postgres.yml
- include_tasks: configure_postgres.yml
...
12 changes: 12 additions & 0 deletions ansible/roles/full_stack/meta/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
dependencies:
- role: common
- role: postfix
- role: cache
- role: queue
- role: db
- role: python
- role: migrator
- role: worker
- role: web
...
6 changes: 6 additions & 0 deletions ansible/roles/migrator/meta/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
dependencies:
- role: common
- role: db
- role: python
...
3 changes: 3 additions & 0 deletions ansible/roles/migrator/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
- include_tasks: migrate_database.yml
...
6 changes: 6 additions & 0 deletions ansible/roles/migrator/tasks/migrate_database.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
- name: Run database migrations
ansible.builtin.command:
cmd: "{{ virtualenv_python_bin }} {{ project_source_dir }}/manage.py migrate --noinput"
environment: "{{ env_vars }}"
...
Loading

0 comments on commit bbe7f3b

Please sign in to comment.