Class: Opener::Webservice::OptionParser

Inherits:
Object
  • Object
show all
Defined in:
lib/opener/webservice/option_parser.rb

Overview

Slop wrapper for parsing webservice options and passing them to Puma.

Constant Summary collapse

ENV_OPTIONS =

Mapping of environment variables and Slop options.

Returns:

  • (Hash)
{
  'OUTPUT_BUCKET'           => :bucket,
  'AUTHENTICATION_TOKEN'    => :token,
  'AUTHENTICATION_SECRET'   => :secret,
  'AUTHENTICATION_ENDPOINT' => :authentication
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, rackup) ⇒ OptionParser

Returns a new instance of OptionParser.

Parameters:

  • name (String)
  • rackup (String)


36
37
38
39
40
# File 'lib/opener/webservice/option_parser.rb', line 36

def initialize(name, rackup)
  @name   = name
  @rackup = rackup
  @parser = configure_slop
end

Instance Attribute Details

#nameString (readonly)

The name of the component.

Returns:

  • (String)


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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/opener/webservice/option_parser.rb', line 17

class OptionParser
  attr_reader :name, :rackup, :parser

  ##
  # Mapping of environment variables and Slop options.
  #
  # @return [Hash]
  #
  ENV_OPTIONS = {
    'OUTPUT_BUCKET'           => :bucket,
    'AUTHENTICATION_TOKEN'    => :token,
    'AUTHENTICATION_SECRET'   => :secret,
    'AUTHENTICATION_ENDPOINT' => :authentication
  }

  ##
  # @param [String] name
  # @param [String] rackup
  #
  def initialize(name, rackup)
    @name   = name
    @rackup = rackup
    @parser = configure_slop
  end

  def parse(*args)
    parser.parse(*args)
  end

  ##
  # Parses the given CLI options and starts Puma.
  #
  # @param [Array] argv
  #
  def run(argv = ARGV)
    parser.parse(argv)
  end

  ##
  # @return [Slop]
  #
  def configure_slop
    outer       = self
    server_name = "#{name}-server"
    cli_name    = server_name.gsub('opener-', '')

    return Slop.new(:strict => false, :indent => 2) do
      banner "Usage: #{cli_name} [RACKUP] [OPTIONS]"

      separator "\nAbout:\n\nRuns the OpeNER component as a webservice using Puma. For example:\n\n    language-identifier-server --daemon\n\nThis would start a language identifier server in the background.\n\nEnvironment Variables:\n\nThese daemons make use of Amazon SQS queues and other Amazon services. In\norder to use these services you should make sure the following environment\nvariables are set:\n\n* AWS_ACCESS_KEY_ID\n* AWS_SECRET_ACCESS_KEY\n* AWS_REGION\n\nIf you're running this daemon on an EC2 instance then the first two\nenvironment variables will be set automatically if the instance has an\nassociated IAM profile. The AWS_REGION variable must _always_ be set.\n\nOptionally you can also set the following extra variables:\n\n* NEWRELIC_TOKEN: when set the daemon will send profiling data to New Relic\n  using this token. The application name will be \"\#{server_name}\".\n\n* ROLLBAR_TOKEN: when set the daemon will report errors to Rollbar using\n  this token. You can freely use this in combination with NEWRELIC_TOKEN.\n\nPuma Options:\n\nThis webserver uses Puma under the hood, but defines its own CLI options.\nAll unrecognized options are passed to the Puma CLI. For more information\non the available options for Puma, run `\#{cli_name} --puma-help`.\n      EOF\n\n      separator \"\\nOptions:\\n\"\n\n      on :h, :help, 'Shows this help message' do\n        abort to_s\n      end\n\n      on :'puma-help', 'Shows the options of Puma' do\n        Puma::CLI.new(['--help']).run\n\n        abort\n      end\n\n      on :b=,\n        :bucket=,\n        'The S3 bucket to store output in',\n        :as => String\n\n      on :authentication=,\n        'An authentication endpoint to use',\n        :as => String\n\n      on :secret=,\n        'Parameter name for the authentication secret',\n        :as => String\n\n      on :token=,\n        'Parameter name for the authentication token',\n        :as => String\n\n      on :'disable-syslog', 'Disables Syslog logging (enabled by default)'\n\n      run do |opts, args|\n        puma_args = [outer.rackup] + args\n\n        ENV['APP_NAME'] = outer.name\n        ENV['APP_ROOT'] = File.expand_path('../../../../', __FILE__)\n        ENV['NRCONFIG'] = File.join(ENV['APP_ROOT'], 'config/newrelic.yml')\n\n        ENV_OPTIONS.each do |key, opt|\n          ENV[key] = opts[opt]\n        end\n\n        unless opts[:'disable-syslog']\n          ENV['ENABLE_SYSLOG'] = '1'\n        end\n\n        if !ENV['RAILS_ENV'] and ENV['RACK_ENV']\n          ENV['RAILS_ENV'] = ENV['RACK_ENV']\n        end\n\n        if ENV['NEWRELIC_TOKEN']\n          NewRelic::Control.instance.init_plugin\n\n          # Enable the GC profiler for New Relic.\n          GC::Profiler.enable\n        end\n\n        if Configuration.syslog?\n          Core::Syslog.open(\n            ENV['APP_NAME'],\n            ::Syslog::LOG_CONS | ::Syslog::LOG_PID\n          )\n        end\n\n        Configuration.configure_rollbar\n\n        # Puma on JRuby does some weird stuff with forking/exec. As a result\n        # of this we *have to* update ARGV as otherwise running Puma as a\n        # daemon does not work.\n        ARGV.replace(puma_args)\n\n        Puma::CLI.new(puma_args).run\n      end\n    end\n  end\nend\n".chomp

