class Redirus::Cli::Init

Attributes

options[R]

Public Instance Methods

parse(args = ARGV) click to toggle source
# File lib/redirus/cli/init.rb, line 12
def parse(args = ARGV)
  init_options(args)
  validate!
end
run() click to toggle source
# File lib/redirus/cli/init.rb, line 17
def run
  generate_templates
  mkdir(@options[:configs_dir])
  mkdir(@options[:log_dir])
end

Private Instance Methods

check_ip!() click to toggle source
# File lib/redirus/cli/init.rb, line 142
def check_ip!
  return if valid_id?

  puts 'ERROR: Server IP is not valid'
  exit(1)
end
check_pid_dir!() click to toggle source
# File lib/redirus/cli/init.rb, line 154
def check_pid_dir!
  return if Dir.exist?(File.dirname(options[:pid]))

  puts 'ERROR: Pid directory does not exist'
  exit(1)
end
check_target_dir!() click to toggle source
# File lib/redirus/cli/init.rb, line 135
def check_target_dir!
  return if Dir.exist?(options[:target])

  puts "ERROR: Directory #{options[:target]} does not exist"
  exit(1)
end
defaults() click to toggle source
# File lib/redirus/cli/init.rb, line 116
def defaults
  {
    target: Dir.pwd,
    ip: '*',
    server_name: 'CHANGE_ME',
    pid: File.join(Dir.pwd, 'nginx.pid'),
    configs_dir: File.join(Dir.pwd, 'configurations'),
    log_dir: File.join(Dir.pwd, 'log'),
    redis: 'redis://localhost:6379',
    queues: ['redirus']
  }
end
generate(tmpl) click to toggle source
# File lib/redirus/cli/init.rb, line 161
def generate(tmpl)
  File.open(target(tmpl), 'w') do |file|
    erb = ERB.new(File.read(tmpl), nil, '-')
    file.write(erb.result(binding))
  end
end
generate_templates() click to toggle source
# File lib/redirus/cli/init.rb, line 25
def generate_templates
  Dir[File.join(tmpl_dir, '*')].each { |tmpl| generate(tmpl) }
end
init_options(args) click to toggle source
# File lib/redirus/cli/init.rb, line 37
def init_options(args)
  opts = parse_options(args)
  @options = defaults.merge(opts)
  @options[:http_template] = target('http.conf.erb')
  @options[:https_template] = target('https.conf.erb')
end
mkdir(dir) click to toggle source
# File lib/redirus/cli/init.rb, line 29
def mkdir(dir)
  Dir.mkdir(dir) unless Dir.exist?(dir)
end
parse_options(args) click to toggle source
# File lib/redirus/cli/init.rb, line 44
def parse_options(args)
  opts = {}

  parser = OptionParser.new do |o|
    o.on('-t', '--target PATH',
         'Target path (default current dir)') do |arg|
      opts[:target] = arg
      opts[:pid] = File.join(arg, 'nginx.pid')
      opts[:configs_dir] = File.join(arg, 'configurations')
      opts[:log_dir] = File.join(arg, 'log')
    end

    o.on('--ip IP', 'Server IP address (default *)') do |arg|
      opts[:ip] = arg
    end

    o.on('--server-name NAME', 'Server name') do |arg|
      opts[:server_name] = arg
    end

    o.on('--ssl-cert PATH', 'Server certificate path') do |arg|
      opts[:ssl_cert] = arg
    end

    o.on('--ssl-cert-key PATH', 'Server certificate key path') do |arg|
      opts[:ssl_cert_key] = arg
    end

    o.on('--nginx-pid PATH',
         'Nginx pid location (default nginx.pid in '\
         'current dir)') do |arg|
      opts[:pid] = arg
    end

    o.on('--configurations DIR',
         'Directory where nginx configs will be generated '\
         '(default "configurations" in current directory)') do |arg|
      opts[:configs_dir] = arg
    end

    o.on('--logs DIR', 'Directory where nginx logs will be placed'\
         '(default "log" dir in current dirrectory)') do |arg|
      opts[:log_dir] = arg
    end

    o.on('--redis URL',
         'Redis location (default "redis://localhost:6379")') do |arg|
      opts[:redis] = arg
    end

    o.on('--queues', Array,
         'List of redirs queues (default ["redirus"]') do |arg|
      opts[:queues] = arg
    end

    o.on_tail('-h', '--help', 'Show this message') do
      puts o
      exit
    end

    o.on_tail('-v', '--version', 'Show version') do
      puts "Redirus #{Redirus::VERSION}"
      exit
    end
  end

  parser.banner = 'redirus-init [options]'
  parser.parse!(args)

  opts
end
target(tmpl) click to toggle source
# File lib/redirus/cli/init.rb, line 168
def target(tmpl)
  File.join(@options[:target], File.basename(tmpl))
end
tmpl_dir() click to toggle source
# File lib/redirus/cli/init.rb, line 33
def tmpl_dir
  @templates ||= File.join(Redirus.root, 'templates')
end
valid_id?() click to toggle source
# File lib/redirus/cli/init.rb, line 149
def valid_id?
  options[:ip] == '*' ||
    options[:ip].match(/\A\d{1,3}\.\d{1,3}\.\d{1,3}.\d{1,3}:\d+\z/)
end
validate!() click to toggle source
# File lib/redirus/cli/init.rb, line 129
def validate!
  check_target_dir!
  check_ip!
  check_pid_dir!
end