shared-infrastructure 0.0.4 → 0.0.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ac6f2b797902cb75d59273db4e59a09801925ad3
4
- data.tar.gz: 2225a1a5ad142f4a34fb75931f7dc86c64dcde2e
3
+ metadata.gz: acbb88aca695caae7288c8b87d27a922f32db19b
4
+ data.tar.gz: 4452c8a7ebaaef449cdefae36b70bd3369cef97f
5
5
  SHA512:
6
- metadata.gz: b5f3c49119662abed44d2caa374be45d57250c936b83b65015cdd71fc3e904d65f5d7b6391ce687a64fb653f630b3cf0334351e8e1d6b8bffdefd6f8a279bfb1
7
- data.tar.gz: 7ecea13f4f49d5e1446ec6862e02cf17bf05d1e6a01a961e3aaf1f563a6af97f0bde66011e74566dcb11823d3eb30583768c8c89c43c164b2f46cbb2ca7cb80e
6
+ metadata.gz: 934e545b36a97a2e1d1385e2bc49edbf913486abb3f311b14c5328c10d205c4bc3015ad092ee39c3d456a4471825f81858080781401a370b5b624acbd708e7ff
7
+ data.tar.gz: de793ba382532a0f9d264416fcd465824b9d2f911071822129cce780ba6a4760667bc799accc1b23a62dcdff0cfa162e2ee65f4d095706265c5c5a4adcedccb0
@@ -4,4 +4,7 @@
4
4
  $LOAD_PATH.unshift File.join(File.dirname(__FILE__), "..", "lib")
5
5
  require "shared_infrastructure"
6
6
 
7
- Runner::Rails.new.main.save
7
+ begin Runner::Rails.new.main.save
8
+ rescue Runner::MissingArgument => e
9
+ e.opts.abort e.to_s + "\n" + e.opts.to_s
10
+ end
@@ -4,4 +4,7 @@
4
4
  $LOAD_PATH.unshift File.join(File.dirname(__FILE__), "..", "lib")
5
5
  require "shared_infrastructure"
6
6
 
7
- Runner::ReverseProxy.new.main.save
7
+ begin Runner::ReverseProxy.new.main.save
8
+ rescue Runner::MissingArgument => e
9
+ e.opts.abort e.to_s + "\n" + e.opts.to_s
10
+ end
@@ -4,4 +4,7 @@
4
4
  $LOAD_PATH.unshift File.join(File.dirname(__FILE__), "..", "lib")
5
5
  require "shared_infrastructure"
6
6
 
7
- Runner::StaticSite.new.main.save
7
+ begin Runner::StaticSite.new.main.save
8
+ rescue Runner::MissingArgument => e
9
+ e.opts.abort e.to_s + "\n" + e.opts.to_s
10
+ end
@@ -12,5 +12,6 @@ require "shared_infrastructure/nginx/builder.rb"
12
12
  require "shared_infrastructure/runner/base.rb"
13
13
  require "shared_infrastructure/runner/reverse_proxy.rb"
14
14
  require "shared_infrastructure/runner/static_site.rb"
15
+ require "shared_infrastructure/runner/rails.rb"
15
16
  require "shared_infrastructure/systemd/systemd.rb"
16
17
  require "shared_infrastructure/systemd/rails.rb"
@@ -39,9 +39,11 @@ Finally, re-run this script to configure nginx for TLS.
39
39
  end
40
40
 
41
41
  def save
42
+ puts "writing server block: #{Nginx.server_block_location(domain_name)}" if Runner.debug
42
43
  File.open(Nginx.server_block_location(domain_name), "w") do |f|
43
44
  f << to_s
44
45
  end
46
+ puts "enabling site" if Runner.debug
45
47
  `ln -fs ../sites-available/#{domain_name} #{Nginx.enabled_server_block_location(domain_name)}`
46
48
  end
47
49
 
@@ -153,7 +155,7 @@ Finally, re-run this script to configure nginx for TLS.
153
155
  end
154
156
 
155
157
  class RailsHttp < Site