#parserSlop (readonly)

Returns:

  • (Slop)


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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/opener/webservice/option_parser.rb', line 17

class OptionParser
  attr_reader :name, :rackup, :parser

  ##
  # Mapping of environment variables and Slop options.
  #
  # @return [Hash]
  #
  ENV_OPTIONS = {
    'OUTPUT_BUCKET'           => :bucket,
    'AUTHENTICATION_TOKEN'    => :token,
    'AUTHENTICATION_SECRET'   => :secret,
    'AUTHENTICATION_ENDPOINT' => :authentication
  }

  ##
  # @param [String] name
  # @param [String] rackup
  #
  def initialize(name, rackup)
    @name   = name
    @rackup = rackup
    @parser = configure_slop
  end

  def parse(*args)
    parser.parse(*args)
  end

  ##
  # Parses the given CLI options and starts Puma.
  #
  # @param [Array] argv
  #
  def run(argv = ARGV)
    parser.parse(argv)
  end

  ##
  # @return [Slop]
  #
  def configure_slop
    outer       = self
    server_name = "#{name}-server"
    cli_name    = server_name.gsub('opener-', '')

    return Slop.new(:strict => false, :indent => 2) do
      banner "Usage: #{cli_name} [RACKUP] [OPTIONS]"

      separator "\nAbout:\n\nRuns the OpeNER component as a webservice using Puma. For example:\n\n    language-identifier-server --daemon\n\nThis would start a language identifier server in the background.\n\nEnvironment Variables:\n\nThese daemons make use of Amazon SQS queues and other Amazon services. In\norder to use these services you should make sure the following environment\nvariables are set:\n\n* AWS_ACCESS_KEY_ID\n* AWS_SECRET_ACCESS_KEY\n* AWS_REGION\n\nIf you're running this daemon on an EC2 instance then the first two\nenvironment variables will be set automatically if the instance has an\nassociated IAM profile. The AWS_REGION variable must _always_ be set.\n\nOptionally you can also set the following extra variables:\n\n* NEWRELIC_TOKEN: when set the daemon will send profiling data to New Relic\n  using this token. The application name will be \"\#{server_name}\".\n\n* ROLLBAR_TOKEN: when set the daemon will report errors to Rollbar using\n  this token. You can freely use this in combination with NEWRELIC_TOKEN.\n\nPuma Options:\n\nThis webserver uses Puma under the hood, but defines its own CLI options.\nAll unrecognized options are passed to the Puma CLI. For more information\non the available options for Puma, run `\#{cli_name} --puma-help`.\n      EOF\n\n      separator \"\\nOptions:\\n\"\n\n      on :h, :help, 'Shows this help message' do\n        abort to_s\n      end\n\n      on :'puma-help', 'Shows the options of Puma' do\n        Puma::CLI.new(['--help']).run\n\n        abort\n      end\n\n      on :b=,\n        :bucket=,\n        'The S3 bucket to store output in',\n        :as => String\n\n      on :authentication=,\n        'An authentication endpoint to use',\n        :as => String\n\n      on :secret=,\n        'Parameter name for the authentication secret',\n        :as => String\n\n      on :token=,\n        'Parameter name for the authentication token',\n        :as => String\n\n      on :'disable-syslog', 'Disables Syslog logging (enabled by default)'\n\n      run do |opts, args|\n        puma_args = [outer.rackup] + args\n\n        ENV['APP_NAME'] = outer.name\n        ENV['APP_ROOT'] = File.expand_path('../../../../', __FILE__)\n        ENV['NRCONFIG'] = File.join(ENV['APP_ROOT'], 'config/newrelic.yml')\n\n        ENV_OPTIONS.each do |key, opt|\n          ENV[key] = opts[opt]\n        end\n\n        unless opts[:'disable-syslog']\n          ENV['ENABLE_SYSLOG'] = '1'\n        end\n\n        if !ENV['RAILS_ENV'] and ENV['RACK_ENV']\n          ENV['RAILS_ENV'] = ENV['RACK_ENV']\n        end\n\n        if ENV['NEWRELIC_TOKEN']\n          NewRelic::Control.instance.init_plugin\n\n          # Enable the GC profiler for New Relic.\n          GC::Profiler.enable\n        end\n\n        if Configuration.syslog?\n          Core::Syslog.open(\n            ENV['APP_NAME'],\n            ::Syslog::LOG_CONS | ::Syslog::LOG_PID\n          )\n        end\n\n        Configuration.configure_rollbar\n\n        # Puma on JRuby does some weird stuff with forking/exec. As a result\n        # of this we *have to* update ARGV as otherwise running Puma as a\n        # daemon does not work.\n        ARGV.replace(puma_args)\n\n        Puma::CLI.new(puma_args).run\n      end\n    end\n  end\nend\n".chomp

