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 +4 -4
- data/bin/create-rails-app +4 -1
- data/bin/create-reverse-proxy +4 -1
- data/bin/create-server-block +4 -1
- data/lib/shared_infrastructure.rb +1 -0
- data/lib/shared_infrastructure/nginx/builder.rb +8 -4
- data/lib/shared_infrastructure/nginx/location.rb +20 -0
- data/lib/shared_infrastructure/nginx/server.rb +1 -1
- data/lib/shared_infrastructure/nginx/server_block.rb +0 -7
- data/lib/shared_infrastructure/runner/base.rb +35 -18
- data/lib/shared_infrastructure/runner/rails.rb +31 -0
- data/lib/shared_infrastructure/runner/reverse_proxy.rb +2 -2
- data/lib/shared_infrastructure/systemd/rails.rb +5 -1
- data/lib/shared_infrastructure/systemd/systemd.rb +1 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: acbb88aca695caae7288c8b87d27a922f32db19b
|
4
|
+
data.tar.gz: 4452c8a7ebaaef449cdefae36b70bd3369cef97f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 934e545b36a97a2e1d1385e2bc49edbf913486abb3f311b14c5328c10d205c4bc3015ad092ee39c3d456a4471825f81858080781401a370b5b624acbd708e7ff
|
7
|
+
data.tar.gz: de793ba382532a0f9d264416fcd465824b9d2f911071822129cce780ba6a4760667bc799accc1b23a62dcdff0cfa162e2ee65f4d095706265c5c5a4adcedccb0
|
data/bin/create-rails-app
CHANGED
data/bin/create-reverse-proxy
CHANGED
@@ -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
|
data/bin/create-server-block
CHANGED
@@ -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,
|
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
|
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
|
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
|
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
|
-
|
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("-
|
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
|
-
|
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
|
85
|
-
|
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
|
-
|
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
|
-
|
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
|
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
|
+
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:
|
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
|