ANSIBLE_CMD = ansible-playbook

build-local:
	docker build -t docker.nsnw.ca/ansible:local .

build-latest:
	docker build -t docker.nsnw.ca/ansible:latest .

run-local:
	docker run --rm -it -e ANSIBLE_SETUP_OPTIONS -e SSH_PRIVATE_KEY -e ANSIBLE_OPTIONS -e SKIP_SETUP -e SKIP_PLAYBOOK -v ${PWD}/run/fact_cache:/app/fact_cache docker.nsnw.ca/ansible:local

run-local-shell:
	docker run --rm -it -e ANSIBLE_SETUP_OPTIONS -e SSH_PRIVATE_KEY -e ANSIBLE_OPTIONS -e SKIP_SETUP -e SKIP_PLAYBOOK docker.nsnw.ca/ansible:local /bin/bash

run-latest:
	docker run --rm -it -e ANSIBLE_SETUP_OPTIONS -e SSH_PRIVATE_KEY -e ANSIBLE_OPTIONS -e SKIP_SETUP -e SKIP_PLAYBOOK docker.nsnw.ca/ansible:latest

push-latest:
	docker push docker.nsnw.ca/ansible:latest

clean:
	rm -rfv tmp

dirs:
	mkdir -pv tmp/fact_cache
	chmod a+rxw tmp/fact_cache

base-latest: build-base-latest push-base-latest

base-local: build-base-local

latest: build-latest push-latest

local: dirs build-local run-local

facts:
	ansible -m setup all

bootstrap-network:
	$(ANSIBLE_CMD) -l $(HOSTNAME) -e ansible_host=$(ANSIBLE_HOST) -t network --skip-tags network_restart general.yml

bootstrap-reboot:
	ssh -i ~/.ssh/ansible ansible@$(ANSIBLE_HOST) -t "sudo reboot" || true

bootstrap-init:
	$(ANSIBLE_CMD) -l $(HOSTNAME) -e ansible_user=graham -e ansible_host=$(ANSIBLE_HOST) -k -K bootstrap.yml

bootstrap-init-root:
	ANSIBLE_CONFIG=ansible-bootstrap.cfg $(ANSIBLE_CMD) -l $(HOSTNAME) -e ansible_user=root -e ansible_host=$(ANSIBLE_HOST) -e ansible_become=no bootstrap.yml

bootstrap: bootstrap-init bootstrap-network bootstrap-reboot

bootstrap-root: bootstrap-init-root bootstrap-network bootstrap-reboot

run-all:
	$(ANSIBLE_CMD) -l $(HOSTNAME) -e ansible_host=$(ANSIBLE_HOST) general.yml

install-ssh-key:
	ssh-copy-id $(ANSIBLE_HOST)

setup-user: install-ssh-key
	./setup-user.sh install $(ANSIBLE_HOST)

reboot:
	ssh -i ~/.ssh/ansible ansible@$(ANSIBLE_HOST) -t "sudo reboot"

qc-web:
	$(ANSIBLE_CMD) -l qc_web -t caddy general.yml
