Vagrantfile 13 KB

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