Class: Exec::ServiceInfo

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

Overview

TODO:

Refactor class helper and all the fuss around it

Allows the user to show pieces of cluster’s services information.

Author:

  • tnoguer

Instance Attribute Summary collapse

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

Returns a new instance of ServiceInfo.



28
29
30
31
32
33
# File 'lib/exec/service_info.rb', line 28

def initialize(argv, stdin, stdout, stderr, command_name)
  super(argv, stdin, stdout, stderr, command_name)
  @ignore_service_attributes = []
  @ignore_attr_service_component = %w{ cluster_name service_name }
  @ignore_attr_host_component = %w{ cluster_name component_name }
end

Instance Attribute Details

#ignore_attr_host_componentObject (protected)

host component attributes to ignore



25
26
27
# File 'lib/exec/service_info.rb', line 25

def ignore_attr_host_component
  @ignore_attr_host_component
end

#ignore_attr_service_componentObject (protected)

service component attributes to ignore



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

def ignore_attr_service_component
  @ignore_attr_service_component
end

#ignore_service_attributesObject (protected)

service attributes to ignore



21
22
23
# File 'lib/exec/service_info.rb', line 21

def ignore_service_attributes
  @ignore_service_attributes
end

Instance Method Details

#execObject

The execution of the command.

Author:

  • tnoguer



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/exec/service_info.rb', line 49

def exec
  Color::print_log("NONE", "getting service list...", @stdout)
  confs = Command::AmbariServiceConfLs.new(@values["cluster"]).exec()
  Color::echo_ok(@stdout)
  @stdout.print "\nCLUSTER #{@values["cluster"]}\n"
  @stdout.print "desired configs: \n"
  confs.each do |conf|
    if conf["active"]
      @stdout.print("  " + "  " + conf["type"] + " " + conf["tag"]+ "\n")
    end
  end
  if @values["service"].nil?
    msg = info_all_services()
  else
    msg = info_service()
  end
  @stdout.print msg
end

#format_component(data, host_name, prefix = "\t", prefix_char = "\t") ⇒ Object (private)

Raises:

  • (StandardError)


172
173
174
175
176
177
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
# File 'lib/exec/service_info.rb', line 172

def format_component(data, host_name, prefix = "\t", prefix_char = "\t")
  raise(StandardError, "Malformed return request") if data["ServiceComponentInfo"].nil?

  infos = ""
  title = ""
  host_comp = ""
  data["ServiceComponentInfo"] = data["ServiceComponentInfo"].sort

  data["ServiceComponentInfo"].each do |info_name, info_value|
    unless @ignore_attr_service_component.include?(info_name.to_s)
      case info_name.to_s
        when "state", "desired_state"
          #  infos += prefix + info_name.to_s + ": " + format_state(info_value.to_s) + "\n"
        when "component_name"
          title = prefix + color_white("Component " + info_value.to_s, 1) + ":\n"
        when "desired_configs", "actual_configs", "configs"
          #infos += prefix + info_name.to_s + ": " + format_configs(info_value, prefix + prefix_char)
        else
          infos += prefix + info_name.to_s + ": " + info_value.to_s + "\n"
      end
    end
  end

  if !data["host_components"].nil? && data["host_components"].length > 0
    data["host_components"] = data["host_components"].sort { |x, y| x["href"] <=> y["href"] }
    data["host_components"].each do |host_component|
      if host_name.nil? || host_component["HostRoles"]["host_name"] == host_name
        host_comp += format_host_component(host_component, prefix + prefix_char, prefix_char)
      end
    end
  else
    host_comp = prefix + "Hosts: none\n"
  end
  return title + infos + host_comp
end

#format_host_component(data, prefix = "\t", prefix_char = "\t") ⇒ Object (private)

Raises:

  • (StandardError)


209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/exec/service_info.rb', line 209

def format_host_component(data, prefix = "\t", prefix_char = "\t")
  raise(StandardError, "Malformed return request") if data["HostRoles"].nil?

  title = ""
  output = ""
  output += color_yellow(prefix + "/!\\ WARNING STATUS\n") if is_bad_status(data)
  data["HostRoles"] = data["HostRoles"].sort

  data["HostRoles"].each do |info_name, info_value|
    unless @ignore_attr_host_component.include?(info_name.to_s)
      case info_name.to_s
        when "host_name"
          title = prefix + color_white("Host: " + info_value.to_s, 1) + "\n"
        when "state", "desired_state"
          output += prefix + info_name.to_s + ": " + format_state(info_value.to_s) + "\n"
        when "config", "desired_config", "actual_configs"
          output += prefix + info_name + format_configs(info_value, prefix + prefix_char)
        else
          output += prefix + info_name.to_s + ": " + info_value.to_s + "\n"
      end
    end
  end
  return title + output
end

#format_info_service(data, host_name, component_name, prefix = "\t", prefix_char = "\t") ⇒ Object (private)

