Vagrantfile 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. # -*- mode: ruby -*-
  2. # vi: set ft=ruby :
  3. BOX_NAME = ENV['BOX_NAME'] || "ubuntu"
  4. BOX_URI = ENV['BOX_URI'] || "http://files.vagrantup.com/precise64.box"
  5. VF_BOX_URI = ENV['BOX_URI'] || "http://files.vagrantup.com/precise64_vmware_fusion.box"
  6. AWS_BOX_URI = ENV['BOX_URI'] || "https://github.com/mitchellh/vagrant-aws/raw/master/dummy.box"
  7. AWS_REGION = ENV['AWS_REGION'] || "us-east-1"
  8. AWS_AMI = ENV['AWS_AMI'] || "ami-69f5a900"
  9. AWS_INSTANCE_TYPE = ENV['AWS_INSTANCE_TYPE'] || 't1.micro'
  10. FORWARD_DOCKER_PORTS = ENV['FORWARD_DOCKER_PORTS']
  11. SSH_PRIVKEY_PATH = ENV["SSH_PRIVKEY_PATH"]
  12. # A script to upgrade from the 12.04 kernel to the raring backport kernel (3.8)
  13. # and install docker.
  14. $script = <<SCRIPT
  15. # The username to add to the docker group will be passed as the first argument
  16. # to the script. If nothing is passed, default to "vagrant".
  17. user="$1"
  18. if [ -z "$user" ]; then
  19. user=vagrant
  20. fi
  21. # Adding an apt gpg key is idempotent.
  22. apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 36A1D7869245C8950F966E92D8576A8BA88D21E9
  23. # Creating the docker.list file is idempotent, but it may overwrite desired
  24. # settings if it already exists. This could be solved with md5sum but it
  25. # doesn't seem worth it.
  26. echo 'deb http://get.docker.io/ubuntu docker main' > \
  27. /etc/apt/sources.list.d/docker.list
  28. # Update remote package metadata. 'apt-get update' is idempotent.
  29. apt-get update -q
  30. # Install docker. 'apt-get install' is idempotent.
  31. apt-get install -q -y lxc-docker
  32. usermod -a -G docker "$user"
  33. tmp=`mktemp -q` && {
  34. # Only install the backport kernel, don't bother upgrading if the backport is
  35. # already installed. We want parse the output of apt so we need to save it
  36. # with 'tee'. NOTE: The installation of the kernel will trigger dkms to
  37. # install vboxguest if needed.
  38. apt-get install -q -y --no-upgrade linux-image-generic-lts-raring | \
  39. tee "$tmp"
  40. # Parse the number of installed packages from the output
  41. NUM_INST=`awk '$2 == "upgraded," && $4 == "newly" { print $3 }' "$tmp"`
  42. rm "$tmp"
  43. }
  44. # If the number of installed packages is greater than 0, we want to reboot (the
  45. # backport kernel was installed but is not running).
  46. if [ "$NUM_INST" -gt 0 ];
  47. then
  48. echo "Rebooting down to activate new kernel."
  49. echo "/vagrant will not be mounted. Use 'vagrant halt' followed by"
  50. echo "'vagrant up' to ensure /vagrant is mounted."
  51. shutdown -r now
  52. fi
  53. SCRIPT
  54. # We need to install the virtualbox guest additions *before* we do the normal
  55. # docker installation. As such this script is prepended to the common docker
  56. # install script above. This allows the install of the backport kernel to
  57. # trigger dkms to build the virtualbox guest module install.
  58. $vbox_script = <<VBOX_SCRIPT + $script
  59. # Install the VirtualBox guest additions if they aren't already installed.
  60. if [ ! -d /opt/VBoxGuestAdditions-4.3.6/ ]; then
  61. # Update remote package metadata. 'apt-get update' is idempotent.
  62. apt-get update -q
  63. # Kernel Headers and dkms are required to build the vbox guest kernel
  64. # modules.
  65. apt-get install -q -y linux-headers-generic-lts-raring dkms
  66. echo 'Downloading VBox Guest Additions...'
  67. wget -cq http://dlc.sun.com.edgesuite.net/virtualbox/4.3.6/VBoxGuestAdditions_4.3.6.iso
  68. echo "95648fcdb5d028e64145a2fe2f2f28c946d219da366389295a61fed296ca79f0 VBoxGuestAdditions_4.3.6.iso" | sha256sum --check || exit 1
  69. mount -o loop,ro /home/vagrant/VBoxGuestAdditions_4.3.6.iso /mnt
  70. /mnt/VBoxLinuxAdditions.run --nox11
  71. umount /mnt
  72. fi
  73. VBOX_SCRIPT
  74. Vagrant::Config.run do |config|
  75. # Setup virtual machine box. This VM configuration code is always executed.
  76. config.vm.box = BOX_NAME
  77. config.vm.box_url = BOX_URI
  78. # Use the specified private key path if it is specified and not empty.
  79. if SSH_PRIVKEY_PATH
  80. config.ssh.private_key_path = SSH_PRIVKEY_PATH
  81. end
  82. config.ssh.forward_agent = true
  83. end
  84. # Providers were added on Vagrant >= 1.1.0
  85. #
  86. # NOTE: The vagrant "vm.provision" appends its arguments to a list and executes
  87. # them in order. If you invoke "vm.provision :shell, :inline => $script"
  88. # twice then vagrant will run the script two times. Unfortunately when you use
  89. # providers and the override argument to set up provisioners (like the vbox
  90. # guest extensions) they 1) don't replace the other provisioners (they append
  91. # to the end of the list) and 2) you can't control the order the provisioners
  92. # are executed (you can only append to the list). If you want the virtualbox
  93. # only script to run before the other script, you have to jump through a lot of
  94. # hoops.
  95. #
  96. # Here is my only repeatable solution: make one script that is common ($script)
  97. # and another script that is the virtual box guest *prepended* to the common
  98. # script. Only ever use "vm.provision" *one time* per provider. That means
  99. # every single provider has an override, and every single one configures
  100. # "vm.provision". Much saddness, but such is life.
  101. Vagrant::VERSION >= "1.1.0" and Vagrant.configure("2") do |config|
  102. config.vm.provider :aws do |aws, override|
  103. username = "ubuntu"
  104. override.vm.box_url = AWS_BOX_URI
  105. override.vm.provision :shell, :inline => $script, :args => username
  106. aws.access_key_id = ENV["AWS_ACCESS_KEY"]
  107. aws.secret_access_key = ENV["AWS_SECRET_KEY"]
  108. aws.keypair_name = ENV["AWS_KEYPAIR_NAME"]
  109. override.ssh.username = username
  110. aws.region = AWS_REGION
  111. aws.ami = AWS_AMI
  112. aws.instance_type = AWS_INSTANCE_TYPE
  113. end
  114. config.vm.provider :rackspace do |rs, override|
  115. override.vm.provision :shell, :inline => $script
  116. rs.username = ENV["RS_USERNAME"]
  117. rs.api_key = ENV["RS_API_KEY"]
  118. rs.public_key_path = ENV["RS_PUBLIC_KEY"]
  119. rs.flavor = /512MB/
  120. rs.image = /Ubuntu/
  121. end
  122. config.vm.provider :vmware_fusion do |f, override|
  123. override.vm.box_url = VF_BOX_URI
  124. override.vm.synced_folder ".", "/vagrant", disabled: true
  125. override.vm.provision :shell, :inline => $script
  126. f.vmx["displayName"] = "docker"
  127. end
  128. config.vm.provider :virtualbox do |vb, override|
  129. override.vm.provision :shell, :inline => $vbox_script
  130. vb.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
  131. vb.customize ["modifyvm", :id, "--natdnsproxy1", "on"]
  132. end
  133. end
  134. # If this is a version 1 config, virtualbox is the only option. A version 2
  135. # config would have already been set in the above provider section.
  136. Vagrant::VERSION < "1.1.0" and Vagrant::Config.run do |config|
  137. config.vm.provision :shell, :inline => $vbox_script
  138. end
  139. if !FORWARD_DOCKER_PORTS.nil?
  140. Vagrant::VERSION < "1.1.0" and Vagrant::Config.run do |config|
  141. (49000..49900).each do |port|
  142. config.vm.forward_port port, port
  143. end
  144. end
  145. Vagrant::VERSION >= "1.1.0" and Vagrant.configure("2") do |config|
  146. (49000..49900).each do |port|
  147. config.vm.network :forwarded_port, :host => port, :guest => port
  148. end
  149. end
  150. end