Class: Exec::NodeDetach

Inherits:
ExecutableCommand show all
Defined in:
lib/exec/node_detach.rb

Overview

Allows the user to detach node's of crowbar server.

Instance Attribute Summary

Attributes inherited from ExecutableCommand

#argv, #command_name, #logger, #options, #stderr, #stdin, #stdout, #values

Instance Method Summary collapse

Methods inherited from ExecutableCommand

#check_parameters, #create_logger, #run

Constructor Details

#initialize(argv, stdin, stdout, stderr, command_name) ⇒ NodeDetach

Note:

Overrides default constructor by passing CustomCommandOption to super().

Default constructor of the class.



20
21
22
# File 'lib/exec/node_detach.rb', line 20

def initialize(argv, stdin, stdout, stderr, command_name)
  super(argv, stdin, stdout, stderr, command_name)
end

Instance Method Details

#detach_node(node_name) ⇒ Object

Detach a node from the server Crowbar

Parameters:

  • node_name (String)

    the name node

Raises:

  • NodeError If the node don't exists or is not Ready



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
# File 'lib/exec/node_detach.rb', line 44

def detach_node(node_name)
  @logger.info("Exec::NodeDetach   detach_node(#{node_name})")
  Color::print_log("NONE", "Detach the node '#{node_name}'...", @stdout)

  output = ""
  unless node_name.nil?
    if if_node_exist(node_name, "Ready")
      cmd = Command::NodeDetach.new(node_name)
      cmd.exec
      #output += "Detach the node '#{node_name}'.\n"
    else
      raise NodeDetachError.new("The node '#{node_name}' doesn't exists or is not 'Ready'.")
    end
  else
    raise NodeDetachError.new("The node name is nil.")
  end

  Color::echo_ok(@stdout)
  @logger.info(output)
  if !output.empty? && output.length > 1
    @stdout.print output
  else
    @logger.info("Exec::NodeDetach   No machines available.")
    @stdout.printf_red "No machines available.\n"
  end
end

#execObject (private)

The execution of the command.



34
35
36
37
# File 'lib/exec/node_detach.rb', line 34

def exec()
  @logger.info("Exec::NodeDetach   Executing NodeDetach")
  detach_node(@values['node'])
end

#if_node_exist(node_name, status) ⇒ Boolean (private)

TODO:

refactor in a library

Returns true if the node exists, false else.

Parameters:

  • node_name (String)

    the name node

Returns:

  • (Boolean)

    true if the node exists, false else



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/exec/node_detach.rb', line 76

def if_node_exist(node_name, status)
  # Retrieves the list of nodes.
  begin
    @logger.info("Exec::NodeDetach   Getting the nodes list.")
    cmd = Command::CrowbarNodeList.new(@logger, status)
    list_nodes = cmd.exec
  rescue => e
    raise NodeListError.new("Retrieves the list of nodes with the status '#{status}'..")
  end

  if !list_nodes.nil? && list_nodes.length > 0
    list_nodes.each { |node|
      if node_name == node.split(" ").at(0).strip.to_s()
        return true
      end
    }
  end

  return false
end

#set_optionsObject (private)

Parse and check the parameters of the function.



27
28
29
30
# File 'lib/exec/node_detach.rb', line 27

def set_options
  @logger.info("Exec::NodeDetach   Setting options")
  @options.add_option("n", "node", "The Node name that will be Destroy.", true, true, method(:check_crowbar_node_name))
end