Class: Exec::ServiceLs

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

Overview

Allows the user to list the services.

Author:

  • tnoguer

Defined Under Namespace

Classes: CustomCommandOption

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, #initialize, #run

Constructor Details

This class inherits a constructor from Exec::ExecutableCommand

Instance Method Details

#execObject

The execution of the command.

Author:

  • tnoguer



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/exec/service_ls.rb', line 31

def exec
  @logger.begin_main_step("service list getting")
  Color::print_log("NONE", "getting service list...")
  begin
    if @values["all"]
      msg=list_all_clusters_services()
    else
      msg=list_services()
    end
    Color::echo_ok
    @stdout.puts(msg)
  rescue => e
    Color::echo_fail(@stdout)
    raise e
  end
  @logger.end_main_step("service list getting")
end

#format_service(service, prefix = '') ⇒ Object (private)

Raises:

  • (StandardError)


93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/exec/service_ls.rb', line 93

def format_service(service, prefix = '')
  raise(StandardError, "Malformed return request") if service["ServiceInfo"].nil? || service["ServiceInfo"]["service_name"].nil?
  output = prefix + service["ServiceInfo"]["service_name"] + "\n"
  unless service["components"].nil?
    service["components"] = service["components"].sort { |x, y| x["href"] <=> y["href"] }
    service["components"].each do |component|
      output+= "\t" + prefix + component["ServiceComponentInfo"]["component_name"] + "\n"
      unless component["host_components"].nil?
        component["host_components"] = component["host_components"].sort { |x, y| x["href"] <=> y["href"] }
        component["host_components"].each do |host|
          output += "\t\t" + prefix + host["HostRoles"]["host_name"] + "\n"
        end
      end
    end
  end
  return output
end

#list_all_clusters_servicesObject (private)

list all th service of all the cluster



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/exec/service_ls.rb', line 51

def list_all_clusters_services
  cmd = Command::AmbariClusterList.new()
  data = cmd.exec()
  output = ""
  unless data["items"].nil?
    data["items"] = data["items"].sort { |x, y| x["href"] <=> y["href"] }
    data["items"].each do |cluster|
      output += "Cluster " + cluster["Clusters"]["cluster_name"] + "\n"
      output += list_services(cluster["Clusters"]["cluster_name"], "\t") + "\n"
    end
  end
  return output
end

#list_services(cluster_name = , prefix = '') ⇒ Object (private)

list the service for one cluster

Parameters:

  • cluster_name (defaults to: )

    Name of the cluster



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

def list_services(cluster_name = @values["cluster"], prefix = '')
  @logger.begin_main_step("listServices")
  begin
    output = prefix + "Services available on cluster #{cluster_name}:"
    command = Command::AmbariServiceList.new(cluster_name)
    data = command.exec()

    return prefix + "No service" if data["items"].nil? || data["items"].length == 0

    data["services"] = data["items"].sort { |x, y| x["href"] <=> y["href"] }
    output += "\n"
    data["services"].each do |service|
      output += format_service(service, prefix + "\t")
    end
  rescue => e
    raise e
  end

  @logger.debug("service list output: #{output}")
  @logger.end_main_step("listServices")

  return output
end

#set_optionsObject (private)

Parse and check the parameters of the function.

Author:

  • tmarmin



22
23
24
25
26
# File 'lib/exec/service_ls.rb', line 22

def set_options
  @logger.info("setting options")
  @options.add_option("C", "cluster", "The cluster name where to list services.", true, true, method(:check_cluster_name))
  @options.add_option("a", "all", "List the services of all clusters.", false)
end