Vagrantfile 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. require 'yaml'
  2. require 'fileutils'
  3. required_plugins_installed = nil
  4. required_plugins = %w( vagrant-hostmanager vagrant-vbguest )
  5. required_plugins.each do |plugin|
  6. unless Vagrant.has_plugin? plugin
  7. system "vagrant plugin install #{plugin}"
  8. required_plugins_installed = true
  9. end
  10. end
  11. # IF plugin[s] was just installed - restart required
  12. if required_plugins_installed
  13. # Get CLI command[s] and call again
  14. system 'vagrant' + ARGV.to_s.gsub(/\[\"|\", \"|\"\]/, ' ')
  15. exit
  16. end
  17. domains = {
  18. frontend: 'y2aa-frontend.test',
  19. backend: 'y2aa-backend.test'
  20. }
  21. config = {
  22. local: './vagrant/config/vagrant-local.yml',
  23. example: './vagrant/config/vagrant-local.example.yml'
  24. }
  25. # copy config from example if local config not exists
  26. FileUtils.cp config[:example], config[:local] unless File.exist?(config[:local])
  27. # read config
  28. options = YAML.load_file config[:local]
  29. # check github token
  30. if options['github_token'].nil? || options['github_token'].to_s.length != 40
  31. puts "You must place REAL GitHub token into configuration:\n/yii2-app-advanced/vagrant/config/vagrant-local.yml"
  32. exit
  33. end
  34. # vagrant configurate
  35. Vagrant.configure(2) do |config|
  36. # select the box
  37. config.vm.box = 'bento/ubuntu-18.04'
  38. # should we ask about box updates?
  39. config.vm.box_check_update = options['box_check_update']
  40. config.vm.provider 'virtualbox' do |vb|
  41. # machine cpus count
  42. vb.cpus = options['cpus']
  43. # machine memory size
  44. vb.memory = options['memory']
  45. # machine name (for VirtualBox UI)
  46. vb.name = options['machine_name']
  47. end
  48. # machine name (for vagrant console)
  49. config.vm.define options['machine_name']
  50. # machine name (for guest machine console)
  51. config.vm.hostname = options['machine_name']
  52. # network settings
  53. config.vm.network 'private_network', ip: options['ip']
  54. # sync: folder 'yii2-app-advanced' (host machine) -> folder '/app' (guest machine)
  55. config.vm.synced_folder './', '/app', owner: 'vagrant', group: 'vagrant'
  56. # disable folder '/vagrant' (guest machine)
  57. config.vm.synced_folder '.', '/vagrant', disabled: true
  58. # hosts settings (host machine)
  59. config.vm.provision :hostmanager
  60. config.hostmanager.enabled = true
  61. config.hostmanager.manage_host = true
  62. config.hostmanager.ignore_private_ip = false
  63. config.hostmanager.include_offline = true
  64. config.hostmanager.aliases = domains.values
  65. # provisioners
  66. config.vm.provision 'shell', path: './vagrant/provision/once-as-root.sh', args: [options['timezone'], options['ip']]
  67. config.vm.provision 'shell', path: './vagrant/provision/once-as-vagrant.sh', args: [options['github_token']], privileged: false
  68. config.vm.provision 'shell', path: './vagrant/provision/always-as-root.sh', run: 'always'
  69. # post-install message (vagrant console)
  70. config.vm.post_up_message = "Frontend URL: http://#{domains[:frontend]}\nBackend URL: http://#{domains[:backend]}"
  71. end