Putting Your Vagrants in the Cloud

Post on 17-Jul-2015

287 views 0 download

Transcript of Putting Your Vagrants in the Cloud

Putting Your Vagrants in the CloudRichard Boyd II rboyd@invodo.com @richardboydii

The Problem• We need Vagrant boxes that are multi-

provider (VirtualBox and AWS). • We need Vagrant boxes that are versioned. • We need Vagrant boxes that use same

‘golden image’ that we deploy servers on. • We need to host those Vagrant boxes in S3.

The Solution• Create a baseline Chef cookbook. • Use Packer to create named Vagrant Boxes using the

baseline for: • Each provider, e.g. baseline_vbox, baseline_aws. • Each version, e.g. baseline_vbox_v0.1.0.box,

baseline_aws_v0.1.0.vbox. • Create a JSON file containing metadata on all versions

and providers. • Automate the creation of the Vagrant boxes when the

baseline is updated.

How We Build It

1. Check the version

2. Run Packer. Provision an AMI and a VirtualBox VM using the Chef cookbook stored in GitHub. Turn those into Vagrant boxes at the end of the Packer run.

3. Upload the boxes to S3.

4. Update the metadata file.

Packer'

h)p://s3.amazonaws.com/vagrant/baseline_box/'

baseline'

h)p://s3.amazonaws.com/vagrant/baseline_box/boxes/'

baseline.json'

baseline_ami'

conveyor.rb'

baseline_vbox'

baseline_aws_v0.1.0.vbox'

baseline_vbox_v0.1.0.vbox'

baseline_aws_v0.1.0.vbox'

baseline_vbox_v0.1.0.vbox'

VERSION'

A Sample AWS Vagrantfile USER = ENV['USER'] TIME = Time.now DATE = TIME.strftime('%Y%m%d%H%M') RAND = TIME.strftime('%Y%m%d%H%M%S%L') HOST = `hostname`

Vagrant.require_version '>= 1.5.0' Vagrant.configure('2') do |config| config.vm.box = "baseline" config.ssh.username = “vagrant" config.ssh.private_key_path = “/path/to/a/key“ config.vm.box_check_update = true config.vm.provider :aws do |aws, override| aws.access_key_id = config.user.aws.access_key aws.secret_access_key = config.user.aws.secret_key aws.region_config “us-east-1“, :ami => “ami-123fd2" aws.instance_type = 'm1.small' aws.security_groups = [‘vagrant_group'] aws.tags = { 'user' => USER, 'date' => DATE, 'host' => HOST, 'segment' => 'dev.vagrant', 'environment' => 'local', 'launcher' => 'vagrant', 'boxname' => 'baseline' } aws.block_device_mapping = [ { 'DeviceName' => '/dev/xvdi', 'Ebs.VolumeSize' => 20, 'Ebs.VolumeType' => 'gp2' } ] end

end

A Sample Virtualbox Vagrantfile USER = ENV['USER'] TIME = Time.now DATE = TIME.strftime('%Y%m%d%H%M') RAND = TIME.strftime('%Y%m%d%H%M%S%L') HOST = `hostname`

Vagrant.require_version '>= 1.5.0' Vagrant.configure('2') do |config| config.vm.box = "baseline" config.ssh.username = "vagrant" config.ssh.private_key_path = “/path/to/key“ config.vm.box_url = “https://s3.amazonaws.com/vagrant/baseline_box/baseline.json" config.vm.network :private_network, ip: “10.20.30.1” config.vm.provider "virtualbox" do |v| v.name = "boxname-\#\{RAND\}" v.memory = 512 end config.vm.box_check_update = true

end

A Multi-provider Metadata File{ "name": NAME, “description": DESCRIPTION, "versions":

[ { “version": VERSION_1, "providers":

[ { "name": TYPE_OF_PROVIDER, “url": URL_TO_BOX_FILE, “checksum_type": TYPE_OF_CHECKSUM, "checksum": CHECKSUM_VALUE }, { "name": TYPE_OF_PROVIDER, “url": URL_TO_BOX_FILE, “checksum_type": TYPE_OF_CHECKSUM, "checksum": CHECKSUM_VALUE } ] }, {

“version": VERSION_2, BLAH },

] }

Example Metadata File{ "name": “box_name", "description": "Debian 7.7 (Wheezy) 64-bit box provisioned with the baseline::default recipe.", "versions": [ { "version": “0.1.0”, "providers": [ { "name": "aws", "url": “https://s3.amazonaws.com/vagrant/baseline_box/boxes/baseline_v0.1.0_vbox.box”, "checksum_type": "sha1", "checksum": "bfdb3e81b09a8daff479e160717413a24f77f0ae" }, { "name": "virtualbox", "url": “https://s3.amazonaws.com/vagrant/baseline_box/boxes/baseline_v0.1.0_aws.box”, "checksum_type": "sha1", "checksum": "53d711379eba58b9b19b15759d022f33aa720592" } ] } ] }

How Do You Use It?

vagrant box add https://s3.amazonaws.com/

vagrant/baseline_box/baseline.json

Things to Watch Out For• The content-type on the metadata file needs to be

set to JSON in S3. • Create a Packer template for each Vagrant box to

be built. • For the AMI, use the community (Debian) official

AMI as a starting point. • The Bento project (from Chef) provided a lot of

inspiration for this.

Thanks!rboyd@invodo.com

https://github.com/richardboydii/

@richardboydii

countze.ro