#rackupString (readonly)

Path to the config.ru to use.

Returns:

  • (String)


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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/opener/webservice/option_parser.rb', line 17

class OptionParser
  attr_reader :name, :rackup, :parser

  ##
  # Mapping of environment variables and Slop options.
  #
  # @return [Hash]
  #
  ENV_OPTIONS = {
    'OUTPUT_BUCKET'           => :bucket,
    'AUTHENTICATION_TOKEN'    => :token,
    'AUTHENTICATION_SECRET'   => :secret,
    'AUTHENTICATION_ENDPOINT' => :authentication
  }

  ##
  # @param [String] name
  # @param [String] rackup
  #
  def initialize(name, rackup)
    @name   = name
    @rackup = rackup
    @parser = configure_slop
  end

  def parse(*args)
    parser.parse(*args)
  end

  ##
  # Parses the given CLI options and starts Puma.
  #
  # @param [Array] argv
  #
  def run(argv = ARGV)
    parser.parse(argv)
  end

  ##
  # @return [Slop]
  #
  def configure_slop
    outer       = self
    server_name = "#{name}-server"
    cli_name    = server_name.gsub('opener-', '')

    return Slop.new(:strict => false, :indent => 2) do
      banner "Usage: #{cli_name} [RACKUP] [OPTIONS]"

      separator "\nAbout:\n\nRuns the OpeNER component as a webservice using Puma. For example:\n\n    language-identifier-server --daemon\n\nThis would start a language identifier server in the background.\n\nEnvironment Variables:\n\nThese daemons make use of Amazon SQS queues and other Amazon services. In\norder to use these services you should make sure the following environment\nvariables are set:\n\n* AWS_ACCESS_KEY_ID\n* AWS_SECRET_ACCESS_KEY\n* AWS_REGION\n\nIf you're running this daemon on an EC2 instance then the first two\nenvironment variables will be set automatically if the instance has an\nassociated IAM profile. The AWS_REGION variable must _always_ be set.\n\nOptionally you can also set the following extra variables:\n\n* NEWRELIC_TOKEN: when set the daemon will send profiling data to New Relic\n  using this token. The application name will be \"\#{server_name}\".\n\n* ROLLBAR_TOKEN: when set the daemon will report errors to Rollbar using\n  this token. You can freely use this in combination with NEWRELIC_TOKEN.\n\nPuma Options:\n\nThis webserver uses Puma under the hood, but defines its own CLI options.\nAll unrecognized options are passed to the Puma CLI. For more information\non the available options for Puma, run `\#{cli_name} --puma-help`.\n      EOF\n\n      separator \"\\nOptions:\\n\"\n\n      on :h, :help, 'Shows this help message' do\n        abort to_s\n      end\n\n      on :'puma-help', 'Shows the options of Puma' do\n        Puma::CLI.new(['--help']).run\n\n        abort\n      end\n\n      on :b=,\n        :bucket=,\n        'The S3 bucket to store output in',\n        :as => String\n\n      on :authentication=,\n        'An authentication endpoint to use',\n        :as => String\n\n      on :secret=,\n        'Parameter name for the authentication secret',\n        :as => String\n\n      on :token=,\n        'Parameter name for the authentication token',\n        :as => String\n\n      on :'disable-syslog', 'Disables Syslog logging (enabled by default)'\n\n      run do |opts, args|\n        puma_args = [outer.rackup] + args\n\n        ENV['APP_NAME'] = outer.name\n        ENV['APP_ROOT'] = File.expand_path('../../../../', __FILE__)\n        ENV['NRCONFIG'] = File.join(ENV['APP_ROOT'], 'config/newrelic.yml')\n\n        ENV_OPTIONS.each do |key, opt|\n          ENV[key] = opts[opt]\n        end\n\n        unless opts[:'disable-syslog']\n          ENV['ENABLE_SYSLOG'] = '1'\n        end\n\n        if !ENV['RAILS_ENV'] and ENV['RACK_ENV']\n          ENV['RAILS_ENV'] = ENV['RACK_ENV']\n        end\n\n        if ENV['NEWRELIC_TOKEN']\n          NewRelic::Control.instance.init_plugin\n\n          # Enable the GC profiler for New Relic.\n          GC::Profiler.enable\n        end\n\n        if Configuration.syslog?\n          Core::Syslog.open(\n            ENV['APP_NAME'],\n            ::Syslog::LOG_CONS | ::Syslog::LOG_PID\n          )\n        end\n\n        Configuration.configure_rollbar\n\n        # Puma on JRuby does some weird stuff with forking/exec. As a result\n        # of this we *have to* update ARGV as otherwise running Puma as a\n        # daemon does not work.\n        ARGV.replace(puma_args)\n\n        Puma::CLI.new(puma_args).run\n      end\n    end\n  end\nend\n".chomp

