Class: Receiver::CrowbarShellAPI
- Defined in:
- lib/receiver/crowbar_shell_api.rb
Overview
Allows to connects to an API Crowbar by the Shell command
Instance Method Summary collapse
-
#copy_barclamp(src, dest, cluster_name, network, mask, vlan_id, cpu, ram, description, verbose) ⇒ Object
Copy the Barclamp Vcluster for create a new vcluster.
-
#create_proposal(barclamp_name) ⇒ Object
create proposal for a barclamp.
-
#erase_node_hard_disk(node_name) ⇒ Object
Erase all data into all hard disks on the node and unmount partitions of /dev.
-
#erase_node_server(node_name) ⇒ Object
Erase a node in the server Crowbar.
-
#execute_shell_command(command) ⇒ Object
Execute a Shell command.
-
#execute_ssh(command, host, login, password) ⇒ Object
Execute a SSH command.
-
#is_json(data) ⇒ Object
private
Check is the variable is Json format.
-
#is_list_empty(list) ⇒ Object
private
Check if the list is empty or not.
-
#node_delete(node_name) ⇒ Object
remove node of the crowbar admin.
-
#node_info(node_name) ⇒ Object
display a information of node.
-
#node_install(node_name) ⇒ Object
Install a node with crowbar.
-
#node_list_nodes(status = nil) ⇒ Object
Retrieves the list of nodes according to the status.
-
#node_reinstall(node_name) ⇒ Object
Reinstall a node with crowbar.
-
#reboot_node(node_name) ⇒ Object
reboot node with crowbar shell.
-
#reload_service(barclamp_name) ⇒ Object
install a barclamp to th server crowbar.
-
#set_cluster_machine(cluster_name, barclamp_name, node_list) ⇒ Object
Set the list of nodes to the proposal.
-
#upload_file(file_name, file_path, destination_path, host, login, password) ⇒ Object
Upload a file to a remote server.
-
#vcluster_delete(vcluster_name) ⇒ Object
Delete a vcluster.
-
#vcluster_info(vcluster_name, proposal_name) ⇒ Object
Get the informations about the vcluster.
-
#vcluster_ls(vcluster_name, proposal_name) ⇒ Object
Displays a list of vclusters.
Methods inherited from Connector
Constructor Details
This class inherits a constructor from Receiver::Connector
Instance Method Details
#copy_barclamp(src, dest, cluster_name, network, mask, vlan_id, cpu, ram, description, verbose) ⇒ Object
Copy the Barclamp Vcluster for create a new vcluster
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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 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 143 144 |
# File 'lib/receiver/crowbar_shell_api.rb', line 34 def (src, dest, cluster_name, network, mask, vlan_id, cpu, ram, description, verbose) vcluster_mask = "vclusterMask" vcluster_network = "vclusterNetwork" vcluster_id_vlan = "vclusterIdvlan" vcluster_name = "vclusterName" vcluster_ram = "vclusterRam" vcluster_cpu = "vclusterCpu" vcluster_desc = "vclusterDescription" output = "" if verbose == "-v" verbose = true else verbose = false end unless File.directory?(src) raise Common::DirectoryNotFound.new("Source directory doesn't exists or is not accessible") end if File.directory?(dest) raise Common::DirectoryAlreadyExist.new("Destination directory already exists." ) end unless FileUtils.mkdir_p(dest) raise Common::DirectoryCreateError.new("An error occurred while trying to create destination directory.") end src_name = Pathname.new(src).basename dest_name= Pathname.new(dest).basename if verbose output += "Copying barclamp from '#{src_name}' to '#{dest_name}'.\n" output += "(#{src} -> #{dest})\n" end FileUtils.cp_r src + "/.", dest if verbose output += "Directories renaming...\n" end Dir.glob("#{dest}/**/*") do |dir| if File.directory? dir dirname = Pathname.new(dir).basename.to_s if (match = dirname.match(/(.*)(#{src_name})(.*)/)) # @todo maybe replace name by nil because it is never used before, name, after = match.captures new_dir_path = Pathname.new(dir).parent.to_s + "/" + before + dest_name + after if verbose output += "Moving #{dir} to #{new_dir_path}.\n" end FileUtils.mv dir, new_dir_path end end end if verbose output += "File renaming...\n" end Dir.glob("#{dest}/**/*") do |file| unless File.directory? file filename = Pathname.new(file).basename.to_s if (match = filename.match(/(.*)(#{src_name})(.*)/)) # @todo maybe replace name by nil because it is never used before, name, after = match.captures new_file_path = Pathname.new(file).parent.to_s + "/" + before + dest_name + after if verbose output += "Moving #{file} to #{new_file_path}.\n" end FileUtils.mv file, new_file_path file = new_file_path end f = File.read(file) if filename == "default.rb" or filename == "bc-template-vcluster.json" or filename == "crowbar.yml" content = f.gsub(/(#{vcluster_mask})/, mask.to_s) content = content.gsub(/(#{vcluster_network})/, network.to_s) content = content.gsub(/(#{vcluster_id_vlan})/, vlan_id.to_s) content = content.gsub(/(#{vcluster_name})/, cluster_name.to_s) content = content.gsub(/(#{vcluster_ram})/, ram.to_s) content = content.gsub(/(#{vcluster_cpu})/, cpu.to_s) content = content.gsub(/(#{vcluster_desc})/, description.to_s) f = File.new(file, "w") f.write(content) f.close end f = File.read(file) if f.match(/(.*)(#{src_name})(.*)/i) if verbose output += "Modifying file #{file}\n" end content = f.gsub(/(#{src_name})/, dest_name) content = content.gsub(/(#{src_name.to_s.capitalize})/, dest_name.to_s.capitalize) f = File.new(file, "w") f.write(content) f.close end end end if verbose output += "Done\n" end return output end |
#create_proposal(barclamp_name) ⇒ Object
create proposal for a barclamp
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 |
# File 'lib/receiver/crowbar_shell_api.rb', line 490 def create_proposal() command = "crowbar #{barclamp_name} proposal create default" # @todo Copier coller FAIL output = "" begin execute_shell_command(command) @logger.info("Receiver::CrowbarShellApi Create the proposal 'default' for the barclamp '#{barclamp_name}'...") rescue => e @logger.info("Receiver::CrowbarShellApi Executing the ssh command '#{command}'") @logger.info("Receiver::CrowbarShellApi Create the proposal 'default' for the barclamp '#{barclamp_name}'...") @logger.debug(e.) raise Common::CreateProposalError.new("Execute the shell command '#{command}'.") end return "Execute the shell command '#{command}'.\n" end |
#erase_node_hard_disk(node_name) ⇒ Object
Change the way to execute the command “erase hard disk” (i.e. having a shell connector to execute shell commands to a distant node)
Erase all data into all hard disks on the node and unmount partitions of /dev.
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/receiver/crowbar_shell_api.rb', line 152 def erase_node_hard_disk(node_name) @logger.info("Receiver::CrowbarShellApi Remove the node '#{node_name}' with crowbar...") output = "" begin output += upload_file("cb-lib-node-erase-hard-disk.sh", File.dirname(__FILE__) + "/../common", "/root", node_name, "root", "crowbar") + "\n" rescue => e @logger.info("Receiver::CrowbarShellApi Error: Uploading the file 'cb-lib-node-erase-hard-disk.sh' into the node '#{node_name}'...") @logger.debug(e.) raise Common::UploadError.new("Error: Uploading the file 'cb-lib-node-erase-hard-disk.sh' into the node '#{node_name}'!") end @logger.info("Receiver::CrowbarShellApi Uploading the file 'cb-lib-node-erase-hard-disk.sh' into the node '#{node_name}'...") begin execute_ssh("chmod 777 ~/cb-lib-node-erase-hard-disk.sh; ~/cb-lib-node-erase-hard-disk.sh", node_name, "root", "crowbar") output += "Execute the file 'cb-lib-node-erase-hard-disk.sh' into the node '#{node_name}'!\n" rescue => e @logger.info("Receiver::CrowbarShellApi Error: Executing the ssh command '#{command}'") @logger.debug(e.) output += "Error: Execute the file 'cb-lib-node-erase-hard-disk.sh' into the node '#{node_name}' : " + e. + "\n" raise Common::SSHError.new("Error: Execute the file 'cb-lib-node-erase-hard-disk.sh' into the node '#{node_name}' : " + e. + "") end @logger.info("Receiver::CrowbarShellApi Changed the chmod of the file 'cb-lib-node-erase-hard-disk.sh'...") @logger.info("Receiver::CrowbarShellApi Execute the file 'cb-lib-node-erase-hard-disk.sh'...") return output end |
#erase_node_server(node_name) ⇒ Object
Erase a node in the server Crowbar
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 |
# File 'lib/receiver/crowbar_shell_api.rb', line 186 def erase_node_server(node_name) command = "updatedb" output = "" begin output += execute_shell_command(command) + "\n" rescue => e @logger.info("Receiver::CrowbarShellApi Error: Executing the command '#{command}'") @logger.debug(e.) raise Common::EraseNodeError.new("Error: Erase the node '#{node_name}' in the server Crowbar.") end @logger.info("Receiver::CrowbarShellApi Executing the command '#{command}'...") command = "locate #{node_name} | grep -v \"/var/lib/ganglia/rrds/Crowbar\" | grep -v \".rrd\"" begin list_files = execute_shell_command(command) list_files.each { |file| if File.exist?(file) File.chmod(0777, file) File.delete(file) end if File.directory?(file) FileUtils.rm_rf(file) end } rescue => e @logger.info("Receiver::CrowbarShellApi Error: Executing the command '#{command}'") @logger.debug(e.) raise Common::EraseNodeError.new("Error: Erase the node '#{node_name}' in the server Crowbar.") end @logger.info("Receiver::CrowbarShellApi Executing the command '#{command}'...") @logger.info("Receiver::CrowbarShellApi Erase the node '#{node_name}' in the server Crowbar...") return output end |
#execute_shell_command(command) ⇒ Object
Execute a Shell command
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/receiver/crowbar_shell_api.rb', line 231 def execute_shell_command(command) @logger.info("Receiver::CrowbarShellApi Execute the command '#{command}'.") begin result = `#{command}` if $?.to_i > 0 @logger.info("Receiver::CrowbarShellApi Error: Executing the ssh command '#{command}'") @logger.error("Receiver::CrowbarShellApi Error: Install the barclamp to the server crowbar...") raise Common::ExecuteShellError.new("Error: Execute the shell command '#{command}'") end rescue => e @logger.info("Error: Executing the command '#{command}'") @logger.debug(e.) raise Common::ExecuteShellError.new("Error: Executing the command '#{command}':#{e.message}") end return result end |
#execute_ssh(command, host, login, password) ⇒ Object
Execute a SSH command
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 |
# File 'lib/receiver/crowbar_shell_api.rb', line 259 def execute_ssh(command, host, login, password) begin results = {} Timeout::timeout(2) do begin Net::SSH.start(host, login, :password => password) do |ssh| results[command] = ssh.exec!(command) end rescue Net::SSH::HostKeyMismatch => e e.remember_host! retry rescue StandardError => e raise e end end @logger.info("Receiver::CrowbarShellApi Execute the ssh command '#{command}'.") return results rescue Timeout::Error => e @logger.info("Receiver::CrowbarShellApi Error: Execute the ssh command '#{command}'.") @logger.info("Receiver::CrowbarShellApi Error: Timed out trying to get a connection.") @logger.debug(e.) raise e end end |
#is_json(data) ⇒ Object (private)
Check is the variable is Json format
290 291 292 293 294 295 296 297 298 |
# File 'lib/receiver/crowbar_shell_api.rb', line 290 def is_json(data) JSON.parse data @logger.info("Receiver::CrowbarShellApi The data is a JSON format.") return true rescue JSON::ParserError => e @logger.info("Receiver::CrowbarShellApi The data is not a JSON format.") @logger.debug(e.) return false end |
#is_list_empty(list) ⇒ Object (private)
Check if the list is empty or not
408 409 410 411 412 413 |
# File 'lib/receiver/crowbar_shell_api.rb', line 408 def is_list_empty(list) # @wtf so useless method xD if !list.nil? && list.length > 0 return false end return true end |
#node_delete(node_name) ⇒ Object
remove node of the crowbar admin
342 343 344 345 346 347 348 349 350 351 352 353 |
# File 'lib/receiver/crowbar_shell_api.rb', line 342 def node_delete(node_name) command = "crowbar_machines -U crowbar -P crowbar delete #{node_name}" begin @logger.info("Receiver::CrowbarShellApi Remove the node '#{node_name}' with crowbar...") return execute_shell_command(command) rescue => e @logger.info("Receiver::CrowbarShellApi Error: Executing the command '#{command}'") @logger.info("Receiver::CrowbarShellApi Error: Remove the node '#{node_name}' with crowbar...") @logger.debug(e.) raise Common::NodeDeleteError.new("Execute the shell command '#{command}'.") end end |
#node_info(node_name) ⇒ Object
display a information of node
323 324 325 326 327 328 329 330 331 332 333 334 335 |
# File 'lib/receiver/crowbar_shell_api.rb', line 323 def node_info(node_name) command = "crowbar_machines show #{node_name}" begin @logger.info("Receiver::CrowbarShellApi Display informations of the node '#{node_name}' with crowbar...") infos = execute_shell_command(command) return JSON.parse(infos) rescue => e @logger.info("Receiver::CrowbarShellApi Error: Executing the command '#{command}'") @logger.info("Receiver::CrowbarShellApi Error: Display informations of the node '#{node_name}' with crowbar...") @logger.debug(e.) raise Common::NodeInfoError.new("Execute the shell command '#{command}'.") end end |
#node_install(node_name) ⇒ Object
Install a node with crowbar
305 306 307 308 309 310 311 312 313 314 315 316 |
# File 'lib/receiver/crowbar_shell_api.rb', line 305 def node_install(node_name) command = "crowbar_machines -U crowbar -P crowbar allocate #{node_name}" begin @logger.info("Receiver::CrowbarShellApi Install the node '#{node_name}' with crowbar...") return execute_shell_command(command) rescue => e @logger.info("Receiver::CrowbarShellApi Error: Executing the command '#{command}'") @logger.info("Receiver::CrowbarShellApi Error: Install the node '#{node_name}' with crowbar...") @logger.debug(e.) raise Common::NodeInstallError.new("Execute the shell command '#{command}'.") end end |
#node_list_nodes(status = nil) ⇒ Object
Retrieves the list of nodes according to the status.
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 |
# File 'lib/receiver/crowbar_shell_api.rb', line 360 def node_list_nodes(status = nil) @logger.info("Receiver::CrowbarShellApi : Retrieves the list of nodes according to the status '#{status}'.") command = "crowbar_node_state -U crowbar -P crowbar status" list_nodes = execute_shell_command(command) hostname = `hostname -s` cluster_name = `hostname` if cluster_name.include? '.' cluster_name = cluster_name.split('.') cluster_name.delete_at(0) cluster_name = ".#{cluster_name.collect { |e| e }.join('.').strip}" else cluster_name = "" end if is_list_empty(list_nodes) @logger.info("Receiver::CrowbarShellApi Warning: The list of nodes is empty...") return nil else list_nodes_filter = Array.new list_nodes.each { |node| node_name = node.split(" ").at(0).strip.to_s status_node = node.split.drop(1).join(' ').strip.to_s if node_name.strip.to_s != hostname.strip.to_s if status.nil? node_to_display = "#{node_name}#{cluster_name} #{status_node}" list_nodes_filter << node_to_display else if status.strip.to_s == status_node.strip.to_s node_to_display = "#{node_name}#{cluster_name} #{status_node}" list_nodes_filter << node_to_display end end end } return list_nodes_filter end end |
#node_reinstall(node_name) ⇒ Object
Reinstall a node with crowbar
420 421 422 423 424 425 426 427 428 429 430 431 |
# File 'lib/receiver/crowbar_shell_api.rb', line 420 def node_reinstall(node_name) command = "crowbar_machines -U crowbar -P crowbar reinstall #{node_name}" begin @logger.info("Receiver::CrowbarShellApi Reinstall the node '#{node_name}' with crowbar...") return execute_shell_command(command) rescue => e @logger.info("Receiver::CrowbarShellApi Error: Executing the command '#{command}'") @logger.info("Receiver::CrowbarShellApi Error: Reinstall the node '#{node_name}' with crowbar...") @logger.debug(e.) raise Common::NodeReinstallError.new("Error: Execute the shell command '#{command}'.") end end |
#reboot_node(node_name) ⇒ Object
reboot node with crowbar shell
474 475 476 477 478 479 480 481 482 483 484 485 |
# File 'lib/receiver/crowbar_shell_api.rb', line 474 def reboot_node(node_name) begin execute_ssh("/sbin/reboot", node_name, "root", "crowbar") @logger.info("Receiver::CrowbarShellApi Reboot the node '#{node_name}'...") return "Reboot the node '#{node_name}'...\n" rescue => e @logger.info("Receiver::CrowbarShellApi Error: Executing the ssh command '/sbin/reboot'") @logger.info("Receiver::CrowbarShellApi Error: Reboot the node '#{node_name}'...") @logger.debug(e.) raise Common::NodeRebootError.new("Error: Execute the shell command '#{command}'.") end end |
#reload_service(barclamp_name) ⇒ Object
install a barclamp to th server crowbar
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 |
# File 'lib/receiver/crowbar_shell_api.rb', line 512 def reload_service() = "/opt/dell/barclamps" output = "" command = "barclamp_install.rb #{barclamps_path}/#{barclamp_name}" begin execute_shell_command(command) @logger.info("Receiver::CrowbarShellApi Install the barclamp '#{barclamp_name}' to the server crowbar...") rescue => e @logger.info("Receiver::CrowbarShellApi Error: Executing the ssh command '#{command}'") @logger.info("Receiver::CrowbarShellApi Error: Install the barclamp '#{barclamp_name}' to the server crowbar...") @logger.debug(e.) raise Common::BarclampInstallError.new("Execute the shell command '#{command}'.") return output end output += "Execute the shell command '#{command}'.\n" command = "service crowbar restart" begin execute_shell_command(command) @logger.info("Receiver::CrowbarShellApi Reboot the server crowbar...") rescue => e @logger.info("Receiver::CrowbarShellApi Error: Executing the ssh command '#{command}'") @logger.info("Receiver::CrowbarShellApi Error: Reboot the server crowbar...") @logger.debug(e.) raise Common::ReloadServiceError.new("Execute the shell command '#{command}'.") end output += "Execute the shell command '#{command}'.\n" return output end |
#set_cluster_machine(cluster_name, barclamp_name, node_list) ⇒ Object
Set the list of nodes to the proposal.
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 |
# File 'lib/receiver/crowbar_shell_api.rb', line 441 def set_cluster_machine(cluster_name, , node_list) @logger.info("Receiver::CrowbarShellApi Set the list of nodes to the proposal...") output = "" begin command = "crowbar #{barclamp_name} proposal show default" json = execute_shell_command(command) p = JSON.parse(json) node_list.each do |node| unless p['deployment'][]['elements'][role].include? node p['deployment'][]['elements'][role].push(node) end json = p.to_json cmd = "crowbar #{barclamp_name} proposal edit default --data '#{json}" execute_shell_command(cmd) end output += "Set the list of nodes to the proposal...\n" return output rescue => e @logger.info("Receiver::CrowbarShellApi Error: Executing the command '#{command}'") @logger.info("Receiver::CrowbarShellApi Error: Set the list of nodes to the proposal...") @logger.debug(e.) raise Common::SetClusterMachineError.new("Error: Execute the shell command '#{command}'.") end end |
#upload_file(file_name, file_path, destination_path, host, login, password) ⇒ Object
Upload a file to a remote server
557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 |
# File 'lib/receiver/crowbar_shell_api.rb', line 557 def upload_file(file_name, file_path, destination_path, host, login, password) begin Timeout::timeout(2) do # use a persistent connection to transfer files Net::SCP.start(host, login, :password => password) do |scp| begin # upload a file to a remote server scp.upload! "#{file_path}/#{file_name}", destination_path @logger.info("Receiver::CrowbarShellApi Upload the file '#{file_name}' to a remote server '#{host}'...") rescue Net::SCP::Error => e @logger.info("Receiver::CrowbarShellApi Error: Upload the file '#{file_name}' to a remote server '#{host}'.") @logger.debug(e.) raise e end end end rescue Timeout::Error => e @logger.info("Receiver::CrowbarShellApi Error: Timed out trying to get a connection to upload the file '#{file_name}'.") @logger.debug(e.) raise e end return "Upload the file '#{file_name}' to a remote server '#{host}'...\n" end |
#vcluster_delete(vcluster_name) ⇒ Object
Delete a vcluster
639 640 641 |
# File 'lib/receiver/crowbar_shell_api.rb', line 639 def vcluster_delete(vcluster_name) # @todo end |
#vcluster_info(vcluster_name, proposal_name) ⇒ Object
Get the informations about the vcluster
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 |
# File 'lib/receiver/crowbar_shell_api.rb', line 589 def vcluster_info(vcluster_name, proposal_name) = "cb#{vcluster_name}" command = "crowbar #{barclamp_name} proposal show #{proposal_name}" begin info_vcluster = execute_shell_command(command) if is_json(info_vcluster) @logger.info("Receiver::CrowbarShellApi Get the informations about the vcluster '#{vcluster_name}'...") return JSON.parse(info_vcluster) else @logger.info("Receiver::CrowbarShellApi Error: The informations are not in JSON format...") @logger.info("Receiver::CrowbarShellApi Error: Get the informations about the vcluster '#{vcluster_name}'...") return nil end rescue => e @logger.info("Receiver::CrowbarShellApi Error: Executing the ssh command '#{command}'") @logger.info("Receiver::CrowbarShellApi Error: Get the informations about the vcluster '#{vcluster_name}'...") @logger.debug(e.) raise Common::ClusterInfoError.new("The vcluster '#{vcluster_name}' doesn't exists or the proposal is not created.") end end |
#vcluster_ls(vcluster_name, proposal_name) ⇒ Object
Displays a list of vclusters
618 619 620 621 622 623 624 625 626 627 628 629 630 631 |
# File 'lib/receiver/crowbar_shell_api.rb', line 618 def vcluster_ls(vcluster_name, proposal_name) = "cb#{vcluster_name}" command = "crowbar #{barclamp_name} proposal show #{proposal_name}" begin list_vcluster = execute_shell_command(command) @logger.info("Receiver::CrowbarShellApi Get the list of vclusters...") return JSON.parse(list_vcluster) rescue => e @logger.info("Receiver::CrowbarShellApi Error: Executing the ssh command '#{command}'") @logger.info("Receiver::CrowbarShellApi Error: Get the list of vclusters...") @logger.debug(e.) raise Common::ClusterLsError.new("The vcluster '#{vcluster_name}' doesn't exists or the proposal is not created.") end end |