Class: DRbQS::ProcessDefinition::Register

Inherits:
Object
  • Object
show all
Defined in:
lib/drbqs/execute/register.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRegister

Returns a new instance of Register.



11
12
13
14
15
16
# File 'lib/drbqs/execute/register.rb', line 11

def initialize
  @__server__ = []
  @__node__ = []
  @__default__ = {}
  @__usage__ = {}
end

Instance Attribute Details

#__default__Array (readonly)

Returns a hash of key and value.

Returns:

  • (Array)

    a hash of key and value



9
10
11
# File 'lib/drbqs/execute/register.rb', line 9

def __default__
  @__default__
end

#__node__Array (readonly)

Keys of the hash are :type, :template, :ssh, :setting, and :args.

Returns:

  • (Array)

    an array of pair [name symbol, definition hash]



6
7
8
# File 'lib/drbqs/execute/register.rb', line 6

def __node__
  @__node__
end

#__server__Array (readonly)

Keys of the hash are :type, :template, :ssh, :setting, and :args.

Returns:

  • (Array)

    an array of pair [name symbol, definition hash]



6
7
8
# File 'lib/drbqs/execute/register.rb', line 6

def __server__
  @__server__
end

#__usage__Array (readonly)

Returns a hash of key and value.

Returns:

  • (Array)

    a hash of key and value



9
10
11
# File 'lib/drbqs/execute/register.rb', line 9

def __usage__
  @__usage__
end

Instance Method Details

#__load__(path) ⇒ Object



304
305
306
# File 'lib/drbqs/execute/register.rb', line 304

def __load__(path)
  instance_eval(File.read(path), path)
end

#clear_node(*args) ⇒ Object

Examples:

Clear node definitions

clear_node :node1, :node2

Parameters:

  • args (Array)

    Symbols of nodes



255
256
257
258
259
260
261
# File 'lib/drbqs/execute/register.rb', line 255

def clear_node(*args)
  args.each do |arg|
    @__node__.delete_if do |name, data|
      name == arg.intern
    end
  end
end

#clear_server(*args) ⇒ Object

Examples:

Clear server definitions

clear_server :server1, :server2

Parameters:

  • args (Array)

    Symbols of servers



244
245
246
247
248
249
250
# File 'lib/drbqs/execute/register.rb', line 244

def clear_server(*args)
  args.each do |arg|
    @__server__.delete_if do |name, data|
      name == arg.intern
    end
  end
end

#default(val = {}) ⇒ Object

We can set default server, default port, and default directory to output log.

Examples:

Set default value

default :port => 13456, :server => :server_local, :log => "/tmp/drbqs_execute_log"

Parameters:

  • val (Hash) (defaults to: {})

    the pair of key and value

Options Hash (val):

  • :port (Fixnum)

    Port number of a server

  • :server (Symbol)

    Server executed by default

  • :node (Array)

    Nodes executed by default if server has no node

  • :log (String)

    Path of log of a server and nods on localhost



271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/drbqs/execute/register.rb', line 271

def default(val = {})
  val.assert_valid_keys(:port, :server, :node, :log)
  val.delete_if { |key, v| !v }
  if val[:server]
    val[:server] = val[:server].intern
  end
  if val[:port]
    val[:port] = val[:port].to_i
  end
  raise "Invalid default value for :node." if val.has_key?(:node) && !(Array === val[:node])
  raise "Invalid default value for :log." if val.has_key?(:log) && !(String === val[:log])
  @__default__.merge!(val)
end

#default_clear(*keys) ⇒ Object

Examples:

Clear default value

default_clear :port, :server, :log


287
288
289
290
291
# File 'lib/drbqs/execute/register.rb', line 287

def default_clear(*keys)
  keys.each do |key|
    @__default__.delete(key)
  end
end

#node(name, opts = {}, &block) ⇒ Object

To set properties of nodes we can use the similar options to the command 'drbqs-node'. When we execute a server over ssh, we can use the similar options to the command 'drbqs-ssh' Exceptionally, we can set a ssh server by 'connect' method. If we omit the 'connect' method then the program tries to connect the name specified as first argument.

Examples:

Nodes on localhost

node :node_local do |nd|
  nd.process 3
  nd.load "load_lib.rb"
  nd.log_prefix "/path/to/log"
  nd.log_level Logger::DEBUG
end

Nodes over ssh

node :node_ssh do |nd, ssh|
  nd.process 3
  nd.load "load_lib.rb"
  nd.log_level Logger::DEBUG

  ssh.connect "hostname"
  ssh.directory "/path/to/dir"
  ssh.shell "bash"
  ssh.rvm "ruby-head"
  ssh.rvm_init "/path/to/scripts/rvm"
  ssh.output "/path/to/output"
  ssh.nice 10
end

Node group

node :node_group, :group => [:node_local, :node_ssh]

Parameters:

  • name (Symbol, String)

    Node name

  • opts (Hash) (defaults to: {})

    The options of node

