You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
hestiacp/func_ruby/ext-modules/passenger_manager.mod

191 lines
5.6 KiB

#!/opt/brepo/ruby33/bin/ruby
require "shell"
class PassengerWorker < Kernel::ModuleCoreWorker
MODULE_ID = "passenger_manager"
def check_domains_with_passenger
true
end
def info
{
ID: 2,
NAME: MODULE_ID,
DESCR: "Added passenger support for nginx",
REQ: "puppet_installer",
CONF: "yes",
}
end
def enable
log_file = get_log
f_inst_pp = get_module_paydata("passenger_installer.pp")
f_uninst_pp = get_module_paydata("passenger_uninstaller.pp")
if !check
inf = info
log("Req error, needed #{inf[:REQ]}")
"Req error, needed #{inf[:REQ]}"
else
begin
prepare_default_ruby_conf
log("install packages for passenger + nginx support: /usr/bin/puppet apply --detailed-exitcodes #{f_inst_pp}")
result_action = `/usr/bin/puppet apply --detailed-exitcodes "#{f_inst_pp}"`
ex_status = $?.exitstatus
if ex_status.to_i == 0 || ex_status.to_i == 2
log(result_action)
super
else
log(result_action)
log("Try to disable action: /usr/bin/puppet apply --detailed-exitcodes #{f_uninst_pp}")
result_action = `/usr/bin/puppet apply --detailed-exitcodes "#{f_uninst_pp}"`
"module installation error. See log #{log_file}"
end
rescue => e
log("module installation error #{e.message} #{e.backtrace.first}")
"module installation error. See log #{log_file}"
end
end
end
def disable
log_file = get_log
f_uninst_pp = get_module_paydata("passenger_uninstaller.pp")
if !check_domains_with_passenger
return "Presents domains with passenger support disable it first"
end
begin
log("uninstall packages for passenger + nginx support")
log("Try to disable action: /usr/bin/puppet apply --detailed-exitcodes #{f_uninst_pp}")
result_action = `/usr/bin/puppet apply --detailed-exitcodes "#{f_uninst_pp}"`
ex_status = $?.exitstatus
if ex_status.to_i == 0 || ex_status.to_i == 2
log(result_action)
super
else
log(result_action)
"module installation error. See log #{log_file}"
end
rescue => e
log("module installation error #{e.message} #{e.backtrace.first}")
"module installation error. See log #{log_file}"
end
end
def prepare_default_ruby_conf
ruby_conf_rubys = get_module_conf("rubys.conf")
return if File.exist?(ruby_conf_rubys)
arr = ["/usr/bin/ruby", "/opt/brepo/ruby33/bin/ruby"]
hestia_write_to_config_with_lock(ruby_conf_rubys, arr)
end
def return_rubys_from_conf
arr = []
ruby_conf_rubys = get_module_conf("rubys.conf")
return arr unless File.exist?(ruby_conf_rubys)
hestia_read_config_with_lock(ruby_conf_rubys)
end
def command(args)
return log_return("Not enough arguments. Needed command") if args.length < 1
m_command = args[0].strip
case m_command
when "get_rubys"
result = return_rubys_from_conf.map { |item| { "RUBY" => item } }
format = (args[1].nil? ? "shell" : args[1].strip)
hestia_print_array_of_hashes(result, format, "RUBY")
ACTION_OK
when "add_ruby"
path = args[1]
if path.nil?
log_return("Path to ruby should be specified. #{args}")
else
path = path.strip
if File.exist?(path)
rubys = return_rubys_from_conf
unless rubys.include? path
rubys << path
ruby_conf_rubys = get_module_conf("rubys.conf")
hestia_write_to_config_with_lock(ruby_conf_rubys, rubys)
end
ACTION_OK
else
log_return("File #{path} doesn't exists")
end
end
when "del_ruby"
path = args[1]
if path.nil?
log_return("Path to ruby should be specified. #{args}")
else
path = path.strip
rubys = return_rubys_from_conf
if rubys.include? path
rubys.delete(path)
ruby_conf_rubys = get_module_conf("rubys.conf")
hestia_write_to_config_with_lock(ruby_conf_rubys, rubys)
end
ACTION_OK
end
when "set_user_ruby"
domain = args[1]
ruby_ver = args[2]
if domain.nil? || ruby_ver.nil?
log_return("Domain or ruby version should be specified. #{args}")
else
if File.exist?(ruby_ver)
dom_file = get_module_conf("domains.conf")
hestia_save_file_key_pair(dom_file, domain, ruby_ver)
ACTION_OK
else
log_return("Ruby path doesn't exists. #{ruby_ver}")
end
end
when "disable_user"
domain = args[1]
if domain.nil?
log_return("Domain should be specified. #{args}")
else
dom_file = get_module_conf("domains.conf")
hestia_save_file_key_pair(dom_file, domain, "")
ACTION_OK
end
when "get_user_ruby"
domain = args[1]
if domain.nil?
log_return("Domain should be specified. #{args}")
else
dom_file = get_module_conf("domains.conf")
format = (args[2].nil? ? "shell" : args[2].strip)
val = hestia_get_file_key_pair(dom_file, domain)
result = Hash.new
result["RUBY"] = val
hestia_print_array_of_hashes(result, format, "RUBY")
ACTION_OK
end
else
log_return("Unknown commands. #{args}")
end
end
implements IPluginInterface
end
module PassengerModule
def get_object
Proc.new { PassengerWorker.new }
end
module_function :get_object
end
class Kernel::PluginConfiguration
include PassengerModule
@@loaded_plugins[PassengerWorker::MODULE_ID] = PassengerModule.get_object
end