Class: Puppetserver::Ca::Cli

Inherits:
Object
  • Object
show all
Defined in:
lib/puppetserver/ca/cli.rb

Constant Summary collapse

<<-BANNER
Usage: puppetserver ca <action> [options]

Manage the Private Key Infrastructure for
Puppet Server's built-in Certificate Authority
BANNER
INIT_ACTIONS =
{
  'import'   => Action::Import,
  'setup'    => Action::Setup,
}
MAINT_ACTIONS =
{
  'clean'    => Action::Clean,
  'generate' => Action::Generate,
  'list'     => Action::List,
  'revoke'   => Action::Revoke,
  'sign'     => Action::Sign
}
VALID_ACTIONS =
INIT_ACTIONS.merge(MAINT_ACTIONS).sort.to_h
ACTION_LIST =
"\nAvailable Actions:\n\n" +
"  Certificate Actions (requires a running Puppet Server):\n\n" +
MAINT_ACTIONS.map do |action, cls|
  "    #{action}\t#{cls::SUMMARY}"
end.join("\n") + "\n\n" +
"  Initialization Actions (requires Puppet Server to be stopped):\n\n" +
INIT_ACTIONS.map do |action, cls|
  "    #{action}\t#{cls::SUMMARY}"
end.join("\n")
ACTION_OPTIONS =
"\nAction Options:\n" +
VALID_ACTIONS.map do |action, cls|
  action_summary = cls.parser.summarize.
                     select{|line| line =~ /^\s*--/ }.
                     reject{|line| line =~ /--help|--version/ }
  summary = action_summary.empty? ? '      N/A' : action_summary.join('')

  "  #{action}:\n" + summary
end.join("\n")

Class Method Summary collapse

Class Method Details

.parse_general_inputs(inputs) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/puppetserver/ca/cli.rb', line 98

def self.parse_general_inputs(inputs)
  parsed = {}
  general_parser = OptionParser.new do |opts|
    opts.banner = BANNER
    opts.separator ACTION_LIST
    opts.separator "\nGeneral Options:"

    opts.on('--help', 'Display this general help output') do |help|
      parsed['help'] = true
    end
    opts.on('--version', 'Display the version') do |v|
      parsed['version'] = true
    end

    opts.separator ACTION_OPTIONS
    opts.separator "\nSee `puppetserver ca <action> --help` for detailed info"

  end

  all,_,_,_ = Utils::CliParsing.parse_without_raising(general_parser, inputs)

  return general_parser, parsed, all
end

.run(cli_args = ARGV, out = STDOUT, err = STDERR) ⇒ Object



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
# File 'lib/puppetserver/ca/cli.rb', line 60

def self.run(cli_args = ARGV, out = STDOUT, err = STDERR)
  logger = Puppetserver::Ca::Logger.new(:info, out, err)
  parser, general_options, unparsed = parse_general_inputs(cli_args)

  if general_options['version']
    logger.inform Puppetserver::Ca::VERSION
    return 0
  end

  action_argument = unparsed.shift
  action_class = VALID_ACTIONS[action_argument]

  if general_options['help']
    if action_class
      logger.inform action_class.parser.help
    else
      logger.inform parser.help
    end

    return 0
  end

  if action_class
    action = action_class.new(logger)
    input, exit_code = action.parse(unparsed)

    if exit_code
      return exit_code
    else
      return action.run(input)
    end
  else
    logger.warn "Unknown action: #{action_argument}"
    logger.warn parser.help
    return 1
  end
end