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