# Makefile

# Python

init:
	chmod -R +x .githooks
	git config core.hooksPath .githooks

install:
	pip install --upgrade \
        pip \
        setuptools \
        wheel
	pip install -r requirements.txt

requirements:
	test ! -d venv || rm -rf venv
	python3 -m venv venv
	venv/bin/python -m pip install --upgrade \
		pip \
		setuptools \
		wheel
	venv/bin/python -m pip install --upgrade \
		requests
	venv/bin/python -m pip freeze | grep -v "pkg-resources" > requirements.txt

lint:
	command -v flake8 || pip install --upgrade \
        flake8
	flake8

test:
	command -v pytest || pip install --upgrade \
        pytest \
        pytest-cov \
        pytest-subtests \
	pytest

clean:
	find . -type f -name '*.pyc' -exec rm -rf {} \;
	find . -type f -name '*.pyo' -exec rm -rf {} \;
	find . -type f -name '*.coverage' -exec rm -rf {} \;
	find . -depth -type d -name '__pycache__' -exec rm -rf {} \;
	find . -depth -type d -name '.pytest_cache' -exec rm -rf {} \;

# Django

init:
	chmod -R +x .githooks
	git config core.hooksPath .githooks

install:
	pip install --upgrade \
        pip \
        setuptools \
        wheel
	pip install -r requirements.txt

requirements:
	test ! -d venv || rm -rf venv
	python3 -m venv venv
	venv/bin/python -m pip install --upgrade \
		pip \
		setuptools \
		wheel
	venv/bin/python -m pip install --upgrade \
		boto3 \
		django \
		django-storages \
		gunicorn \
		mistune \
		psycopg2-binary \
		requests \
		sentry-sdk
	venv/bin/python -m pip freeze | grep -v "pkg-resources" > requirements.txt

lint:
	command -v flake8 || pip install --upgrade \
        flake8
	flake8

test:
	command -v pytest || pip install --upgrade \
        pytest \
        pytest-cov \
        pytest-subtests \
        pytest-django
	pytest

check:
	python -Wa manage.py check
	python -Wa manage.py check --deploy
	python -Wa manage.py makemigrations --check

collectstatic:
	python -Wa manage.py collectstatic --no-input

makemigrations:
	python -Wa manage.py makemigrations --no-input

migrate:
	python -Wa manage.py migrate --no-input

dumpdata:
	python -Wa manage.py dumpdata \
		--natural-foreign \
		--natural-primary \
		--exclude redirects.redirect \
		--exclude sessions.session \
		--indent 4 \
		> data.json

loaddata:
	python -Wa manage.py loaddata data.json

dumpsqlite:
	command -v sqlite3 || sudo apt install sqlite3
	sqlite3 db.sqlite3 .dump > data.sql
	# sqlite3 db.sqlite3 ".backup data.bak"

loadsqlite:
	command -v sqlite3 || sudo apt install sqlite3
	sqlite3 db.sqlite3 < data.sql
	# sqlite3 db.sqlite3 ".restore data.bak"

dumppsql:
	pg_dump \
		--create \
		--format custom \
		--username postgres \
		--dbname postgres \
		data.sql

loadpsql:
	pg_restore \
		--create \
		--format custom \
		--username postgres \
		--dbname postgres \
		data.sql

clean:
	find . -type f -name '*.pyc' -exec rm -rf {} \;
	find . -type f -name '*.pyo' -exec rm -rf {} \;
	find . -type f -name '*.coverage' -exec rm -rf {} \;
	find . -depth -type d -name '__pycache__' -exec rm -rf {} \;
	find . -depth -type d -name '.pytest_cache' -exec rm -rf {} \;

# Terraform

init:
	chmod -R +x .githooks
	git config core.hooksPath .githooks

TERRAFORM_VERSION = 0.12.18
install:
	wget https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/terraform_${TERRAFORM_VERSION}_linux_amd64.zip -O terraform_${TERRAFORM_VERSION}_linux_amd64.zip
	unzip terraform_${TERRAFORM_VERSION}_linux_amd64.zip
	sudo mv terraform /usr/local/bin
	rm terraform_${TERRAFORM_VERSION}_linux_amd64.zip
	terraform version

fmt:
	terraform fmt -recursive

lint:
	terraform fmt -check -recursive -diff

validate:
	terraform validate

# Ansible Role

ROLE_NAME = role

init:
	chmod -R +x .githooks
	git config core.hooksPath .githooks

install:
	pip install --upgrade \
        pip \
        setuptools \
        wheel
	pip install -r requirements.txt

requirements:
	test ! -d venv || rm -rf venv
	python3 -m venv venv
	venv/bin/python -m pip install --upgrade \
		pip \
		setuptools \
		wheel
	venv/bin/python -m pip install --upgrade \
		ansible
	venv/bin/python -m pip freeze | grep -v "pkg-resources" > requirements.txt

create:
	command -v ansible-galaxy || pip install --upgrade \
        ansible
	ansible-galaxy init ${ROLE_NAME}
	command -v molecule || pip install --upgrade \
        molecule \
        docker
	molecule init scenario
	mv ${ROLE_NAME}/* .
	mv ${ROLE_NAME}/.travis.yml .
	rm -d ${ROLE_NAME}

lint:
	command -v molecule || pip install --upgrade \
        molecule \
        docker
	molecule lint

test:
	command -v molecule || pip install --upgrade \
        molecule \
        docker
	molecule test

# Ansible Playbook

init:
	chmod -R +x .githooks
	git config core.hooksPath .githooks

install:
	pip install --upgrade \
        pip \
        setuptools \
        wheel
	pip install -r requirements.txt

requirements:
	test ! -d venv || rm -rf venv
	python3 -m venv venv
	venv/bin/python -m pip install --upgrade \
		pip \
		setuptools \
		wheel
	venv/bin/python -m pip install --upgrade \
		ansible
	venv/bin/python -m pip freeze | grep -v "pkg-resources" > requirements.txt

lint:
	command -v ansible-lint || pip install --upgrade \
        ansible-lint
	ansible-lint -v

plan:
	ansible-galaxy install -f -r requirements.yml
	ansible-playbook -i inventory.yml playbook.yml --ask-become-pass --check

apply:
	ansible-galaxy install -f -r requirements.yml
	ansible-playbook -i inventory.yml playbook.yml --ask-become-pass