156
- def initialize(domain_name, user, _certificate_domain = nil)
158
+ def initialize(domain_name, user, _certificate_domain = nil, accel_location: nil)
157
159
  super(domain_name,
158
160
  user,
159
161
  Nginx::RailsServerBlock.new(
@@ -162,8 +164,9 @@ Finally, re-run this script to configure nginx for TLS.
162
164
  listen: Nginx::ListenHttp.new,
163
165
  location: [
164
166
  Nginx::RailsLocation.new(domain_name),
167
+ accel_location ? Nginx::AccelLocation.new(accel_location) : nil,
165
168
  Nginx::ActionCableLocation.new(domain_name)
166
- ]
169
+ ].compact
167
170
  )
168
171
  )
169
172
  end
@@ -176,7 +179,7 @@ Finally, re-run this script to configure nginx for TLS.
176
179
  class RailsHttps < Site
177
180
  include Https
178
181
 
179
- def initialize(domain_name, user, _certificate_domain = nil)
182
+ def initialize(domain_name, user, certificate_domain = nil, accel_location: nil)
180
183
  @certificate_domain = certificate_domain || domain_name
181
184
  super(domain_name,
182
185
  user,
@@ -186,8 +189,9 @@ Finally, re-run this script to configure nginx for TLS.
186
189
  listen: Nginx::ListenHttps.new(domain_name, certificate_domain),
187
190
  location: [
188
191
  Nginx::RailsLocation.new(domain_name),
192
+ accel_location ? Nginx::AccelLocation.new(accel_location) : nil,
189
193
  Nginx::ActionCableLocation.new(domain_name)
190
- ]
194
+ ].compact
191
195
  ),
192
196
  Nginx::TlsRedirectServerBlock.new(domain_name)
193
197
  )
@@ -17,6 +17,22 @@ module Nginx
17
17
  attr_reader :location
18
18
  end
19
19
 
20
+ class AccelLocation < Location
21
+ def initialize(location, root = "/")
22
+ super(location)
23
+ @root = root
24
+ end
25
+
26
+ def to_s(level = 0)
27
+ Lines.new("location /#{location.chomp("/").reverse.chomp("/").reverse}/ {",
28
+ " internal;",
29
+ " root #{root};",
30
+ "}").format(level)
31
+ end
32
+
33
+ attr_reader :location, :root
34
+ end
35
+
20
36
  class AcmeLocation < Location
21
37
  def initialize(certificate_domain, location = "/.well-known")
22
38
  super(location)
@@ -84,6 +100,10 @@ module Nginx
84
100
  @proxy_url = proxy_url
85
101
  end
86
102
 
103
+ ##
104
+ # Don't change any of the response headers
105
+ # http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_redirect
106
+ # The URI is added if none is specified in the proxy_url.
87
107
  def to_s(level = 0)
