ruby-nginx 1.0.0.pre.alpha
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 +7 -0
- data/CHANGELOG.md +10 -0
- data/LICENSE.txt +21 -0
- data/README.md +113 -0
- data/exe/ruby-nginx +5 -0
- data/lib/ruby/nginx/cli.rb +61 -0
- data/lib/ruby/nginx/configuration.rb +86 -0
- data/lib/ruby/nginx/templates/nginx.conf.erb +96 -0
- data/lib/ruby/nginx/utils/command.rb +29 -0
- data/lib/ruby/nginx/utils/mkcert.rb +28 -0
- data/lib/ruby/nginx/utils/nginx.rb +63 -0
- data/lib/ruby/nginx/utils/safe_file.rb +36 -0
- data/lib/ruby/nginx/version.rb +7 -0
- data/lib/ruby/nginx.rb +44 -0
- metadata +103 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: b353ecaf5dec92d755ac159abf88fc2b921e453985d12ef6be729e2fcd9c2765
|
4
|
+
data.tar.gz: 16c92ddbba0f1e570c3840ccf7bc6564f7da3f333a49f3239155f56ee69857de
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 90db5eb4b5c372b497a0c89ff5d02dde13424ff8dff51a128ade28c55b18ce637584da752b5cb85e3515f2430baefa4852292ecc841e7e453de514c19955bbac
|
7
|
+
data.tar.gz: '039257b96c36d8db7d8476c6b65428bf8d4b68cf5e662e3cb2a9a3e29b4c215b5bda3fe2659b10ae08ce8a80efc1dcdc2acae511b80a9b75b82727dd943c552d'
|
data/CHANGELOG.md
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2024 Bert McCutchen
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,113 @@
|
|
1
|
+
# Ruby NGINX
|
2
|
+
|
3
|
+
Utility gem with an added CLI for configuring NGINX with SSL.
|
4
|
+
|
5
|
+
1. ERB for NGINX configuration templating.
|
6
|
+
2. Mkcert for SSL certificate generation.
|
7
|
+
3. Tried and true NGINX for reverse proxying.
|
8
|
+
|
9
|
+
This gem is intended to be an aid to your development environment - complemented by [Rails NGINX](https://github.com/bert-mccutchen/rails-nginx). **Don't use this in production.**
|
10
|
+
|
11
|
+
[](https://ko-fi.com/M4M76DVZR)
|
12
|
+
|
13
|
+
## Installation
|
14
|
+
|
15
|
+
Install via Bundler:
|
16
|
+
```bash
|
17
|
+
bundle add ruby-nginx
|
18
|
+
```
|
19
|
+
|
20
|
+
Or install it manually:
|
21
|
+
```bash
|
22
|
+
gem install ruby-nginx
|
23
|
+
```
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
### Library Usage
|
28
|
+
|
29
|
+
#### Adding an NGINX configuration
|
30
|
+
```ruby
|
31
|
+
Ruby::Nginx.add!(
|
32
|
+
# required
|
33
|
+
domain: "example.test",
|
34
|
+
|
35
|
+
# required
|
36
|
+
port: 3000,
|
37
|
+
|
38
|
+
# default: 127.0.0.1
|
39
|
+
host: "localhost",
|
40
|
+
|
41
|
+
# default: included template
|
42
|
+
template_path: "$HOME/projects/example-app/nginx.conf.erb",
|
43
|
+
|
44
|
+
# default: $PWD
|
45
|
+
root_path: "$HOME/projects/example-app/public",
|
46
|
+
|
47
|
+
# default: false
|
48
|
+
ssl: true,
|
49
|
+
|
50
|
+
# default: false
|
51
|
+
log: true,
|
52
|
+
|
53
|
+
# default: ~/.ruby-nginx/certs/_[DOMAIN].pem
|
54
|
+
ssl_certificate_path: "$HOME/projects/example-app/tmp/nginx/_example.test.pem",
|
55
|
+
|
56
|
+
# default: ~/.ruby-nginx/certs/_[DOMAIN]-key.pem
|
57
|
+
ssl_certificate_key_path: "$HOME/projects/example-app/tmp/nginx/_example.test-key.pem",
|
58
|
+
|
59
|
+
# default: ~/.ruby-nginx/logs/[DOMAIN].access.log
|
60
|
+
access_log_path: "$HOME/projects/example-app/log/nginx/example.test.access.log",
|
61
|
+
|
62
|
+
# default: ~/.ruby-nginx/logs/[DOMAIN].error.log
|
63
|
+
error_log_path: "$HOME/projects/example-app/log/nginx/example.test.error.log"
|
64
|
+
)
|
65
|
+
```
|
66
|
+
|
67
|
+
#### Removing an NGINX configuration
|
68
|
+
```ruby
|
69
|
+
Ruby::Nginx.remove!(domain: "example.test")
|
70
|
+
```
|
71
|
+
|
72
|
+
### CLI Usage
|
73
|
+
|
74
|
+
#### Adding an NGINX configuration
|
75
|
+
```
|
76
|
+
Usage:
|
77
|
+
ruby-nginx add -d, --domain=DOMAIN -p, --port=N
|
78
|
+
|
79
|
+
Options:
|
80
|
+
-d, --domain=DOMAIN # eg. your-app.test
|
81
|
+
-p, --port=N # eg. 3000
|
82
|
+
-h, [--host=HOST] # default: 127.0.0.1
|
83
|
+
-r, [--root-path=ROOT_PATH] # default: $PWD
|
84
|
+
-s, [--ssl], [--no-ssl], [--skip-ssl] # default: false
|
85
|
+
-l, [--log], [--no-log], [--skip-log] # default: false
|
86
|
+
-cert-file, [--ssl-certificate-path=SSL_CERTIFICATE_PATH] # default: ~/.ruby-nginx/certs/_[DOMAIN].pem
|
87
|
+
-key-file, [--ssl-certificate-key-path=SSL_CERTIFICATE_KEY_PATH] # default: ~/.ruby-nginx/certs/_[DOMAIN]-key.pem
|
88
|
+
-access-log, [--access-log-path=ACCESS_LOG_PATH] # default: ~/.ruby-nginx/logs/[DOMAIN].access.log
|
89
|
+
-error-log, [--error-log-path=ERROR_LOG_PATH] # default: ~/.ruby-nginx/logs/[DOMAIN].error.log
|
90
|
+
```
|
91
|
+
|
92
|
+
#### Removing an NGINX configuration
|
93
|
+
```
|
94
|
+
Usage:
|
95
|
+
ruby-nginx remove -d, --domain=DOMAIN
|
96
|
+
|
97
|
+
Options:
|
98
|
+
-d, --domain=DOMAIN # eg. your-app.test
|
99
|
+
```
|
100
|
+
|
101
|
+
## Development
|
102
|
+
|
103
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
104
|
+
|
105
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
106
|
+
|
107
|
+
## Contributing
|
108
|
+
|
109
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/bert-mccutchen/ruby-nginx.
|
110
|
+
|
111
|
+
## License
|
112
|
+
|
113
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/exe/ruby-nginx
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "thor"
|
4
|
+
require_relative "../nginx"
|
5
|
+
|
6
|
+
module Ruby
|
7
|
+
module Nginx
|
8
|
+
class CLI < Thor
|
9
|
+
include Thor::Actions
|
10
|
+
|
11
|
+
FAKE_CONFIG = Ruby::Nginx::Configuration.new(domain: "[DOMAIN]")
|
12
|
+
|
13
|
+
def self.defaults(param)
|
14
|
+
"default: #{FAKE_CONFIG.defaults[param]}"
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.default_paths(param)
|
18
|
+
"default: #{FAKE_CONFIG.default_paths[param]}"
|
19
|
+
end
|
20
|
+
|
21
|
+
desc "add", "Add a NGINX server configuration"
|
22
|
+
method_option :domain, aliases: "-d", type: :string, required: true, desc: "eg. example.test"
|
23
|
+
method_option :port, aliases: "-p", type: :numeric, required: true, desc: "eg. 3000"
|
24
|
+
method_option :host, aliases: "-h", type: :string, desc: defaults(:host)
|
25
|
+
method_option :root_path, aliases: "-r", type: :string, desc: "default: $PWD"
|
26
|
+
method_option :ssl, aliases: "-s", type: :boolean, desc: defaults(:ssl)
|
27
|
+
method_option :log, aliases: "-l", type: :boolean, desc: defaults(:log)
|
28
|
+
method_option :ssl_certificate_path, aliases: "-cert-file", type: :string, desc: default_paths(:ssl_certificate_path)
|
29
|
+
method_option :ssl_certificate_key_path, aliases: "-key-file", type: :string, desc: default_paths(:ssl_certificate_key_path)
|
30
|
+
method_option :access_log_path, aliases: "-access-log", type: :string, desc: default_paths(:access_log_path)
|
31
|
+
method_option :error_log_path, aliases: "-error-log", type: :string, desc: default_paths(:error_log_path)
|
32
|
+
def add
|
33
|
+
config = {
|
34
|
+
domain: options.domain,
|
35
|
+
port: options.port,
|
36
|
+
host: options.host,
|
37
|
+
root_path: options.root_path,
|
38
|
+
ssl: options.ssl,
|
39
|
+
log: options.log,
|
40
|
+
ssl_certificate_path: options.ssl_certificate_path,
|
41
|
+
ssl_certificate_key_path: options.ssl_certificate_key_path,
|
42
|
+
access_log_path: options.access_log_path,
|
43
|
+
error_log_path: options.error_log_path
|
44
|
+
}.compact
|
45
|
+
|
46
|
+
Ruby::Nginx.add!(**config)
|
47
|
+
end
|
48
|
+
|
49
|
+
desc "remove", "Remove a NGINX server configuration"
|
50
|
+
method_option :domain, aliases: "-d", type: :string, required: true, desc: "eg. example.test"
|
51
|
+
def remove
|
52
|
+
Ruby::Nginx.remove!(domain: options.domain)
|
53
|
+
end
|
54
|
+
|
55
|
+
desc "template", "Copy a NGINX server configuration template"
|
56
|
+
def template
|
57
|
+
copy_file File.expand_path("templates/nginx.conf.erb", __dir__), "nginx.conf.erb"
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,86 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "erb"
|
4
|
+
require_relative "utils/mkcert"
|
5
|
+
require_relative "utils/safe_file"
|
6
|
+
|
7
|
+
module Ruby
|
8
|
+
module Nginx
|
9
|
+
class Configuration
|
10
|
+
DEFAULT_PATH = "~/.ruby-nginx"
|
11
|
+
|
12
|
+
attr_accessor :options
|
13
|
+
|
14
|
+
def initialize(options = {})
|
15
|
+
@options = defaults.merge(options)
|
16
|
+
end
|
17
|
+
|
18
|
+
def name
|
19
|
+
"ruby_nginx_#{options[:domain].gsub(/\W/, "_")}"
|
20
|
+
end
|
21
|
+
|
22
|
+
def generate!
|
23
|
+
validate!
|
24
|
+
apply_dynamic_defaults!
|
25
|
+
|
26
|
+
create_ssl_certs! if options[:ssl]
|
27
|
+
create_log_files! if options[:log]
|
28
|
+
|
29
|
+
ERB.new(File.read(options[:template_path])).result(binding)
|
30
|
+
end
|
31
|
+
|
32
|
+
def defaults
|
33
|
+
{
|
34
|
+
host: "127.0.0.1",
|
35
|
+
root_path: Dir.pwd,
|
36
|
+
template_path: File.expand_path("templates/nginx.conf.erb", __dir__),
|
37
|
+
ssl: false,
|
38
|
+
log: false
|
39
|
+
}
|
40
|
+
end
|
41
|
+
|
42
|
+
def default_paths
|
43
|
+
{
|
44
|
+
ssl_certificate_path: default_path("certs/_#{options[:domain]}.pem"),
|
45
|
+
ssl_certificate_key_path: default_path("certs/_#{options[:domain]}-key.pem"),
|
46
|
+
access_log_path: default_path("logs/#{options[:domain]}.access.log"),
|
47
|
+
error_log_path: default_path("logs/#{options[:domain]}.error.log")
|
48
|
+
}
|
49
|
+
end
|
50
|
+
|
51
|
+
private
|
52
|
+
|
53
|
+
def default_path(path)
|
54
|
+
"~/.ruby-nginx/#{path}"
|
55
|
+
end
|
56
|
+
|
57
|
+
def realize_option_path!(option)
|
58
|
+
options[option] = Utils::SafeFile.touch(options[option])
|
59
|
+
end
|
60
|
+
|
61
|
+
def validate!
|
62
|
+
raise ArgumentError, "domain is required" unless options[:domain]
|
63
|
+
raise ArgumentError, "port is required" unless options[:port]
|
64
|
+
raise ArgumentError, "template_path is required" unless options[:template_path]
|
65
|
+
end
|
66
|
+
|
67
|
+
def apply_dynamic_defaults!
|
68
|
+
self.options = default_paths.merge(options)
|
69
|
+
end
|
70
|
+
|
71
|
+
def create_ssl_certs!
|
72
|
+
Utils::Mkcert.setup!
|
73
|
+
Utils::Mkcert.create!(
|
74
|
+
options[:domain],
|
75
|
+
realize_option_path!(:ssl_certificate_path),
|
76
|
+
realize_option_path!(:ssl_certificate_key_path)
|
77
|
+
)
|
78
|
+
end
|
79
|
+
|
80
|
+
def create_log_files!
|
81
|
+
realize_option_path!(:access_log_path)
|
82
|
+
realize_option_path!(:error_log_path)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
@@ -0,0 +1,96 @@
|
|
1
|
+
# Generic and overly simple NGINX configuration file for a web application.
|
2
|
+
# It's not great. If you want great, you should probably provide your own.
|
3
|
+
# https://www.digitalocean.com/community/tools/nginx
|
4
|
+
|
5
|
+
map $http_upgrade $connection_upgrade {
|
6
|
+
default upgrade;
|
7
|
+
'' close;
|
8
|
+
}
|
9
|
+
|
10
|
+
map $remote_addr $proxy_forwarded_elem {
|
11
|
+
# IPv4 addresses can be sent as-is
|
12
|
+
~^[0-9.]+$ "for=$remote_addr";
|
13
|
+
|
14
|
+
# IPv6 addresses need to be bracketed and quoted
|
15
|
+
~^[0-9A-Fa-f:.]+$ "for=\"[$remote_addr]\"";
|
16
|
+
|
17
|
+
# Unix domain socket names cannot be represented in RFC 7239 syntax
|
18
|
+
default "for=unknown";
|
19
|
+
}
|
20
|
+
|
21
|
+
map $http_forwarded $proxy_add_forwarded {
|
22
|
+
# If the incoming Forwarded header is syntactically valid, append to it
|
23
|
+
"~^(,[ \\t]*)*([!#$%&'*+.^_`|~0-9A-Za-z-]+=([!#$%&'*+.^_`|~0-9A-Za-z-]+|\"([\\t \\x21\\x23-\\x5B\\x5D-\\x7E\\x80-\\xFF]|\\\\[\\t \\x21-\\x7E\\x80-\\xFF])*\"))?(;([!#$%&'*+.^_`|~0-9A-Za-z-]+=([!#$%&'*+.^_`|~0-9A-Za-z-]+|\"([\\t \\x21\\x23-\\x5B\\x5D-\\x7E\\x80-\\xFF]|\\\\[\\t \\x21-\\x7E\\x80-\\xFF])*\"))?)*([ \\t]*,([ \\t]*([!#$%&'*+.^_`|~0-9A-Za-z-]+=([!#$%&'*+.^_`|~0-9A-Za-z-]+|\"([\\t \\x21\\x23-\\x5B\\x5D-\\x7E\\x80-\\xFF]|\\\\[\\t \\x21-\\x7E\\x80-\\xFF])*\"))?(;([!#$%&'*+.^_`|~0-9A-Za-z-]+=([!#$%&'*+.^_`|~0-9A-Za-z-]+|\"([\\t \\x21\\x23-\\x5B\\x5D-\\x7E\\x80-\\xFF]|\\\\[\\t \\x21-\\x7E\\x80-\\xFF])*\"))?)*)?)*$" "$http_forwarded, $proxy_forwarded_elem";
|
24
|
+
|
25
|
+
# Otherwise, replace it
|
26
|
+
default "$proxy_forwarded_elem";
|
27
|
+
}
|
28
|
+
|
29
|
+
upstream <%= name %> {
|
30
|
+
server 127.0.0.1:<%= options[:port] %> fail_timeout=0;
|
31
|
+
}
|
32
|
+
|
33
|
+
server {
|
34
|
+
listen 80;
|
35
|
+
server_name <%= options[:domain] %>;
|
36
|
+
root <%= options[:root_path] %>;
|
37
|
+
try_files $uri/index.html $uri @<%= name %>;
|
38
|
+
|
39
|
+
<% if options[:ssl] %>
|
40
|
+
# ssl
|
41
|
+
listen 443 ssl;
|
42
|
+
http2 on;
|
43
|
+
ssl_certificate <%= options[:ssl_certificate_path] %>;
|
44
|
+
ssl_certificate_key <%= options[:ssl_certificate_key_path] %>;
|
45
|
+
<% end %>
|
46
|
+
|
47
|
+
<% if options[:log] %>
|
48
|
+
# logging
|
49
|
+
access_log <%= options[:access_log_path] %> combined buffer=512k flush=1m;
|
50
|
+
error_log <%= options[:error_log_path] %> warn;
|
51
|
+
<% end %>
|
52
|
+
|
53
|
+
# reverse proxy
|
54
|
+
location @<%= name %> {
|
55
|
+
proxy_pass http://<%= name %>;
|
56
|
+
proxy_http_version 1.1;
|
57
|
+
proxy_cache_bypass $http_upgrade;
|
58
|
+
|
59
|
+
<% if options[:ssl] %>
|
60
|
+
# proxy SSL
|
61
|
+
proxy_ssl_server_name on;
|
62
|
+
<% end %>
|
63
|
+
|
64
|
+
# proxy headers
|
65
|
+
proxy_set_header Host $host;
|
66
|
+
proxy_set_header Upgrade $http_upgrade;
|
67
|
+
proxy_set_header Connection $connection_upgrade;
|
68
|
+
proxy_set_header X-Real-IP $remote_addr;
|
69
|
+
proxy_set_header Forwarded $proxy_add_forwarded;
|
70
|
+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
71
|
+
proxy_set_header X-Forwarded-Proto $scheme;
|
72
|
+
proxy_set_header X-Forwarded-Host $host;
|
73
|
+
proxy_set_header X-Forwarded-Port $server_port;
|
74
|
+
}
|
75
|
+
|
76
|
+
# favicon.ico
|
77
|
+
location = /favicon.ico {
|
78
|
+
log_not_found off;
|
79
|
+
}
|
80
|
+
|
81
|
+
# robots.txt
|
82
|
+
location = /robots.txt {
|
83
|
+
log_not_found off;
|
84
|
+
}
|
85
|
+
|
86
|
+
# assets, media
|
87
|
+
location ~* \.(?:css(\.map)?|js(\.map)?|jpe?g|png|gif|ico|cur|heic|webp|tiff?|mp3|m4a|aac|ogg|midi?|wav|mp4|mov|webm|mpe?g|avi|ogv|flv|wmv)$ {
|
88
|
+
expires 7d;
|
89
|
+
}
|
90
|
+
|
91
|
+
# svg, fonts
|
92
|
+
location ~* \.(?:svgz?|ttf|ttc|otf|eot|woff2?)$ {
|
93
|
+
add_header Access-Control-Allow-Origin "*";
|
94
|
+
expires 7d;
|
95
|
+
}
|
96
|
+
}
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Ruby
|
4
|
+
module Nginx
|
5
|
+
module Utils
|
6
|
+
class Command
|
7
|
+
attr_reader :stdout, :stderr, :status
|
8
|
+
|
9
|
+
def initialize(raise: nil)
|
10
|
+
@error_type = raise
|
11
|
+
@stdout = nil
|
12
|
+
@stderr = nil
|
13
|
+
@status = nil
|
14
|
+
end
|
15
|
+
|
16
|
+
def run(cmd)
|
17
|
+
@stdout, @stderr, @status = Open3.capture3(cmd)
|
18
|
+
raise @error_type, @stderr if @error_type && !@status.success?
|
19
|
+
|
20
|
+
self
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.run(cmd)
|
24
|
+
new.run(cmd)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "open3"
|
4
|
+
require_relative "command"
|
5
|
+
require_relative "safe_file"
|
6
|
+
|
7
|
+
module Ruby
|
8
|
+
module Nginx
|
9
|
+
module Utils
|
10
|
+
class Mkcert
|
11
|
+
Error = Class.new(StandardError)
|
12
|
+
SetupError = Class.new(Error)
|
13
|
+
CreateError = Class.new(Error)
|
14
|
+
|
15
|
+
class << self
|
16
|
+
def setup!
|
17
|
+
Command.new(raise: SetupError).run("mkcert -install")
|
18
|
+
end
|
19
|
+
|
20
|
+
def create!(domain, cert_file_path, key_file_path)
|
21
|
+
cmd = "mkcert -cert-file #{cert_file_path} -key-file #{key_file_path} #{domain}"
|
22
|
+
Command.new(raise: CreateError).run(cmd)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "open3"
|
4
|
+
require_relative "command"
|
5
|
+
require_relative "safe_file"
|
6
|
+
|
7
|
+
module Ruby
|
8
|
+
module Nginx
|
9
|
+
module Utils
|
10
|
+
class Nginx
|
11
|
+
class Error < StandardError; end
|
12
|
+
|
13
|
+
class InstallError < Error; end
|
14
|
+
|
15
|
+
class ConfigError < Error; end
|
16
|
+
|
17
|
+
class StartError < Error; end
|
18
|
+
|
19
|
+
class StopError < Error; end
|
20
|
+
|
21
|
+
class << self
|
22
|
+
def add_server_config(name, config)
|
23
|
+
SafeFile.write(server_config_path(name), config)
|
24
|
+
end
|
25
|
+
|
26
|
+
def remove_server_config(name)
|
27
|
+
FileUtils.rm_f(server_config_path(name))
|
28
|
+
end
|
29
|
+
|
30
|
+
def validate_config!
|
31
|
+
Command.new(raise: ConfigError).run("nginx -t")
|
32
|
+
end
|
33
|
+
|
34
|
+
def start!
|
35
|
+
Command.new(raise: StartError).run("nginx")
|
36
|
+
end
|
37
|
+
|
38
|
+
def stop!
|
39
|
+
Command.new(raise: StopError).run("nginx -s stop")
|
40
|
+
rescue StopError => e
|
41
|
+
raise unless e.message.include?("invalid PID number")
|
42
|
+
end
|
43
|
+
|
44
|
+
def restart!
|
45
|
+
stop!
|
46
|
+
start!
|
47
|
+
end
|
48
|
+
|
49
|
+
private
|
50
|
+
|
51
|
+
def config_file_path
|
52
|
+
result = Command.new(raise: Error).run("nginx -V")
|
53
|
+
result.stderr.split.find { |s| s.include?("--conf-path=") }.delete_prefix("--conf-path=")
|
54
|
+
end
|
55
|
+
|
56
|
+
def server_config_path(name)
|
57
|
+
"#{File.dirname(config_file_path)}/servers/#{name}.conf"
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "fileutils"
|
4
|
+
|
5
|
+
module Ruby
|
6
|
+
module Nginx
|
7
|
+
module Utils
|
8
|
+
class SafeFile
|
9
|
+
class << self
|
10
|
+
def touch(file_path)
|
11
|
+
safe_path = File.expand_path(file_path)
|
12
|
+
|
13
|
+
FileUtils.mkdir_p(File.dirname(safe_path))
|
14
|
+
FileUtils.touch(safe_path)
|
15
|
+
|
16
|
+
safe_path
|
17
|
+
end
|
18
|
+
|
19
|
+
def write(file_path, content)
|
20
|
+
safe_path = touch(file_path)
|
21
|
+
|
22
|
+
File.write(safe_path, content)
|
23
|
+
|
24
|
+
safe_path
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def safe_path(file_path)
|
30
|
+
File.expand_path(file_path)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
data/lib/ruby/nginx.rb
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "nginx/configuration"
|
4
|
+
require_relative "nginx/utils/nginx"
|
5
|
+
require_relative "nginx/version"
|
6
|
+
|
7
|
+
module Ruby
|
8
|
+
module Nginx
|
9
|
+
class Error < StandardError; end
|
10
|
+
|
11
|
+
def self.add!(options = {}, &block)
|
12
|
+
conf = config(options, &block)
|
13
|
+
|
14
|
+
Utils::Nginx.add_server_config(conf.name, conf.generate!)
|
15
|
+
Utils::Nginx.validate_config!
|
16
|
+
Utils::Nginx.restart!
|
17
|
+
|
18
|
+
conf
|
19
|
+
rescue Utils::Nginx::Error
|
20
|
+
remove!
|
21
|
+
raise
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.remove!(options = {}, &block)
|
25
|
+
conf = config(options, &block)
|
26
|
+
|
27
|
+
Utils::Nginx.remove_server_config(conf.name)
|
28
|
+
Utils::Nginx.restart!
|
29
|
+
|
30
|
+
conf
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
def self.config(options = {})
|
36
|
+
conf = Configuration.new(options)
|
37
|
+
|
38
|
+
yield conf if block_given?
|
39
|
+
|
40
|
+
conf
|
41
|
+
end
|
42
|
+
private_class_method :config
|
43
|
+
end
|
44
|
+
end
|
metadata
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ruby-nginx
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0.pre.alpha
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Bert McCutchen
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-11-22 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: erb
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: open3
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: thor
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Utility gem with an added CLI for configuring NGINX with SSL.
|
56
|
+
email:
|
57
|
+
- mail@bertm.dev
|
58
|
+
executables:
|
59
|
+
- ruby-nginx
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- CHANGELOG.md
|
64
|
+
- LICENSE.txt
|
65
|
+
- README.md
|
66
|
+
- exe/ruby-nginx
|
67
|
+
- lib/ruby/nginx.rb
|
68
|
+
- lib/ruby/nginx/cli.rb
|
69
|
+
- lib/ruby/nginx/configuration.rb
|
70
|
+
- lib/ruby/nginx/templates/nginx.conf.erb
|
71
|
+
- lib/ruby/nginx/utils/command.rb
|
72
|
+
- lib/ruby/nginx/utils/mkcert.rb
|
73
|
+
- lib/ruby/nginx/utils/nginx.rb
|
74
|
+
- lib/ruby/nginx/utils/safe_file.rb
|
75
|
+
- lib/ruby/nginx/version.rb
|
76
|
+
homepage: https://github.com/bert-mccutchen/ruby-nginx
|
77
|
+
licenses:
|
78
|
+
- MIT
|
79
|
+
metadata:
|
80
|
+
homepage_uri: https://github.com/bert-mccutchen/ruby-nginx
|
81
|
+
source_code_uri: https://github.com/bert-mccutchen/ruby-nginx
|
82
|
+
changelog_uri: https://github.com/bert-mccutchen/ruby-nginx/blob/main/CHANGELOG.md
|
83
|
+
rubygems_mfa_required: 'true'
|
84
|
+
post_install_message:
|
85
|
+
rdoc_options: []
|
86
|
+
require_paths:
|
87
|
+
- lib
|
88
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: 3.0.0
|
93
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
requirements: []
|
99
|
+
rubygems_version: 3.5.22
|
100
|
+
signing_key:
|
101
|
+
specification_version: 4
|
102
|
+
summary: Utility for configuring NGINX with SSL.
|
103
|
+
test_files: []
|