44BOX_NAME = ENV [ 'BOX_NAME' ] || "ubuntu"
55BOX_URI = ENV [ 'BOX_URI' ] || "http://files.vagrantup.com/precise64.box"
66VF_BOX_URI = ENV [ 'BOX_URI' ] || "http://files.vagrantup.com/precise64_vmware_fusion.box"
7+ AWS_BOX_URI = ENV [ 'BOX_URI' ] || "https://github.com/mitchellh/vagrant-aws/raw/master/dummy.box"
78AWS_REGION = ENV [ 'AWS_REGION' ] || "us-east-1"
8- AWS_AMI = ENV [ 'AWS_AMI' ] || "ami-d0f89fb9"
9+ AWS_AMI = ENV [ 'AWS_AMI' ] || "ami-69f5a900"
10+ AWS_INSTANCE_TYPE = ENV [ 'AWS_INSTANCE_TYPE' ] || 't1.micro'
11+
912FORWARD_DOCKER_PORTS = ENV [ 'FORWARD_DOCKER_PORTS' ]
1013
14+ SSH_PRIVKEY_PATH = ENV [ "SSH_PRIVKEY_PATH" ]
15+
16+ # A script to upgrade from the 12.04 kernel to the raring backport kernel (3.8)
17+ # and install docker.
18+ $script = <<SCRIPT
19+ # The username to add to the docker group will be passed as the first argument
20+ # to the script. If nothing is passed, default to "vagrant".
21+ user="$1"
22+ if [ -z "$user" ]; then
23+ user=vagrant
24+ fi
25+
26+ # Adding an apt gpg key is idempotent.
27+ wget -q -O - https://get.docker.io/gpg | apt-key add -
28+
29+ # Creating the docker.list file is idempotent, but it may overrite desired
30+ # settings if it already exists. This could be solved with md5sum but it
31+ # doesn't seem worth it.
32+ echo 'deb http://get.docker.io/ubuntu docker main' > \
33+ /etc/apt/sources.list.d/docker.list
34+
35+ # Update remote package metadata. 'apt-get update' is idempotent.
36+ apt-get update -q
37+
38+ # Install docker. 'apt-get install' is idempotent.
39+ apt-get install -q -y lxc-docker
40+
41+ usermod -a -G docker "$user"
42+
43+ tmp=`mktemp -q` && {
44+ # Only install the backport kernel, don't bother upgrade if the backport is
45+ # already installed. We want parse the output of apt so we need to save it
46+ # with 'tee'. NOTE: The installation of the kernel will trigger dkms to
47+ # install vboxguest if needed.
48+ apt-get install -q -y --no-upgrade linux-image-generic-lts-raring | \
49+ tee "$tmp"
50+
51+ # Parse the number of installed packages from the output
52+ NUM_INST=`awk '$2 == "upgraded," && $4 == "newly" { print $3 }' "$tmp"`
53+ rm "$tmp"
54+ }
55+
56+ # If the number of installed packages is greater than 0, we want to reboot (the
57+ # backport kernel was installed but is not running).
58+ if [ "$NUM_INST" -gt 0 ];
59+ then
60+ echo "Rebooting down to activate new kernel."
61+ echo "/vagrant will not be mounted. Use 'vagrant halt' followed by"
62+ echo "'vagrant up' to ensure /vagrant is mounted."
63+ shutdown -r now
64+ fi
65+ SCRIPT
66+
67+ # We need to install the virtualbox guest additions *before* we do the normal
68+ # docker installation. As such this script is prepended to the common docker
69+ # install script above. This allows the install of the backport kernel to
70+ # trigger dkms to build the virtualbox guest module install.
71+ $vbox_script = <<VBOX_SCRIPT + $script
72+ # Install the VirtualBox guest additions if they aren't already installed.
73+ if [ ! -d /opt/VBoxGuestAdditions-4.3.2/ ]; then
74+ # Update remote package metadata. 'apt-get update' is idempotent.
75+ apt-get update -q
76+
77+ # Kernel Headers and dkms are required to build the vbox guest kernel
78+ # modules.
79+ apt-get install -q -y linux-headers-generic-lts-raring dkms
80+
81+ echo 'Downloading VBox Guest Additions...'
82+ wget -cq http://dlc.sun.com.edgesuite.net/virtualbox/4.3.2/VBoxGuestAdditions_4.3.2.iso
83+
84+ mount -o loop,ro /home/vagrant/VBoxGuestAdditions_4.3.2.iso /mnt
85+ /mnt/VBoxLinuxAdditions.run --nox11
86+ umount /mnt
87+ fi
88+ VBOX_SCRIPT
89+
1190Vagrant ::Config . run do |config |
1291 # Setup virtual machine box. This VM configuration code is always executed.
1392 config . vm . box = BOX_NAME
1493 config . vm . box_url = BOX_URI
1594
16- config . ssh . forward_agent = true
17-
18- # Provision docker and new kernel if deployment was not done.
19- # It is assumed Vagrant can successfully launch the provider instance.
20- if Dir . glob ( "#{ File . dirname ( __FILE__ ) } /.vagrant/machines/default/*/id" ) . empty?
21- # Add lxc-docker package
22- pkg_cmd = "wget -q -O - https://get.docker.io/gpg | apt-key add -;" \
23- "echo deb http://get.docker.io/ubuntu docker main > /etc/apt/sources.list.d/docker.list;" \
24- "apt-get update -qq; apt-get install -q -y --force-yes lxc-docker; "
25- # Add Ubuntu raring backported kernel
26- pkg_cmd << "apt-get update -qq; apt-get install -q -y linux-image-generic-lts-raring; "
27- # Add guest additions if local vbox VM. As virtualbox is the default provider,
28- # it is assumed it won't be explicitly stated.
29- if ENV [ "VAGRANT_DEFAULT_PROVIDER" ] . nil? && ARGV . none? { |arg | arg . downcase . start_with? ( "--provider" ) }
30- pkg_cmd << "apt-get install -q -y linux-headers-generic-lts-raring dkms; " \
31- "echo 'Downloading VBox Guest Additions...'; " \
32- "wget -q http://dlc.sun.com.edgesuite.net/virtualbox/4.2.12/VBoxGuestAdditions_4.2.12.iso; "
33- # Prepare the VM to add guest additions after reboot
34- pkg_cmd << "echo -e 'mount -o loop,ro /home/vagrant/VBoxGuestAdditions_4.2.12.iso /mnt\n " \
35- "echo yes | /mnt/VBoxLinuxAdditions.run\n umount /mnt\n " \
36- "rm /root/guest_additions.sh; ' > /root/guest_additions.sh; " \
37- "chmod 700 /root/guest_additions.sh; " \
38- "sed -i -E 's#^exit 0#[ -x /root/guest_additions.sh ] \\ &\\ & /root/guest_additions.sh#' /etc/rc.local; " \
39- "echo 'Installation of VBox Guest Additions is proceeding in the background.'; " \
40- "echo '\" vagrant reload\" can be used in about 2 minutes to activate the new guest additions.'; "
41- end
42- # Add vagrant user to the docker group
43- pkg_cmd << "usermod -a -G docker vagrant; "
44- # Activate new kernel
45- pkg_cmd << "shutdown -r +1; "
46- config . vm . provision :shell , :inline => pkg_cmd
95+ # Use the specified private key path if it is specified and not empty.
96+ if SSH_PRIVKEY_PATH
97+ config . ssh . private_key_path = SSH_PRIVKEY_PATH
4798 end
48- end
4999
100+ config . ssh . forward_agent = true
101+ end
50102
51103# Providers were added on Vagrant >= 1.1.0
104+ #
105+ # NOTE: The vagrant "vm.provision" appends its arguments to a list and executes
106+ # them in order. If you invoke "vm.provision :shell, :inline => $script"
107+ # twice then vagrant will run the script two times. Unfortunately when you use
108+ # providers and the override argument to set up provisioners (like the vbox
109+ # guest extensions) they 1) don't replace the other provisioners (they append
110+ # to the end of the list) and 2) you can't control the order the provisioners
111+ # are executed (you can only append to the list). If you want the virtualbox
112+ # only script to run before the other script, you have to jump through a lot of
113+ # hoops.
114+ #
115+ # Here is my only repeatable solution: make one script that is common ($script)
116+ # and another script that is the virtual box guest *prepended* to the common
117+ # script. Only ever use "vm.provision" *one time* per provider. That means
118+ # every single provider has an override, and every single one configures
119+ # "vm.provision". Much saddness, but such is life.
52120Vagrant ::VERSION >= "1.1.0" and Vagrant . configure ( "2" ) do |config |
53121 config . vm . provider :aws do |aws , override |
54- aws . access_key_id = ENV [ "AWS_ACCESS_KEY_ID" ]
55- aws . secret_access_key = ENV [ "AWS_SECRET_ACCESS_KEY" ]
122+ username = "ubuntu"
123+ override . vm . box_url = AWS_BOX_URI
124+ override . vm . provision :shell , :inline => $script, :args => username
125+ aws . access_key_id = ENV [ "AWS_ACCESS_KEY" ]
126+ aws . secret_access_key = ENV [ "AWS_SECRET_KEY" ]
56127 aws . keypair_name = ENV [ "AWS_KEYPAIR_NAME" ]
57- override . ssh . private_key_path = ENV [ "AWS_SSH_PRIVKEY" ]
58- override . ssh . username = "ubuntu"
128+ override . ssh . username = username
59129 aws . region = AWS_REGION
60130 aws . ami = AWS_AMI
61- aws . instance_type = "t1.micro"
131+ aws . instance_type = AWS_INSTANCE_TYPE
62132 end
63133
64- config . vm . provider :rackspace do |rs |
65- config . ssh . private_key_path = ENV [ "RS_PRIVATE_KEY" ]
134+ config . vm . provider :rackspace do |rs , override |
135+ override . vm . provision :shell , :inline => $script
66136 rs . username = ENV [ "RS_USERNAME" ]
67137 rs . api_key = ENV [ "RS_API_KEY" ]
68138 rs . public_key_path = ENV [ "RS_PUBLIC_KEY" ]
@@ -71,20 +141,25 @@ Vagrant::VERSION >= "1.1.0" and Vagrant.configure("2") do |config|
71141 end
72142
73143 config . vm . provider :vmware_fusion do |f , override |
74- override . vm . box = BOX_NAME
75144 override . vm . box_url = VF_BOX_URI
76145 override . vm . synced_folder "." , "/vagrant" , disabled : true
146+ override . vm . provision :shell , :inline => $script
77147 f . vmx [ "displayName" ] = "docker"
78148 end
79149
80- config . vm . provider :virtualbox do |vb |
81- config . vm . box = BOX_NAME
82- config . vm . box_url = BOX_URI
150+ config . vm . provider :virtualbox do |vb , override |
151+ override . vm . provision :shell , :inline => $vbox_script
83152 vb . customize [ "modifyvm" , :id , "--natdnshostresolver1" , "on" ]
84153 vb . customize [ "modifyvm" , :id , "--natdnsproxy1" , "on" ]
85154 end
86155end
87156
157+ # If this is a version 1 config, virtualbox is the only option. A version 2
158+ # config would have already been set in the above provider section.
159+ Vagrant ::VERSION < "1.1.0" and Vagrant ::Config . run do |config |
160+ config . vm . provision :shell , :inline => $vbox_script
161+ end
162+
88163if !FORWARD_DOCKER_PORTS . nil?
89164 Vagrant ::VERSION < "1.1.0" and Vagrant ::Config . run do |config |
90165 ( 49000 ..49900 ) . each do |port |
0 commit comments