Skip to content

Instantly share code, notes, and snippets.

@Paldom
Created December 16, 2015 10:02
Show Gist options
  • Save Paldom/54f8b437dc4c918278dc to your computer and use it in GitHub Desktop.
Save Paldom/54f8b437dc4c918278dc to your computer and use it in GitHub Desktop.
Homestead/Vagrant initializer that opens config files and share your default site on your local IP.
#!/bin/bash
echo "Opening folder ~/code"
open ~/code
echo "Opening file ~/.homestead/Homestead.yaml"
open -e ~/.homestead/Homestead.yaml
echo "Opening file ~/Homestead/Vagrantfile"
open -e ~/Homestead/Vagrantfile
echo "Perform vagrant up"
cd Homestead
vagrant up
echo "Vagrant cheat sheeet: vagrant up, vagrant halt, vagrant ssh"
echo "New host: vagrant provision"
echo "Default Host: serve domain.app /home/vagrant/Code/path/to/public/directory 80"
echo "Host Domain: /etc/hosts"
require 'json'
require 'yaml'
VAGRANTFILE_API_VERSION = "2"
confDir = $confDir ||= File.expand_path("~/.homestead")
homesteadYamlPath = confDir + "/Homestead.yaml"
homesteadJsonPath = confDir + "/Homestead.json"
afterScriptPath = confDir + "/after.sh"
aliasesPath = confDir + "/aliases"
require File.expand_path(File.dirname(__FILE__) + '/scripts/homestead.rb')
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
if File.exists? aliasesPath then
config.vm.provision "file", source: aliasesPath, destination: "~/.bash_aliases"
end
if File.exists? homesteadYamlPath then
Homestead.configure(config, YAML::load(File.read(homesteadYamlPath)))
elsif File.exists? homesteadJsonPath then
Homestead.configure(config, JSON.parse(File.read(homesteadJsonPath)))
end
if File.exists? afterScriptPath then
config.vm.provision "shell", path: afterScriptPath
end
config.vm.network "forwarded_port", guest: 80, host: 8000 # HTTP
config.vm.network "forwarded_port", guest: 8080, host: 8333 # HTTP
config.trigger.after [:provision, :up, :reload] do
system('echo "
rdr pass on lo0 inet proto tcp from any to 127.0.0.1 port 80 -> 127.0.0.1 port 8000
rdr pass on lo0 inet proto tcp from any to 127.0.0.1 port 8080 -> 127.0.0.1 port 8333
" | sudo pfctl -ef - > /dev/null 2>&1; echo "==> Fowarding Ports: 80 -> 8000, 8080 -> 8333 & Enabling pf"')
end
config.trigger.after [:halt, :destroy] do
system("sudo pfctl -df /etc/pf.conf > /dev/null 2>&1; echo '==> Removing Port Forwarding & Disabling pf'")
end
#config.vm.network :forwarded_port, guest: 8080, host: 8080
#config.vm.forward_port 8080, 8080
end
@wouldhide
Copy link

Before using the script you have to install vagrant triggers plugin:

vagrant plugin install vagrant-triggers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment