Class: Exec::ClusterLs

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

Overview

Allows user to list a cluster

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) ⇒ ClusterLs

Note:

Overrides default constructor by passing CustomCommandOption to super().

Default constructor of the class.

Author:

  • mbretaud



23
24
25
# File 'lib/exec/cluster_ls.rb', line 23

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

Instance Method Details

#display_list(list) ⇒ Object (private)

Displays the elements of the list

Parameters:

  • list

    The list of elements

Author:

  • mbretaud



138
139
140
141
142
143
144
145
146
147
148
# File 'lib/exec/cluster_ls.rb', line 138

def display_list(list)
  output = ""
  begin
    list.sort.each { |element|
      output += element
    }
  rescue
    output += "Error: Displays the element of the list.\n"
  end
  return output
end

#execObject (private)

The execution of the command.

Author:

  • tnoguer



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

def exec
  @logger.info("Exec::ClusterLs   Executing ClusterLs")
  output = ""

  ### Displays only vclusters ###
  if !@values['cluster']
    @logger.info("Exec::ClusterLs   Displays vclusters into the bash screen...")
    Color::print_log("NONE", "Display vclusters into the bash screen...", @stdout)

    begin
      # Getting vclusters list
      list_clusters = Dir["/opt/dell/barclamps/cb*"]
      list_clusters_to_diplay = Array.new
    rescue => e
      raise ClusterLsError.new("Retrieves the list of vclusters.")
    end

    begin
      # Displays of the vclusters list
      list_clusters.each { |dir|
        if File::directory?(dir)
          cluster = File.basename(dir)
          list_clusters_to_diplay.<< "   - #{cluster[2..-1]}\n"
        end
      }

      unless list_clusters_to_diplay.empty?
        output += "\n"
        output += display_list(list_clusters_to_diplay)
        output += "\n"
      end
    rescue => e
      raise ClusterLsError.new("Displays the list of vclusters.")
    end

    Color::echo_ok(@stdout)
    @logger.info(output)
    if !output.empty?
      @stdout.print output
    else
      raise ClusterLsError.new("No vclusters available.")
    end
  end

  ### Displays machines associated with the vcluster ###
  if @values['cluster']
    @logger.info("Exec::ClusterLs   Displays machines associated with the vcluster '#{@values['cluster']}' into the bash screen...")
    Color::print_log("NONE", "Displays machines associated with the vcluster '#{@values['cluster']}' into the bash screen...", @stdout)

    cluster_name = @values['cluster']
    barclamp = "cb#{cluster_name}"
    proposal_name = "default"
    role = "#{barclamp}-server"

    # Retrieves informations about the vcluster
    cmd = Command::ClusterInfo.new(cluster_name, proposal_name)
    p = cmd.exec()

    error = "false"
    out_error = ""

    # Displays the list of machines on the vclusters associated
    list_machines_to_display = Array.new

    if p['deployment'][barclamp] != nil
      if p['deployment'][barclamp]['elements'][role] != nil
        p['deployment'][barclamp]['elements'][role].each do |machine|
          machine2=`cat /etc/hosts | grep #{machine} | grep #{cluster_name} | awk '{ print $2 }'`
          if machine2 == ""
            out_error=out_error + "Error: The machine #{machine} doesn't exists in the file /etc/hosts" + "\n"
          else
            list_machines_to_display.<< "   - #{cluster_name}-#{machine}\n"
          end
        end
      end
    end

    if error == "true"
      raise ClusterLsError.new(out_error)
    elsif !list_machines_to_display.empty?
      output += "\n"
      output += display_list(list_machines_to_display)
      output += "\n"
    elsif list_machines_to_display.empty?
      raise ClusterLsError.new("No machines are allocated to the vcluster '#{cluster_name}'.")
    end

    Color::echo_ok(@stdout)
    @logger.info(output)
    if !output.empty?
      @stdout.print output
    else
      @logger.info("Exec::ClusterLs   No vclusters available.")
      @stdout.print "No vclusters available.\n"
    end
  end
end

#set_optionsObject (private)

Parse and check the parameters of the function.

Author:

  • tnoguer



30
31
32
# File 'lib/exec/cluster_ls.rb', line 30

def set_options
  @options.add_option("C", "cluster", "Displays informations about a Cluster.", false, true, method(:check_cluster_name))
end