Instance Method Details

#configure_slopSlop

Returns:

  • (Slop)


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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/opener/webservice/option_parser.rb', line 58

def configure_slop
  outer       = self
  server_name = "#{name}-server"
  cli_name    = server_name.gsub('opener-', '')

  return Slop.new(:strict => false, :indent => 2) do
    banner "Usage: #{cli_name} [RACKUP] [OPTIONS]"

    separator "\nAbout:\n\n    Runs the OpeNER component as a webservice using Puma. For example:\n\n  language-identifier-server --daemon\n\n    This would start a language identifier server in the background.\n\nEnvironment Variables:\n\n    These daemons make use of Amazon SQS queues and other Amazon services. In\n    order to use these services you should make sure the following environment\n    variables are set:\n\n    * AWS_ACCESS_KEY_ID\n    * AWS_SECRET_ACCESS_KEY\n    * AWS_REGION\n\n    If you're running this daemon on an EC2 instance then the first two\n    environment variables will be set automatically if the instance has an\n    associated IAM profile. The AWS_REGION variable must _always_ be set.\n\n    Optionally you can also set the following extra variables:\n\n    * NEWRELIC_TOKEN: when set the daemon will send profiling data to New Relic\nusing this token. The application name will be \"\#{server_name}\".\n\n    * ROLLBAR_TOKEN: when set the daemon will report errors to Rollbar using\nthis token. You can freely use this in combination with NEWRELIC_TOKEN.\n\nPuma Options:\n\n    This webserver uses Puma under the hood, but defines its own CLI options.\n    All unrecognized options are passed to the Puma CLI. For more information\n    on the available options for Puma, run `\#{cli_name} --puma-help`.\n    EOF\n\n    separator \"\\nOptions:\\n\"\n\n    on :h, :help, 'Shows this help message' do\n      abort to_s\n    end\n\n    on :'puma-help', 'Shows the options of Puma' do\n      Puma::CLI.new(['--help']).run\n\n      abort\n    end\n\n    on :b=,\n      :bucket=,\n      'The S3 bucket to store output in',\n      :as => String\n\n    on :authentication=,\n      'An authentication endpoint to use',\n      :as => String\n\n    on :secret=,\n      'Parameter name for the authentication secret',\n      :as => String\n\n    on :token=,\n      'Parameter name for the authentication token',\n      :as => String\n\n    on :'disable-syslog', 'Disables Syslog logging (enabled by default)'\n\n    run do |opts, args|\n      puma_args = [outer.rackup] + args\n\n      ENV['APP_NAME'] = outer.name\n      ENV['APP_ROOT'] = File.expand_path('../../../../', __FILE__)\n      ENV['NRCONFIG'] = File.join(ENV['APP_ROOT'], 'config/newrelic.yml')\n\n      ENV_OPTIONS.each do |key, opt|\n        ENV[key] = opts[opt]\n      end\n\n      unless opts[:'disable-syslog']\n        ENV['ENABLE_SYSLOG'] = '1'\n      end\n\n      if !ENV['RAILS_ENV'] and ENV['RACK_ENV']\n        ENV['RAILS_ENV'] = ENV['RACK_ENV']\n      end\n\n      if ENV['NEWRELIC_TOKEN']\n        NewRelic::Control.instance.init_plugin\n\n        # Enable the GC profiler for New Relic.\n        GC::Profiler.enable\n      end\n\n      if Configuration.syslog?\n        Core::Syslog.open(\n          ENV['APP_NAME'],\n          ::Syslog::LOG_CONS | ::Syslog::LOG_PID\n        )\n      end\n\n      Configuration.configure_rollbar\n\n      # Puma on JRuby does some weird stuff with forking/exec. As a result\n      # of this we *have to* update ARGV as otherwise running Puma as a\n      # daemon does not work.\n      ARGV.replace(puma_args)\n\n      Puma::CLI.new(puma_args).run\n    end\n  end\nend\n".chomp

#parse(*args) ⇒ Object



42
43
44
# File 'lib/opener/webservice/option_parser.rb', line 42

def parse(*args)
  parser.parse(*args)
end

#run(argv = ARGV) ⇒ Object

Parses the given CLI options and starts Puma.

Parameters:

  • argv (Array) (defaults to: ARGV)


51
52
53
# File 'lib/opener/webservice/option_parser.rb', line 51

def run(argv = ARGV)
  parser.parse(argv)
end