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.
-
#tags ⇒ Object
:name is this is just a descriptive name for the node.
Class Method Summary collapse
Instance Method Summary collapse
- #code(command, sudo = true) ⇒ Object
- #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.
-
#tail(filename, n = 10) ⇒ Object
return the latest ānā lines of the file specified by the `filename` parameter.
-
#to_hash ⇒ Object
def self.create(h).
-
#usage ⇒ Object
Return a hash descriptor of the status of the node.
-
#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 |
#tags ⇒ 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 @tags 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 54 55 56 57 58 59 |
# 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 # if the parameter h has a key :tags if h.has_key?(:tags) && !h[:tags].nil? # validate: the parameter h[:tags] is an array or a string errors << "The parameter h[:tags] is not an array or a string" unless h[:tags].is_a?(Array) || h[:tags].is_a?(String) end # return errors end |
Instance Method Details
#code(command, sudo = true) ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/blackstack-nodes.rb', line 120 def code(command, sudo=true) s = nil if sudo command.gsub!(/'/, "\\\\'") if self.using_password? s = "echo '#{self.ssh_password.gsub(/'/, "\\\\'")}' | sudo -S su root -c '#{command}'" elsif self.using_private_key_file? s = "sudo -S su root -c '#{command}'" end else s = command end s end |
#connect ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/blackstack-nodes.rb', line 104 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
116 117 118 |
# File 'lib/blackstack-nodes.rb', line 116 def disconnect self.ssh.close end |
#exec(command, sudo = true) ⇒ Object
135 136 137 138 139 |
# File 'lib/blackstack-nodes.rb', line 135 def exec(command, sudo=true) code = self.code(command, sudo) s = self.ssh.exec!(code) s end |
#initialize(h, i_logger = nil) ⇒ Object
def self.descriptor_errors(h)
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/blackstack-nodes.rb', line 61 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] # parse the tags if h.has_key?(:tags) && !h[:tags].nil? self. = h[:tags].is_a?(Array) ? h[:tags] : [h[:tags]] else self. = [] end # create a logger self.logger = !i_logger.nil? ? i_logger : BlackStack::BaseLogger.new(nil) end |
#reboot ⇒ Object
def exec
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 168 169 170 171 172 173 174 175 |
# File 'lib/blackstack-nodes.rb', line 141 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 |
#tail(filename, n = 10) ⇒ Object
return the latest ānā lines of the file specified by the `filename` parameter
230 231 232 233 234 235 |
# File 'lib/blackstack-nodes.rb', line 230 def tail(filename, n=10) self.connect s = self.ssh.exec!("tail -n #{n.to_s} #{filename}") self.disconnect s end |
#to_hash ⇒ Object
def self.create(h)
82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/blackstack-nodes.rb', line 82 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, :tags => self. } end |
#usage ⇒ Object
Return a hash descriptor of the status of the node
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/blackstack-nodes.rb', line 178 def usage() ret = {} self.connect ret[:b_total_memory] = self.ssh.exec!('cat /proc/meminfo | grep MemTotal').delete('^0-9').to_i*1024 ret[:kb_total_memory] = ret[:b_total_memory] / 1024 ret[:mb_total_memory] = ret[:kb_total_memory] / 1024 ret[:gb_total_memory] = ret[:mb_total_memory] / 1024 ret[:kb_free_memory] = self.ssh.exec!('cat /proc/meminfo | grep MemFree').delete('^0-9').to_i ret[:mb_free_memory] = ret[:kb_free_memory] / 1024 ret[:gb_free_memory] = ret[:mb_free_memory] / 1024 # run bash commend to get the total disk space ret[:mb_total_disk] = self.ssh.exec!('df -m / | tail -1 | awk \'{print $2}\'').to_i ret[:gb_total_disk] = ret[:mb_total_disk] / 1024 # run bash command to get the free disk space ret[:mb_free_disk] = self.ssh.exec!('df -m / | tail -1 | awk \'{print $4}\'').to_i ret[:gb_free_disk] = ret[:mb_free_disk] / 1024 # run bash command to get hostname ret[:hostname] = self.ssh.exec!('hostname').strip! # run bash command to get the CPU load # reference: https://stackoverflow.com/questions/9229333/how-to-get-overall-cpu-usage-e-g-57-on-linux ret[:cpu_load_average] = self.ssh.exec!("awk '{u=$2+$4; t=$2+$4+$5; if (NR==1){u1=u; t1=t;} else print ($2+$4-u1) * 100 / (t-t1) \"%\"; }' <(grep 'cpu ' /proc/stat) <(sleep 1;grep 'cpu ' /proc/stat)").to_s # TODO: monitor the overall Network I/O load # TODO: monitor the overall Disk I/O load # mapping cpu status ret[:cpu_architecture] = self.ssh.exec!('lscpu | grep Architecture').split(':')[1].strip! ret[:cpu_speed] = self.ssh.exec!('lscpu | grep "CPU MHz:"').split(':')[1].strip!.to_f.round #ret[:cpu_model] = self.ssh.exec!('lscpu | grep "Model"').split(':')[1].strip! #ret[:cpu_type] = ret[:cpu_model].split(' ')[0] ret[:cpu_number] = self.ssh.exec!('lscpu | grep "^CPU(s):"').split(':')[1].strip!.to_i # mapping disk status #self.disk_total = mb_total_disk.to_i #self.disk_free = mb_free_disk.to_i # mapping lan attributes ret[:net_mac_address] = self.ssh.exec!('ifconfig | grep ether').split[1].upcase.strip.gsub(':', '-') self.disconnect ret end |
#using_password? ⇒ Boolean
return true if the node is all set to connect using ssh user and password.
95 96 97 |
# File 'lib/blackstack-nodes.rb', line 95 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.
100 101 102 |
# File 'lib/blackstack-nodes.rb', line 100 def using_private_key_file? !self.net_remote_ip.nil? && !self.ssh_username.nil? && !self.ssh_private_key_file.nil? end |