|
1 class Homestead |
|
2 def Homestead.configure(config, settings) |
|
3 # Set The VM Provider |
|
4 ENV['VAGRANT_DEFAULT_PROVIDER'] = settings["provider"] ||= "virtualbox" |
|
5 |
|
6 # Configure Local Variable To Access Scripts From Remote Location |
|
7 scriptDir = File.dirname(__FILE__) |
|
8 |
|
9 # Prevent TTY Errors |
|
10 config.ssh.shell = "bash -c 'BASH_ENV=/etc/profile exec bash'" |
|
11 |
|
12 # Configure The Box |
|
13 config.vm.box = "laravel/homestead" |
|
14 config.vm.hostname = "homestead" |
|
15 |
|
16 # Configure A Private Network IP |
|
17 config.vm.network :private_network, ip: settings["ip"] ||= "192.168.10.10" |
|
18 |
|
19 # Configure A Few VirtualBox Settings |
|
20 config.vm.provider "virtualbox" do |vb| |
|
21 vb.name = 'homestead' |
|
22 vb.customize ["modifyvm", :id, "--memory", settings["memory"] ||= "2048"] |
|
23 vb.customize ["modifyvm", :id, "--cpus", settings["cpus"] ||= "1"] |
|
24 vb.customize ["modifyvm", :id, "--natdnsproxy1", "on"] |
|
25 vb.customize ["modifyvm", :id, "--natdnshostresolver1", "on"] |
|
26 vb.customize ["modifyvm", :id, "--ostype", "Ubuntu_64"] |
|
27 end |
|
28 |
|
29 # Configure A Few VMware Settings |
|
30 ["vmware_fusion", "vmware_workstation"].each do |vmware| |
|
31 config.vm.provider vmware do |v| |
|
32 v.vmx["displayName"] = "homestead" |
|
33 v.vmx["memsize"] = settings["memory"] ||= 2048 |
|
34 v.vmx["numvcpus"] = settings["cpus"] ||= 1 |
|
35 v.vmx["guestOS"] = "ubuntu-64" |
|
36 end |
|
37 end |
|
38 |
|
39 # Configure Port Forwarding To The Box |
|
40 config.vm.network "forwarded_port", guest: 80, host: 8000 |
|
41 config.vm.network "forwarded_port", guest: 443, host: 44300 |
|
42 config.vm.network "forwarded_port", guest: 3306, host: 33060 |
|
43 config.vm.network "forwarded_port", guest: 5432, host: 54320 |
|
44 |
|
45 # Add Custom Ports From Configuration |
|
46 if settings.has_key?("ports") |
|
47 settings["ports"].each do |port| |
|
48 config.vm.network "forwarded_port", guest: port["guest"] || port["to"], host: port["host"] || port["send"], protocol: port["protocol"] ||= "tcp" |
|
49 end |
|
50 end |
|
51 |
|
52 # Configure The Public Key For SSH Access |
|
53 if settings.include? 'authorize' |
|
54 config.vm.provision "shell" do |s| |
|
55 s.inline = "echo $1 | grep -xq \"$1\" /home/vagrant/.ssh/authorized_keys || echo $1 | tee -a /home/vagrant/.ssh/authorized_keys" |
|
56 s.args = [File.read(File.expand_path(settings["authorize"]))] |
|
57 end |
|
58 end |
|
59 |
|
60 # Copy The SSH Private Keys To The Box |
|
61 if settings.include? 'keys' |
|
62 settings["keys"].each do |key| |
|
63 config.vm.provision "shell" do |s| |
|
64 s.privileged = false |
|
65 s.inline = "echo \"$1\" > /home/vagrant/.ssh/$2 && chmod 600 /home/vagrant/.ssh/$2" |
|
66 s.args = [File.read(File.expand_path(key)), key.split('/').last] |
|
67 end |
|
68 end |
|
69 end |
|
70 |
|
71 # Register All Of The Configured Shared Folders |
|
72 if settings.include? 'folders' |
|
73 settings["folders"].each do |folder| |
|
74 mount_opts = folder["type"] == "nfs" ? ['actimeo=1'] : [] |
|
75 config.vm.synced_folder folder["map"], folder["to"], type: folder["type"] ||= nil, mount_options: mount_opts |
|
76 end |
|
77 end |
|
78 |
|
79 # Install All The Configured Nginx Sites |
|
80 settings["sites"].each do |site| |
|
81 config.vm.provision "shell" do |s| |
|
82 if (site.has_key?("hhvm") && site["hhvm"]) |
|
83 s.path = scriptDir + "/serve-hhvm.sh" |
|
84 s.args = [site["map"], site["to"], site["port"] ||= "80", site["ssl"] ||= "443"] |
|
85 else |
|
86 s.path = scriptDir + "/serve.sh" |
|
87 s.args = [site["map"], site["to"], site["port"] ||= "80", site["ssl"] ||= "443"] |
|
88 end |
|
89 end |
|
90 end |
|
91 |
|
92 # Configure All Of The Configured Databases |
|
93 settings["databases"].each do |db| |
|
94 config.vm.provision "shell" do |s| |
|
95 s.path = scriptDir + "/create-mysql.sh" |
|
96 s.args = [db] |
|
97 end |
|
98 |
|
99 config.vm.provision "shell" do |s| |
|
100 s.path = scriptDir + "/create-postgres.sh" |
|
101 s.args = [db] |
|
102 end |
|
103 end |
|
104 |
|
105 # Configure All Of The Server Environment Variables |
|
106 if settings.has_key?("variables") |
|
107 settings["variables"].each do |var| |
|
108 config.vm.provision "shell" do |s| |
|
109 s.inline = "echo \"\nenv[$1] = '$2'\" >> /etc/php5/fpm/php-fpm.conf" |
|
110 s.args = [var["key"], var["value"]] |
|
111 end |
|
112 |
|
113 config.vm.provision "shell" do |s| |
|
114 s.inline = "echo \"\n#Set Homestead environment variable\nexport $1=$2\" >> /home/vagrant/.profile" |
|
115 s.args = [var["key"], var["value"]] |
|
116 end |
|
117 end |
|
118 |
|
119 config.vm.provision "shell" do |s| |
|
120 s.inline = "service php5-fpm restart" |
|
121 end |
|
122 end |
|
123 |
|
124 # Update Composer On Every Provision |
|
125 config.vm.provision "shell" do |s| |
|
126 s.inline = "/usr/local/bin/composer self-update" |
|
127 end |
|
128 |
|
129 # Configure Blackfire.io |
|
130 if settings.has_key?("blackfire") |
|
131 config.vm.provision "shell" do |s| |
|
132 s.path = scriptDir + "/blackfire.sh" |
|
133 s.args = [ |
|
134 settings["blackfire"][0]["id"], |
|
135 settings["blackfire"][0]["token"], |
|
136 settings["blackfire"][0]["client-id"], |
|
137 settings["blackfire"][0]["client-token"] |
|
138 ] |
|
139 end |
|
140 end |
|
141 end |
|
142 end |