Class: Runner::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/shared_infrastructure/runner/base.rb

Overview

Basic runner for nginx config file generation.

Direct Known Subclasses

Rails, ReverseProxy, StaticSite

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#builder_classObject (readonly)

Returns the value of attribute builder_class.



100
101
102
# File 'lib/shared_infrastructure/runner/base.rb', line 100

def builder_class
  @builder_class
end

Instance Method Details

#mainObject



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/shared_infrastructure/runner/base.rb', line 14

def main
  options = process_options

  puts "options: #{options.inspect}" if Runner.debug

  Nginx.prepare_fake_files(options[:domain_name], options[:certificate_domain]) if Nginx.root?

  @builder_class = protocol_factory(options)
  puts "builder_class: #{builder_class.inspect}" if Runner.debug
  builder_class
end

#options_for_config(options) ⇒ Object



26
27
28
# File 'lib/shared_infrastructure/runner/base.rb', line 26

def options_for_config(options)
  options.select { |k, _v| k == :user }
end

#process_args(opts = nil) ⇒ Object

Raises:



30
31
32
33
# File 'lib/shared_infrastructure/runner/base.rb', line 30

def process_args(opts = nil)
  raise MissingArgument.new("domain required", opts) if ARGV.size == 0
  { domain_name: ARGV }
end

#process_options(http_builder_class = Nginx::Builder::SiteHttp, https_builder_class = Nginx::Builder::SiteHttps) ⇒ Object



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
# File 'lib/shared_infrastructure/runner/base.rb', line 35

def process_options(http_builder_class = Nginx::Builder::SiteHttp,
  https_builder_class = Nginx::Builder::SiteHttps)
  options = {}
  opts = OptionParser.new do |opts|
    opts.banner = "Usage: [options]"

    # FIXME: This is only applicable to Rails apps.
    opts.on("-a LOCATION",
      "--accel LOCATION",
      "Location below application root to serve when app responds with 'X-Accel'") do |accel_location|
      options[:accel_location] = accel_location
    end

    opts.on("-c DOMAIN",
      "--certificate-domain DOMAIN",
      "Use the certificate for DOMAIN.") do |certificate_domain|
      options[:certificate_domain] = certificate_domain
    end

    opts.on("-d", "--debug", "Print debugging information.") do
      options[:debug] = true
      Runner.debug = true
    end

    opts.on("-d RAILS_ENV", "--rails-env RAILS_ENV", "Build files for the specified RAILS_ENV") do |rails_env|
      options[:rails_env] = rails_env
    end

    opts.on("-P PROTOCOL",
      "--protocol PROTOCOL",
      "HTTP|HTTPS. Default: HTTPS if key files exist, else HTTP.") do |protocol|
      options[:protocol] = case protocol.upcase
                           when "HTTP"
                             http_builder_class
                           when "HTTPS"
                             https_builder_class
                           else
                             opts.abort opts.help
                           end
    end

    opts.on("-r DIRECTORY",
      "--root DIRECTORY",
      "DIRECTORY. Set a root for files. This options is for debugging.") do |directory|
      Nginx.chroot(directory)
      SharedInfrastructure::Output.fake_root(directory)
    end

    opts.on("-u USER",
      "--user USER",
      "User to be the owner of certain files. Default: the current user.") do |user|
      options[:user] = user
    end

    opts.on("--dhparam KEYSIZE",
      "KEYSIZE. Default: 2048 should be used. This option is for testing.") do |keysize|
      Nginx.dhparam = keysize
    end

    options.merge! yield opts if block_given?
  end
  opts.parse!
  options.merge!(process_args(opts))
end

#protocol_factory(options, http_builder_class = Nginx::Builder::SiteHttp, https_builder_class = Nginx::Builder::SiteHttps) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/shared_infrastructure/runner/base.rb', line 102

def protocol_factory(options,
  http_builder_class = Nginx::Builder::SiteHttp,
  https_builder_class = Nginx::Builder::SiteHttps)
  if options[:protocol]
    options[:protocol]
  else
    certificate_directory = Nginx.certificate_directory(
      options[:certificate_domain] || options[:domain_name].first # FIXME:
    )
    if File.exist?(File.join(certificate_directory, "privkey.pem")) &&
       File.exist?(File.join(certificate_directory, "fullchain.pem")) &&
       File.exist?(File.join(certificate_directory, "chain.pem")) &&
       File.exist?(File.join(certificate_directory, "cert.pem"))
      https_builder_class
    else
      http_builder_class
    end
  end
end