Vagrantfile 2.7 KB

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