Class: MCollective::Application::Choria

Inherits:
MCollective::Application show all
Defined in:
lib/mcollective/application/choria.rb

Instance Attribute Summary

Attributes inherited from MCollective::Application

#options

Instance Method Summary collapse

Methods inherited from MCollective::Application

[], []=, #application_cli_arguments, #application_description, #application_failure, application_options, #application_options, #application_parse_options, #application_usage, #clioptions, #configuration, description, #disconnect, exclude_argument_sections, external, external_help, #external_help, #external_main, #halt, #halt_code, #help, intialize_application_options, option, #rpcclient, #run, usage, #validate_cli_options, #validate_option

Methods included from RPC

const_missing, discovered, #empty_filter?, #printrpc, #printrpcstats, #rpcclient, #rpcoptions, stats

Instance Method Details

#choriaUtil::Choria

Creates and cache a Choria helper class

Returns:



164
165
166
# File 'lib/mcollective/application/choria.rb', line 164

def choria
  @_choria ||= Util::Choria.new(false)
end

#confirm(msg) ⇒ void

Note:

exits the application on no

This method returns an undefined value.

Asks the user to confirm something on the CLI

Parameters:

  • msg (String)

    the message to ask



180
181
182
183
184
185
186
# File 'lib/mcollective/application/choria.rb', line 180

def confirm(msg)
  print("%s (y/n) " % msg)

  $stdout.flush

  exit(1) unless $stdin.gets.strip.match?(/^(?:y|yes)$/i)
end

#mainObject



34
35
36
37
38
39
40
# File 'lib/mcollective/application/choria.rb', line 34

def main
  send("%s_command" % configuration[:command])
rescue Util::Choria::UserError
  warn("Encountered a critical error: %s" % Util.colorize(:red, $!.to_s))
rescue Util::Choria::Abort
  exit(1)
end

#post_option_parser(configuration) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/mcollective/application/choria.rb', line 17

def post_option_parser(configuration)
  if ARGV.length >= 1
    configuration[:command] = ARGV.shift
  else
    abort("Please specify a command, valid commands are: %s" % valid_commands.join(", "))
  end
end

#request_cert_commandvoid

This method returns an undefined value.

Requests a certificate from the CA



45
46
47
48
# File 'lib/mcollective/application/choria.rb', line 45

def request_cert_command
  puts("Please use 'choria enroll' to enroll in the security subsystem")
  raise(Util::Choria::Abort, "1")
end

#show_config_commandObject

rubocop:disable Metrics/MethodLength



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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/mcollective/application/choria.rb', line 50

