Vagrantfile 13 KB

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