Automating your Server Images with Packer

Gabriel Baker

Who am I?

Head of caffeine consumption and 

official poker of things @fusions

`Systems Developer`

@gabriel403

http://g403.co

Automating the crap out of things since 2010

Server Images

What are they &

why do we use them

Base Images

  • AWS, Rackspace, Digital Ocean
     all provide base images
  • Ease and Speed of Server Creation
  • Identical Servers

Generated Images

  • Customised images
  • Reproducibility
  • Speed of producing production ready servers

Server Images

Why Automate them?

Automating Server Images

  • Changes to your base image 
    are easier to include
  • Simple one step to rebuild images
  • More time to do something else

Packer

Automating Awesomeness

What's great about Packer?

  • Single point to build all your server images
  • Interfaces with nearly all virtual server providers
  • Can work with most provisioning software
  • Completely open source
  • Easily extendable
{
  "builders": [{
    "type"                   : "virtualbox-iso",
    "iso_url"                : "http://releases.ubuntu.com/14.04/ubuntu-14.04-server-amd64.iso",
    "iso_checksum"           : "01545fa976c8367b4f0d59169ac4866c",
    "iso_checksum_type"      : "md5",
    "disk_size"              : 80000,
    "guest_os_type"          : "Ubuntu_64",
    "http_directory"         : "preseed",
    "ssh_username"           : "vagrant",
    "ssh_password"           : "vagrant",
    "output_directory"       : "output/14.04_5.5_64_virtualbox",
    "boot_command"           : [
      "",
      "/install/vmlinuz noapic ",
      "preseed/url=http://{{ .HTTPIP }}:{{ .HTTPPort }}/preseed.cfg ",
      "debian-installer=en_GB auto locale=en_GB kbd-chooser/method=uk ",
      "hostname={{ .Name }} ",
      "fb=false debconf/frontend=noninteractive ",
      "keyboard-configuration/modelcode=SKIP keyboard-configuration/layout=GB ",
      "keyboard-configuration/variant=GB console-setup/ask_detect=false ",
      "initrd=/install/initrd.gz -- "
    ],
    "vboxmanage"             : [
      ["modifyvm", "{{.Name}}", "--memory", "1024"]
    ],
    "shutdown_command"       : "echo 'shutdown -P now' > shutdown.sh; echo 'vagrant'|sudo -S sh 'shutdown.sh'"
  }],

Boring bit

"provisioners" : [{
  "type": "shell",
  "execute_command": "echo 'vagrant' | sudo -S sh '{{ .Path }}'",
  "inline": [
    "apt-get update -y",
    "apt-get install -y linux-headers-$(uname -r) build-essential dkms puppet-common nfs-kernel-server nfs-common ruby-hiera",
    "apt-get clean",
    "mount -o loop VBoxGuestAdditions.iso /media/cdrom",
    "sh /media/cdrom/VBoxLinuxAdditions.run",
    "umount /media/cdrom",
    "mkdir ~/.ssh",
    "wget -qO- https://raw.github.com/mitchellh/vagrant/master/keys/vagrant.pub >> ~/.ssh/authorized_keys",
    "echo 'vagrant ALL=NOPASSWD:ALL' > /tmp/vagrant",
    "chmod 0440 /tmp/vagrant",
    "mv /tmp/vagrant /etc/sudoers.d/"
  ]
},

Provisioners (bash inline)

{
  "type"          : "puppet-masterless",
  "manifest_file" : "manifests/dev.pp",
  "module_paths"  : ["../../puppet/modules"],
  "facter"        : {
    "fqdn"        : "dev.fusionspim.com",
    "env"         : "dev"
  }
}],

Provisioners (puppet, local file)

"post-processors"          : [
  {
    "type"               : "vagrant",
    "only"               : ["virtualbox-iso"],
    "output"             : "output/ubuntu_14.04_5.5_base_v5.box"
  }
]}

Post-processors

{
  "builders": [
  {
    "type"          : "digitalocean",
    "client_id"     : "client_id",
    "api_key"       : "api_key",
    "image"         : "ubuntu-14-04-x64",
    "region"        : "nyc2",
    "size"          : "4gb",
    "snapshot_name" : "do-dev-fusions-snap",
    "droplet_name"  : "do-dev.fusionspim.com"
  }],
  "provisioners": [{
    "type": "shell",
    "execute_command": "echo 'vagrant' | sudo -S sh '{{ .Path }}'",
    "inline": [
      "boring bits here"
    ]
  },{
    "type"          : "puppet-masterless",
    "manifest_file" : "manifests/site.pp",
    "module_paths"  : ["../../puppet/modules"],
    "facter"        : {
      "fqdn"        : "do-dev.fusionspim.com",
      "env"         : "dev"
    }
  }]
}

Digitalocean

gabriel at dixie-flatline in ~/Projects/jarvis/packer on master
$ packer build digitalocean-yolo.fusions-14.04-5.5.json

And then install puppet

And then run your puppet scripts

And then take your snapshot

gabriel at dixie-flatline in ~/Projects/jarvis/packer on master
$ packer build digitalocean-yolo.fusions-14.04-5.5.json
gabriel at dixie-flatline in ~/Projects/jarvis/packer on master
$ packer build virtualbox_provision_14.04_5.5.json

A whole load of boring crap involving 2 sets of wizards

And then remembering to download the Ubuntu iso

And then attaching the iso to the vm

And then going through the ubuntu

install menu

And then install puppet

And then run your puppet scripts

And then take your snapshot

And then make your snapshot a valid vagrant file

gabriel at dixie-flatline in ~/Projects/jarvis/packer on master
$ packer build virtualbox_provision_14.04_5.5.json

AWS

Is complicated

  • security groups
  • run tags
  • spot pricing
  • public ip associating
  • and a whole bunch more

Packer does some of the complicated

https://www.packer.io

@gabriel403

me@g403.co

https://joind.in/12974

Made with Slides.com