88
108
  Lines.new("location #{location} {",
89
109
  " proxy_pass #{proxy_url};",
@@ -42,7 +42,7 @@ module Nginx
42
42
  "# http://stackoverflow.com/a/11313241/3109926 said the following",
43
43
  "# is what serves from public directly without hitting Puma",
44
44
  "root #{root_directory};",
45
- "try_files $uri/index.html $uri @example.com;",
45
+ "try_files $uri/index.html $uri @#{domain_name};",
46
46
  "error_page 500 502 503 504 /500.html;",
47
47
  "client_max_body_size 4G;",
48
48
  "keepalive_timeout 10;"
@@ -11,13 +11,6 @@ module Nginx
11
11
  @upstream = upstream
12
12
  end
13
13
 
14
- def save
15
- File.open(Nginx.server_block_location(server.domain_name), "w") do |f|
16
- f << to_s
17
- end
18
- `ln -fs ../sites-available/#{server.domain_name} #{Nginx.enabled_server_block_location(server.domain_name)}`
19
- end
20
-
21
14
  def to_s
22
15
  [
23
16
  upstream_string,
@@ -3,19 +3,23 @@
3
3
  require "optparse"
4
4
 
5
5
  module Runner
6
+ @debug = false
7
+ class << self
8
+ attr_accessor :debug
9
+ end
10
+
6
11
  ##
7
12
  # Basic runner for nginx config file generation.
8
13
  class Base
9
14
  def main
10
15
  options = process_options
11
- options.merge!(process_args)
12
16
 
13
- puts "options: #{options.inspect}" if options[:debug]
17
+ puts "options: #{options.inspect}" if Runner.debug
14
18
 
15
19
  Nginx.prepare_fake_files(options[:domain_name], options[:certificate_domain]) if Nginx.root?
16
20
 
17
21
  @builder_class = protocol_factory(options)
18
- puts "builder_class: #{builder_class.inspect}" if options[:debug]
22
+ puts "builder_class: #{builder_class.inspect}" if Runner.debug
19
23
  builder_class
20
24
  end
21
25
 
@@ -23,43 +27,46 @@ module Runner
23
27
  options.select { |k, _v| k == :user }
24
28
  end
25
29
 
26
- def process_args
27
- $stderr.puts "domain required" unless ARGV.size == 1
30
+ def process_args(opts = nil)
31
+ raise MissingArgument.new("domain required", opts) unless ARGV.size == 1
28
32
  { domain_name: ARGV[0] }
29
33
  end
30
34
 
31
35
  def process_options(http_builder_class = Nginx::Builder::SiteHttp,
32
36
  https_builder_class = Nginx::Builder::SiteHttps)
33
37
  options = {}
34
- OptionParser.new do |opts|
38
+ opts = OptionParser.new do |opts|
35
39
  opts.banner = "Usage: [options]"
36
40
 
41
+ # FIXME: This is only applicable to Rails apps.
42
+ opts.on("-a LOCATION",
43
+ "--accel LOCATION",
44
+ "Location to serve when app responds with 'X-Accel'") do |accel_location|
45
+ options[:accel_location] = accel_location
46
+ puts "FOUND X-ACCEL"
47
+ end
48
+
37
49
  opts.on("-c DOMAIN",
38
50
  "--certificate-domain DOMAIN",
39
51
  "Use the certificate for DOMAIN.") do |certificate_domain|
40
52
  options[:certificate_domain] = certificate_domain
41
53
  end
42
54
 
43
- opts.on("-h", "--help", "Prints this help") do
44
- puts opts
45
- exit
46
- end
47
-
48
55
  opts.on("-d", "--debug", "Print debugging information.") do
49
56
  options[:debug] = true
57
+ Runner.debug = true
50
58
  end
51
59
 
52
- opts.on("-p PROTOCOL",
60
+ opts.on("-P PROTOCOL",
53
61
  "--protocol PROTOCOL",
54
62
  "HTTP|HTTPS. Default: HTTPS if key files exist, else HTTP.") do |protocol|
55
- options[:protocol] = case protocol
63
+ options[:protocol] = case protocol.upcase
56
64
  when "HTTP"
57
65
  http_builder_class
58
66
  when "HTTPS"
59
67
  https_builder_class
60
68
  else
61
- puts opts
62
- exit
69
+ opts.abort opts.help
63
70
  end
64
71
  end
65
72
 
@@ -80,9 +87,10 @@ module Runner
80
87
  Nginx.dhparam = keysize
81
88
  end
82
89
 
83
- yield opts if block_given?
84
- end.parse!
85
- options
90
+ options.merge! yield opts if block_given?
91
+ end
92
+ opts.parse!
93
+ options.merge!(process_args(opts))
86
94
  end
87
95
 
88
96
  attr_reader :builder_class
@@ -107,4 +115,13 @@ module Runner
107
115
  end
108
116
  end
109
117
  end
118
+
119
+ class MissingArgument < RuntimeError
120
+ def initialize(msg, opts)
121
+ @opts = opts
122
+ super msg
123
+ end
124
+ attr_reader :msg
125
+ attr_reader :opts
126
+ end
110
127
  end
@@ -0,0 +1,31 @@
1
+ module Runner
2
+ class Rails < Base
3
+ def main
4
+ builder = super
5
+ FileUtils.mkdir_p(File.dirname(Systemd.unit_file("example.com"))) if Nginx.root?
6
+ builder
7
+ end
8
+
9
+ def process_options
10
+ super(Nginx::Builder::RailsHttp, Nginx::Builder::RailsHttps)
11
+ end
12
+
13
+ def protocol_factory(options)
14
+ protocol_class = super(
15
+ options,
16
+ Nginx::Builder::RailsHttp,
17
+ Nginx::Builder::RailsHttps
18
+ )
19
+
20
+ # puts "Runner::Rails protocol_class: #{protocol_class}"
21
+ # TODO: Each class has a subtly different group of lines here.
22
+ # There's almost certainly a refactoring that would make this less
23
+ # convoluted.
24
+ domain_name = options.delete(:domain_name)
25
+ user = options.delete(:user) || "ubuntu"
26
+ certificate_domain = options.delete(:certificate_domain)
27
+ accel_location = options.delete(:accel_location)
28
+ protocol_class.new(domain_name, user, certificate_domain, accel_location: accel_location)
29
+ end
30
+ end
31
+ end
@@ -8,8 +8,8 @@ module Runner
8
8
  super(options).merge(proxy_url: ARGV[1])
9
9
  end
10
10
 
11
- def process_args
12
- $stderr.puts "domain and target url required" unless ARGV.size == 2
11
+ def process_args(opts = nil)
12
+ raise MissingArgument.new("domain and target url required", opts) unless ARGV.size == 2
13
13
  {
14
14
  domain_name: ARGV[0],
15
15
  proxy_url: ARGV[1]
@@ -19,6 +19,8 @@ module Systemd
19
19
  raise "Missing environment variable"
20
20
  end
21
21
 
22
+ puts "writing unit file (domain_name): #{Systemd.unit_file(domain_name)} (#{domain_name})" if Runner.debug
23
+
22
24
  result = File.open(Systemd.unit_file(domain_name), "w") do |f|
23
25
  f << <<~UNIT_FILE
24
26
  [Unit]
@@ -62,8 +64,10 @@ module Systemd
62
64
  UNIT_FILE
63
65
  end
64
66
 
67
+ puts "changing mode of unit file" if Runner.debug
65
68
  FileUtils.chmod(0o600, Systemd.unit_file(domain_name))
66
- `systemctl enable $domain_name.service` if Process.uid.zero?
69
+ puts "enabling service" if Runner.debug && Process.uid.zero?
70
+ `systemctl enable #{domain_name}.service` if Process.uid.zero?
67
71
 
68
72
  result
69
73
  end
@@ -1,7 +1,7 @@
1
1
  module Systemd
2
2
  class Configuration
3
3
  def initialize
4
- @unit_file_path = File.join(Nginx.root, "/lib/systemd/system")
4
+ @unit_file_path = "#{Nginx.root}/lib/systemd/system"
5
5
  end
6
6
 
7
7
  def unit_file(domain_name)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shared-infrastructure
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - Larry Reid
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-10-30 00:00:00.000000000 Z
11
+ date: 2018-04-05 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: 'For static sites, Rails apps, and reverse proxies.
14
14
 
@@ -35,6 +35,7 @@ files:
35
35
  - lib/shared_infrastructure/nginx/site.rb
36
36
  - lib/shared_infrastructure/nginx/upstream.rb
37
37
  - lib/shared_infrastructure/runner/base.rb
38
+ - lib/shared_infrastructure/runner/rails.rb
38
39
  - lib/shared_infrastructure/runner/reverse_proxy.rb
39
40
  - lib/shared_infrastructure/runner/static_site.rb
40
41
  - lib/shared_infrastructure/systemd/rails.rb
@@ -59,7 +60,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
59
60
  version: '0'
60
61
  requirements: []
61
62
  rubyforge_project:
62
- rubygems_version: 2.5.1
63
+ rubygems_version: 2.5.2.1
63
64
  signing_key:
64
65
  specification_version: 4
65
66
  summary: Configure nginx, systemd, and/or Puma