shared-infrastructure 0.0.13 → 0.0.15
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/shared_infrastructure.rb +4 -1
- data/lib/shared_infrastructure/domain.rb +33 -0
- data/lib/shared_infrastructure/nginx/accel.rb +27 -0
- data/lib/shared_infrastructure/nginx/builder.rb +99 -78
- data/lib/shared_infrastructure/nginx/location.rb +7 -5
- data/lib/shared_infrastructure/nginx/server.rb +10 -9
- data/lib/shared_infrastructure/nginx/server_block.rb +7 -4
- data/lib/shared_infrastructure/output.rb +51 -0
- data/lib/shared_infrastructure/runner/base.rb +0 -1
- data/lib/shared_infrastructure/runner/deploy.rb +55 -0
- data/lib/shared_infrastructure/runner/rails.rb +2 -1
- data/lib/shared_infrastructure/runner/reverse_proxy.rb +2 -1
- data/lib/shared_infrastructure/runner/static_site.rb +2 -1
- data/lib/shared_infrastructure/systemd/rails.rb +9 -8
- metadata +5 -2
- data/lib/shared_infrastructure/nginx/site.rb +0 -29
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: da04dfe750e140c1338ffdb29a780aec11cc5d67
|
4
|
+
data.tar.gz: f07c6a555130703d979dddec88f58c5eb02b7d51
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f82b761326514cc4ff6f9b3c65e0906546e2fe8f9adee2aa503e26a9069d45a311ffbe0e24ab2b62511345ad7e6031b61e4bca9a10ee429f2e19a3df177cd27a
|
7
|
+
data.tar.gz: 1818e9387a3037ae3ec2a26eeca27a1558fd44cfb0ccbf1d2fc14f276fbfbec51a2b33468cf1a80ab71ff5704288040e6ad8db2ec52f91ddc103673149550ef4
|
@@ -1,5 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require "shared_infrastructure/output.rb"
|
4
|
+
require "shared_infrastructure/domain.rb"
|
3
5
|
require "shared_infrastructure/nginx/nginx.rb"
|
4
6
|
require "shared_infrastructure/nginx/server_block.rb"
|
5
7
|
require "shared_infrastructure/nginx/server.rb"
|
@@ -7,11 +9,12 @@ require "shared_infrastructure/nginx/lines.rb"
|
|
7
9
|
require "shared_infrastructure/nginx/listen.rb"
|
8
10
|
require "shared_infrastructure/nginx/location.rb"
|
9
11
|
require "shared_infrastructure/nginx/upstream.rb"
|
10
|
-
require "shared_infrastructure/nginx/site.rb"
|
11
12
|
require "shared_infrastructure/nginx/builder.rb"
|
13
|
+
require "shared_infrastructure/nginx/accel.rb"
|
12
14
|
require "shared_infrastructure/runner/base.rb"
|
13
15
|
require "shared_infrastructure/runner/reverse_proxy.rb"
|
14
16
|
require "shared_infrastructure/runner/static_site.rb"
|
15
17
|
require "shared_infrastructure/runner/rails.rb"
|
18
|
+
require "shared_infrastructure/runner/deploy.rb"
|
16
19
|
require "shared_infrastructure/systemd/systemd.rb"
|
17
20
|
require "shared_infrastructure/systemd/rails.rb"
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module SharedInfrastructure
|
2
|
+
class Domain
|
3
|
+
def available_site
|
4
|
+
"/etc/nginx/sites-available/#{domain_name}"
|
5
|
+
end
|
6
|
+
|
7
|
+
def certbot_domain_names
|
8
|
+
"#{domain_name} www.#{domain_name}"
|
9
|
+
end
|
10
|
+
|
11
|
+
def certificate_directory
|
12
|
+
"/etc/letsencrypt/live/#{domain_name}"
|
13
|
+
end
|
14
|
+
|
15
|
+
def enabled_site
|
16
|
+
"/etc/nginx/sites-enabled/#{domain_name}"
|
17
|
+
end
|
18
|
+
|
19
|
+
def initialize(domain_name)
|
20
|
+
@domain_name = domain_name
|
21
|
+
end
|
22
|
+
|
23
|
+
def secrets
|
24
|
+
File.join(site_root, "secrets")
|
25
|
+
end
|
26
|
+
|
27
|
+
def site_root
|
28
|
+
"/var/www/#{domain_name}/html"
|
29
|
+
end
|
30
|
+
|
31
|
+
attr_reader :domain_name
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Nginx
|
4
|
+
class Accel
|
5
|
+
def initialize(location_directory, domain: nil)
|
6
|
+
@domain = domain
|
7
|
+
@location_directory = location_directory.chomp("/").reverse.chomp("/").reverse
|
8
|
+
end
|
9
|
+
|
10
|
+
attr_reader :domain, :location_directory
|
11
|
+
|
12
|
+
def alias_string(domain_name)
|
13
|
+
File.join(Nginx.configuration.root_directory(domain ? domain.domain_name : domain_name), location_directory).to_s
|
14
|
+
end
|
15
|
+
|
16
|
+
def location
|
17
|
+
"/#{location_directory}"
|
18
|
+
end
|
19
|
+
|
20
|
+
def proxy_set_header(domain_name)
|
21
|
+
[
|
22
|
+
" proxy_set_header X-Sendfile-Type X-Accel-Redirect;",
|
23
|
+
" proxy_set_header X-Accel-Mapping #{alias_string(domain_name)}/=#{location}/;"
|
24
|
+
].join("\n")
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -7,7 +7,9 @@ module Nginx
|
|
7
7
|
module Builder
|
8
8
|
module Https
|
9
9
|
def save
|
10
|
-
|
10
|
+
pem_file = "#{Nginx.certificate_directory(certificate_domain)}/dhparam.pem"
|
11
|
+
FileUtils.mkdir_p File.dirname(pem_file)
|
12
|
+
`openssl dhparam #{Nginx.dhparam} -out #{pem_file}`
|
11
13
|
super
|
12
14
|
end
|
13
15
|
end
|
@@ -21,7 +23,7 @@ sudo nginx -s reload
|
|
21
23
|
|
22
24
|
Then run the following command:
|
23
25
|
|
24
|
-
sudo certbot certonly --webroot -w #{Nginx.root_directory(domain_name)} #{Nginx.certbot_domain_names(domain_name)}
|
26
|
+
sudo certbot certonly --webroot -w #{Nginx.root_directory(domain.domain_name)} #{Nginx.certbot_domain_names(domain.domain_name)}
|
25
27
|
|
26
28
|
You can test renewal with:
|
27
29
|
|
@@ -31,42 +33,42 @@ Finally, re-run this script to configure nginx for TLS.
|
|
31
33
|
)
|
32
34
|
end
|
33
35
|
|
34
|
-
def initialize(
|
36
|
+
def initialize(*server_blocks, domain: nil)
|
35
37
|
# puts "Base#initialize domain_name: #{domain_name}"
|
36
38
|
# puts "Base#initialize server_blocks.inspect: #{server_blocks.inspect}"
|
37
39
|
@server_blocks = server_blocks
|
38
|
-
@
|
40
|
+
@domain = domain
|
39
41
|
end
|
40
42
|
|
41
43
|
def save
|
42
|
-
puts "writing server block: #{Nginx.server_block_location(domain_name)}" if Runner.debug
|
43
|
-
File.open(Nginx.server_block_location(domain_name), "w") do |f|
|
44
|
+
puts "writing server block: #{Nginx.server_block_location(domain.domain_name)}" if Runner.debug
|
45
|
+
File.open(Nginx.server_block_location(domain.domain_name), "w") do |f|
|
44
46
|
f << to_s
|
45
47
|
end
|
46
48
|
puts "enabling site" if Runner.debug
|
47
|
-
`ln -fs ../sites-available/#{domain_name} #{Nginx.enabled_server_block_location(domain_name)}`
|
49
|
+
`ln -fs ../sites-available/#{domain.domain_name} #{Nginx.enabled_server_block_location(domain.domain_name)}`
|
48
50
|
end
|
49
51
|
|
50
52
|
def to_s
|
51
53
|
server_blocks.map(&:to_s).join("\n")
|
52
54
|
end
|
53
55
|
|
54
|
-
attr_reader :
|
56
|
+
attr_reader :domain, :server_blocks
|
55
57
|
end
|
56
58
|
|
57
59
|
class ReverseProxyHttp < Base
|
58
|
-
def initialize(
|
59
|
-
super(
|
60
|
-
Nginx::
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
60
|
+
def initialize(proxy_url, certificate_domain = nil, domain: nil)
|
61
|
+
super(Nginx::ServerBlock.new(
|
62
|
+
server: Nginx::Server.new(domain: domain),
|
63
|
+
listen: Nginx::ListenHttp.new,
|
64
|
+
location: [
|
65
|
+
# TODO: the following should really only happen when the domains
|
66
|
+
# are different.
|
67
|
+
Nginx::AcmeLocation.new(certificate_domain || domain.domain_name),
|
68
|
+
Nginx::ReverseProxyLocation.new(proxy_url)
|
69
|
+
]
|
70
|
+
),
|
71
|
+
domain: domain
|
70
72
|
)
|
71
73
|
end
|
72
74
|
|
@@ -80,16 +82,16 @@ Finally, re-run this script to configure nginx for TLS.
|
|
80
82
|
class ReverseProxyHttps < Base
|
81
83
|
include Https
|
82
84
|
|
83
|
-
def initialize(
|
84
|
-
@certificate_domain = certificate_domain || domain_name
|
85
|
+
def initialize(proxy_url, certificate_domain = nil, domain: nil)
|
86
|
+
@certificate_domain = certificate_domain || domain.domain_name
|
85
87
|
|
86
|
-
super(
|
87
|
-
Nginx::
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
),
|
92
|
-
|
88
|
+
super(Nginx::ServerBlock.new(
|
89
|
+
server: Nginx::Server.new(domain: domain),
|
90
|
+
listen: Nginx::ListenHttps.new(domain.domain_name, certificate_domain),
|
91
|
+
location: Nginx::ReverseProxyLocation.new(proxy_url)
|
92
|
+
),
|
93
|
+
Nginx::TlsRedirectServerBlock.new(domain.domain_name),
|
94
|
+
domain: domain
|
93
95
|
)
|
94
96
|
end
|
95
97
|
|
@@ -97,17 +99,17 @@ Finally, re-run this script to configure nginx for TLS.
|
|
97
99
|
end
|
98
100
|
|
99
101
|
class Site < Base
|
100
|
-
def initialize(
|
101
|
-
super(
|
102
|
+
def initialize(user, *server_blocks, domain: nil)
|
103
|
+
super(*server_blocks, domain: domain)
|
102
104
|
@user = user
|
103
105
|
end
|
104
106
|
|
105
107
|
def save
|
106
|
-
FileUtils.mkdir_p(Nginx.root_directory(domain_name))
|
108
|
+
FileUtils.mkdir_p(Nginx.root_directory(domain.domain_name))
|
107
109
|
if Process.uid.zero?
|
108
110
|
FileUtils.chown(user,
|
109
111
|
"www-data",
|
110
|
-
Nginx.root_directory(domain_name))
|
112
|
+
Nginx.root_directory(domain.domain_name))
|
111
113
|
end
|
112
114
|
super
|
113
115
|
end
|
@@ -116,14 +118,14 @@ Finally, re-run this script to configure nginx for TLS.
|
|
116
118
|
end
|
117
119
|
|
118
120
|
class SiteHttp < Site
|
119
|
-
def initialize(
|
120
|
-
super(
|
121
|
-
user,
|
121
|
+
def initialize(user, _certificate_domain = nil, domain: nil)
|
122
|
+
super(user,
|
122
123
|
Nginx::StaticServerBlock.new(
|
123
|
-
server: Nginx::
|
124
|
+
server: Nginx::StaticServer.new(domain: domain),
|
124
125
|
listen: Nginx::ListenHttp.new,
|
125
126
|
location: Nginx::Location.new
|
126
|
-
)
|
127
|
+
),
|
128
|
+
domain: domain
|
127
129
|
)
|
128
130
|
end
|
129
131
|
|
@@ -137,71 +139,90 @@ Finally, re-run this script to configure nginx for TLS.
|
|
137
139
|
class SiteHttps < Site
|
138
140
|
include Https
|
139
141
|
|
140
|
-
def initialize(
|
141
|
-
@certificate_domain = certificate_domain || domain_name
|
142
|
+
def initialize(user, certificate_domain = nil, domain: nil)
|
143
|
+
@certificate_domain = certificate_domain || domain.domain_name
|
142
144
|
|
143
|
-
super(
|
144
|
-
user,
|
145
|
+
super(user,
|
145
146
|
Nginx::StaticServerBlock.new(
|
146
|
-
server: Nginx::
|
147
|
-
listen: Nginx::ListenHttps.new(domain_name, certificate_domain),
|
147
|
+
server: Nginx::StaticServer.new(domain: domain),
|
148
|
+
listen: Nginx::ListenHttps.new(domain.domain_name, certificate_domain),
|
148
149
|
location: Nginx::Location.new
|
149
150
|
),
|
150
|
-
Nginx::TlsRedirectServerBlock.new(domain_name)
|
151
|
+
Nginx::TlsRedirectServerBlock.new(domain.domain_name),
|
152
|
+
domain: domain
|
151
153
|
)
|
152
154
|
end
|
153
155
|
|
154
156
|
attr_reader :certificate_domain
|
155
157
|
end
|
156
158
|
|
157
|
-
class
|
158
|
-
def
|
159
|
-
|
160
|
-
|
159
|
+
class Rails < Site
|
160
|
+
def save
|
161
|
+
env = {}
|
162
|
+
%w[SECRET_KEY_BASE
|
163
|
+
DATABASE_USERNAME
|
164
|
+
DATABASE_PASSWORD
|
165
|
+
EMAIL_PASSWORD].each do |var|
|
166
|
+
env[var.to_sym] = if ENV[var].nil?
|
167
|
+
puts "Enter #{var}: "
|
168
|
+
$stdin.gets.strip
|
169
|
+
else
|
170
|
+
ENV[var]
|
171
|
+
end
|
172
|
+
end
|
173
|
+
File.open(SharedInfrastructure::Output.file_name(File.join(domain.site_root, "secrets")), "w", 0o600) do |io|
|
174
|
+
io << env.map { |pair| "#{pair[0]}=#{pair[1]}\n" }.join
|
175
|
+
end &&
|
176
|
+
Systemd::Rails.write_unit_file(domain.domain_name, domain) &&
|
177
|
+
super
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
181
|
+
class RailsHttp < Rails
|
182
|
+
def initialize(user, _certificate_domain = nil, accel_location: nil, domain: nil)
|
183
|
+
accel_location = Accel.new(accel_location, domain: domain) if accel_location
|
184
|
+
super(user,
|
161
185
|
Nginx::RailsServerBlock.new(
|
162
|
-
upstream: Nginx::Upstream.new(domain_name),
|
163
|
-
server: Nginx::RailsServer.new(
|
186
|
+
upstream: Nginx::Upstream.new(domain.domain_name),
|
187
|
+
server: Nginx::RailsServer.new(domain: domain),
|
164
188
|
listen: Nginx::ListenHttp.new,
|
165
189
|
location: [
|
166
|
-
Nginx::RailsLocation.new(domain_name),
|
167
|
-
accel_location ? Nginx::AccelLocation.new(accel_location) : nil,
|
168
|
-
Nginx::ActionCableLocation.new(domain_name)
|
169
|
-
].compact
|
170
|
-
|
190
|
+
Nginx::RailsLocation.new(domain.domain_name),
|
191
|
+
accel_location ? Nginx::AccelLocation.new(domain.domain_name, accel_location) : nil,
|
192
|
+
Nginx::ActionCableLocation.new(domain.domain_name)
|
193
|
+
].compact,
|
194
|
+
accel_location: accel_location,
|
195
|
+
domain: domain
|
196
|
+
),
|
197
|
+
domain: domain
|
171
198
|
)
|
172
199
|
end
|
173
|
-
|
174
|
-
def save
|
175
|
-
Systemd::Rails.write_unit_file(domain_name) && super
|
176
|
-
end
|
177
200
|
end
|
178
201
|
|
179
|
-
class RailsHttps <
|
202
|
+
class RailsHttps < Rails
|
180
203
|
include Https
|
181
204
|
|
182
|
-
def initialize(
|
183
|
-
@certificate_domain = certificate_domain || domain_name
|
184
|
-
|
185
|
-
|
205
|
+
def initialize(user, certificate_domain = nil, accel_location: nil, domain: nil)
|
206
|
+
@certificate_domain = certificate_domain || domain.domain_name
|
207
|
+
accel_location = Accel.new(accel_location, domain) if accel_location
|
208
|
+
super(user,
|
186
209
|
Nginx::RailsServerBlock.new(
|
187
|
-
upstream: Nginx::Upstream.new(domain_name),
|
188
|
-
server: Nginx::RailsServer.new(
|
189
|
-
listen: Nginx::ListenHttps.new(domain_name, certificate_domain),
|
210
|
+
upstream: Nginx::Upstream.new(domain.domain_name),
|
211
|
+
server: Nginx::RailsServer.new(domain: domain),
|
212
|
+
listen: Nginx::ListenHttps.new(domain.domain_name, certificate_domain),
|
190
213
|
location: [
|
191
|
-
Nginx::RailsLocation.new(domain_name),
|
192
|
-
accel_location ? Nginx::AccelLocation.new(accel_location) : nil,
|
193
|
-
Nginx::ActionCableLocation.new(domain_name)
|
194
|
-
].compact
|
214
|
+
Nginx::RailsLocation.new(domain.domain_name),
|
215
|
+
accel_location ? Nginx::AccelLocation.new(domain.domain_name, accel_location) : nil,
|
216
|
+
Nginx::ActionCableLocation.new(domain.domain_name)
|
217
|
+
].compact,
|
218
|
+
accel_location: accel_location,
|
219
|
+
domain: domain
|
195
220
|
),
|
196
|
-
Nginx::TlsRedirectServerBlock.new(domain_name)
|
221
|
+
Nginx::TlsRedirectServerBlock.new(domain.domain_name),
|
222
|
+
domain: domain
|
197
223
|
)
|
198
224
|
end
|
199
225
|
|
200
|
-
# FIXME: DRY this up with the HTTP class.
|
201
|
-
def save
|
202
|
-
Systemd::Rails.write_unit_file(domain_name) && super
|
203
|
-
end
|
204
|
-
|
205
226
|
attr_reader :certificate_domain
|
206
227
|
end
|
207
228
|
end
|
@@ -18,19 +18,21 @@ module Nginx
|
|
18
18
|
end
|
19
19
|
|
20
20
|
class AccelLocation < Location
|
21
|
-
def initialize(
|
21
|
+
def initialize(domain_name, accel, domain: nil)
|
22
22
|
super(location)
|
23
|
-
@
|
23
|
+
@domain = domain
|
24
|
+
@domain_name = domain_name
|
25
|
+
@accel = accel
|
24
26
|
end
|
25
27
|
|
26
28
|
def to_s(level = 0)
|
27
|
-
Lines.new("location
|
29
|
+
Lines.new("location #{accel.location} {",
|
28
30
|
" internal;",
|
29
|
-
"
|
31
|
+
" alias #{accel.alias_string(domain ? domain.name : domain_name)};",
|
30
32
|
"}").format(level)
|
31
33
|
end
|
32
34
|
|
33
|
-
attr_reader :
|
35
|
+
attr_reader :accel, :domain, :domain_name
|
34
36
|
end
|
35
37
|
|
36
38
|
class AcmeLocation < Location
|
@@ -4,20 +4,21 @@ module Nginx
|
|
4
4
|
##
|
5
5
|
# The server_name line of a server block.
|
6
6
|
class Server
|
7
|
-
|
7
|
+
def initialize(domain: nil)
|
8
|
+
@domain = domain
|
9
|
+
end
|
10
|
+
|
11
|
+
attr_reader :domain
|
8
12
|
|
9
|
-
def
|
10
|
-
|
13
|
+
def root_directory
|
14
|
+
domain.site_root
|
11
15
|
end
|
12
16
|
|
13
17
|
def to_s(level = 0)
|
14
|
-
Lines.new("server_name #{
|
18
|
+
Lines.new("server_name #{domain.certbot_domain_names};").format(level)
|
15
19
|
end
|
16
20
|
end
|
17
21
|
|
18
|
-
##
|
19
|
-
# Server name and site location for a static site.
|
20
|
-
# TODO: I don't like the way this gets twisted when subclassing.
|
21
22
|
class StaticServer < Server
|
22
23
|
def to_s(level = 0)
|
23
24
|
[
|
@@ -32,7 +33,7 @@ module Nginx
|
|
32
33
|
|
33
34
|
class RailsServer < Server
|
34
35
|
def root_directory
|
35
|
-
File.join(
|
36
|
+
File.join(domain.site_root, "public")
|
36
37
|
end
|
37
38
|
|
38
39
|
def to_s(level = 0)
|
@@ -42,7 +43,7 @@ module Nginx
|
|
42
43
|
"# http://stackoverflow.com/a/11313241/3109926 said the following",
|
43
44
|
"# is what serves from public directly without hitting Puma",
|
44
45
|
"root #{root_directory};",
|
45
|
-
"try_files $uri/index.html $uri @#{domain_name};",
|
46
|
+
"try_files $uri/index.html $uri @#{domain.domain_name};",
|
46
47
|
"error_page 500 502 503 504 /500.html;",
|
47
48
|
"client_max_body_size 4G;",
|
48
49
|
"keepalive_timeout 10;"
|
@@ -4,7 +4,9 @@
|
|
4
4
|
# Write nginx configuration files.
|
5
5
|
module Nginx
|
6
6
|
class ServerBlock
|
7
|
-
def initialize(upstream: nil, server: nil, listen: nil, location: nil)
|
7
|
+
def initialize(upstream: nil, server: nil, listen: nil, location: nil, accel_location: nil, domain: nil)
|
8
|
+
@accel_location = accel_location
|
9
|
+
@domain = domain
|
8
10
|
@listen = listen
|
9
11
|
@location = Array(location)
|
10
12
|
@server = server
|
@@ -26,6 +28,7 @@ module Nginx
|
|
26
28
|
#{[
|
27
29
|
@server&.to_s(1),
|
28
30
|
@listen&.to_s(1),
|
31
|
+
@accel_location&.proxy_set_header(server.domain.domain_name),
|
29
32
|
@location&.map { |l| l.to_s(1) }
|
30
33
|
].compact.join("\n\n")}
|
31
34
|
}
|
@@ -36,7 +39,7 @@ SERVER_BLOCK
|
|
36
39
|
upstream&.to_s
|
37
40
|
end
|
38
41
|
|
39
|
-
attr_reader :listen, :location, :server, :upstream
|
42
|
+
attr_reader :accel_location, :domain, :listen, :location, :server, :upstream
|
40
43
|
end
|
41
44
|
|
42
45
|
class SiteServerBlock < ServerBlock
|
@@ -57,7 +60,7 @@ SERVER_BLOCK
|
|
57
60
|
|
58
61
|
class RailsServerBlock < SiteServerBlock
|
59
62
|
def root_directory
|
60
|
-
File.join(
|
63
|
+
File.join(domain.site_root, "/public")
|
61
64
|
end
|
62
65
|
end
|
63
66
|
|
@@ -67,7 +70,7 @@ SERVER_BLOCK
|
|
67
70
|
class TlsRedirectServerBlock < ServerBlock
|
68
71
|
def initialize(domain_name)
|
69
72
|
super(
|
70
|
-
server: Server.new(domain_name),
|
73
|
+
server: Server.new(domain: SharedInfrastructure::Domain.new(domain_name)),
|
71
74
|
listen: ListenHttp.new,
|
72
75
|
location: RedirectLocation.new
|
73
76
|
)
|
@@ -0,0 +1,51 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SharedInfrastructure
|
4
|
+
module OutputHelpers
|
5
|
+
# @param indent_string The string to use for indenting. Defaults to the
|
6
|
+
# first character of `s`.
|
7
|
+
# @param amount The number of `indent_string` to put at the start of each
|
8
|
+
# line. Default: 2.
|
9
|
+
# @param indent_empty_lines Don't indent empty lines unless this is true.
|
10
|
+
def indent(s, amount = 2, indent_string = nil, indent_empty_lines = false)
|
11
|
+
indent_string = indent_string || s[/^[ \t]/] || " "
|
12
|
+
re = indent_empty_lines ? /^/ : /^(?!$)/
|
13
|
+
s.gsub(re, indent_string * amount)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
class Output < File
|
18
|
+
def initialize(file_name, *args)
|
19
|
+
super Output.file_name(file_name), *args
|
20
|
+
end
|
21
|
+
|
22
|
+
class << self
|
23
|
+
##
|
24
|
+
# Fake root. If block is given, change the root only for the duration
|
25
|
+
# of the block. If no block is given, is the same as configure.
|
26
|
+
def fake_root(root = nil)
|
27
|
+
if block_given?
|
28
|
+
begin
|
29
|
+
save_root = Output.root
|
30
|
+
fake_root(root)
|
31
|
+
result = yield
|
32
|
+
ensure
|
33
|
+
fake_root(save_root)
|
34
|
+
result
|
35
|
+
end
|
36
|
+
else
|
37
|
+
self.root = root
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def file_name(file_name)
|
42
|
+
return file_name unless Output.root
|
43
|
+
file_name = File.join(Output.root, file_name)
|
44
|
+
FileUtils.mkdir_p(File.dirname(file_name))
|
45
|
+
file_name
|
46
|
+
end
|
47
|
+
|
48
|
+
attr_accessor :root
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "optparse"
|
4
|
+
require "production"
|
5
|
+
|
6
|
+
module Runner
|
7
|
+
class << self
|
8
|
+
attr_accessor :debug
|
9
|
+
end
|
10
|
+
self.debug = false
|
11
|
+
|
12
|
+
##
|
13
|
+
# Runner for deployment
|
14
|
+
class Deploy
|
15
|
+
def main
|
16
|
+
process_options
|
17
|
+
raise MissingArgument, "repository required" unless ARGV.size == 1
|
18
|
+
Production.repository = ARGV[0]
|
19
|
+
end
|
20
|
+
|
21
|
+
def process_options
|
22
|
+
options = OptionParser.new do |opts|
|
23
|
+
opts.banner = "Usage: [options] REPOSITORY"
|
24
|
+
|
25
|
+
opts.on("-b BRANCH", "--branch BRANCH", "Branch in repository to deploy.") do |branch|
|
26
|
+
Production.branch = branch
|
27
|
+
end
|
28
|
+
|
29
|
+
opts.on("-d", "--debug", "Print debugging information.") do
|
30
|
+
Runner.debug = true
|
31
|
+
end
|
32
|
+
|
33
|
+
opts.on("-r DIRECTORY",
|
34
|
+
"--root DIRECTORY",
|
35
|
+
"DIRECTORY. Set a root for files. This options is for debugging.") do |directory|
|
36
|
+
Output.fake_root(directory)
|
37
|
+
end
|
38
|
+
|
39
|
+
opts.on("-u REPOSITORY_USER",
|
40
|
+
"--user REPOSITORY_USER",
|
41
|
+
"REPOSITORY_USER. User name for the respository.") do |user|
|
42
|
+
Production.user = user
|
43
|
+
end
|
44
|
+
end
|
45
|
+
options.parse!
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
class MissingArgument < RuntimeError
|
50
|
+
def initialize(msg)
|
51
|
+
super msg
|
52
|
+
end
|
53
|
+
attr_reader :msg
|
54
|
+
end
|
55
|
+
end
|
@@ -25,7 +25,8 @@ module Runner
|
|
25
25
|
user = options.delete(:user) || "ubuntu"
|
26
26
|
certificate_domain = options.delete(:certificate_domain)
|
27
27
|
accel_location = options.delete(:accel_location)
|
28
|
-
|
28
|
+
domain = SharedInfrastructure::Domain.new(domain_name)
|
29
|
+
protocol_class.new(user, certificate_domain, accel_location: accel_location, domain: domain)
|
29
30
|
end
|
30
31
|
end
|
31
32
|
end
|
@@ -30,7 +30,8 @@ module Runner
|
|
30
30
|
domain_name = options.delete(:domain_name)
|
31
31
|
proxy_url = options.delete(:proxy_url)
|
32
32
|
certificate_domain = options.delete(:certificate_domain)
|
33
|
-
|
33
|
+
domain = SharedInfrastructure::Domain.new(domain_name)
|
34
|
+
protocol_class.new(proxy_url, certificate_domain, domain: domain)
|
34
35
|
end
|
35
36
|
end
|
36
37
|
end
|
@@ -14,7 +14,8 @@ module Runner
|
|
14
14
|
domain_name = options.delete(:domain_name)
|
15
15
|
user = options.delete(:user) || "ubuntu"
|
16
16
|
certificate_domain = options.delete(:certificate_domain)
|
17
|
-
|
17
|
+
domain = SharedInfrastructure::Domain.new(domain_name)
|
18
|
+
protocol_class.new(user, certificate_domain, domain: domain)
|
18
19
|
end
|
19
20
|
end
|
20
21
|
end
|
@@ -11,14 +11,14 @@ module Systemd
|
|
11
11
|
"redis." + domain_name
|
12
12
|
end
|
13
13
|
|
14
|
-
def write_unit_file(domain_name)
|
15
|
-
if ENV["SECRET_KEY_BASE"].nil? ||
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
end
|
21
|
-
|
14
|
+
def write_unit_file(domain_name, domain)
|
15
|
+
# if ENV["SECRET_KEY_BASE"].nil? ||
|
16
|
+
# ENV["DATABASE_USERNAME"].nil? ||
|
17
|
+
# ENV["DATABASE_PASSWORD"].nil? ||
|
18
|
+
# ENV["EMAIL_PASSWORD"].nil?
|
19
|
+
# raise "Missing environment variable"
|
20
|
+
# end
|
21
|
+
#
|
22
22
|
puts "writing unit file (domain_name): #{Systemd.unit_file(domain_name)} (#{domain_name})" if Runner.debug
|
23
23
|
|
24
24
|
result = File.open(Systemd.unit_file(domain_name), "w") do |f|
|
@@ -44,6 +44,7 @@ module Systemd
|
|
44
44
|
# Environment=PUMA_DEBUG=1
|
45
45
|
Environment=RACK_ENV=production
|
46
46
|
Environment=RAILS_ENV=production
|
47
|
+
EnvironmentFile=#{domain.secrets}
|
47
48
|
Environment=SECRET_KEY_BASE=#{ENV['SECRET_KEY_BASE']}
|
48
49
|
Environment=DATABASE_USERNAME=#{ENV['DATABASE_USERNAME']}
|
49
50
|
Environment=DATABASE_PASSWORD=#{ENV['DATABASE_PASSWORD']}
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
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.15
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Larry Reid
|
@@ -25,6 +25,8 @@ files:
|
|
25
25
|
- bin/create-reverse-proxy
|
26
26
|
- bin/create-server-block
|
27
27
|
- lib/shared_infrastructure.rb
|
28
|
+
- lib/shared_infrastructure/domain.rb
|
29
|
+
- lib/shared_infrastructure/nginx/accel.rb
|
28
30
|
- lib/shared_infrastructure/nginx/builder.rb
|
29
31
|
- lib/shared_infrastructure/nginx/lines.rb
|
30
32
|
- lib/shared_infrastructure/nginx/listen.rb
|
@@ -32,9 +34,10 @@ files:
|
|
32
34
|
- lib/shared_infrastructure/nginx/nginx.rb
|
33
35
|
- lib/shared_infrastructure/nginx/server.rb
|
34
36
|
- lib/shared_infrastructure/nginx/server_block.rb
|
35
|
-
- lib/shared_infrastructure/nginx/site.rb
|
36
37
|
- lib/shared_infrastructure/nginx/upstream.rb
|
38
|
+
- lib/shared_infrastructure/output.rb
|
37
39
|
- lib/shared_infrastructure/runner/base.rb
|
40
|
+
- lib/shared_infrastructure/runner/deploy.rb
|
38
41
|
- lib/shared_infrastructure/runner/rails.rb
|
39
42
|
- lib/shared_infrastructure/runner/reverse_proxy.rb
|
40
43
|
- lib/shared_infrastructure/runner/static_site.rb
|
@@ -1,29 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Nginx
|
4
|
-
##
|
5
|
-
# Server name and site location for a static site.
|
6
|
-
# TODO: I don't like the way this gets twisted when subclassing.
|
7
|
-
class Site < Server
|
8
|
-
attr_reader :user
|
9
|
-
|
10
|
-
def initialize(domain_name, user = "ubuntu")
|
11
|
-
super domain_name
|
12
|
-
@user = user
|
13
|
-
end
|
14
|
-
|
15
|
-
def root_directory
|
16
|
-
Nginx.root_directory(domain_name)
|
17
|
-
end
|
18
|
-
|
19
|
-
def to_s(level = 0)
|
20
|
-
[
|
21
|
-
super(level),
|
22
|
-
Lines.new(
|
23
|
-
"root #{Nginx.root_directory(domain_name)};",
|
24
|
-
"index index.html index.htm;"
|
25
|
-
).format(level)
|
26
|
-
].join("\n\n")
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|