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

non-database attributes, used for ssh connection and logging



12
13
14
# File 'lib/blackstack-nodes.rb', line 12

def logger
  @logger
end

#nameObject

:name is this is just a descriptive name for the node. It is not the host name, nor the domain, nor any ip.



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

def name
  @name
end

#net_remote_ipObject

:name is this is just a descriptive name for the node. It is not the host name, nor the domain, nor any ip.



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

def net_remote_ip
  @net_remote_ip
end

#sshObject

non-database attributes, used for ssh connection and logging



12
13
14
# File 'lib/blackstack-nodes.rb', line 12

def ssh
  @ssh
end

#ssh_passwordObject

:name is this is just a descriptive name for the node. It is not the host name, nor the domain, nor any ip.



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

def ssh_password
  @ssh_password
end

#ssh_portObject

:name is this is just a descriptive name for the node. It is not the host name, nor the domain, nor any ip.



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

def ssh_port
  @ssh_port
end

#ssh_private_key_fileObject

:name is this is just a descriptive name for the node. It is not the host name, nor the domain, nor any ip.



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

def ssh_private_key_file
  @ssh_private_key_file
end

#ssh_usernameObject

:name is this is just a descriptive name for the node. It is not the host name, nor the domain, nor any ip.



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

def ssh_username
  @ssh_username
end

Class Method Details

.descriptor_errors(h) ⇒ Object



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
44
45
46
47
48
49
50
51
52
# File 'lib/blackstack-nodes.rb', line 14

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 parameter h has a key :name
  errors << "The parameter h does not have a key :name" unless h.has_key?(:name)

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

  # 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



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/blackstack-nodes.rb', line 91

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



103
104
105
# File 'lib/blackstack-nodes.rb', line 103

def disconnect
  self.ssh.close
end

#exec(command) ⇒ Object



107
108
109
110
111
112
113
114
115
# File 'lib/blackstack-nodes.rb', line 107

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)



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/blackstack-nodes.rb', line 54

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



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
143
144
145
146
147
148
149
150
151
# File 'lib/blackstack-nodes.rb', line 117

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)



70
71
72
73
74
75
76
77
78
79
# File 'lib/blackstack-nodes.rb', line 70

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)


82
83
84
# File 'lib/blackstack-nodes.rb', line 82

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)


87
88
89
# File 'lib/blackstack-nodes.rb', line 87

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