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
# 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 <<-EOF.chomp

About:

Runs the OpeNER component as a webservice using Puma. For example:

    language-identifier-server --daemon

This would start a language identifier server in the background.

Environment Variables:

These daemons make use of Amazon SQS queues and other Amazon services. In
order to use these services you should make sure the following environment
variables are set:

* AWS_ACCESS_KEY_ID
* AWS_SECRET_ACCESS_KEY
* AWS_REGION

If you're running this daemon on an EC2 instance then the first two
environment variables will be set automatically if the instance has an
associated IAM profile. The AWS_REGION variable must _always_ be set.

Optionally you can also set the following extra variables:

* NEWRELIC_TOKEN: when set the daemon will send profiling data to New Relic
  using this token. The application name will be "#{server_name}".

* ROLLBAR_TOKEN: when set the daemon will report errors to Rollbar using
  this token. You can freely use this in combination with NEWRELIC_TOKEN.

Puma Options:

This webserver uses Puma under the hood, but defines its own CLI options.
All unrecognized options are passed to the Puma CLI. For more information
on the available options for Puma, run `#{cli_name} --puma-help`.
      EOF

      separator "\nOptions:\n"

      on :h, :help, 'Shows this help message' do
        abort to_s
      end

      on :'puma-help', 'Shows the options of Puma' do
        Puma::CLI.new(['--help']).run

        abort
      end

      on :bucket=,
        'The S3 bucket to store output in',
        :as => String

      on :authentication=,
        'An authentication endpoint to use',
        :as => String

      on :secret=,
        'Parameter name for the authentication secret',
        :as => String

      on :token=,
        'Parameter name for the authentication token',
        :as => String

      on :'disable-syslog', 'Disables Syslog logging (enabled by default)'

      run do |opts, args|
        puma_args = [outer.rackup] + args

        ENV['APP_NAME'] = outer.name
        ENV['APP_ROOT'] = File.expand_path('../../../../', __FILE__)
        ENV['NRCONFIG'] = File.join(ENV['APP_ROOT'], 'config/newrelic.yml')

        ENV_OPTIONS.each do |key, opt|
          ENV[key] = opts[opt]
        end

        unless opts[:'disable-syslog']
          ENV['ENABLE_SYSLOG'] = '1'
        end

        if !ENV['RAILS_ENV'] and ENV['RACK_ENV']
          ENV['RAILS_ENV'] = ENV['RACK_ENV']
        end

        if ENV['NEWRELIC_TOKEN']
          NewRelic::Control.instance.init_plugin

          # Enable the GC profiler for New Relic.
          GC::Profiler.enable
        end

        if Configuration.syslog?
          Core::Syslog.open(
            ENV['APP_NAME'],
            ::Syslog::LOG_CONS | ::Syslog::LOG_PID
          )
        end

        Configuration.configure_rollbar

        # Puma on JRuby does some weird stuff with forking/exec. As a result
        # of this we *have to* update ARGV as otherwise running Puma as a
        # daemon does not work.
        ARGV.replace(puma_args)

        Puma::CLI.new(puma_args).run
      end
    end
  end
end

#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
# 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 <<-EOF.chomp

About:

Runs the OpeNER component as a webservice using Puma. For example:

    language-identifier-server --daemon

This would start a language identifier server in the background.

Environment Variables:

These daemons make use of Amazon SQS queues and other Amazon services. In
order to use these services you should make sure the following environment
variables are set:

* AWS_ACCESS_KEY_ID
* AWS_SECRET_ACCESS_KEY
* AWS_REGION

If you're running this daemon on an EC2 instance then the first two
environment variables will be set automatically if the instance has an
associated IAM profile. The AWS_REGION variable must _always_ be set.

Optionally you can also set the following extra variables:

* NEWRELIC_TOKEN: when set the daemon will send profiling data to New Relic
  using this token. The application name will be "#{server_name}".

* ROLLBAR_TOKEN: when set the daemon will report errors to Rollbar using
  this token. You can freely use this in combination with NEWRELIC_TOKEN.

Puma Options:

