Vagrantfile 13 KB

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