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