Vagrantfile 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. app: 'yii2basic.test'
  19. }
  20. vagrantfile_dir_path = File.dirname(__FILE__)
  21. config = {
  22. local: vagrantfile_dir_path + '/vagrant/config/vagrant-local.yml',
  23. example: vagrantfile_dir_path + '/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-basic/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. # quick fix for failed guest additions installations
  66. # config.vbguest.auto_update = false
  67. # provisioners
  68. config.vm.provision 'shell', path: './vagrant/provision/once-as-root.sh', args: [options['timezone'], options['ip']]
  69. config.vm.provision 'shell', path: './vagrant/provision/once-as-vagrant.sh', args: [options['github_token']], privileged: false
  70. config.vm.provision 'shell', path: './vagrant/provision/always-as-root.sh', run: 'always'
  71. # post-install message (vagrant console)
  72. config.vm.post_up_message = "App URL: http://#{domains[:app]}"
  73. end