def show_config_command # rubocop:disable Metrics/MethodLength
  disconnect

  puts "Active Choria configuration:"
  puts
  puts "The active configuration used in Choria comes from using Puppet AIO defaults, querying SRV"
  puts "records and reading configuration files.  The below information shows the completely resolved"
  puts "configuration that will be used when running MCollective commands"
  puts
  puts "MCollective related:"
  puts
  puts "    MCollective Version: %s" % MCollective::VERSION
  puts "         Choria Version: %s" % Util::Choria::VERSION
  puts "     Client Config File: %s" % Util.config_file_for_user
  puts "     Active Config File: %s" % Config.instance.configfile
  puts "      Plugin Config Dir: %s" % File.join(Config.instance.configdir, "plugin.d")
  puts "      Using SRV Records: %s" % choria.should_use_srv?
  puts "              Federated: %s" % choria.federated?
  puts "             SRV Domain: %s" % choria.srv_domain
  puts "               NATS NGS: %s" % choria.ngs?

  middleware_servers = choria.middleware_servers("puppet", 4222).map {|s, p| "%s:%s" % [s, p]}.join(", ")

  puts "     Middleware Servers: %s" % middleware_servers

  if choria.federated?
    fed_servers = choria.federation_middleware_servers
    if fed_servers
      puts "     Federation Servers: %s" % fed_servers.map {|s, p| "%s:%s" % [s, p]}.join(", ")
    else
      puts "     Federation Servers: %s (fallback to normal client settings)" % middleware_servers
    end
    puts " Federation Collectives: %s" % choria.federation_collectives.join(", ")
  end

  puts

  puppet_server = choria.puppet_server
  puppetca_server = choria.puppetca_server
  puppetdb_server = choria.puppetdb_server

  puts "Puppet related:"
  puts
  puts "       Puppet Server: %s:%s" % [puppet_server[:target], puppet_server[:port]]
  puts "     PuppetCA Server: %s:%s" % [puppetca_server[:target], puppetca_server[:port]]
  puts "     PuppetDB Server: %s:%s" % [puppetdb_server[:target], puppetdb_server[:port]]

  if choria.proxied_discovery?
    proxy_server = choria.discovery_server
    puts "     Discovery Proxy: %s:%s" % [proxy_server[:target], proxy_server[:port]]
  else
    puts "     Discovery Proxy: not using a proxy"
  end

  puts "      Facter Command: %s" % choria.facter_cmd
  puts "       Facter Domain: %s" % choria.facter_domain

  puts

  puts "Security setup:"
  puts

  valid_ssl = choria.check_ssl_setup(false) rescue false

  if valid_ssl
    puts "     Valid SSL Setup: %s" % [Util.colorize(:green, "yes")]
  else
    puts "     Valid SSL Setup: %s try running 'choria enroll'" % [Util.colorize(:red, "no")]
  end

  puts "   Security Provider: %s" % [choria.security_provider]
  puts "            Certname: %s" % [choria.certname]
  puts "       SSL Directory: %s (%s)" % [choria.ssl_dir, File.exist?(choria.ssl_dir) ? Util.colorize(:green, "found") : Util.colorize(:red, "absent")]
  puts "  Client Public Cert: %s (%s)" % [choria.client_public_cert, choria.has_client_public_cert? ? Util.colorize(:green, "found") : Util.colorize(:red, "absent")]
  puts "  Client Private Key: %s (%s)" % [choria.client_private_key, choria.has_client_private_key? ? Util.colorize(:green, "found") : Util.colorize(:red, "absent")]
  puts "             CA Path: %s (%s)" % [choria.ca_path, choria.has_ca? ? Util.colorize(:green, "found") : Util.colorize(:red, "absent")]
  puts "            CSR Path: %s (%s)" % [choria.csr_path, choria.has_csr? ? Util.colorize(:green, "found") : Util.colorize(:red, "absent")]

  if choria.has_client_public_cert?
    cn = choria.valid_certificate?(File.read(choria.client_public_cert), choria.certname, false)

    puts "      Public Cert CN: %s (%s)" % [cn, cn == choria.certname ? Util.colorize(:green, "match") : Util.colorize(:red, "does not match certname")]
  end

  if choria.credential_file?
    puts "    NATS Credentials: %s (%s)" % [
      choria.credential_file,
      File.exist?(choria.credential_file) ? Util.colorize(:green, "exit") : Util.colorize(:red, "does not exist")
    ]
    puts "         'nkeys' gem: %s" % choria.nkeys?
  end

  puts

  puts "Active Choria configuration settings as found in configuration files:"
  puts

  choria_settings = Config.instance.pluginconf.select {|k, _| k.start_with?("choria")}
  padding = choria_settings.empty? ? 2 : choria_settings.keys.map(&:length).max + 2

  if choria_settings.empty?
    puts "  No custom Choria settings found in your configuration files"
  else
    choria_settings.each do |k, v|
      puts "%#{padding}s: %s" % [k, v]
    end
  end

  puts
end

#valid_commandsArray<String>

List of valid commands this application respond to

Returns:



171
172
173
# File 'lib/mcollective/application/choria.rb', line 171

def valid_commands
  methods.grep(/_command$/).map {|c| c.to_s.gsub("_command", "")}
end

#validate_configuration(configuration) ⇒ void

This method returns an undefined value.

Validates the configuration



28
29
30
31
32
# File 'lib/mcollective/application/choria.rb', line 28

def validate_configuration(configuration)
  Util.loadclass("MCollective::Util::Choria")

  abort("Unknown command %s, valid commands are: %s" % [configuration[:command], valid_commands.join(", ")]) unless valid_commands.include?(configuration[:command])
end