|
| 1 | +# |
| 2 | +# WPLib Box Bootstrapping Script |
| 3 | +# |
| 4 | +# @example |
| 5 | +# b = Box.new(Vagrant) |
| 6 | +# |
| 7 | +# puts b.filepath |
| 8 | +# puts b.hostname |
| 9 | +# puts b.box_version |
| 10 | +# |
| 11 | +# b = Box.new() |
| 12 | +# b.prerequisites() |
| 13 | +# b.add_aliases([ |
| 14 | +# "www.{hostname}", |
| 15 | +# "adminer.{hostname}", |
| 16 | +# "mailhog.{hostname}", |
| 17 | +# "docs.{hostname}", |
| 18 | +# ]) |
| 19 | +# b.startup(config) |
| 20 | +# |
| 21 | + |
| 22 | +require 'json' |
| 23 | + |
| 24 | +class Box |
| 25 | + attr_accessor :filepath |
| 26 | + |
| 27 | + def hostname() |
| 28 | + @project['hostname'] |
| 29 | + end |
| 30 | + |
| 31 | + def box_version() |
| 32 | + @project['localdev']['version'] |
| 33 | + end |
| 34 | + |
| 35 | + def add_aliases(aliases) |
| 36 | + @project['aliases'] = aliases |
| 37 | + end |
| 38 | + |
| 39 | + def initialize() |
| 40 | + @filepath = File.expand_path "project.json" |
| 41 | + if File.exists?(@filepath) |
| 42 | + @project = JSON.parse(File.read(@filepath)) |
| 43 | + else |
| 44 | + @project = {} |
| 45 | + @project['readme'] = [] |
| 46 | + @project['readme'] << "This file allows you to configure WPLib Box." |
| 47 | + @project['readme'] << "Find available components by typing 'vagrant ssh' in terminal followed by 'box component available'." |
| 48 | + @project['readme'] << "You can delete this `readme` section assuming it is still valid JSON after deletion." |
| 49 | + @project['hostname'] = "wplib.box" |
| 50 | + @project['aliases'] = %w( docs.{hostname} www.{hostname} adminer.{hostname} mailhog.{hostname} ) |
| 51 | + @project['localdev'] = {} |
| 52 | + @project['localdev']['name'] = "WPLib Box" |
| 53 | + @project['localdev']['version'] = "0.17.1" |
| 54 | + @project['localdev']['website'] = "http://wplib.org/box/" |
| 55 | + @project['stack'] = {} |
| 56 | + @project['stack']['wordpress/dbserver'] = 'wplib/mysql:5.5.60' |
| 57 | + @project['stack']['wordpress/webserver'] = 'wplib/nginx:1.14.0' |
| 58 | + @project['stack']['wordpress/processvm'] = 'wplib/php:7.1.18' |
| 59 | + @project['stack']['wordpress/cacheserver'] = 'wplib/redis:4.0.9' |
| 60 | + @project['stack']['mkdocs/webserver'] = 'wplib/mkdocs:0.15.3' |
| 61 | + @project['stack']['box/mailsender'] = 'wplib/mailhog:1.0.0' |
| 62 | + @project['stack']['box/webproxy'] = 'wplib/proxy:1.14.0' |
| 63 | + @project['stack']['box/sqladmin'] = 'wplib/adminer:4.6.2' |
| 64 | + File.write(@filepath,JSON.pretty_generate(@project)) |
| 65 | + end |
| 66 | + @project |
| 67 | + end |
| 68 | + |
| 69 | + def prerequisites() |
| 70 | + Vagrant.require_version ">= 2.1" |
| 71 | + |
| 72 | + vboxmanage = Vagrant::Util::Which.which("VBoxManage") || Vagrant::Util::Which.which("VBoxManage.exe") |
| 73 | + if vboxmanage == nil |
| 74 | + abort "\nWPLib Box could not find VirtualBox 5.2+. If you know it is installed please\n" \ |
| 75 | + "check your path to ensure we can find it. If not installed, please download\n" \ |
| 76 | + "version 5.2 or greater from:\n\n\thttps://www.virtualbox.org/wiki/Downloads\n" |
| 77 | + else |
| 78 | + version = Vagrant::Util::Subprocess.execute(vboxmanage, '--version') |
| 79 | + version = Gem::Version.create(version.stdout.strip!) |
| 80 | + unless version >= Gem::Version.create('5.2') |
| 81 | + abort "\nWPLib Box needs VirtualBox 5.2 or greater. Your current version is " + version.version + "\n" \ |
| 82 | + "Please download a newer version of VirtualBox from:\n\n\thttps://www.virtualbox.org/wiki/Downloads\n" |
| 83 | + end |
| 84 | + end |
| 85 | + |
| 86 | + system "vagrant plugin install vagrant-hostsupdater" \ |
| 87 | + unless Vagrant.has_plugin? "vagrant-hostsupdater" |
| 88 | + |
| 89 | + end |
| 90 | + |
| 91 | + def start(config) |
| 92 | + |
| 93 | + config.vm.box = "wplib/wplib" |
| 94 | + config.vm.box_version = box_version |
| 95 | + |
| 96 | + config.vm.hostname = hostname |
| 97 | + |
| 98 | + config.hostsupdater.aliases = @project['aliases'] |
| 99 | + config.hostsupdater.aliases.map! do |_alias| |
| 100 | + _alias.sub! '{hostname}', hostname |
| 101 | + end |
| 102 | + |
| 103 | + config.vm.provider :vmware_fusion do |vmware| |
| 104 | + vmware.vmx["ethernet0.pcislotnumber"] = "33" |
| 105 | + end |
| 106 | + |
| 107 | + File.write('IP', "10.10.10.#{rand(10..250)}") if not File.exists?('IP') |
| 108 | + config.vm.network 'private_network', ip: IO.read('IP').strip |
| 109 | + |
| 110 | + if Gem::Version.new(box_version) >= Gem::Version.new("0.17.0") |
| 111 | + # 0.17.0 and onwards. |
| 112 | + config.vm.synced_folder ".", "/vagrant", disabled: true |
| 113 | + config.vm.synced_folder ".", "/projects/wplib.box" |
| 114 | + config.ssh.username = "boxuser" |
| 115 | + else |
| 116 | + #pre-0.17.0. |
| 117 | + config.vm.synced_folder "www", "/var/www" |
| 118 | + config.ssh.username = "vagrant" |
| 119 | + end |
| 120 | + |
| 121 | + config.ssh.forward_agent = true |
| 122 | + config.ssh.insert_key = false |
| 123 | + |
| 124 | + config.trigger.after [:up, :reload] do |trigger| |
| 125 | + trigger.run_remote = {inline: "box first-time-provision --short"} |
| 126 | + end |
| 127 | + |
| 128 | + config.trigger.after [:up, :reload] do |trigger| |
| 129 | + trigger.run_remote = {inline: "box startup --short"} |
| 130 | + end |
| 131 | + |
| 132 | + config.trigger.before :halt do |trigger| |
| 133 | + trigger.run_remote = {inline: "box database backup"} |
| 134 | + end |
| 135 | + end |
| 136 | + |
| 137 | + |
| 138 | +end |
| 139 | + |
0 commit comments