Class: CertificateDepot::Runner
- Inherits:
-
Object
- Object
- CertificateDepot::Runner
- Defined in:
- lib/certificate_depot/runner.rb
Overview
The Runner class handles commands issued to the command-line utility.
Instance Method Summary collapse
-
#initialize(argv) ⇒ Runner
constructor
A new instance of Runner.
-
#no_path(argv) ⇒ Object
Utility method which returns false if there is a path in argv.
-
#parser ⇒ Object
Returns an option parser.
-
#run ⇒ Object
Runs the command found in the arguments.
-
#run_command(command, argv) ⇒ Object
Runs command with arguments.
Constructor Details
#initialize(argv) ⇒ Runner
Returns a new instance of Runner.
6 7 8 9 |
# File 'lib/certificate_depot/runner.rb', line 6 def initialize(argv) @argv = argv @options = {} end |
Instance Method Details
#no_path(argv) ⇒ Object
Utility method which returns false if there is a path in argv. When there is no path in argv it returns true and prins a warning.
74 75 76 77 78 79 80 81 |
# File 'lib/certificate_depot/runner.rb', line 74 def no_path(argv) if argv.length == 0 puts "[!] Please specify the path to the depot you want to operate on" true else false end end |
#parser ⇒ Object
Returns an option parser.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 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 |
# File 'lib/certificate_depot/runner.rb', line 12 def parser @parser ||= OptionParser.new do |opts| # --------------------------------------------------------------------------- opts. = "Usage: depot [command] [options]" opts.separator "" opts.separator "Commands:" opts.separator " init <path> [name] Create a new depot on disk. You probably want" opts.separator " to run init as root to make sure your keys" opts.separator " will be safe." opts.separator "" opts.separator " generate <path> Create a new certificate. Writes a pem" opts.separator " with a private key and a certificate to" opts.separator " standard output" opts.separator " --type Create a client or server certificate" opts.separator "" opts.separator " config <path> Shows a configuration example for Apache for" opts.separator " the depot." opts.separator "" opts.separator " start <path> Start a server." opts.separator "" opts.separator " stop Stop a running server." opts.separator "" opts.separator "Options:" opts.on("-c", "--cn [COMMON_NAME]", "Set the common name to use in the generated certificate") do |common_name| @options[:common_name] = common_name end opts.on("-e", "--email [EMAIL]", "Set the email to use in the generated certificate") do |email| @options[:email_address] = email end opts.on( "-u", "--uid [USERID]", "Set the user id to use in the generated certificate" ) do |user_id| @options[:user_id] = user_id end opts.on("-t", "--type [TYPE]", "Generate a certificate of a certain type (server|client)") do |type| @options[:type] = type.intern end opts.on("-H", "--host [HOST]", "IP address or hostname to listen on (127.0.0.1)") do |host| @options[:host] = host end opts.on("-P", "--port [PORT]", "The port to listen on (35553)") do |port| @options[:port] = port.to_i end opts.on("-n", "--process-count [COUNT]", "The number of worker processes to spawn (2)") do |process_count| @options[:process_count] = process_count.to_i end opts.on("-q", "--max-connection-queue [MAX]", "The number of requests to queue on the server (10)") do |max_connection_queue| @options[:max_connection_queue] = max_connection_queue.to_i end opts.on("-p", "--pid-file [PID_FILE]", "The file to store the server PID in (/var/run/depot.pid)") do |pid_file| @options[:pid_file] = pid_file end opts.on("-l", "--log-file [LOG_FILE]", "The file to store the server log in (/var/log/depot.log)") do |log_file| @options[:log_file] = log_file end opts.on("-h", "--help", "Show help") do puts opts exit end end end |
#run ⇒ Object
Runs the command found in the arguments. If the arguments don’t contain a command the help message is show.
128 129 130 131 132 133 134 135 136 |
# File 'lib/certificate_depot/runner.rb', line 128 def run argv = @argv.dup parser.parse!(argv) if command = argv.shift run_command(command.to_sym, argv) else puts parser.to_s end end |
#run_command(command, argv) ⇒ Object
Runs command with arguments. Commands and arguments are documented in the help message of the command-line utility.
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 |
# File 'lib/certificate_depot/runner.rb', line 85 def run_command(command, argv) path = File.(argv[0].to_s) case command when :init return if no_path(argv) if argv[1] label = argv[1..-1].join(' ') else label = path.split('/').last end CertificateDepot.create(path, label, @options) when :generate return if no_path(argv) unless [:server, :client].include?(@options[:type]) puts "[!] Unknown certificate type `#{@options[:type]}', please specify either server or client with the --type option" else keypair, certificate = CertificateDepot.generate_keypair_and_certificate(path, @options) puts keypair.private_key.to_s puts certificate.certificate.to_s end when :config return if no_path(argv) puts CertificateDepot.configuration_example(path) when :start return if no_path(argv) if CertificateDepot.start(path, @options) puts "[!] Starting server" else puts "[!] Can't start the server" end when :stop if CertificateDepot.stop(@options) puts "[!] Stopping server" else puts "[!] Can't find a running server" end else puts parser.to_s end end |