Piro / YUKI Hiroshi
null+****@clear*****
Tue Sep 23 20:21:23 JST 2014
Piro / YUKI Hiroshi 2014-09-23 20:21:23 +0900 (Tue, 23 Sep 2014) New Revision: 6370bd1bb6dd0a6efbc77b14e2fd4c47a7955f65 https://github.com/droonga/droonga-engine/commit/6370bd1bb6dd0a6efbc77b14e2fd4c47a7955f65 Message: Extract codes to check service installation status Added files: lib/droonga/service_installation.rb Modified files: bin/droonga-engine-configure Modified: bin/droonga-engine-configure (+25 -96) =================================================================== --- bin/droonga-engine-configure 2014-09-23 16:46:34 +0900 (b9db48b) +++ bin/droonga-engine-configure 2014-09-23 20:21:23 +0900 (e9b2410) @@ -22,6 +22,7 @@ require "yaml" require "droonga/path" require "droonga/command/droonga_engine" require "droonga/safe_file_writer" +require "droonga/service_installation" options = { :quiet => nil, @@ -48,60 +49,6 @@ configuration.add_command_line_options(parser) parser.parse!(ARGV) -def ensure_have_write_permission - test_file = Droonga::Path.base + "#{Time.now.to_i}.test" - begin - FileUtils.touch(test_file.to_s) - rescue Errno::EACCES => error - end - unless test_file.exist? - puts("You have no permission to write files under " + - "<#{Droonga::Path.base.to_s}>.") - puts("Try again with right permission.") - exit(false) - end - FileUtils.rm_f(test_file.to_s) -end - -def service_user_exist? - system("id", service_user_name, - :out => "/dev/null", - :err => "/dev/null") -end - -def installed_as_service? - return false unless service_user_exist? - - succeeded = system("service", "droonga-engine", "status", - :out => "/dev/null", - :err => "/dev/null") - return true if succeeded - - result = `env SYSTEMCTL_SKIP_REDIRECT=yes service droonga-engine status` - result.include?("running") or result.include?("droonga-engine is stopped") -end - -def service_user_name - "droonga-engine" -end - -def service_base_directory - "/home/#{service_user_name}/droonga" -end - -def running?(configuration) - if installed_as_service? - result = `env SYSTEMCTL_SKIP_REDIRECT=yes service droonga-engine status` - result.include?("running") - else - system("droonga-engine-status", - "--base-dir", Droonga::Path.base.to_s, - "--pid-file", configuration.pid_file_path.to_s, - :out => "/dev/null", - :err => "/dev/null") - end -end - def unjoin(configuration) system("droonga-engine-unjoin", "--host", configuration.host, @@ -109,31 +56,6 @@ def unjoin(configuration) :err => "/dev/null") end -def stop_service(configuration) - if installed_as_service? - system("service", "droonga-engine", "stop", - :out => "/dev/null", - :err => "/dev/null") - else - system("droonga-engine-stop", - "--base-dir", Droonga::Path.base.to_s, - "--pid-file", configuration.pid_file_path.to_s, - :out => "/dev/null", - :err => "/dev/null") - end -end - -def start_service - if installed_as_service? - system("service", "droonga-engine", "start", - :out => "/dev/null", - :err => "/dev/null") - else - puts("The droonga-engine service is still stopped.") - puts("You need to start the service again manually.") - end -end - def input(message, default_value=nil) print "#{message} [#{default_value}]: " response = gets.strip @@ -168,8 +90,11 @@ def path_from_base_dir(path) end +service_installation = Droonga::ServiceInstallation.new +service_installation.ensure_using_service_base_directory + running = false -if running?(configuration) +if service_installation.running?(configuration.pid_file_path) if !options[:quiet] puts("The droonga-engine service is now running.") puts("Before reconfiguration, the service is going to be stopped " + @@ -181,10 +106,12 @@ if running?(configuration) running = true end - -Droonga::Path.base = service_base_directory if service_user_exist? - -ensure_have_write_permission +unless service_installation.have_write_permission? + puts("You have no permission to write files under " + + "<#{Droonga::Path.base.to_s}>.") + puts("Try again with right permission.") + exit(false) +end data_files = [ @@ -214,7 +141,7 @@ end if running unjoin(configuration) - stop_service(configuration) + service_installation.stop_service(configuration.pid_file_path) end if options[:clear] @@ -244,7 +171,7 @@ if options[:reset_config] or options[:reset_catalog] end if options[:reset_config] - if service_user_exist? + if service_installation.user_exist? daemon = true elsif configuration.have_given_daemon? or options[:quiet] daemon = configuration.daemon? @@ -252,7 +179,7 @@ if options[:reset_config] daemon = confirmed?("run as a daemon?") end - if service_user_exist? + if service_installation.user_exist? log_file = Droonga::Path.default_log_file elsif configuration.have_given_log_file? log_file = configuration.log_file @@ -269,8 +196,8 @@ if options[:reset_config] end pid_file_path = nil - unless installed_as_service? - if options[:quiet] or service_user_exist? + unless service_installation.installed_as_service? + if options[:quiet] or service_installation.user_exist? pid_file_path = Droonga::Path.default_pid_file elsif configuration.have_given_pid_file? pid_file_path = configuration.pid_file_path @@ -292,9 +219,7 @@ if options[:reset_config] end Droonga::SafeFileWriter.write(Droonga::Path.config, YAML.dump(new_configuration)) - if service_user_exist? - FileUtils.chown(service_user_name, service_user_name, Droonga::Path.config) - end + service_installation.ensure_correct_file_permission(Droonga::Path.config) end if options[:reset_catalog] @@ -303,11 +228,15 @@ if options[:reset_catalog] "--hosts", host, "--port", port.to_s, "--tag", tag) - if service_user_exist? - FileUtils.chown(service_user_name, service_user_name, Droonga::Path.catalog) - end + service_installation.ensure_correct_file_permission(Droonga::Path.catalog) end -start_service if running +if running + successfully_started = service_installation.start + unless successfully_started + puts("The droonga-engine service is still stopped.") + puts("You need to start the service again manually.") + end +end exit(true) Added: lib/droonga/service_installation.rb (+127 -0) 100644 =================================================================== --- /dev/null +++ lib/droonga/service_installation.rb 2014-09-23 20:21:23 +0900 (fa1a1b5) @@ -0,0 +1,127 @@ +# Copyright (C) 2014 Droonga Project +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License version 2.1 as published by the Free Software Foundation. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +require "fileutils" + +require "droonga/path" + +module Droonga + class ServiceInstallation + class << self + end + + class MissingPidFilePath < ArgumentError + end + + def user_name + "droonga-engine" + end + + def group_name + user_name + end + + def base_directory + @base_directory ||= Pathname("/home/#{user_name}/droonga") + end + + def ensure_using_service_base_directory + if user_exist? + Path.base = base_directory + end + end + + def have_write_permission? + test_file = base_directory + "#{Time.now.to_i}.test" + begin + FileUtils.touch(test_file.to_s) + rescue Errno::EACCES => error + end + unless test_file.exist? + return false + end + FileUtils.rm_f(test_file.to_s) + true + end + + def user_exist? + system("id", user_name, + :out => "/dev/null", + :err => "/dev/null") + end + + def installed_as_service? + return false unless user_exist? + + succeeded = system("service", "droonga-engine", "status", + :out => "/dev/null", + :err => "/dev/null") + return true if succeeded + + result = `env SYSTEMCTL_SKIP_REDIRECT=yes service droonga-engine status` + result.include?("running") or \ + result.include?("droonga-engine is stopped") + end + + def ensure_correct_file_permission(file) + if user_exist? + FileUtils.chown(user_name, group_name, file) + end + end + + def running?(pid_file_path=nil) + if installed_as_service? + result = `env SYSTEMCTL_SKIP_REDIRECT=yes service droonga-engine status` + result.include?("running") + else + if pid_file_path.nil? + raise MissingPidFilePath.new + end + system("droonga-engine-status", + "--base-dir", Path.base.to_s, + "--pid-file", pid_file_path.to_s, + :out => "/dev/null", + :err => "/dev/null") + end + end + + def start + if installed_as_service? + system("service", "droonga-engine", "start", + :out => "/dev/null", + :err => "/dev/null") + else + false + end + end + + def stop(pid_file_path=nil) + if installed_as_service? + system("service", "droonga-engine", "stop", + :out => "/dev/null", + :err => "/dev/null") + else + if pid_file_path.nil? + raise MissingPidFilePath.new + end + system("droonga-engine-stop", + "--base-dir", Path.base.to_s, + "--pid-file", pid_file_path.to_s, + :out => "/dev/null", + :err => "/dev/null") + end + end + end +end -------------- next part -------------- HTML����������������������������...Download