Class: Lbspec::Util

Inherits:
Object
  • Object
show all
Defined in:
lib/lbspec/util.rb

Overview

Lbspec::Util provides some utilities

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.log_levelObject

Returns the value of attribute log_level.



9
10
11
# File 'lib/lbspec/util.rb', line 9

def log_level
  @log_level
end

.loggerObject

Returns the value of attribute logger.



9
10
11
# File 'lib/lbspec/util.rb', line 9

def logger
  @logger
end

Class Method Details

.add_string(target, addition) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/lbspec/util.rb', line 20

def self.add_string(target, addition)
  if target.nil?
    target = addition
  else
    target << addition
  end
end

.create_proveObject



28
29
30
31
# File 'lib/lbspec/util.rb', line 28

def self.create_prove
  t = Time.now
  t.to_i.to_s + t.nsec.to_s
end

.exec_command(command, node = nil) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/lbspec/util.rb', line 41

def self.exec_command(command, node = nil)
  if node
    exec_command_remote(command, node)
  else
    exec_command_local(command)
  end
end

.exec_command_local(command) ⇒ Object



59
60
61
62
63
64
# File 'lib/lbspec/util.rb', line 59

def self.exec_command_local(command)
  output = command + "\n"
  log.debug("execute locally: #{command}")
  output << `#{command}`.to_s
  output
end

.exec_command_remote(command, node = nil) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/lbspec/util.rb', line 49

def self.exec_command_remote(command, node = nil)
  output = command + "\n"
  log.debug("ssh to #{node}:#{command}")
  options = { config: true, verbose: log_level }
  Net::SSH.start(node, ssh_user(node), options) do |ssh|
    output << ssh.exec!(command).to_s
  end
  output
end

.logObject



12
13
14
15
16
17
18
# File 'lib/lbspec/util.rb', line 12

def self.log
  unless logger
    logger = Logger.new(STDOUT)
    logger.level = log_level
  end
  logger
end

.split_addr_port_path(addr_port_path) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/lbspec/util.rb', line 33

def self.split_addr_port_path(addr_port_path)
  splits = addr_port_path.split(/[:\/]/)
  addr = splits[0]
  port = (/\d+/ =~ splits[1]) ? splits[1].to_i : nil
  path = (/\d+/ =~ splits[1]) ? '/' + splits[2].to_s : '/' + splits[1].to_s
  [addr, port, path]
end

.ssh_user(node) ⇒ Object



66
67
68
69
70
71
72
73
# File 'lib/lbspec/util.rb', line 66

def self.ssh_user(node)
  # if there is no user for the node in ~/.ssh/config
  # use current user name for login
  user = Net::SSH::Config.for(node)[:user]
  user = user ? user : `whoami`.chomp
  log.debug("ssh #{node} as user:#{user}")
  user
end