Vagrantfile 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. # -*- mode: ruby -*-
  2. # vi: set ft=ruby :
  3. # This Vagrantfile exists to test packaging. Read more about its use in the
  4. # vagrant section in TESTING.asciidoc.
  5. # Licensed to Elasticsearch under one or more contributor
  6. # license agreements. See the NOTICE file distributed with
  7. # this work for additional information regarding copyright
  8. # ownership. Elasticsearch licenses this file to you under
  9. # the Apache License, Version 2.0 (the "License"); you may
  10. # not use this file except in compliance with the License.
  11. # You may obtain a copy of the License at
  12. #
  13. # http://www.apache.org/licenses/LICENSE-2.0
  14. #
  15. # Unless required by applicable law or agreed to in writing,
  16. # software distributed under the License is distributed on an
  17. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  18. # KIND, either express or implied. See the License for the
  19. # specific language governing permissions and limitations
  20. # under the License.
  21. Vagrant.configure(2) do |config|
  22. config.vm.define "ubuntu-1404" do |config|
  23. config.vm.box = "elastic/ubuntu-14.04-x86_64"
  24. ubuntu_common config
  25. end
  26. config.vm.define "ubuntu-1604" do |config|
  27. config.vm.box = "elastic/ubuntu-16.04-x86_64"
  28. ubuntu_common config, extra: <<-SHELL
  29. # Install Jayatana so we can work around it being present.
  30. [ -f /usr/share/java/jayatanaag.jar ] || install jayatana
  31. SHELL
  32. end
  33. # Wheezy's backports don't contain Openjdk 8 and the backflips required to
  34. # get the sun jdk on there just aren't worth it. We have jessie for testing
  35. # debian and it works fine.
  36. config.vm.define "debian-8" do |config|
  37. config.vm.box = "elastic/debian-8-x86_64"
  38. deb_common config
  39. end
  40. config.vm.define "centos-6" do |config|
  41. config.vm.box = "elastic/centos-6-x86_64"
  42. rpm_common config
  43. end
  44. config.vm.define "centos-7" do |config|
  45. config.vm.box = "elastic/centos-7-x86_64"
  46. rpm_common config
  47. end
  48. config.vm.define "oel-6" do |config|
  49. config.vm.box = "elastic/oraclelinux-6-x86_64"
  50. rpm_common config
  51. end
  52. config.vm.define "oel-7" do |config|
  53. config.vm.box = "elastic/oraclelinux-7-x86_64"
  54. rpm_common config
  55. end
  56. config.vm.define "fedora-25" do |config|
  57. config.vm.box = "elastic/fedora-25-x86_64"
  58. dnf_common config
  59. end
  60. config.vm.define "opensuse-42" do |config|
  61. config.vm.box = "elastic/opensuse-42-x86_64"
  62. opensuse_common config
  63. end
  64. config.vm.define "sles-12" do |config|
  65. config.vm.box = "elastic/sles-12-x86_64"
  66. sles_common config
  67. end
  68. # Switch the default share for the project root from /vagrant to
  69. # /elasticsearch because /vagrant is confusing when there is a project inside
  70. # the elasticsearch project called vagrant....
  71. config.vm.synced_folder ".", "/vagrant", disabled: true
  72. config.vm.synced_folder ".", "/elasticsearch"
  73. # Expose project directory
  74. PROJECT_DIR = ENV['VAGRANT_PROJECT_DIR'] || Dir.pwd
  75. config.vm.synced_folder PROJECT_DIR, "/project"
  76. config.vm.provider "virtualbox" do |v|
  77. # Give the boxes 3GB because Elasticsearch defaults to using 2GB
  78. v.memory = 3072
  79. end
  80. if Vagrant.has_plugin?("vagrant-cachier")
  81. config.cache.scope = :box
  82. end
  83. config.vm.defined_vms.each do |name, config|
  84. config.options[:autostart] = false
  85. set_prompt = lambda do |config|
  86. # Sets up a consistent prompt for all users. Or tries to. The VM might
  87. # contain overrides for root and vagrant but this attempts to work around
  88. # them by re-source-ing the standard prompt file.
  89. config.vm.provision "prompt", type: "shell", inline: <<-SHELL
  90. cat \<\<PROMPT > /etc/profile.d/elasticsearch_prompt.sh
  91. export PS1='#{name}:\\w$ '
  92. PROMPT
  93. grep 'source /etc/profile.d/elasticsearch_prompt.sh' ~/.bashrc |
  94. cat \<\<SOURCE_PROMPT >> ~/.bashrc
  95. # Replace the standard prompt with a consistent one
  96. source /etc/profile.d/elasticsearch_prompt.sh
  97. SOURCE_PROMPT
  98. grep 'source /etc/profile.d/elasticsearch_prompt.sh' ~vagrant/.bashrc |
  99. cat \<\<SOURCE_PROMPT >> ~vagrant/.bashrc
  100. # Replace the standard prompt with a consistent one
  101. source /etc/profile.d/elasticsearch_prompt.sh
  102. SOURCE_PROMPT
  103. SHELL
  104. # Creates a file to mark the machine as created by vagrant. Tests check
  105. # for this file and refuse to run if it is not present so that they can't
  106. # be run unexpectedly.
  107. config.vm.provision "markerfile", type: "shell", inline: <<-SHELL
  108. touch /etc/is_vagrant_vm
  109. SHELL
  110. end
  111. config.config_procs.push ['2', set_prompt]
  112. end
  113. end
  114. def ubuntu_common(config, extra: '')
  115. deb_common config, extra: extra
  116. end
  117. def deb_common(config, extra: '')
  118. # http://foo-o-rama.com/vagrant--stdin-is-not-a-tty--fix.html
  119. config.vm.provision "fix-no-tty", type: "shell" do |s|
  120. s.privileged = false
  121. s.inline = "sudo sed -i '/tty/!s/mesg n/tty -s \\&\\& mesg n/' /root/.profile"
  122. end
  123. provision(config,
  124. update_command: "apt-get update",
  125. update_tracking_file: "/var/cache/apt/archives/last_update",
  126. install_command: "apt-get install -y",
  127. extra: extra)
  128. end
  129. def rpm_common(config)
  130. provision(config,
  131. update_command: "yum check-update",
  132. update_tracking_file: "/var/cache/yum/last_update",
  133. install_command: "yum install -y")
  134. end
  135. def dnf_common(config)
  136. provision(config,
  137. update_command: "dnf check-update",
  138. update_tracking_file: "/var/cache/dnf/last_update",
  139. install_command: "dnf install -y",
  140. install_command_retries: 5)
  141. if Vagrant.has_plugin?("vagrant-cachier")
  142. # Autodetect doesn't work....
  143. config.cache.auto_detect = false
  144. config.cache.enable :generic, { :cache_dir => "/var/cache/dnf" }
  145. end
  146. end
  147. def opensuse_common(config)
  148. suse_common config, ''
  149. end
  150. def suse_common(config, extra)
  151. provision(config,
  152. update_command: "zypper --non-interactive list-updates",
  153. update_tracking_file: "/var/cache/zypp/packages/last_update",
  154. install_command: "zypper --non-interactive --quiet install --no-recommends",
  155. extra: extra)
  156. end
  157. def sles_common(config)
  158. extra = <<-SHELL
  159. zypper rr systemsmanagement_puppet puppetlabs-pc1
  160. zypper --non-interactive install git-core
  161. SHELL
  162. suse_common config, extra
  163. end
  164. # Register the main box provisioning script.
  165. # @param config Vagrant's config object. Required.
  166. # @param update_command [String] The command used to update the package
  167. # manager. Required. Think `apt-get update`.
  168. # @param update_tracking_file [String] The location of the file tracking the
  169. # last time the update command was run. Required. Should be in a place that
  170. # is cached by vagrant-cachier.
  171. # @param install_command [String] The command used to install a package.
  172. # Required. Think `apt-get install #{package}`.
  173. # @param extra [String] Extra provisioning commands run before anything else.
  174. # Optional. Used for things like setting up the ppa for Java 8.
  175. def provision(config,
  176. update_command: 'required',
  177. update_tracking_file: 'required',
  178. install_command: 'required',
  179. install_command_retries: 0,
  180. extra: '')
  181. # Vagrant run ruby 2.0.0 which doesn't have required named parameters....
  182. raise ArgumentError.new('update_command is required') if update_command == 'required'
  183. raise ArgumentError.new('update_tracking_file is required') if update_tracking_file == 'required'
  184. raise ArgumentError.new('install_command is required') if install_command == 'required'
  185. config.vm.provider "virtualbox" do |v|
  186. # Give the box more memory and cpu because our tests are beasts!
  187. v.memory = Integer(ENV['VAGRANT_MEMORY'] || 8192)
  188. v.cpus = Integer(ENV['VAGRANT_CPUS'] || 4)
  189. end
  190. config.vm.synced_folder "#{Dir.home}/.gradle/caches", "/home/vagrant/.gradle/caches",
  191. create: true,
  192. owner: "vagrant"
  193. config.vm.provision "dependencies", type: "shell", inline: <<-SHELL
  194. set -e
  195. set -o pipefail
  196. # Retry install command up to $2 times, if failed
  197. retry_installcommand() {
  198. n=0
  199. while true; do
  200. #{install_command} $1 && break
  201. let n=n+1
  202. if [ $n -ge $2 ]; then
  203. echo "==> Exhausted retries to install $1"
  204. return 1
  205. fi
  206. echo "==> Retrying installing $1, attempt $((n+1))"
  207. # Add a small delay to increase chance of metalink providing updated list of mirrors
  208. sleep 5
  209. done
  210. }
  211. installed() {
  212. command -v $1 2>&1 >/dev/null
  213. }
  214. install() {
  215. # Only apt-get update if we haven't in the last day
  216. if [ ! -f #{update_tracking_file} ] || [ "x$(find #{update_tracking_file} -mtime +0)" == "x#{update_tracking_file}" ]; then
  217. echo "==> Updating repository"
  218. #{update_command} || true
  219. touch #{update_tracking_file}
  220. fi
  221. echo "==> Installing $1"
  222. if [ #{install_command_retries} -eq 0 ]
  223. then
  224. #{install_command} $1
  225. else
  226. retry_installcommand $1 #{install_command_retries}
  227. fi
  228. }
  229. ensure() {
  230. installed $1 || install $1
  231. }
  232. #{extra}
  233. installed java || {
  234. echo "==> Java is not installed on vagrant box ${config.vm.box}"
  235. return 1
  236. }
  237. ensure tar
  238. ensure curl
  239. ensure unzip
  240. installed bats || {
  241. # Bats lives in a git repository....
  242. ensure git
  243. echo "==> Installing bats"
  244. git clone https://github.com/sstephenson/bats /tmp/bats
  245. # Centos doesn't add /usr/local/bin to the path....
  246. /tmp/bats/install.sh /usr
  247. rm -rf /tmp/bats
  248. }
  249. installed gradle || {
  250. echo "==> Installing Gradle"
  251. curl -sS -o /tmp/gradle.zip -L https://services.gradle.org/distributions/gradle-3.3-bin.zip
  252. unzip /tmp/gradle.zip -d /opt
  253. rm -rf /tmp/gradle.zip
  254. ln -s /opt/gradle-3.3/bin/gradle /usr/bin/gradle
  255. # make nfs mounted gradle home dir writeable
  256. chown vagrant:vagrant /home/vagrant/.gradle
  257. }
  258. cat \<\<VARS > /etc/profile.d/elasticsearch_vars.sh
  259. export ZIP=/elasticsearch/distribution/zip/build/distributions
  260. export TAR=/elasticsearch/distribution/tar/build/distributions
  261. export RPM=/elasticsearch/distribution/rpm/build/distributions
  262. export DEB=/elasticsearch/distribution/deb/build/distributions
  263. export BATS=/project/build/bats
  264. export BATS_UTILS=/project/build/bats/utils
  265. export BATS_TESTS=/project/build/bats/tests
  266. export BATS_ARCHIVES=/project/build/bats/archives
  267. export GRADLE_HOME=/opt/gradle-3.3
  268. VARS
  269. cat \<\<SUDOERS_VARS > /etc/sudoers.d/elasticsearch_vars
  270. Defaults env_keep += "ZIP"
  271. Defaults env_keep += "TAR"
  272. Defaults env_keep += "RPM"
  273. Defaults env_keep += "DEB"
  274. Defaults env_keep += "BATS"
  275. Defaults env_keep += "BATS_UTILS"
  276. Defaults env_keep += "BATS_TESTS"
  277. Defaults env_keep += "BATS_ARCHIVES"
  278. SUDOERS_VARS
  279. chmod 0440 /etc/sudoers.d/elasticsearch_vars
  280. SHELL
  281. end