<commit_before>SHELL := /bin/bash
.PHONY: build ship test

## Display this help message
help:
	@awk '/^##.*$$/,/[a-zA-Z_-]+:/' $(MAKEFILE_LIST) | awk '!(NR%2){print $$0p}{p=$$0}' | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}' | sort

# ------------------------------------------------
# Container builds

# we get these from shippable if available, otherwise from git
COMMIT ?= $(shell git rev-parse --short HEAD)
BRANCH ?= $(shell git rev-parse --abbrev-ref HEAD)
TAG := $(BRANCH)-$(COMMIT)

## Builds the application container images
build:
	docker-compose -f local-compose.yml build
	docker tag -f workshop_nginx autopilotpattern/workshop-nginx:$(TAG)
	docker tag -f workshop_customers autopilotpattern/workshop-customers:$(TAG)
	docker tag -f workshop_sales autopilotpattern/workshop-sales:$(TAG)

## Pushes the application container images to the Docker Hub
ship:
	docker push autopilotpattern/workshop-nginx:$(TAG)
	docker push autopilotpattern/workshop-customers:$(TAG)
	docker push autopilotpattern/workshop-sales:$(TAG)


# ------------------------------------------------
# Test running

LOG_LEVEL ?= INFO
KEY := ~/.ssh/TritonTestingKey

# if you pass `TRACE=1` into the call to `make` then the Python tests will
# run under the `trace` module (provides detailed call logging)
PYTHON := $(shell if [ -z ${TRACE} ]; then echo "python"; else echo "python -m trace"; fi)

# sets up the Docker context for running the build container locally
# `make test` runs in the build container on Shippable where the boot script
# will pull the source from GitHub first, but if we want to debug locally
# we'll need to mount the local source into the container.
# TODO: remove mount to ~/src/autopilotpattern/testing
LOCALRUN := \
	-e PATH=/root/venv/3.5/bin \
	-e COMPOSE_HTTP_TIMEOUT=300 \
	-w /src \
	-v ~/.triton:/root/.triton \
	-v ~/src/autopilotpattern/testing/testcases.py:/usr/local/lib/python2.7/dist-packages/testcases.py \
	-v $(shell pwd)/tests:/tests \
	-v $(shell pwd)/docker-compose.yml:/docker-compose.yml \
	-v $(shell pwd)/Makefile:/Makefile \
	joyent/test


## Build the test running container
test-runner:
	docker build -f tests/Dockerfile -t="joyent/test" .

# configure triton profile
~/.triton/profiles.d/us-sw-1.json:
	{ \
	  cp /tmp/ssh/TritonTestingKey $(KEY) ;\
	  ssh-keygen -y -f $(KEY) > $(KEY).pub ;\
	  FINGERPRINT=$$(ssh-keygen -l -f $(KEY) | awk '{print $$2}' | sed 's/MD5://') ;\
	  printf '{"url": "https://us-sw-1.api.joyent.com", "name": "TritonTesting", "account": "timgross", "keyId": "%s"}' $${FINGERPRINT} > profile.json ;\
	}
	cat profile.json | triton profile create -f -
	-rm profile.json

# TODO: replace this user w/ a user specifically for testing
## Run the build container (on Shippable) and deploy tests on Triton
test: ~/.triton/profiles.d/us-sw-1.json
	cp tests/tests.py . && \
		DOCKER_TLS_VERIFY=1 \
		DOCKER_CERT_PATH=~/.triton/docker/timgross@us-sw-1_api_joyent_com \
		DOCKER_HOST=tcp://us-sw-1.docker.joyent.com:2376 \
		COMPOSE_HTTP_TIMEOUT=300 \
		PATH=/root/venv/3.5/bin \
		$(PYTHON) tests.py

## Run the build container locally and deploy tests on Triton.
test-local-triton: ~/.triton/profiles.d/us-sw-1.json
	docker run -it --rm \
		-e DOCKER_TLS_VERIFY=1 \
		-e DOCKER_CERT_PATH=~/.triton/docker/timgross@us-sw-1_api_joyent_com \
		-e DOCKER_HOST=tcp://us-sw-1.docker.joyent.com:2376 \
		$(LOCALRUN) $(PYTHON) tests.py


## Run the build container locally and deploy tests on local Docker too.<commit_msg>Fix ~ path for DOCKER_PATH_PATH in makefile targets<commit_after>SHELL := /bin/bash
.PHONY: build ship test