This webserver uses Puma under the hood, but defines its own CLI options.
All unrecognized options are passed to the Puma CLI. For more information
on the available options for Puma, run `#{cli_name} --puma-help`.
      EOF

      separator "\nOptions:\n"

      on :h, :help, 'Shows this help message' do
        abort to_s
      end

      on :'puma-help', 'Shows the options of Puma' do
        Puma::CLI.new(['--help']).run

        abort
      end

      on :bucket=,
        'The S3 bucket to store output in',
        :as => String

      on :authentication=,
        'An authentication endpoint to use',
        :as => String

      on :secret=,
        'Parameter name for the authentication secret',
        :as => String

      on :token=,
        'Parameter name for the authentication token',
        :as => String

      on :'disable-syslog', 'Disables Syslog logging (enabled by default)'

      run do |opts, args|
        puma_args = [outer.rackup] + args

        ENV['APP_NAME'] = outer.name
        ENV['APP_ROOT'] = File.expand_path('../../../../', __FILE__)
        ENV['NRCONFIG'] = File.join(ENV['APP_ROOT'], 'config/newrelic.yml')

        ENV_OPTIONS.each do |key, opt|
          ENV[key] = opts[opt]
        end

        unless opts[:'disable-syslog']
          ENV['ENABLE_SYSLOG'] = '1'
        end

        if !ENV['RAILS_ENV'] and ENV['RACK_ENV']
          ENV['RAILS_ENV'] = ENV['RACK_ENV']
        end

        if ENV['NEWRELIC_TOKEN']
          NewRelic::Control.instance.init_plugin

          # Enable the GC profiler for New Relic.
          GC::Profiler.enable
        end

        if Configuration.syslog?
          Core::Syslog.open(
            ENV['APP_NAME'],
            ::Syslog::LOG_CONS | ::Syslog::LOG_PID
          )
        end

        Configuration.configure_rollbar

        # Puma on JRuby does some weird stuff with forking/exec. As a result
        # of this we *have to* update ARGV as otherwise running Puma as a
        # daemon does not work.
        ARGV.replace(puma_args)

        Puma::CLI.new(puma_args).run
      end
    end
  end
end

#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
# 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 <<-EOF.chomp

About:

Runs the OpeNER component as a webservice using Puma. For example:

    language-identifier-server --daemon

This would start a language identifier server in the background.

Environment Variables:

These daemons make use of Amazon SQS queues and other Amazon services. In
order to use these services you should make sure the following environment
variables are set:

* AWS_ACCESS_KEY_ID
* AWS_SECRET_ACCESS_KEY
* AWS_REGION

If you're running this daemon on an EC2 instance then the first two
environment variables will be set automatically if the instance has an
associated IAM profile. The AWS_REGION variable must _always_ be set.

Optionally you can also set the following extra variables:

* NEWRELIC_TOKEN: when set the daemon will send profiling data to New Relic
  using this token. The application name will be "#{server_name}".

* ROLLBAR_TOKEN: when set the daemon will report errors to Rollbar using
  this token. You can freely use this in combination with NEWRELIC_TOKEN.

Puma Options:

This webserver uses Puma under the hood, but defines its own CLI options.
All unrecognized options are passed to the Puma CLI. For more information
on the available options for Puma, run `#{cli_name} --puma-help`.
      EOF

      separator "\nOptions:\n"

      on :h, :help, 'Shows this help message' do
        abort to_s
      end

      on :'puma-help', 'Shows the options of Puma' do
        Puma::CLI.new(['--help']).run

        abort
      end

      on :bucket=,
        'The S3 bucket to store output in',
        :as => String

      on :authentication=,
        'An authentication endpoint to use',
        :as => String

      on :secret=,
        'Parameter name for the authentication secret',
        :as => String

      on :token=,
        'Parameter name for the authentication token',
        :as => String

      on :'disable-syslog', 'Disables Syslog logging (enabled by default)'

      run do |opts, args|
        puma_args = [outer.rackup] + args

        ENV['APP_NAME'] = outer.name
        ENV['APP_ROOT'] = File.expand_path('../../../../', __FILE__)
        ENV['NRCONFIG'] = File.join(ENV['APP_ROOT'], 'config/newrelic.yml')

        ENV_OPTIONS.each do |key, opt|
          ENV[key] = opts[opt]
        end

        unless opts[:'disable-syslog']
          ENV['ENABLE_SYSLOG'] = '1'
        end

        if !ENV['RAILS_ENV'] and ENV['RACK_ENV']
          ENV['RAILS_ENV'] = ENV['RACK_ENV']
        end

        if ENV['NEWRELIC_TOKEN']
          NewRelic::Control.instance.init_plugin

          # Enable the GC profiler for New Relic.
          GC::Profiler.enable
        end

        if Configuration.syslog?
          Core::Syslog.open(
            ENV['APP_NAME'],
            ::Syslog::LOG_CONS | ::Syslog::LOG_PID
          )
        end

        Configuration.configure_rollbar

        # Puma on JRuby does some weird stuff with forking/exec. As a result
        # of this we *have to* update ARGV as otherwise running Puma as a
        # daemon does not work.
        ARGV.replace(puma_args)

        Puma::CLI.new(puma_args).run
      end
    end
  end
