ruby-nginx 1.0.0.pre.alpha → 1.0.0.pre.beta.1

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.
Files changed (33) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +136 -23
  3. data/lib/ruby/nginx/cli.rb +4 -5
  4. data/lib/ruby/nginx/commands/add_host_mapping.rb +30 -0
  5. data/lib/ruby/nginx/commands/add_nginx_config.rb +18 -0
  6. data/lib/ruby/nginx/commands/create_mkcert_certificate.rb +18 -0
  7. data/lib/ruby/nginx/commands/install_mkcert.rb +65 -0
  8. data/lib/ruby/nginx/commands/install_nginx.rb +56 -0
  9. data/lib/ruby/nginx/commands/nginx_options.rb +33 -0
  10. data/lib/ruby/nginx/commands/nginx_version.rb +22 -0
  11. data/lib/ruby/nginx/commands/remove_host_mapping.rb +47 -0
  12. data/lib/ruby/nginx/commands/remove_nginx_config.rb +18 -0
  13. data/lib/ruby/nginx/commands/setup_mkcert.rb +15 -0
  14. data/lib/ruby/nginx/commands/setup_nginx.rb +75 -0
  15. data/lib/ruby/nginx/commands/start_nginx.rb +28 -0
  16. data/lib/ruby/nginx/commands/stop_nginx.rb +31 -0
  17. data/lib/ruby/nginx/commands/terminal_command.rb +51 -0
  18. data/lib/ruby/nginx/commands/validate_nginx_config.rb +28 -0
  19. data/lib/ruby/nginx/configuration.rb +20 -7
  20. data/lib/ruby/nginx/constants.rb +10 -0
  21. data/lib/ruby/nginx/exceptions.rb +16 -0
  22. data/lib/ruby/nginx/system/hosts.rb +23 -0
  23. data/lib/ruby/nginx/system/mkcert.rb +27 -0
  24. data/lib/ruby/nginx/system/nginx.rb +62 -0
  25. data/lib/ruby/nginx/system/os.rb +48 -0
  26. data/lib/ruby/nginx/{utils → system}/safe_file.rb +10 -4
  27. data/lib/ruby/nginx/templates/nginx.conf.erb +4 -0
  28. data/lib/ruby/nginx/version.rb +3 -1
  29. data/lib/ruby/nginx.rb +24 -9
  30. metadata +57 -13
  31. data/lib/ruby/nginx/utils/command.rb +0 -29
  32. data/lib/ruby/nginx/utils/mkcert.rb +0 -28
  33. data/lib/ruby/nginx/utils/nginx.rb +0 -63
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b353ecaf5dec92d755ac159abf88fc2b921e453985d12ef6be729e2fcd9c2765
4
- data.tar.gz: 16c92ddbba0f1e570c3840ccf7bc6564f7da3f333a49f3239155f56ee69857de
3
+ metadata.gz: 4eb0c324fb8b79dd62fcb04019a2b3a85dcb64f42aec5672fee73a8a222aff77
4
+ data.tar.gz: ded353048e2e065b6bd3395b5c15299ff2a13056b2bccb974e95eec808b77dcf
5
5
  SHA512:
6
- metadata.gz: 90db5eb4b5c372b497a0c89ff5d02dde13424ff8dff51a128ade28c55b18ce637584da752b5cb85e3515f2430baefa4852292ecc841e7e453de514c19955bbac
7
- data.tar.gz: '039257b96c36d8db7d8476c6b65428bf8d4b68cf5e662e3cb2a9a3e29b4c215b5bda3fe2659b10ae08ce8a80efc1dcdc2acae511b80a9b75b82727dd943c552d'
6
+ metadata.gz: e1c8b60d5092576ac44959f01e341d0c581e60e1c2900c65868b1c5cf401f0f439a0453ef83deb9941deb6d6d76bba0286d052abac31b7161209a18e0799464c
7
+ data.tar.gz: 8e9c6495558319fa91d8710abf75d92e85fc688cdd032458d52e3c1a4b1e8c92b09da81dd1135836020f63e65c52890368d2f945187ad259999c13b91033dd43
data/README.md CHANGED
@@ -1,15 +1,57 @@
1
1
  # Ruby NGINX
2
2
 
3
- Utility gem with an added CLI for configuring NGINX with SSL.
3
+ Utility gem with an added CLI for setting up reverse proxies with custom domains. Because sometimes you just don't want to use containers, or your platform doesn't natively run containers without a virtual machine - I'm looking at you - Darwin.
4
4
 