## Display this help message
help:
	@awk '/^##.*$$/,/[a-zA-Z_-]+:/' $(MAKEFILE_LIST) | awk '!(NR%2){print $$0p}{p=$$0}' | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}' | sort

# ------------------------------------------------
# Container builds

# we get these from shippable if available, otherwise from git
COMMIT ?= $(shell git rev-parse --short HEAD)
BRANCH ?= $(shell git rev-parse --abbrev-ref HEAD)
TAG := $(BRANCH)-$(COMMIT)

## Builds the application container images
build:
	docker-compose -f local-compose.yml build
	docker tag -f workshop_nginx autopilotpattern/workshop-nginx:$(TAG)
	docker tag -f workshop_customers autopilotpattern/workshop-customers:$(TAG)
	docker tag -f workshop_sales autopilotpattern/workshop-sales:$(TAG)

## Pushes the application container images to the Docker Hub
ship:
	docker push autopilotpattern/workshop-nginx:$(TAG)
	docker push autopilotpattern/workshop-customers:$(TAG)
	docker push autopilotpattern/workshop-sales:$(TAG)


# ------------------------------------------------
# Test running

LOG_LEVEL ?= INFO
KEY := ~/.ssh/TritonTestingKey

# if you pass `TRACE=1` into the call to `make` then the Python tests will
# run under the `trace` module (provides detailed call logging)
PYTHON := $(shell if [ -z ${TRACE} ]; then echo "python"; else echo "python -m trace"; fi)

# sets up the Docker context for running the build container locally
# `make test` runs in the build container on Shippable where the boot script
# will pull the source from GitHub first, but if we want to debug locally
# we'll need to mount the local source into the container.
# TODO: remove mount to ~/src/autopilotpattern/testing
LOCALRUN := \
	-e PATH=/root/venv/3.5/bin \
	-e COMPOSE_HTTP_TIMEOUT=300 \
	-w /src \
	-v ~/.triton:/root/.triton \
	-v ~/src/autopilotpattern/testing/testcases.py:/usr/local/lib/python2.7/dist-packages/testcases.py \
	-v $(shell pwd)/tests:/tests \
	-v $(shell pwd)/docker-compose.yml:/docker-compose.yml \
	-v $(shell pwd)/Makefile:/Makefile \
	joyent/test


## Build the test running container
test-runner:
	docker build -f tests/Dockerfile -t="joyent/test" .

# configure triton profile
~/.triton/profiles.d/us-sw-1.json:
	{ \
	  cp /tmp/ssh/TritonTestingKey $(KEY) ;\
	  ssh-keygen -y -f $(KEY) > $(KEY).pub ;\
	  FINGERPRINT=$$(ssh-keygen -l -f $(KEY) | awk '{print $$2}' | sed 's/MD5://') ;\
	  printf '{"url": "https://us-sw-1.api.joyent.com", "name": "TritonTesting", "account": "timgross", "keyId": "%s"}' $${FINGERPRINT} > profile.json ;\
	}
	cat profile.json | triton profile create -f -
	-rm profile.json

# TODO: replace this user w/ a user specifically for testing
## Run the build container (on Shippable) and deploy tests on Triton
test: ~/.triton/profiles.d/us-sw-1.json
	cp tests/tests.py . && \
		DOCKER_TLS_VERIFY=1 \
		DOCKER_CERT_PATH=/root/.triton/docker/timgross@us-sw-1_api_joyent_com \
		DOCKER_HOST=tcp://us-sw-1.docker.joyent.com:2376 \
		COMPOSE_HTTP_TIMEOUT=300 \
		PATH=/root/venv/3.5/bin \
		$(PYTHON) tests.py

## Run the build container locally and deploy tests on Triton.
test-local-triton: ~/.triton/profiles.d/us-sw-1.json
	docker run -it --rm \
		-e DOCKER_TLS_VERIFY=1 \
		-e DOCKER_CERT_PATH=/root/.triton/docker/timgross@us-sw-1_api_joyent_com \
		-e DOCKER_HOST=tcp://us-sw-1.docker.joyent.com:2376 \
		$(LOCALRUN) $(PYTHON) tests.py


## Run the build container locally and deploy tests on local Docker too.
{'fraction_non_alphanumeric': 0.17839909168322451, 'fraction_numerical': 0.013908600624467783, 'mean_word_length': 3.387920298879203, 'pattern_counts': {'":': 10, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6751117', 'n_tokens_mistral': 2702, 'n_tokens_neox': 2495, 'n_words': 671}