Skip to content

Instantly share code, notes, and snippets.

@nathwill
Created July 24, 2015 19:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nathwill/764d58238cf7d76fb0e1 to your computer and use it in GitHub Desktop.
Save nathwill/764d58238cf7d76fb0e1 to your computer and use it in GitHub Desktop.
packer template for role-based packer builds
{
"description": "build th base image",
"min_packer_version": "0.8.0",
"variables": {
"user": "centos",
"image": "d5f2c6b8-ae55-41d3-b9ba-15e0f2350659",
"flavor": "2",
"role1": "common",
"role2": "common",
"role3": "common",
"chef_environment": "staging",
"chef_server_url": "{{env `CHEF_SERVER_URL`}}",
"secret_file": "{{env `HOME`}}/.chef/encrypted_data_bag_secret",
"validation_client_name": "dummy",
"validation_key_path": "{{env `HOME`}}/.chef/dummy.pem"
},
"builders": [
{
"type": "openstack",
"ssh_username": "{{user `user`}}",
"ssh_pty": true,
"image_name": "{{user `role2`}}-{{timestamp}}",
"source_image": "{{user `image`}}",
"flavor": "{{user `flavor`}}",
"networks": [
"f6392107-4a9d-6797-854b-e39af2b487ae"
],
"security_groups": [
"default"
],
"use_floating_ip": true,
"floating_ip": "123.34.45.56"
}
],
"provisioners": [
{
"type": "shell",
"inline": [
"sudo yum -y upgrade",
"sudo yum clean all",
"sudo mkdir /etc/chef",
"mkdir /tmp/packer-chef-client",
"chmod 0755 /tmp/packer-chef-client",
"sudo mount -o bind /tmp/packer-chef-client /etc/chef"
]
},
{
"type": "file",
"source": "{{user `secret_file`}}",
"destination": "/etc/chef/encrypted_data_bag_secret"
},
{
"type": "chef-client",
"server_url": "{{user `chef_server_url`}}",
"chef_environment": "{{user `chef_environment`}}",
"run_list": [
"role[common]",
"role[{{user `role1`}}]",
"role[{{user `role2`}}]",
"role[{{user `role3`}}]"
],
"validation_client_name": "{{user `validation_client_name`}}",
"validation_key_path": "{{user `validation_key_path`}}"
},
{
"type": "shell",
"inline": [
"sudo systemctl disable sensu-client.service",
"sudo rm -rf /etc/ssh/ssh_host*"
]
}
]
}
@nathwill
Copy link
Author

# Enable packer debug logging
PACKER_LOG := 1
PACKER_LOG_PATH := /tmp/packer-debug
export PACKER_LOG PACKER_LOG_PATH

# Set up the straightforward builds
roles := bastion common codechallenge_db es_server ruby_app sensu_server

define packer_role_targets
$1:
    @packer build \
        -var "role2=${1}" \
        roles.json
endef

$(foreach role,$(roles), $(eval $(call packer_role_targets,$(role))))

# Set up the more complex builds
ml_roles := cc_docker load_balancer ruby_app_ml workspace_mgr_app ws_docker

define packer_ml_role_targets
$1:
    @packer build \
        -var "role2=${1}" \
        -var "role3=kernel_ml" \
        roles.json
endef

$(foreach role,$(ml_roles), $(eval $(call packer_ml_role_targets,$(role))))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment