Navigation Menu

Skip to content

Commit

Permalink
Extract basic codes to implement commands based on serf to a base class
Browse files Browse the repository at this point in the history
  • Loading branch information
piroor committed Apr 21, 2015
1 parent 7938224 commit 855178e
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 52 deletions.
65 changes: 13 additions & 52 deletions bin/droonga-engine-set-role
Expand Up @@ -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)
78 changes: 78 additions & 0 deletions lib/droonga/command/remote_command_base.rb
@@ -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

0 comments on commit 855178e

Please sign in to comment.