Class: Opener::Webservice::OptionParser
- Inherits:
-
Object
- Object
- Opener::Webservice::OptionParser
- 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.
{ 'OUTPUT_BUCKET' => :bucket, 'AUTHENTICATION_TOKEN' => :token, 'AUTHENTICATION_SECRET' => :secret, 'AUTHENTICATION_ENDPOINT' => :authentication }
Instance Attribute Summary collapse
-
#name ⇒ String
readonly
The name of the component.
- #parser ⇒ Slop readonly
-
#rackup ⇒ String
readonly
Path to the config.ru to use.
Instance Method Summary collapse
- #configure_slop ⇒ Slop
-
#initialize(name, rackup) ⇒ OptionParser
constructor
A new instance of OptionParser.
- #parse(*args) ⇒ Object
-
#run(argv = ARGV) ⇒ Object
Parses the given CLI options and starts Puma.
Constructor Details
#initialize(name, rackup) ⇒ OptionParser
Returns a new instance of OptionParser.
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
#name ⇒ String (readonly)
The name of the component.
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 "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.('../../../../', __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. # 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 |
#parser ⇒ Slop (readonly)
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 "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.('../../../../', __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. # 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 |
#rackup ⇒ String (readonly)
Path to the config.ru to use.
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 "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.('../../../../', __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. # 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_slop ⇒ 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 "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.('../../../../', __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. # 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.
51 52 53 |
# File 'lib/opener/webservice/option_parser.rb', line 51 def run(argv = ARGV) parser.parse(argv) end |