Skip to content

Instantly share code, notes, and snippets.

@igorlg
Created February 11, 2019 23:49
Show Gist options
  • Save igorlg/ec517c1b9db29b8ef64b3c481be41163 to your computer and use it in GitHub Desktop.
Save igorlg/ec517c1b9db29b8ef64b3c481be41163 to your computer and use it in GitHub Desktop.
Makefile for Packer builds - IN PROGRESS
inspec_version = 3.6.4
packer_version = 1.3.4
packer_inspec_version = 0.2.1
terraform_version = 0.11.11
uname_s = $(shell uname -s)
uname_m = $(shell uname -m)
cmd_unzip = @unzip -u -q
cmd_wget = @wget -q -O
cmd_rm = @rm -f
cmd_dpkg = @dpkg -i
check_ansible = $(shell command -v ansible)
check_inspec =
ifeq ($(uname_s),Linux)
inspec_url = https://packages.chef.io/files/stable/inspec/$(inspec_version)/ubuntu/16.04/inspec_$(inspec_version)-1_amd64.deb
packer_url = https://releases.hashicorp.com/packer/$(packer_version)/packer_$(packer_version)_linux_amd64.zip
packer_inspec_url = https://github.com/jrbeilke/packer-provisioner-inspec/releases/download/v$(packer_inspec_version)/packer-provisioner-inspec_linux_amd64.zip
terraform_url = https://releases.hashicorp.com/terraform/$(terraform_version)/terraform_$(terraform_version)_linux_amd64.zip
else ifeq ($(uname_s),Darwin)
packer_url = https://releases.hashicorp.com/packer/$(packer_version)/packer_$(packer_version)_darwin_amd64.zip
packer_inspec_url = https://github.com/jrbeilke/packer-provisioner-inspec/releases/download/v$(packer_inspec_version)/packer-provisioner-inspec_darwin_amd64.zip
terraform_url = https://releases.hashicorp.com/terraform/$(terraform_version)/terraform_$(terraform_version)_darwin_amd64.zip
endif
install_objs = clean install-packer-inspec
ifeq ($(shell command -v packer),)
install_objs += install-packer
endif
ifeq ($(shell command -v terraform),)
install_objs += install-terraform
endif
ifeq ($(shell command -v inspec),)
install_objs += install-inspec-$(uname_s)
endif
ifeq ($(shell command -v ansible),)
install_objs += install-ansible
endif
install: $(install_objs)
clean:
@echo "Cleaning up files. Removed:"
@rm -vf packer packer-provisioner-inspec
@rm -fv terraform
@rm -fv packer-manifest.json
@rm -fv *.log
@find . -type f -iname '*.retry' -exec rm -fv {} \;
# install-ok:
# @test -x packer-provisioner-inspec
# @test -x packer || command -v packer > /dev/null
# @test -x terraform || command -v terraform > /dev/null
# @command -v inspec > /dev/null
# @command -v ansible-playbook > /dev/null
install-packer:
@echo "Downloading Packer v$(packer_version) from $(packer_url)"
$(cmd_wget) packer.zip $(packer_url)
$(cmd_unzip) packer.zip
$(cmd_rm) packer.zip
install-packer-inspec:
@echo "Downloading Packer Inspec plugin v$(packer_inspec_version) from $(packer_inspec_url)"
$(cmd_wget) packer-inspec.zip $(packer_inspec_url)
$(cmd_unzip) packer-inspec.zip
$(cmd_rm) packer-inspec.zip
install-terraform:
@echo "Downloading Terraform v$(terraform_version) from $(terraform_url)"
$(cmd_wget) terraform.zip $(terraform_url)
$(cmd_unzip) terraform.zip
$(cmd_rm) terraform.zip
install-inspec-Linux:
@echo "Downloading and Installing InSpec v$(inspec_version) from $(inspec_url)"
$(cmd_wget) inspec.deb $(inspec_url)
$(cmd_dpkg) inspec.deb
$(cmd_rm) inspec.deb
install-inspec-Darwin:
@echo "Installing InSpec via HomeBrew Cask from chef/chef/inspec"
@brew cask install chef/chef/inspec
install-ansible:
@echo "Installing Ansible and Boto3 from pip"
@pip install ansible boto3
build: install
packer build -color=false -machine-readable -timestamp-ui packer.json | tee packer-build.log
@tail -2 packer-build.log | awk 'match($0, /ami-.*/) { print substr($0, RSTART, RLENGTH) }' > ami.log
cat ami.log
build-ok:
@test -f packer-manifest.json
deploy: install build-ok
ami_id=`cat ami.log`
@echo "Deploying AMI ${ami_id} using Terraform"
# cd terraform/
# terraform init
# terraform plan -out terraform.tfplan
# terraform apply terraform.tfplan
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment