YUKI Hiroshi
null+****@clear*****
Tue Apr 21 16:12:09 JST 2015
YUKI Hiroshi 2015-04-21 16:12:09 +0900 (Tue, 21 Apr 2015) New Revision: 855178ec14c7a725517a6beafca3eca5bd466b14 https://github.com/droonga/droonga-engine/commit/855178ec14c7a725517a6beafca3eca5bd466b14 Message: Extract basic codes to implement commands based on serf to a base class Added files: lib/droonga/command/remote_command_base.rb Modified files: bin/droonga-engine-set-role Modified: bin/droonga-engine-set-role (+13 -52) =================================================================== --- bin/droonga-engine-set-role 2015-04-21 16:11:35 +0900 (834004b) +++ bin/droonga-engine-set-role 2015-04-21 16:12:09 +0900 (8672789) @@ -15,75 +15,36 @@ # 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 "slop" require "socket" -require "droonga/engine/version" -require "droonga/node_name" -require "droonga/serf" +require "droonga/command/remote_command_base" module Droonga - class SetRoleCommand + module Command + class SetRole << RemoteCommandBase def run - parse_options - puts "Setting role of #{@options[:host]} to #{@options[:role]}..." - set_node_role - puts("Done.") - exit(true) - end - - private - def parse_options - options = Slop.parse(:help => true) do |option| + parse_options do |option| option.on(:role=, "New role for the target node.", :required => true) - - option.separator("Connections:") - option.on(:host=, - "Host name of the target node.", - :required => true) - option.on("receiver-host=", - "Host name of this host.", - :default => Socket.gethostname) - option.on(:dataset=, - "Dataset name of for the target node.", - :default => NodeName::DEFAULT_DATASET) - option.on(:port=, - "Port number of the source cluster to be connected.", - :as => Integer, - :default => NodeName::DEFAULT_PORT) - option.on(:tag=, - "Tag name of the soruce cluster to be connected.", - :default => NodeName::DEFAULT_TAG) - - option.separator("Miscellaneous:") - option.on(:verbose, "Output details for internal operations.", - :default => false) end - @options = options - rescue Slop::MissingOptionError => error - $stderr.puts(error) - exit(false) - end - def target_node - "#{@options[:host]}:#{@options[:port]}/#{@options[:tag]}" - end + puts "Setting role of #{@options[:host]} to #{@options[:role]}..." + succeeded = set_node_role - def target_node_serf - @target_node_serf ||= Serf.new(target_node, - :verbose => @options[:verbose]) + puts("Done.") if succeeded + succeeded end + private def set_node_role - target_node_serf.ensure_restarted do - target_node_serf.send_query("change_role", - "node" => target_node, + serf.ensure_restarted do + serf.send_query("change_role", + "node" => node.to_s, "role" => @options[:role]) end end end end -Droonga::SetRoleCommand.new.run +exit(Droonga::Command::SetRole.new.run) Added: lib/droonga/command/remote_command_base.rb (+78 -0) 100755 =================================================================== --- /dev/null +++ lib/droonga/command/remote_command_base.rb 2015-04-21 16:12:09 +0900 (896126f) @@ -0,0 +1,78 @@ +#!/usr/bin/env ruby +# +# Copyright (C) 2015 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 "slop" + +require "droonga/engine/version" +require "droonga/node_name" +require "droonga/serf" + +module Droonga + module Command + class RemoteCommandBase + private + def parse_options(&block) + options = Slop.parse(:help => true) do |option| + yield(option) if block_given? + + option.separator("Connections:") + option.on(:host=, + "Host name of the target node.", + :required => true) + option.on(:port=, + "Port number of the source cluster to be connected.", + :as => Integer, + :default => NodeName::DEFAULT_PORT) + option.on(:tag=, + "Tag name of the soruce cluster to be connected.", + :default => NodeName::DEFAULT_TAG) + + option.separator("Miscellaneous:") + option.on(:verbose, "Output details for internal operations.", + :default => false) + end + @options = options + rescue Slop::MissingOptionError => error + $stderr.puts(error) + exit(false) + end + + def host + @options[:host] + end + + def port + @options[:port] + end + + def tag + @options[:tag] + end + + def node + @node ||= NodeName.new(:host => host, + :port => port, + :tag => tag) + end + + def serf + @serf ||= Serf.new(node.to_s, + :verbose => @options[:verbose]) + end + end + end +end -------------- next part -------------- HTML����������������������������...Download