lib/invoker/power/setup/linux_setup.rb in invoker-1.4.1 vs lib/invoker/power/setup/linux_setup.rb in invoker-1.5.1
- old
+ new
@@ -1,7 +1,9 @@
require "invoker/power/setup/distro/base"
require "facter"
+require 'erb'
+require 'fileutils'
module Invoker
module Power
class LinuxSetup < Setup
attr_accessor :distro_installer
@@ -20,66 +22,81 @@
Invoker::Logger.puts("Invoker is not configured to serve from subdomains".color(:red))
end
self
end
- def create_config_file
- Invoker.setup_config_location
- Invoker::Power::Config.create(
- http_port: port_finder.http_port,
- https_port: port_finder.https_port
- )
+ def uninstall_invoker
+ system("systemctl disable socat_invoker.service")
+ system("systemctl stop socat_invoker.service")
+ system("rm #{Invoker::Power::Distro::Base::SOCAT_SYSTEMD}")
+ system("rm #{Invoker::Power::Distro::Base::SOCAT_SHELLSCRIPT}")
+ initialize_distro_installer
+ remove_resolver_file
+ drop_to_normal_user
+ Invoker::Power::Config.delete
end
- def uninstall_invoker
- Invoker::Logger.puts("Uninstall is not yet supported on Linux."\
- " You can remove invoker changes by uninstalling dnsmasq and rinetd")
+ def resolver_file
+ distro_installer.resolver_file
end
+ def forwarder_script
+ File.join(File.dirname(__FILE__), "files/invoker_forwarder.sh.erb")
+ end
+
+ def socat_unit
+ File.join(File.dirname(__FILE__), "files/socat_invoker.service")
+ end
+
private
def initialize_distro_installer
# Create a new facter check for systemctl (in systemd)
Facter.add(:systemctl) do
setcode do
Facter::Util::Resolution.exec("[ -e /usr/bin/systemctl ] && echo 'true' || echo 'false'")
end
end
- @distro_installer = Invoker::Power::Distro::Base.distro_installer
+ @distro_installer = Invoker::Power::Distro::Base.distro_installer(tld)
end
def install_resolver
- File.open(distro_installer.resolver_file, "w") do |fl|
- fl.write(tld_setup)
+ File.open(resolver_file, "w") do |fl|
+ fl.write(resolver_file_content)
end
end
def install_port_forwarder
- File.open(distro_installer.rinetd_file, "a") do |fl|
- fl << "\n"
- fl << rinetd_setup(port_finder.http_port, port_finder.https_port)
- end
+ install_forwarder_script(port_finder.http_port, port_finder.https_port)
+ install_systemd_unit
end
- def tld_setup
- tld_string =<<-EOD
-local=/dev/
-address=/dev/127.0.0.1
+ def resolver_file_content
+ content =<<-EOD
+local=/#{tld}/
+address=/#{tld}/127.0.0.1
EOD
- tld_string
+ content
end
- def rinetd_setup(http_port, https_port)
- rinetd_string =<<-EOD
-0.0.0.0 80 0.0.0.0 #{http_port}
-0.0.0.0 443 0.0.0.0 #{https_port}
- EOD
- rinetd_string
+ def install_forwarder_script(http_port, https_port)
+ script_template = File.read(forwarder_script)
+ renderer = ERB.new(script_template)
+ script_output = renderer.result(binding)
+ File.open(Invoker::Power::Distro::Base::SOCAT_SHELLSCRIPT, "w") do |fl|
+ fl.write(script_output)
+ end
+ system("chmod +x #{Invoker::Power::Distro::Base::SOCAT_SHELLSCRIPT}")
end
+ def install_systemd_unit
+ FileUtils.cp(socat_unit, Invoker::Power::Distro::Base::SOCAT_SYSTEMD)
+ system("chmod 644 #{Invoker::Power::Distro::Base::SOCAT_SYSTEMD}")
+ end
+
def get_user_confirmation?
- Invoker::Logger.puts("Invoker is going to install dnsmasq and rinetd on this machine."\
- " It is also going to install a local resolver for .dev domain and a rinetd rule"\
+ Invoker::Logger.puts("Invoker is going to install dnsmasq and socat on this machine."\
+ " It is also going to install a local resolver for .#{tld} domain and a socat service"\
" which will forward all local requests on port 80 and 443 to another port")
Invoker::Logger.puts("If you still want to proceed with installation, press y.")
Invoker::CLI::Question.agree("Proceed with installation (y/n) : ")
end
end