Options Hash (opts):

  • :template (Boolean)

    Template for other nodes to load, not actual node

  • :load (Symbol)

    Inherit definition of other node

  • :group (Boolean)

    Define the group of node



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/drbqs/execute/register.rb', line 209

def node(name, opts = {}, &block)
  opts.assert_valid_keys(:template, :load, :group)
  name = name.intern
  load_def = opts[:load]
  if ind = @__node__.index { |n, data| name == n }
    old_data = @__node__.delete_at(ind)
    if (opts[:group] && old_data[1][:type] != :group) ||
        (!opts[:group] && old_data[1][:type] == :group)
      raise ArgumentError, "Change type of definition on reconfiguring."
    elsif (!opts[:group] && load_def)
      raise ArgumentError, "Can not set both reconfiguring and loading."
    end
    load_def = old_data[1][:setting]
  else
    old_data = nil
  end
  if opts[:group]
    unless Array === opts[:group]
      raise ":group must be an array of node names."
    end
    data = {
      :type => :group, :template => true, :ssh => nil, :setting => nil,
      :args => opts[:group].map(&:intern)
    }
    @__node__ << [name, data]
  elsif block_given?
    __register_node__(opts[:template], name, load_def, &block)
  else
    raise ArgumentError, "Block to define settings is not given."
  end
end

#server(name, hostname, opts = {}) ⇒ Object #server(name, opts = {}) ⇒ Object

To set properties of server we can use the similar options to the command 'drbqs-server'. When we execute a server over ssh, we can use the similar options to the command 'drbqs-ssh' Exceptionally, we can set files to load by 'load' method and set a ssh server by 'connect' method. If we omit the 'connect' method then the program tries to connect the name specified as first argument.

Examples:

A server on localhost (Connections from nodes is over SSH)

server :server_local, "example.com" do |srv|
  srv.load "server_definition.rb"
  srv.acl "/path/to/acl"
  srv.log_file "/path/to/log"
  srv.log_level Logger::ERROR
  srv.sftp_user "username"
  srv.sftp_host "example.com"
end

A server executed over SSH

server :server_ssh, "example.co.jp" do |srv, ssh|
  srv.load "server_definition.rb"
  srv.acl "/path/to/acl"
  srv.log_level Logger::ERROR
  srv.sftp_user "username"
  srv.sftp_host "example.com"

  ssh.connect "hostname"
  ssh.directory "/path/to/dir"
  ssh.shell "bash"
  ssh.rvm "ruby-head"
  ssh.rvm_init "/path/to/scripts/rvm"
  ssh.output "/path/to/output"
  ssh.nice 10
end

A server on localhost that uses UNIX domain socket (No node on other computer)

server :server_unix_domain_socket do |srv|
  srv.load "server_definition.rb"
  srv.execute_node 4
end

Overloads:

  • #server(name, hostname, opts = {}) ⇒ Object

    Parameters:

    • name (Symbol, String)

      Server name

    • hostname (Symbol, String)

      Hostname of server to access over SSH

    • opts (Hash) (defaults to: {})

      The options of server

    Options Hash (opts):

    • :template (Boolean)

      Template for other servers to load, not actual server

    • :load (Symbol)

      Inherit definition of other server

  • #server(name, opts = {}) ⇒ Object

    If hostname is omitted, the server uses UNIX domain socket and executes some nodes together.

    Parameters:

    • name (Symbol, String)

      Server name

    • opts (Hash) (defaults to: {})

      The options of server

    Options Hash (opts):

    • :template (Boolean)

      Template for other servers to load, not actual server

    • :load (Symbol)

      Inherit definition of other server



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/drbqs/execute/register.rb', line 144

def server(name, *args, &block)
  unless block_given?
    raise ArgumentError, "Block to define settings is not given."
  end
  name = name.intern
  if ind = @__server__.index { |n, data| name == n }
    old_data = @__server__.delete_at(ind)
  else
    old_data = nil
  end
  opts = args.extract_options!
  opts.assert_valid_keys(:template, :load)
  if args.size > 1
    raise ArgumentError, "Invalid number of arguments."
  end
  hostname = args[0]
  if old_data
    if opts[:load]
      raise ArgumentError, "Can not set both reconfiguring and loading."
    end
    load_def = old_data[1][:setting]
    hostname = old_data[1][:args][0] if !hostname
  else
    load_def = opts[:load]
  end
  __register_server__(opts[:template], name, load_def, hostname, &block)
end

#usage(opts = {}) ⇒ Object

We can set some messages shown by drbqs-execute -h.

Examples:

Set usage

usage :message => 'Calculate some value', :server => 'server.rb'

Parameters:

  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :message (String)

    Simple message strings

  • :server (String)

    Path of server definition to output as help of server



299
300
301
302
# File 'lib/drbqs/execute/register.rb', line 299

def usage(opts = {})
  opts.assert_valid_keys(:message, :server)
  @__usage__.merge!(opts)
end