Vagrantfile 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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. define_opts = {
  22. autostart: false
  23. }.freeze
  24. Vagrant.configure(2) do |config|
  25. config.vm.provider 'virtualbox' do |vbox|
  26. # Give the box more memory and cpu because our tests are beasts!
  27. vbox.memory = Integer(ENV['VAGRANT_MEMORY'] || 8192)
  28. vbox.cpus = Integer(ENV['VAGRANT_CPUS'] || 4)
  29. # see https://github.com/hashicorp/vagrant/issues/9524
  30. vbox.customize ["modifyvm", :id, "--audio", "none"]
  31. end
  32. # Switch the default share for the project root from /vagrant to
  33. # /elasticsearch because /vagrant is confusing when there is a project inside
  34. # the elasticsearch project called vagrant....
  35. config.vm.synced_folder '.', '/vagrant', disabled: true
  36. config.vm.synced_folder '.', '/elasticsearch'
  37. # Expose project directory. Note that VAGRANT_CWD may not be the same as Dir.pwd
  38. PROJECT_DIR = ENV['VAGRANT_PROJECT_DIR'] || Dir.pwd
  39. config.vm.synced_folder PROJECT_DIR, '/project'
  40. 'ubuntu-1604'.tap do |box|
  41. config.vm.define box, define_opts do |config|
  42. config.vm.box = 'elastic/ubuntu-16.04-x86_64'
  43. deb_common config, box, extra: <<-SHELL
  44. # Install Jayatana so we can work around it being present.
  45. [ -f /usr/share/java/jayatanaag.jar ] || install jayatana
  46. SHELL
  47. end
  48. end
  49. 'ubuntu-1804'.tap do |box|
  50. config.vm.define box, define_opts do |config|
  51. config.vm.box = 'elastic/ubuntu-18.04-x86_64'
  52. deb_common config, box, extra: <<-SHELL
  53. # Install Jayatana so we can work around it being present.
  54. [ -f /usr/share/java/jayatanaag.jar ] || install jayatana
  55. SHELL
  56. end
  57. end
  58. # Wheezy's backports don't contain Openjdk 8 and the backflips
  59. # required to get the sun jdk on there just aren't worth it. We have
  60. # jessie and stretch for testing debian and it works fine.
  61. 'debian-8'.tap do |box|
  62. config.vm.define box, define_opts do |config|
  63. config.vm.box = 'elastic/debian-8-x86_64'
  64. deb_common config, box
  65. end
  66. end
  67. 'debian-9'.tap do |box|
  68. config.vm.define box, define_opts do |config|
  69. config.vm.box = 'elastic/debian-9-x86_64'
  70. deb_common config, box
  71. end
  72. end
  73. 'centos-6'.tap do |box|
  74. config.vm.define box, define_opts do |config|
  75. config.vm.box = 'elastic/centos-6-x86_64'
  76. rpm_common config, box
  77. end
  78. end
  79. 'centos-7'.tap do |box|
  80. config.vm.define box, define_opts do |config|
  81. config.vm.box = 'elastic/centos-7-x86_64'
  82. rpm_common config, box
  83. end
  84. end
  85. 'oel-6'.tap do |box|
  86. config.vm.define box, define_opts do |config|
  87. config.vm.box = 'elastic/oraclelinux-6-x86_64'
  88. rpm_common config, box
  89. end
  90. end
  91. 'oel-7'.tap do |box|
  92. config.vm.define box, define_opts do |config|
  93. config.vm.box = 'elastic/oraclelinux-7-x86_64'
  94. rpm_common config, box
  95. end
  96. end
  97. 'fedora-28'.tap do |box|
  98. config.vm.define box, define_opts do |config|
  99. config.vm.box = 'elastic/fedora-28-x86_64'
  100. dnf_common config, box
  101. end
  102. end
  103. 'fedora-29'.tap do |box|
  104. config.vm.define box, define_opts do |config|
  105. config.vm.box = 'elastic/fedora-28-x86_64'
  106. dnf_common config, box
  107. end
  108. end
  109. 'opensuse-42'.tap do |box|
  110. config.vm.define box, define_opts do |config|
  111. config.vm.box = 'elastic/opensuse-42-x86_64'
  112. suse_common config, box
  113. end
  114. end
  115. 'sles-12'.tap do |box|
  116. config.vm.define box, define_opts do |config|
  117. config.vm.box = 'elastic/sles-12-x86_64'
  118. sles_common config, box
  119. end
  120. end
  121. windows_2012r2_box = ENV['VAGRANT_WINDOWS_2012R2_BOX']
  122. if windows_2012r2_box && windows_2012r2_box.empty? == false
  123. 'windows-2012r2'.tap do |box|
  124. config.vm.define box, define_opts do |config|
  125. config.vm.box = windows_2012r2_box
  126. windows_common config, box
  127. end
  128. end
  129. end
  130. windows_2016_box = ENV['VAGRANT_WINDOWS_2016_BOX']
  131. if windows_2016_box && windows_2016_box.empty? == false
  132. 'windows-2016'.tap do |box|
  133. config.vm.define box, define_opts do |config|
  134. config.vm.box = windows_2016_box
  135. windows_common config, box
  136. end
  137. end
  138. end
  139. end
  140. def deb_common(config, name, extra: '')
  141. # http://foo-o-rama.com/vagrant--stdin-is-not-a-tty--fix.html
  142. config.vm.provision 'fix-no-tty', type: 'shell' do |s|
  143. s.privileged = false
  144. s.inline = "sudo sed -i '/tty/!s/mesg n/tty -s \\&\\& mesg n/' /root/.profile"
  145. end
  146. linux_common(
  147. config,
  148. name,
  149. update_command: 'apt-get update',
  150. update_tracking_file: '/var/cache/apt/archives/last_update',
  151. install_command: 'apt-get install -y',
  152. extra: extra
  153. )
  154. end
  155. def rpm_common(config, name)
  156. linux_common(
  157. config,
  158. name,
  159. update_command: 'yum check-update',
  160. update_tracking_file: '/var/cache/yum/last_update',
  161. install_command: 'yum install -y'
  162. )
  163. end
  164. def dnf_common(config, name)
  165. # Autodetect doesn't work....
  166. if Vagrant.has_plugin?('vagrant-cachier')
  167. config.cache.auto_detect = false
  168. config.cache.enable :generic, { :cache_dir => '/var/cache/dnf' }
  169. end
  170. linux_common(
  171. config,
  172. name,
  173. update_command: 'dnf check-update',
  174. update_tracking_file: '/var/cache/dnf/last_update',
  175. install_command: 'dnf install -y',
  176. install_command_retries: 5
  177. )
  178. end
  179. def suse_common(config, name, extra: '')
  180. linux_common(
  181. config,
  182. name,
  183. update_command: 'zypper --non-interactive list-updates',
  184. update_tracking_file: '/var/cache/zypp/packages/last_update',
  185. install_command: 'zypper --non-interactive --quiet install --no-recommends',
  186. extra: extra
  187. )
  188. end
  189. def sles_common(config, name)
  190. extra = <<-SHELL
  191. zypper rr systemsmanagement_puppet puppetlabs-pc1
  192. zypper --non-interactive install git-core
  193. SHELL
  194. suse_common config, name, extra: extra
  195. end
  196. # Configuration needed for all linux boxes
  197. # @param config Vagrant's config object. Required.
  198. # @param name [String] The box name. Required.
  199. # @param update_command [String] The command used to update the package
  200. # manager. Required. Think `apt-get update`.
  201. # @param update_tracking_file [String] The location of the file tracking the
  202. # last time the update command was run. Required. Should be in a place that
  203. # is cached by vagrant-cachier.
  204. # @param install_command [String] The command used to install a package.
  205. # Required. Think `apt-get install #{package}`.
  206. # @param install_command_retries [Integer] Number of times to retry
  207. # a failed install command
  208. # @param extra [String] Additional script to run before installing
  209. # dependencies
  210. #
  211. def linux_common(config,
  212. name,
  213. update_command: 'required',
  214. update_tracking_file: 'required',
  215. install_command: 'required',
  216. install_command_retries: 0,
  217. extra: '')
  218. raise ArgumentError, 'update_command is required' if update_command == 'required'
  219. raise ArgumentError, 'update_tracking_file is required' if update_tracking_file == 'required'
  220. raise ArgumentError, 'install_command is required' if install_command == 'required'
  221. if Vagrant.has_plugin?('vagrant-cachier')
  222. config.cache.scope = :box
  223. end
  224. config.vm.provision 'markerfile', type: 'shell', inline: <<-SHELL
  225. touch /etc/is_vagrant_vm
  226. touch /is_vagrant_vm # for consistency between linux and windows
  227. SHELL
  228. # This prevents leftovers from previous tests using the
  229. # same VM from messing up the current test
  230. config.vm.provision 'clean es installs in tmp', run: 'always', type: 'shell', inline: <<-SHELL
  231. rm -rf /tmp/elasticsearch*
  232. SHELL
  233. sh_set_prompt config, name
  234. sh_install_deps(
  235. config,
  236. update_command,
  237. update_tracking_file,
  238. install_command,
  239. install_command_retries,
  240. extra
  241. )
  242. end
  243. # Sets up a consistent prompt for all users. Or tries to. The VM might
  244. # contain overrides for root and vagrant but this attempts to work around
  245. # them by re-source-ing the standard prompt file.
  246. def sh_set_prompt(config, name)
  247. config.vm.provision 'set prompt', type: 'shell', inline: <<-SHELL
  248. cat \<\<PROMPT > /etc/profile.d/elasticsearch_prompt.sh
  249. export PS1='#{name}:\\w$ '
  250. PROMPT
  251. grep 'source /etc/profile.d/elasticsearch_prompt.sh' ~/.bashrc |
  252. cat \<\<SOURCE_PROMPT >> ~/.bashrc
  253. # Replace the standard prompt with a consistent one
  254. source /etc/profile.d/elasticsearch_prompt.sh
  255. SOURCE_PROMPT
  256. grep 'source /etc/profile.d/elasticsearch_prompt.sh' ~vagrant/.bashrc |
  257. cat \<\<SOURCE_PROMPT >> ~vagrant/.bashrc
  258. # Replace the standard prompt with a consistent one
  259. source /etc/profile.d/elasticsearch_prompt.sh
  260. SOURCE_PROMPT
  261. SHELL
  262. end
  263. def sh_install_deps(config,
  264. update_command,
  265. update_tracking_file,
  266. install_command,
  267. install_command_retries,
  268. extra)
  269. config.vm.provision 'install dependencies', type: 'shell', inline: <<-SHELL
  270. set -e
  271. set -o pipefail
  272. # Retry install command up to $2 times, if failed
  273. retry_installcommand() {
  274. n=0
  275. while true; do
  276. #{install_command} $1 && break
  277. let n=n+1
  278. if [ $n -ge $2 ]; then
  279. echo "==> Exhausted retries to install $1"
  280. return 1
  281. fi
  282. echo "==> Retrying installing $1, attempt $((n+1))"
  283. # Add a small delay to increase chance of metalink providing updated list of mirrors
  284. sleep 5
  285. done
  286. }
  287. installed() {
  288. command -v $1 2>&1 >/dev/null
  289. }
  290. install() {
  291. # Only apt-get update if we haven't in the last day
  292. if [ ! -f #{update_tracking_file} ] || [ "x$(find #{update_tracking_file} -mtime +0)" == "x#{update_tracking_file}" ]; then
  293. echo "==> Updating repository"
  294. #{update_command} || true
  295. touch #{update_tracking_file}
  296. fi
  297. echo "==> Installing $1"
  298. if [ #{install_command_retries} -eq 0 ]
  299. then
  300. #{install_command} $1
  301. else
  302. retry_installcommand $1 #{install_command_retries}
  303. fi
  304. }
  305. ensure() {
  306. installed $1 || install $1
  307. }
  308. #{extra}
  309. installed java || {
  310. echo "==> Java is not installed"
  311. return 1
  312. }
  313. cat \<\<JAVA > /etc/profile.d/java_home.sh
  314. if [ -z "\\\$JAVA_HOME" ]; then
  315. export JAVA_HOME=$(dirname $(dirname $(readlink -f $(which java))))
  316. fi
  317. export SYSTEM_JAVA_HOME=\\\$JAVA_HOME
  318. unset JAVA_HOME
  319. JAVA
  320. ensure tar
  321. ensure curl
  322. ensure unzip
  323. ensure rsync
  324. installed bats || {
  325. # Bats lives in a git repository....
  326. ensure git
  327. echo "==> Installing bats"
  328. git clone https://github.com/sstephenson/bats /tmp/bats
  329. # Centos doesn't add /usr/local/bin to the path....
  330. /tmp/bats/install.sh /usr
  331. rm -rf /tmp/bats
  332. }
  333. cat \<\<VARS > /etc/profile.d/elasticsearch_vars.sh
  334. export ZIP=/elasticsearch/distribution/zip/build/distributions
  335. export TAR=/elasticsearch/distribution/tar/build/distributions
  336. export RPM=/elasticsearch/distribution/rpm/build/distributions
  337. export DEB=/elasticsearch/distribution/deb/build/distributions
  338. export BATS=/project/build/bats
  339. export BATS_UTILS=/project/build/packaging/bats/utils
  340. export BATS_TESTS=/project/build/packaging/bats/tests
  341. export PACKAGING_ARCHIVES=/project/build/packaging/archives
  342. export PACKAGING_TESTS=/project/build/packaging/tests
  343. VARS
  344. cat \<\<SUDOERS_VARS > /etc/sudoers.d/elasticsearch_vars
  345. Defaults env_keep += "ZIP"
  346. Defaults env_keep += "TAR"
  347. Defaults env_keep += "RPM"
  348. Defaults env_keep += "DEB"
  349. Defaults env_keep += "BATS"
  350. Defaults env_keep += "BATS_UTILS"
  351. Defaults env_keep += "BATS_TESTS"
  352. Defaults env_keep += "PACKAGING_ARCHIVES"
  353. Defaults env_keep += "PACKAGING_TESTS"
  354. Defaults env_keep += "JAVA_HOME"
  355. Defaults env_keep += "SYSTEM_JAVA_HOME"
  356. SUDOERS_VARS
  357. chmod 0440 /etc/sudoers.d/elasticsearch_vars
  358. SHELL
  359. end
  360. def windows_common(config, name)
  361. config.vm.provision 'markerfile', type: 'shell', inline: <<-SHELL
  362. $ErrorActionPreference = "Stop"
  363. New-Item C:/is_vagrant_vm -ItemType file -Force | Out-Null
  364. SHELL
  365. config.vm.provision 'set prompt', type: 'shell', inline: <<-SHELL
  366. $ErrorActionPreference = "Stop"
  367. $ps_prompt = 'function Prompt { "#{name}:$($ExecutionContext.SessionState.Path.CurrentLocation)>" }'
  368. $ps_prompt | Out-File $PsHome/Microsoft.PowerShell_profile.ps1
  369. SHELL
  370. config.vm.provision 'set env variables', type: 'shell', inline: <<-SHELL
  371. $ErrorActionPreference = "Stop"
  372. [Environment]::SetEnvironmentVariable("PACKAGING_ARCHIVES", "C:/project/build/packaging/archives", "Machine")
  373. $javaHome = [Environment]::GetEnvironmentVariable("JAVA_HOME", "Machine")
  374. [Environment]::SetEnvironmentVariable("SYSTEM_JAVA_HOME", $javaHome, "Machine")
  375. [Environment]::SetEnvironmentVariable("PACKAGING_TESTS", "C:/project/build/packaging/tests", "Machine")
  376. [Environment]::SetEnvironmentVariable("JAVA_HOME", $null, "Machine")
  377. SHELL
  378. end