Module: PuppetForgeServer::Utils::OptionParser

Included in:
Server
Defined in:
lib/puppet_forge_server/utils/option_parser.rb

Constant Summary collapse

@@DEFAULT_DAEMONIZE =
false
@@DEFAULT_PORT =
8080
@@DEFAULT_PID_FILE =
File.join(Dir.tmpdir.to_s, 'puppet-forge-server', 'server.pid')
@@DEFAULT_CACHE_DIR =
File.join(Dir.tmpdir.to_s, 'puppet-forge-server', 'cache')
@@DEFAULT_LOG_DIR =
File.join(Dir.tmpdir.to_s, 'puppet-forge-server', 'log')
@@DEFAULT_WEBUI_ROOT =
File.expand_path('../app', File.dirname(__FILE__))
@@DEFAULT_HOST =
'0.0.0.0'

Instance Method Summary collapse

Instance Method Details

#parse_options(args) ⇒ Object



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
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
# File 'lib/puppet_forge_server/utils/option_parser.rb', line 32

def parse_options(args)
  options = {:daemonize => @@DEFAULT_DAEMONIZE, :cache_basedir => @@DEFAULT_CACHE_DIR, :port => @@DEFAULT_PORT, :webui_root => @@DEFAULT_WEBUI_ROOT, :host => @@DEFAULT_HOST}
  option_parser = ::OptionParser.new do |opts|
    opts.banner = "Usage: #{File.basename $0} [options]"
    opts.version = PuppetForgeServer::VERSION

    opts.on('-p', '--port PORT', "Port number to bind to (default: #{@@DEFAULT_PORT})") do |port|
      options[:port] = port
    end

    opts.on('-b', '--bind HOST', "Host name or IP address to bind to (default: #{@@DEFAULT_HOST})") do |host|
      options[:host] = host
    end

    opts.on('-D', '--daemonize', "Run server in the background (default: #{@@DEFAULT_DAEMONIZE})") do
      options[:daemonize] = true
    end

    opts.on('--pidfile FILE', "Pid file location (default: #{@@DEFAULT_PID_FILE})") do |pidfile|
      options[:pidfile] = pidfile
    end

    options[:backend] = {'Directory' => [], 'Proxy' => [], 'Source' => []}
    opts.on('-m', '--module-dir DIR', 'Directory containing packaged modules (recursively searched)') do |module_dir|
      options[:backend]['Directory'] << module_dir
    end
    opts.on('-x', '--proxy URL', 'Remote forge URL') do |url|
      options[:backend]['Proxy'] << url
    end

    opts.on('--cache-basedir DIR', "Proxy module cache base directory (default: #{@@DEFAULT_CACHE_DIR})") do |cache_basedir|
      options[:cache_basedir] = cache_basedir
    end

    opts.on('--log-dir DIR', "Log directory (default: #{@@DEFAULT_LOG_DIR})") do |log_dir|
      options[:log_dir] = log_dir
    end

    opts.on('--webui-root DIR', "Directory containing views and other public files used for web UI: #{@@DEFAULT_WEBUI_ROOT})") do |webui_root|
      options[:webui_root] = webui_root
    end

    opts.on('--debug', 'Log everything into STDERR') do
      options[:debug] = true
    end
  end
  begin
    option_parser.parse(args)
  rescue ::OptionParser::InvalidOption => parse_error
    raise PuppetForgeServer::Errors::Expected, parse_error.message + "\n" + option_parser.help
  end

  raise PuppetForgeServer::Errors::Expected, "Web UI directory doesn't exist: #{options[:webui_root]}" unless Dir.exists?(options[:webui_root])

  # Handle option dependencies
  if options[:daemonize]
    options[:pidfile] = @@DEFAULT_PID_FILE unless options[:pidfile]
    options[:log_dir] = @@DEFAULT_LOG_DIR unless options[:log_dir]
  end

  if options[:log_dir] && !options[:daemonize]
    options[:debug] = true
  end

  return options
end