lib/invoker/power/setup.rb in invoker-1.0.0 vs lib/invoker/power/setup.rb in invoker-1.0.1
- old
+ new
@@ -5,17 +5,27 @@
class Setup
RESOLVER_FILE = "/etc/resolver/dev"
FIREWALL_PLIST_FILE = "/Library/LaunchDaemons/com.codemancers.invoker.firewall.plist"
def self.install
installer = new
- unless installer.check_if_already_setup?
- installer.setup_invoker
+ if Invoker.darwin?
+ if installer.check_if_setup_can_run?
+ installer.setup_invoker
+ else
+ Invoker::Logger.puts("The setup has been already run.".color(:red))
+ end
else
- Invoker::Logger.puts("The setup has been already run.".color(:red))
+ Invoker::Logger.puts("Domain feature is currently not supported on systems other than OSX".color(:red))
end
+ installer
end
+ def self.uninstall
+ installer = new
+ installer.uninstall_invoker
+ end
+
def setup_invoker
if setup_resolver_file
find_open_ports
install_resolver(port_finder.dns_port)
install_firewall(port_finder.http_port)
@@ -27,10 +37,22 @@
Invoker::Logger.puts("Invoker is not configured to serve from subdomains".color(:red))
end
self
end
+ def uninstall_invoker
+ uninstall_invoker_flag = agree("Are you sure you want to uninstall firewall rules created by setup (y/n) : ")
+
+ if uninstall_invoker_flag
+ remove_resolver_file
+ unload_firewall_rule(true)
+ flush_dns_rules
+ Invoker::Power::Config.delete
+ Invoker::Logger.puts("Firewall rules were removed")
+ end
+ end
+
def drop_to_normal_user
EventMachine.set_effective_user(ENV["SUDO_USER"])
end
def flush_dns_rules
@@ -59,19 +81,37 @@
rescue Errno::EACCES
Invoker::Logger.puts("Running setup requires root access, please rerun it with sudo".color(:red))
raise
end
- def check_if_already_setup?
- File.exists?(Invoker::Power::Config::CONFIG_LOCATION)
+ def remove_resolver_file
+ if File.exists?(RESOLVER_FILE)
+ File.delete(RESOLVER_FILE)
+ end
+ rescue Errno::EACCES
+ Invoker::Logger.puts("Running uninstall requires root access, please rerun it with sudo".color(:red))
+ raise
end
+ def check_if_setup_can_run?
+ !File.exists?(Invoker::Power::Config::CONFIG_LOCATION)
+ end
+
def install_firewall(balancer_port)
File.open(FIREWALL_PLIST_FILE, "w") { |fl|
fl.write(plist_string(balancer_port))
}
- system("launchctl unload -w #{FIREWALL_PLIST_FILE} 2>/dev/null")
+ unload_firewall_rule
+ load_firewall_rule
+ end
+
+ def load_firewall_rule
system("launchctl load -Fw #{FIREWALL_PLIST_FILE} 2>/dev/null")
+ end
+
+ def unload_firewall_rule(remove = false)
+ system("launchctl unload -w #{FIREWALL_PLIST_FILE} 2>/dev/null")
+ system("rm -rf #{FIREWALL_PLIST_FILE}") if remove
end
# Ripped from POW code
def plist_string(balancer_port)
plist =<<-EOD