#!/usr/bin/env ruby require 'gli' require 'redis' require 'xcflushd/gli_helpers' include GLI::App include Xcflushd::GLIHelpers program_desc 'XC flush daemon' version Xcflushd::VERSION subcommand_option_handling :normal arguments :strict desc 'Starts the XC flusher' arg_name ' ' command :run do |c| c.desc 'Redis URI' c.flag [:r, :redis], required: true, type: RedisURI c.desc '3scale backend URI' c.flag [:b, :backend], required: true, type: BackendURI c.desc '3scale provider key' c.flag [:k, :'provider-key'], required: true c.desc 'Validity of authorizations (in seconds)' c.flag [:a, :'auth-ttl'], required: true, type: Integer, must_match: POSITIVE_N_RE c.desc 'Reporting frequency (in seconds)' c.flag [:f, :frequency], required: true, type: Integer, must_match: POSITIVE_N_RE c.desc 'Number of threads for the main thread pool (min:max)' c.flag [:t, :threads], default_value: 'auto', type: PositiveMinMaxInt c.desc 'Number of threads for the priority auth renewer (min:max)' c.flag [:p, :'prio-threads'], default_value: 'auto', type: PositiveMinMaxInt c.desc 'Run this program as a background service' c.switch :daemonize, default_value: false, negatable: true, multiple: true c.desc 'Use HTTPS scheme to connect to 3scale backend' c.switch :secure, default_value: true, negatable: true, multiple: true c.action do |_global_options, options, _args| # options contains 2 keys for each option. One is a String and the other a # Symbol. That way we can use options['redis'] and options[:redis]. # That's fine as long as we only read from the hash. If we need to modify # it, it is problematic because we need to remember to modify two values. # For simplicity, let's just keep the elements that have symbols as keys. options.keep_if { |k, _v| k.is_a?(Symbol) } # Reporting frequency should be lower or equal to the authorization TTL. # Otherwise authorizations would be renewed without taking into account the # last available reports. Note: whenever we do reporting we also do renewal # of authorizations; the TTL is important in case of problems renewing # authorizations. if options[:frequency] > options[:'auth-ttl'] exit_now!('frequency needs to be <= auth-ttl') end if (options[:backend].scheme == 'http' && options[:secure]) || (options[:backend].scheme == 'https' && !options[:secure]) exit_now!("the specified backend scheme does not match the --[no-]secure" \ " flag.\nCan't continue with conflicting settings.") end if options[:threads] == 'auto' options.delete :threads options.delete :t end if options[:'prio-threads'] == 'auto' options.delete :'prio-threads' options.delete :p end banner = "xcflushd [#{Xcflushd::VERSION}]" if options[:daemonize] require 'daemons' Daemons.run_proc(banner) { start_xcflusher(options) } else set_title(banner) start_xcflusher(options) end end end pre do |_global, _command, _options, _args| # Pre logic here # Return true to proceed; false to abort and not call the # chosen command # Use skips_pre before a command to skip this block # on that command only true end post do |_global, _command, _options, _args| # Post logic here # Use skips_post before a command to skip this # block on that command only end on_error do |exception| # Error logic here # return false to skip default error handling # When forking, the parent executes 'exit', and GLI error handling kicks in. # Let's just skip it when that's the case. not SystemExit === exception end exit run(ARGV)