Raises:

  • (StandardError)


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
168
169
# File 'lib/exec/service_info.rb', line 137

def format_info_service(data, host_name, component_name, prefix = "\t", prefix_char = "\t")
  raise(StandardError, "Malformed return request") if data["ServiceInfo"].nil?
  output = ""
  data["ServiceInfo"] = data["ServiceInfo"].sort

  data["ServiceInfo"].each do |info_name, info_value|
    unless @ignore_service_attributes.include?(info_name.to_s)
      if info_name.to_s != "state" && info_name.to_s != "desired_state"
        output += prefix + info_name.to_s + ": "
      end
      case info_name.to_s
        when "desired_configs", "actual_configs", "configs"
          output += format_configs(info_value, prefix + prefix_char)
        else
          output += info_value.to_s + "\n"
      end
    end
  end

  if data["components"].nil?
    output += "No component in this cluster."
  else
    output += "Components: \n" if component_name.nil?
    output += "Components (only #{component_name}): \n" if !component_name.nil?
    data["components"] = data["components"].sort { |x, y| x["href"] <=> y["href"] }
    data["components"].each do |component|
      if component_name.nil? || component["ServiceComponentInfo"]["component_name"] == component_name
        output += format_component(component, host_name, prefix + prefix_char, prefix_char)
      end
    end
  end
  return output
end

#info_all_servicesObject (private)

Shows the informations for all the services.

Returns:

  • The information to print.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/exec/service_info.rb', line 71

def info_all_services
  @logger.begin_execution("infoService")

  # Getting service list
  data = Command::AmbariServiceList.new(@values["cluster"]).exec()

  output = ""

  # Call infoservice for each service
  data["items"].each do |service|
    service_name = service["ServiceInfo"]["service_name"].to_s
    output += "\n======== #{service_name} ========\n"
    output += info_service(@values["cluster"], service_name, @values["host"], @values["component"])
  end

  @logger.end_execution()
  return output
end

#info_service(cluster_name = , service_name = , host_name = , component_name = , prefix = " ", prefix_char = " ") ⇒ Object (private)

Shows the information for specific services.

Parameters:

  • cluster_name (defaults to: )

    The name of the cluster.

  • service_name (defaults to: )

    The name of the service.

  • host_name (defaults to: )

    The name of the host.

  • component_name (defaults to: )

    The name of the component.

  • prefix (defaults to: " ")

    The prefix to show before the services.

  • prefix_char (defaults to: " ")

    The prefix character to show before the services.

Returns:

  • The information to print.



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/exec/service_info.rb', line 99

def info_service(cluster_name = @values["cluster"], service_name = @values["service"], host_name = @values["host"], component_name = @values["component"], prefix = "  ", prefix_char = "  ")
  @logger.begin_execution("infoService")
  begin
    output = color_white("Service #{service_name} on cluster #{cluster_name}", 1) + " :\n"
    output += "Display only host '#{host_name}'\n" if !host_name.nil?
    output += "Display only component '#{component_name}'\n" if !component_name.nil?

    data = Command::AmbariServiceInfo.new(cluster_name, service_name).exec()

    data["components"] = data["components"].sort { |x, y| x["href"] <=> y["href"] }
    @logger.debug(data)
    output += format_info_service(data, host_name, component_name, prefix + prefix_char, prefix_char)
  rescue => e
    raise e
  end

  @logger.info("output info service: #{output}")
  @logger.end_execution()
  return output
end

#is_bad_status(data) ⇒ Object (private)

Indicates if a status is incorrect.

Parameters:

  • data

    The data containing the status to check.

Returns:

  • True if the status is incorrect, false otherwise.



124
125
126
127
128
129
130
131
132
133
134
# File 'lib/exec/service_info.rb', line 124

def is_bad_status(data)
  bad = false
  if !data["HostRoles"].nil? && !data["HostRoles"]["desired_state"].nil? && !data["HostRoles"]["state"].nil?
    desired_state = data["HostRoles"]["desired_state"]
    state = data["HostRoles"]["state"]
    if desired_state != state && !Helper.is_transitional_status?(state)
      bad = true
    end
  end
  return bad
end

#set_optionsObject (private)

Parse and check the parameters of the function.

Author:

  • tnoguer



38
39
40
41
42
43
44
# File 'lib/exec/service_info.rb', line 38

def set_options
  @logger.info("setting options")
  @options.add_option("C", "cluster", "The cluster name.", true, true, method(:check_cluster_name))
  @options.add_option("s", "service", "Limit the request to this service.", false, true, method(:check_hadoop_service_name))
  @options.add_option("H", "host", "Limit the request to this host.", false, true, method(:check_ambari_host_name))
  @options.add_option("c", "component", "Limit the request to this component type.", false, true, method(:check_hadoop_component_name))
end