Module: BlackStack::Infrastructure::NodeModule

Included in:
Node
Defined in:
lib/blackstack-nodes.rb

Overview

this module has attributes an methods used by both classes Node and Node.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#loggerObject

Returns the value of attribute logger.



9
10
11
# File 'lib/blackstack-nodes.rb', line 9

def logger
  @logger
end

#sshObject

Returns the value of attribute ssh.



9
10
11
# File 'lib/blackstack-nodes.rb', line 9

def ssh
  @ssh
end

Class Method Details

.descriptor_errors(h) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/blackstack-nodes.rb', line 11

def self.descriptor_errors(h)
  errors = []

  # validate: the parameter h is a hash
  errors << "The parameter h is not a hash" unless h.is_a?(Hash)

  # validate: the paramerer h has a key :net_remote_ip
  errors << "The parameter h does not have a key :net_remote_ip" unless h.has_key?(:net_remote_ip)

  # validate: the paramerer h has a key :ssh_username
  errors << "The parameter h does not have a key :ssh_username" unless h.has_key?(:ssh_username)

  # validate: the parameter h[:ssh_username] is a string
  errors << "The parameter h[:ssh_username] is not a string" unless h[:ssh_username].is_a?(String)

  # if the parameter h has a key :ssh_private_key_file
  if h.has_key?(:ssh_private_key_file)
    # validate: the parameter h[:ssh_private_key_file] is a string
    errors << "The parameter h[:ssh_private_key_file] is not a string" unless h[:ssh_private_key_file].is_a?(String)

    # validate: the parameter h[:ssh_private_key_file] is a string
    errors << "The parameter h[:ssh_private_key_file] is not a string" unless h[:ssh_private_key_file].is_a?(String)
  else
    # validate: the parameter h has a key :ssh_password
    errors << "The parameter h does not have a key :ssh_password nor :ssh_private_key_file" unless h.has_key?(:ssh_password)

    # validate: the parameter h[:ssh_password] is a string
    errors << "The parameter h[:ssh_password] is not a string" unless h[:ssh_password].is_a?(String)
  end

  # return
  errors
end

Instance Method Details

#connectObject



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/blackstack-nodes.rb', line 82

def connect
  # connect
  if self.using_password?
    self.ssh = Net::SSH.start(self.net_remote_ip, self.ssh_username, :password => self.ssh_password, :port => self.ssh_port)
  elsif self.using_private_key_file?
    self.ssh = Net::SSH.start(self.net_remote_ip, self.ssh_username, :keys => self.ssh_private_key_file, :port => self.ssh_port)
  else
    raise "No ssh credentials available"
  end
  self.ssh
end

#disconnectObject

def connect



94
95
96
# File 'lib/blackstack-nodes.rb', line 94

def disconnect
  self.ssh.close
end

#exec(command) ⇒ Object



98
99
100
101
102
103
104
105
106
# File 'lib/blackstack-nodes.rb', line 98

def exec(command)
  s = nil
  if self.using_password?
    s = self.ssh.exec!("echo '#{self.ssh_password.gsub("'", "\\'")}' | sudo -S su root -c '#{command.gsub("'", "\\'")}'")
  elsif self.using_private_key_file?
    s = self.ssh.exec!("sudo -S su root -c '#{command.gsub("'", "\\'")}'")
  end
  s
end

#initialize(h, i_logger = nil) ⇒ Object

def self.descriptor_errors(h)



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/blackstack-nodes.rb', line 45

def initialize(h, i_logger=nil)
  errors = BlackStack::Infrastructure::NodeModule.descriptor_errors(h)
  # raise an exception if any error happneed
  raise "The node descriptor is not valid: #{errors.uniq.join(".\n")}" if errors.length > 0
  # map attributes
  self.name = h[:name]
  self.net_remote_ip = h[:net_remote_ip]
  self.ssh_username = h[:ssh_username]
  self.ssh_password = h[:ssh_password] 
  self.ssh_port = h[:ssh_port]
  self.ssh_private_key_file = h[:ssh_private_key_file]

  # create a logger
  self.logger = !i_logger.nil? ? i_logger : BlackStack::BaseLogger.new(nil)
end

#rebootObject

def exec



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/blackstack-nodes.rb', line 108

def reboot()
  tries = 0
  max_tries = 20
  success = false

  host = self

  logger.logs 'reboot... '
  #stdout = host.reboot
  begin
  stdout = host.ssh.exec!("echo '#{host.ssh_password.gsub("'", "\\'")}' | sudo -S su root -c 'reboot'")
  rescue
  end
  logger.done #logf("done (#{stdout})")

  while tries < max_tries && !success
      begin
          tries += 1

          delay = 10
          logger.logs "wait #{delay.to_s} seconds... "
          sleep(delay)
          logger.done

          logger.logs "connecting (try #{tries.to_s})... "
          host.connect
          logger.done

          success = true
      rescue => e
          logger.logf e.to_s #error e
      end
  end # while 
  raise 'reboot failed' if !success
end

#to_hashObject

def self.create(h)



61
62
63
64
65
66
67
68
69
70
# File 'lib/blackstack-nodes.rb', line 61

def to_hash
  {
    :name => self.name,
    :net_remote_ip => self.net_remote_ip,
    :ssh_username => self.ssh_username,
    :ssh_password => self.ssh_password, 
    :ssh_port => self.ssh_port,
    :ssh_private_key_file => self.ssh_private_key_file,
  }
end

#using_password?Boolean

return true if the node is all set to connect using ssh user and password.

Returns:

  • (Boolean)


73
74
75
# File 'lib/blackstack-nodes.rb', line 73

def using_password?
  !self.net_remote_ip.nil? && !self.ssh_username.nil? && !self.ssh_password.nil?
end

#using_private_key_file?Boolean

return true if the node is all set to connect using a private key file.

Returns:

  • (Boolean)


78
79
80
# File 'lib/blackstack-nodes.rb', line 78

def using_private_key_file?
  !self.net_remote_ip.nil? && !self.ssh_username.nil? && !self.ssh_private_key_file.nil?
end