5
- 1. ERB for NGINX configuration templating.
6
- 2. Mkcert for SSL certificate generation.
7
- 3. Tried and true NGINX for reverse proxying.
5
+ :heart: ERB for NGINX configuration templating.
8
6
 
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.**
7
+ :yellow_heart: Self-signed certificate generation via mkcert for HTTPS.
8
+
9
+ :green_heart: Tried and true NGINX for reverse proxying, and hosts mapping for DNS.
10
10
 
11
11
  [![ko-fi](https://ko-fi.com/img/githubbutton_sm.svg)](https://ko-fi.com/M4M76DVZR)
12
12
 
13
+ ---
14
+
15
+ ### Key Features
16
+
17
+ The aim of this gem is to be easy to use, while keeping the user in control of their own machine. With Ruby NGINX's automation, you should feel comfortable, informed, and in control of the process.
18
+
19
+ - Automated installation of NGINX and mkcert that are entirely optional.
20
+ - You will be prompted before installation. You may reject installation to install NGINX or mkcert on your own terms.
21
+ - Sudo - only when necessary, and on your own terms.
22
+ - By default, all attempts to configure or interact with NGINX, /etc/hosts, and mkcert, are done without elevated privileges. Sudo is only used when the initial attempt fails with your users privileges.
23
+ - You will be prompted to accept sudo elevation, and why it is required. Rejection will immediately abort the process.
24
+ - Isolated NGINX configuration.
25
+ - Your NGINX configuration will automatically be updated to include configuration files from `~/.ruby-nginx/servers`. This ensures a clean separation exists between your personal NGINX configuration and Ruby NGINX's automation.
26
+ - Cross-platform and support for multiple package managers.
27
+ - macOS - brew
28
+ - Linux - apt-get, pacman, yum, zypper
29
+ - Complete configuration - everything is configurable. You can even bring your own NGINX config, and if you'd like, your own :beers:.
30
+
31
+ > [!WARNING]
32
+ >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 gem in production.**
33
+
34
+ ---
35
+
36
+ ### Contents
37
+
38
+ - [Installation](#installation)
39
+ - [Library Usage](#library-usage)
40
+ - [Adding an NGINX configuration](#adding-an-nginx-configuration)
41
+ - [Removing an NGINX configuration](#removing-an-nginx-configuration)
42
+ - [CLI Usage](#cli-usage)
43
+ - [Help](#help)
44
+ - [Adding an NGINX configuration](#adding-an-nginx-configuration-1)
45
+ - [Removing an NGINX configuration](#removing-an-nginx-configuration-1)
46
+ - [Development](#development)
47
+ - [Setup](#setup)
48
+ - [Lint / Test](#lint--test)
49
+ - [Debug Console](#debug-console)
50
+ - [CLI Executable](#cli-executable)
51
+ - [Release](#release)
52
+ - [Contributing](#contributing)
53
+ - [License](#license)
54
+
13
55
  ## Installation
14
56
 
15
57
  Install via Bundler:
@@ -22,11 +64,11 @@ Or install it manually:
22
64
  gem install ruby-nginx
23
65
  ```
24
66
 
25
- ## Usage
67
+ ## Library Usage
26
68
 
27
- ### Library Usage
69
+ ### Adding an NGINX configuration
28
70
 
29
- #### Adding an NGINX configuration
71
+ You can pass configuration options directly.
30
72
  ```ruby
31
73
  Ruby::Nginx.add!(
32
74
  # required
@@ -38,11 +80,11 @@ Ruby::Nginx.add!(
38
80
  # default: 127.0.0.1
39
81
  host: "localhost",
40
82
 
41
- # default: included template
42
- template_path: "$HOME/projects/example-app/nginx.conf.erb",
83
+ # default: [packaged template]
84
+ template_path: "~/projects/example-app/nginx.conf.erb",
43
85
 
44
86
  # default: $PWD
45
- root_path: "$HOME/projects/example-app/public",
87
+ root_path: "~/projects/example-app/public",
46
88
 
47
89
  # default: false
48
90
  ssl: true,
@@ -51,33 +93,64 @@ Ruby::Nginx.add!(
51
93
  log: true,
52
94
 
53
95
  # default: ~/.ruby-nginx/certs/_[DOMAIN].pem
54
- ssl_certificate_path: "$HOME/projects/example-app/tmp/nginx/_example.test.pem",
96
+ ssl_certificate_path: "~/projects/example-app/tmp/nginx/_example.test.pem",
55
97
 
56
98
  # default: ~/.ruby-nginx/certs/_[DOMAIN]-key.pem
57
- ssl_certificate_key_path: "$HOME/projects/example-app/tmp/nginx/_example.test-key.pem",
99
+ ssl_certificate_key_path: "~/projects/example-app/tmp/nginx/_example.test-key.pem",
58
100
 
59
101
  # default: ~/.ruby-nginx/logs/[DOMAIN].access.log
60
- access_log_path: "$HOME/projects/example-app/log/nginx/example.test.access.log",
102
+ access_log_path: "~/projects/example-app/log/nginx/example.test.access.log",
61
103
 
62
104
  # default: ~/.ruby-nginx/logs/[DOMAIN].error.log
63
- error_log_path: "$HOME/projects/example-app/log/nginx/example.test.error.log"
105
+ error_log_path: "~/projects/example-app/log/nginx/example.test.error.log"
64
106
  )
65
107
  ```
66
108
 
67
- #### Removing an NGINX configuration
109
+ OR you can interact with the configuration object via a block.
110
+ ```ruby
111
+ Ruby::Nginx.add! do |config|
112
+ config.options[:domain] = "example.test"
113
+ config.options[:port] = 3000
114
+ # etc.
115
+ end
116
+ ```
117
+
118
+ ### Removing an NGINX configuration
119
+
120
+ You can pass configuration options directly.
68
121
  ```ruby
69
122
  Ruby::Nginx.remove!(domain: "example.test")
70
123
  ```
71
124
 
72
- ### CLI Usage
125
+ OR you can interact with the configuration object via a block. However, only domain is used during removal - all other options are ignored.
126
+ ```ruby
127
+ Ruby::Nginx.remove! do |config|
128
+ config.options[:domain] = "example.test"
129
+ end
130
+ ```
131
+
132
+ ## CLI Usage
133
+
134
+ ### Help
135
+
136
+ To print the help text.
137
+ ```
138
+ > ruby-nginx help
139
+ Commands:
140
+ ruby-nginx add -d, --domain=DOMAIN -p, --port=N # Add a NGINX server configuration
141
+ ruby-nginx help [COMMAND] # Describe available commands or one specific command
142
+ ruby-nginx remove -d, --domain=DOMAIN # Remove a NGINX server configuration
143
+ ```
144
+
145
+ ### Adding an NGINX configuration
73
146
 
74
- #### Adding an NGINX configuration
75
147
  ```
148
+ > ruby-nginx help add
76
149
  Usage:
77
150
  ruby-nginx add -d, --domain=DOMAIN -p, --port=N
78
151
 
79
152
  Options:
80
- -d, --domain=DOMAIN # eg. your-app.test
153
+ -d, --domain=DOMAIN # eg. example.test
81
154
  -p, --port=N # eg. 3000
82
155
  -h, [--host=HOST] # default: 127.0.0.1
83
156
  -r, [--root-path=ROOT_PATH] # default: $PWD
@@ -87,22 +160,62 @@ Options:
87
160
  -key-file, [--ssl-certificate-key-path=SSL_CERTIFICATE_KEY_PATH] # default: ~/.ruby-nginx/certs/_[DOMAIN]-key.pem
88
161
  -access-log, [--access-log-path=ACCESS_LOG_PATH] # default: ~/.ruby-nginx/logs/[DOMAIN].access.log
89
162
  -error-log, [--error-log-path=ERROR_LOG_PATH] # default: ~/.ruby-nginx/logs/[DOMAIN].error.log
163
+
164
+ Add a NGINX server configuration
90
165
  ```
91
166
 
92
- #### Removing an NGINX configuration
167
+ ### Removing an NGINX configuration
168
+
93
169
  ```
170
+ > ruby-nginx help remove
94
171
  Usage:
95
172
  ruby-nginx remove -d, --domain=DOMAIN
96
173
 
97
174
  Options:
98
- -d, --domain=DOMAIN # eg. your-app.test
175
+ -d, --domain=DOMAIN # eg. example.test
176
+
177
+ Remove a NGINX server configuration
99
178
  ```
100
179
 
101
180
  ## Development
102
181
 
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.
182
+ ### Setup
183
+
184
+ Install development dependencies.
185
+ ```
186
+ ./bin/setup
187
+ ```
188
+
189
+ ### Lint / Test
190
+
191
+ Run the Standard Ruby linter, and RSpec test suite.
192
+ ```
193
+ bundle exec rake
194
+ ```
195
+
196
+ ### Debug Console
197
+
198
+ Start an interactive Ruby console (IRB).
199
+ ```
200
+ ./bin/console
201
+ ```
202
+
203
+ ### CLI Executable
204
+
205
+ Run the gem's Thor CLI directly.
206
+ ```
207
+ ./exe/ruby-nginx
208
+ ```
209
+
210
+ OR build and install the gem to your local machine.
211
+ ```
212
+ bundle exec rake install
213
+ ruby-nginx help
214
+ ```
215
+
216
+ ### Release
104
217
 
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).
218
+ A new release will automatically be built and uploaded to RubyGems by a [GitHub Actions workflow](./.github/workflows/gem-push.yml) upon the push of a new Git tag.
106
219
 
107
220
  ## Contributing
108
221
 
@@ -44,17 +44,16 @@ module Ruby
44
44
  }.compact
45
45
 
46
46
  Ruby::Nginx.add!(**config)
47
+ rescue Ruby::Nginx::Error => e
48
+ abort "[Ruby::Nginx] #{e.message}"
47
49
  end
48
50
 
49
51
  desc "remove", "Remove a NGINX server configuration"
50
52
  method_option :domain, aliases: "-d", type: :string, required: true, desc: "eg. example.test"
51
53
  def remove
52
54
  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"
55
+ rescue Ruby::Nginx::Error => e
56
+ abort "[Ruby::Nginx] #{e.message}"
58
57
  end
59
58
  end
60
59
  end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "terminal_command"
4
+
5
+ module Ruby
6
+ module Nginx
7
+ module Commands
8
+ class AddHostMapping < TerminalCommand
9
+ def initialize(host, ip, sudo: false)
10
+ @host = host
11
+ @ip = ip
12
+ @sudo = sudo
13
+ sudo_reason = "Allow sudo elevation to add \"#{host}\" to /etc/hosts?"
14
+ cmd = "echo \"#{ip} #{host}\" | #{sudoify("tee -a /etc/hosts", sudo, sudo_reason)}"
15
+
16
+ super(cmd:, raise: Ruby::Nginx::ConfigError)
17
+ end
18
+
19
+ def run
20
+ super
21
+ rescue Ruby::Nginx::ConfigError
22
+ raise if @sudo
23
+
24
+ # Elevate to sudo and try again.
25
+ self.class.new(@host, @ip, sudo: true).run
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../constants"
4
+ require_relative "../system/safe_file"
5
+
6
+ module Ruby
7
+ module Nginx
8
+ module Commands
9
+ class AddNginxConfig
10
+ include Ruby::Nginx::Constants
11
+
12
+ def run(name, config)
13
+ Ruby::Nginx::System::SafeFile.write("#{SERVERS_PATH}/#{name}.conf", config)
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "terminal_command"
4
+
5
+ module Ruby
6
+ module Nginx
7
+ module Commands
8
+ class CreateMkcertCertificate < TerminalCommand
9
+ def initialize(domain, cert_file_path, key_file_path)
10
+ super(
11
+ cmd: "mkcert -cert-file #{cert_file_path} -key-file #{key_file_path} #{domain}",
12
+ raise: Ruby::Nginx::CreateError
13
+ )
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "terminal_command"
4
+ require_relative "../system/os"
5
+
6
+ module Ruby
7
+ module Nginx
8
+ module Commands
9
+ class InstallMkcert < TerminalCommand
10
+ def initialize
11
+ super(cmd: resolve_command, raise: Ruby::Nginx::InstallError, printer: :pretty)
12
+ end
13
+
14
+ def run
15
+ return if installed?
16
+
17
+ if yes?("Would you like to install mkcert?")
18
+ super
19
+ else
20
+ raise Ruby::Nginx::AbortError, "mkcert is required to continue. Please install mkcert."
21
+ end
22
+ end
23
+
24
+ private
25
+
26
+ def installed?
27
+ TTY::Command.new(printer: :null).run!("which mkcert").success?
28
+ end
29
+
30
+ def apt_get_command
31
+ "sudo apt-get install -y libnss3-tools && " \
32
+ "curl -L -C - \"https://dl.filippo.io/mkcert/latest?for=linux/amd64\" -o ./mkcert_download && " \
33
+ "chmod +x ./mkcert_download && " \
34
+ "sudo mv ./mkcert_download /usr/local/bin/mkcert"
35
+ end
36
+
37
+ def brew_command
38
+ "brew install mkcert nss"
39
+ end
40
+
41
+ def pacman_command
42
+ "sudo pacman -Syu nss mkcert"
43
+ end
44
+
45
+ def yum_command
46
+ "sudo yum install -y nss-tools && " \
47
+ "curl -L -C - \"https://dl.filippo.io/mkcert/latest?for=linux/amd64\" -o ./mkcert_download && " \
48
+ "chmod +x ./mkcert_download && " \
49
+ "sudo mv ./mkcert_download /usr/local/bin/mkcert"
50
+ end
51
+
52
+ def zypper_command
53
+ "sudo zypper install -y mozilla-nss-tools && " \
54
+ "curl -L -C - \"https://dl.filippo.io/mkcert/latest?for=linux/amd64\" -o ./mkcert_download && " \
55
+ "chmod +x ./mkcert_download && " \
56
+ "sudo mv ./mkcert_download /usr/local/bin/mkcert"
57
+ end
58
+
59
+ def resolve_command
60
+ send("#{Ruby::Nginx::System::OS.instance.package_manager}_command")
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "terminal_command"
4
+ require_relative "../system/os"
5
+
6
+ module Ruby
7
+ module Nginx
8
+ module Commands
9
+ class InstallNginx < TerminalCommand
10
+ def initialize
11
+ super(cmd: resolve_command, raise: Ruby::Nginx::InstallError, printer: :pretty)
12
+ end
13
+
14
+ def run
15
+ return if installed?
16
+
17
+ if yes?("Would you like to install Nginx?")
18
+ super
19
+ else
20
+ raise Ruby::Nginx::AbortError, "Nginx is required to continue. Please install Nginx."
21
+ end
22
+ end
23
+
24
+ private
25
+
26
+ def installed?
27
+ TTY::Command.new(printer: :null).run!("which nginx").success?
28
+ end
29
+
30
+ def apt_get_command
31
+ "sudo apt-get install -y nginx"
32
+ end
33
+
34
+ def pacman_command
35
+ "sudo pacman -Syu nginx"
36
+ end
37
+
38
+ def brew_command
39
+ "brew install nginx"
40
+ end
41
+
42
+ def yum_command
43
+ "sudo yum install -y nginx"
44
+ end
45
+
46
+ def zypper_command
47
+ "sudo zypper install -y nginx"
48
+ end
49
+
50
+ def resolve_command
51
+ send("#{Ruby::Nginx::System::OS.instance.package_manager}_command")
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "terminal_command"
4
+
5
+ module Ruby
6
+ module Nginx
7
+ module Commands
8
+ class NginxOptions < TerminalCommand
9
+ def initialize
10
+ super(cmd: "nginx -V", raise: Ruby::Nginx::Error)
11
+ end
12
+
13
+ def run
14
+ super
15
+
16
+ # nginx -V outputs to stderr
17
+ format_output(result.stderr)
18
+ end
19
+
20
+ private
21
+
22
+ def format_output(output)
23
+ output.split(/configure arguments: */)[1].split.map(&method(:format_option)).to_h
24
+ end
25
+
26
+ def format_option(option)
27
+ key_val = option.split("=")
28
+ [key_val[0], key_val[1] || true]
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "terminal_command"
4
+
5
+ module Ruby
6
+ module Nginx
7
+ module Commands
8
+ class NginxVersion < TerminalCommand
9
+ def initialize
10
+ super(cmd: "nginx -v", raise: Ruby::Nginx::Error)
11
+ end
12
+
13
+ def run
14
+ super
15
+
16
+ # nginx -v outputs to stderr
17
+ Ruby::Nginx::Version.new(result.stderr.match(/\d+\.\d+\.?\d?/).to_s)
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "terminal_command"
4
+ require_relative "../system/os"
5
+
6
+ module Ruby
7
+ module Nginx
8
+ module Commands
9
+ class RemoveHostMapping < TerminalCommand
10
+ def initialize(host, sudo: false)
11
+ @host = host
12
+ @sudo = sudo
13
+ @sudo_reason = "Allow sudo elevation to remove \"#{host}\" from /etc/hosts?"
14
+
15
+ super(cmd: resolve_command, raise: Ruby::Nginx::ConfigError)
16
+ end
17
+
18
+ def run
19
+ super
20
+ rescue Ruby::Nginx::ConfigError
21
+ raise if @sudo
22
+
23
+ # Elevate to sudo and try again.
24
+ self.class.new(@host, sudo: true).run
25
+ end
26
+
27
+ private
28
+
29
+ def darwin_command
30
+ sudoify("sed -i '' '/#{@host}/d' /etc/hosts", @sudo, @sudo_reason)
31
+ end
32
+
33
+ def linux_command
34
+ sudoify("sed -i '/#{@host}/d' /etc/hosts", @sudo, @sudo_reason)
35
+ end
36
+
37
+ def resolve_command
38
+ if Ruby::Nginx::System::OS.instance.darwin?
39
+ darwin_command
40
+ else
41
+ linux_command
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../constants"
4
+ require_relative "../system/safe_file"
5
+
6
+ module Ruby
7
+ module Nginx
8
+ module Commands
9
+ class RemoveNginxConfig
10
+ include Ruby::Nginx::Constants
11
+
12
+ def run(name)
13
+ Ruby::Nginx::System::SafeFile.rm("#{SERVERS_PATH}/#{name}.conf")
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "terminal_command"
4
+
5
+ module Ruby
6
+ module Nginx
7
+ module Commands
8
+ class SetupMkcert < TerminalCommand
9
+ def initialize
10
+ super(cmd: "mkcert -install", raise: Ruby::Nginx::SetupError)
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,75 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../constants"
4
+ require_relative "../system/nginx"
5
+ require_relative "../system/safe_file"
6
+ require_relative "terminal_command"
7
+
8
+ module Ruby
9
+ module Nginx
10
+ module Commands
11
+ class SetupNginx < TerminalCommand
12
+ include Ruby::Nginx::Constants
13
+
14
+ INCLUDE_STATEMENT = "include #{File.expand_path(SERVERS_PATH)}/*;"
15
+ EXTERNAL_INCLUDE_STATEMENTS = [
16
+ "include /etc/nginx/sites-enabled/\\*;",
17
+ "include servers/\\*;"
18
+ ]
19
+
20
+ def initialize(sudo: false)
21
+ @sudo = sudo
22
+ sudo_reason = "Allow sudo elevation to add \"#{INCLUDE_STATEMENT}\" to NGINX configuration file?"
23
+ cmd = "echo \"#{new_config}\" | #{sudoify("tee #{config_file_path}", sudo, sudo_reason)}"
24
+
25
+ super(cmd:, raise: Ruby::Nginx::SetupError)
26
+ end
27
+
28
+ def run
29
+ super unless setup?
30
+ rescue Ruby::Nginx::SetupError
31
+ raise if @sudo
32
+
33
+ # Elevate to sudo and try again.
34
+ self.class.new(sudo: true).run
35
+ end
36
+
37
+ private
38
+
39
+ def setup?
40
+ config.include?(INCLUDE_STATEMENT)
41
+ end
42
+
43
+ def config_file_path
44
+ @config_file_path ||= Ruby::Nginx::System::Nginx.options["--conf-path"]
45
+ end
46
+
47
+ def config
48
+ @config ||= Ruby::Nginx::System::SafeFile.read(config_file_path)
49
+ end
50
+
51
+ def external_include_statement
52
+ EXTERNAL_INCLUDE_STATEMENTS.each do |statement|
53
+ match = config.match(/(?<indent>\s*)(?<statement>#{statement})/)
54
+ return match if match
55
+ end
56
+
57
+ raise Ruby::Nginx::SetupError,
58
+ "Could not find a suitable place to add the include statement.\n" \
59
+ "Please add the following line to your NGINX configuration file:\n" \
60
+ "#{INCLUDE_STATEMENT}"
61
+ end
62
+
63
+ def new_config
64
+ match = external_include_statement
65
+ indent = match[:indent]
66
+ statement = match.to_s
67
+
68
+ config
69
+ .gsub(statement, "#{statement}\n#{indent}#{INCLUDE_STATEMENT}\n")
70
+ .gsub("\"", "\\\"")
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end