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
-
#logger ⇒ Object
non-database attributes, used for ssh connection and logging.
-
#name ⇒ Object
:name is this is just a descriptive name for the node.
-
#net_remote_ip ⇒ Object
:name is this is just a descriptive name for the node.
-
#ssh ⇒ Object
non-database attributes, used for ssh connection and logging.
-
#ssh_password ⇒ Object
:name is this is just a descriptive name for the node.
-
#ssh_port ⇒ Object
:name is this is just a descriptive name for the node.
-
#ssh_private_key_file ⇒ Object
:name is this is just a descriptive name for the node.
-
#ssh_username ⇒ Object
:name is this is just a descriptive name for the node.
Class Method Summary collapse
Instance Method Summary collapse
- #connect ⇒ Object
-
#disconnect ⇒ Object
def connect.
- #exec(command, sudo = true) ⇒ Object
-
#initialize(h, i_logger = nil) ⇒ Object
def self.descriptor_errors(h).
-
#reboot ⇒ Object
def exec.
-
#to_hash ⇒ Object
def self.create(h).
-
#using_password? ⇒ Boolean
return true if the node is all set to connect using ssh user and password.
-
#using_private_key_file? ⇒ Boolean
return true if the node is all set to connect using a private key file.
Instance Attribute Details
#logger ⇒ Object
non-database attributes, used for ssh connection and logging
13 14 15 |
# File 'lib/blackstack-nodes.rb', line 13 def logger @logger end |
#name ⇒ Object
:name is this is just a descriptive name for the node. It is not the host name, nor the domain, nor any ip.
11 12 13 |
# File 'lib/blackstack-nodes.rb', line 11 def name @name end |
#net_remote_ip ⇒ Object
:name is this is just a descriptive name for the node. It is not the host name, nor the domain, nor any ip.
11 12 13 |
# File 'lib/blackstack-nodes.rb', line 11 def net_remote_ip @net_remote_ip end |
#ssh ⇒ Object
non-database attributes, used for ssh connection and logging
13 14 15 |
# File 'lib/blackstack-nodes.rb', line 13 def ssh @ssh end |
#ssh_password ⇒ Object
:name is this is just a descriptive name for the node. It is not the host name, nor the domain, nor any ip.
11 12 13 |
# File 'lib/blackstack-nodes.rb', line 11 def ssh_password @ssh_password end |
#ssh_port ⇒ Object
:name is this is just a descriptive name for the node. It is not the host name, nor the domain, nor any ip.
11 12 13 |
# File 'lib/blackstack-nodes.rb', line 11 def ssh_port @ssh_port end |
#ssh_private_key_file ⇒ Object
:name is this is just a descriptive name for the node. It is not the host name, nor the domain, nor any ip.
11 12 13 |
# File 'lib/blackstack-nodes.rb', line 11 def ssh_private_key_file @ssh_private_key_file end |
#ssh_username ⇒ Object
:name is this is just a descriptive name for the node. It is not the host name, nor the domain, nor any ip.
11 12 13 |
# File 'lib/blackstack-nodes.rb', line 11 def ssh_username @ssh_username end |
Class Method Details
.descriptor_errors(h) ⇒ Object
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 53 |
# File 'lib/blackstack-nodes.rb', line 15 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) && !h[:ssh_private_key_file].nil? # 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
#connect ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/blackstack-nodes.rb', line 92 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 |
#disconnect ⇒ Object
def connect
104 105 106 |
# File 'lib/blackstack-nodes.rb', line 104 def disconnect self.ssh.close end |
#exec(command, sudo = true) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/blackstack-nodes.rb', line 108 def exec(command, sudo=true) code = nil if sudo if self.using_password? code = "echo '#{self.ssh_password}' | sudo -S su root -c '#{command}'" elsif self.using_private_key_file? code = "sudo -S su root -c '#{command}'" end else code = command end #puts #puts #puts code #puts #puts s = self.ssh.exec!(code) #puts #puts #puts s #puts #puts s end |
#initialize(h, i_logger = nil) ⇒ Object
def self.descriptor_errors(h)
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/blackstack-nodes.rb', line 55 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 |
#reboot ⇒ Object
def exec
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/blackstack-nodes.rb', line 133 def reboot() tries = 0 max_tries = 20 success = false host = self logger.logs 'reboot... ' #stdout = host.reboot begin stdout = self.exec("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_hash ⇒ Object
def self.create(h)
71 72 73 74 75 76 77 78 79 80 |
# File 'lib/blackstack-nodes.rb', line 71 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.
83 84 85 |
# File 'lib/blackstack-nodes.rb', line 83 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.
88 89 90 |
# File 'lib/blackstack-nodes.rb', line 88 def using_private_key_file? !self.net_remote_ip.nil? && !self.ssh_username.nil? && !self.ssh_private_key_file.nil? end |