end

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
# 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 <<-EOF.chomp

About:

    Runs the OpeNER component as a webservice using Puma. For example:

  language-identifier-server --daemon

    This would start a language identifier server in the background.

Environment Variables:

    These daemons make use of Amazon SQS queues and other Amazon services. In
    order to use these services you should make sure the following environment
    variables are set:

    * AWS_ACCESS_KEY_ID
    * AWS_SECRET_ACCESS_KEY
    * AWS_REGION

    If you're running this daemon on an EC2 instance then the first two
    environment variables will be set automatically if the instance has an
    associated IAM profile. The AWS_REGION variable must _always_ be set.

    Optionally you can also set the following extra variables:

    * NEWRELIC_TOKEN: when set the daemon will send profiling data to New Relic
using this token. The application name will be "#{server_name}".

    * ROLLBAR_TOKEN: when set the daemon will report errors to Rollbar using
this token. You can freely use this in combination with NEWRELIC_TOKEN.

Puma Options:

    This webserver uses Puma under the hood, but defines its own CLI options.
    All unrecognized options are passed to the Puma CLI. For more information
    on the available options for Puma, run `#{cli_name} --puma-help`.
    EOF

    separator "\nOptions:\n"

    on :h, :help, 'Shows this help message' do
      abort to_s
    end

    on :'puma-help', 'Shows the options of Puma' do
      Puma::CLI.new(['--help']).run

      abort
    end

    on :bucket=,
      'The S3 bucket to store output in',
      :as => String

    on :authentication=,
      'An authentication endpoint to use',
      :as => String

    on :secret=,
      'Parameter name for the authentication secret',
      :as => String

    on :token=,
      'Parameter name for the authentication token',
      :as => String

    on :'disable-syslog', 'Disables Syslog logging (enabled by default)'

    run do |opts, args|
      puma_args = [outer.rackup] + args

      ENV['APP_NAME'] = outer.name
      ENV['APP_ROOT'] = File.expand_path('../../../../', __FILE__)
      ENV['NRCONFIG'] = File.join(ENV['APP_ROOT'], 'config/newrelic.yml')

      ENV_OPTIONS.each do |key, opt|
        ENV[key] = opts[opt]
      end

      unless opts[:'disable-syslog']
        ENV['ENABLE_SYSLOG'] = '1'
      end

      if !ENV['RAILS_ENV'] and ENV['RACK_ENV']
        ENV['RAILS_ENV'] = ENV['RACK_ENV']
      end

      if ENV['NEWRELIC_TOKEN']
        NewRelic::Control.instance.init_plugin

        # Enable the GC profiler for New Relic.
        GC::Profiler.enable
      end

      if Configuration.syslog?
        Core::Syslog.open(
          ENV['APP_NAME'],
          ::Syslog::LOG_CONS | ::Syslog::LOG_PID
        )
      end

      Configuration.configure_rollbar

      # Puma on JRuby does some weird stuff with forking/exec. As a result
      # of this we *have to* update ARGV as otherwise running Puma as a
      # daemon does not work.
      ARGV.replace(puma_args)

      Puma::CLI.new(puma_args).run
    end
  end
end

#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