ruby-nginx 1.0.0 → 1.0.2

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
  SHA256:
3
- metadata.gz: 7e053789e61b0bf22afb92ea549a0ecb68bc4615761ae7342ff246e095100932
4
- data.tar.gz: de3c4bd02f4745cca14cbf15f034a5187c10d3cddd083b1948ab63d9d958a6d2
3
+ metadata.gz: f05254d3fb1670cd6c832b3d0cff71f7f4d04483c004de96b6ee94917a0bfc8f
4
+ data.tar.gz: 4b22e857122735828aea9ab8a506dc2c4d60d8a65caa8893b1810fba70cb3dd4
5
5
  SHA512:
6
- metadata.gz: 154b601c107623263d962fdcb5832d8143a579e2725dfe8788f7f764aa6fed22d7bf955ed248227385e484e9183008fb5231eae078c39febe7491f4e309a9bfa
7
- data.tar.gz: 128c4ff6ae5b29351872953e7597ed900219178855ea4f53f9953ce3b31973b71dfb4e429710fac82339072811d4c8b9286994ae575d67d6eba1d4571865c8a2
6
+ metadata.gz: 18ddfaf53cce1326a30c8741ba22773ac0ecc78a336ae4c3418c388345dbea7ff9c62255954a63a336383a55fbdee061a2781995debd90b05b2d4707c8b32851
7
+ data.tar.gz: f42671b12a2ad714dfd698e14cb43738b880903cdaf5cc730131bac5d973e93a81012d2b8334c231e151d0b687f04cdf36cf7b7ea695e4c7e88e3c4fc391029e
@@ -11,19 +11,35 @@ module Ruby
11
11
  @ip = ip
12
12
  @sudo = sudo
13
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
14
 
16
- super(cmd:, raise: Ruby::Nginx::ConfigError)
15
+ super(
16
+ cmd: "echo \"#{host_config}\" | #{sudoify("tee -a /etc/hosts", sudo, sudo_reason)}",
17
+ raise: Ruby::Nginx::ConfigError
18
+ )
17
19
  end
18
20
 
19
21
  def run
20
- super
22
+ super unless added?
21
23
  rescue Ruby::Nginx::ConfigError
22
24
  raise if @sudo
23
25
 
24
26
  # Elevate to sudo and try again.
25
27
  self.class.new(@host, @ip, sudo: true).run
26
28
  end
29
+
30
+ private
31
+
32
+ def added?
33
+ hosts.include?(host_config)
34
+ end
35
+
36
+ def hosts
37
+ @hosts ||= Ruby::Nginx::System::SafeFile.read("/etc/hosts")
38
+ end
39
+
40
+ def host_config
41
+ "#{@ip} #{@host}"
42
+ end
27
43
  end
28
44
  end
29
45
  end
@@ -9,8 +9,30 @@ module Ruby
9
9
  class AddNginxConfig
10
10
  include Ruby::Nginx::Constants
11
11
 
12
- def run(name, config)
13
- Ruby::Nginx::System::SafeFile.write("#{SERVERS_PATH}/#{name}.conf", config)
12
+ def initialize(name, config)
13
+ @name = name
14
+ @config = config
15
+ end
16
+
17
+ def run
18
+ return false unless changed?
19
+
20
+ Ruby::Nginx::System::SafeFile.write(config_path, @config)
21
+ true
22
+ end
23
+
24
+ private
25
+
26
+ def changed?
27
+ !Ruby::Nginx::System::SafeFile.exist?(config_path) || (nginx_config != @config)
28
+ end
29
+
30
+ def config_path
31
+ @config_path ||= "#{SERVERS_PATH}/#{@name}.conf"
32
+ end
33
+
34
+ def nginx_config
35
+ @nginx_config ||= Ruby::Nginx::System::SafeFile.read(config_path)
14
36
  end
15
37
  end
16
38
  end
@@ -7,8 +7,9 @@ module Ruby
7
7
  module Nginx
8
8
  module Commands
9
9
  class RemoveHostMapping < TerminalCommand
10
- def initialize(host, sudo: false)
10
+ def initialize(host, ignore_ip: nil, sudo: false)
11
11
  @host = host
12
+ @ignore_ip = ignore_ip
12
13
  @sudo = sudo
13
14
  @sudo_reason = "Allow sudo elevation to remove \"#{host}\" from /etc/hosts?"
14
15
 
@@ -16,16 +17,26 @@ module Ruby
16
17
  end
17
18
 
18
19
  def run
19
- super
20
+ super unless removed?
20
21
  rescue Ruby::Nginx::ConfigError
21
22
  raise if @sudo
22
23
 
23
24
  # Elevate to sudo and try again.
24
- self.class.new(@host, sudo: true).run
25
+ self.class.new(@host, ignore_ip: @ignore_ip, sudo: true).run
25
26
  end
26
27
 
27
28
  private
28
29
 
30
+ def removed?
31
+ hosts.each_line.none? do |host_config|
32
+ host_config.include?(@host) && (@ignore_ip.nil? || !host_config.include?(@ignore_ip))
33
+ end
34
+ end
35
+
36
+ def hosts
37
+ @hosts ||= Ruby::Nginx::System::SafeFile.read("/etc/hosts")
38
+ end
39
+
29
40
  def darwin_command
30
41
  sudoify("sed -i '' '/#{@host}/d' /etc/hosts", @sudo, @sudo_reason)
31
42
  end
@@ -9,8 +9,25 @@ module Ruby
9
9
  class RemoveNginxConfig
10
10
  include Ruby::Nginx::Constants
11
11
 
12
- def run(name)
13
- Ruby::Nginx::System::SafeFile.rm("#{SERVERS_PATH}/#{name}.conf")
12
+ def initialize(name)
13
+ @name = name
14
+ end
15
+
16
+ def run
17
+ return false unless exists?
18
+
19
+ Ruby::Nginx::System::SafeFile.rm(config_path)
20
+ true
21
+ end
22
+
23
+ private
24
+
25
+ def exists?
26
+ Ruby::Nginx::System::SafeFile.exist?(config_path)
27
+ end
28
+
29
+ def config_path
30
+ @config_path ||= "#{SERVERS_PATH}/#{@name}.conf"
14
31
  end
15
32
  end
16
33
  end
@@ -20,9 +20,11 @@ module Ruby
20
20
  def initialize(sudo: false)
21
21
  @sudo = sudo
22
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
23
 
25
- super(cmd:, raise: Ruby::Nginx::SetupError)
24
+ super(
25
+ cmd: "echo \"#{new_config}\" | #{sudoify("tee #{config_file_path}", sudo, sudo_reason)}",
26
+ raise: Ruby::Nginx::SetupError
27
+ )
26
28
  end
27
29
 
28
30
  def run
@@ -9,9 +9,8 @@ module Ruby
9
9
  def initialize(sudo: false)
10
10
  @sudo = sudo
11
11
  sudo_reason = "Allow sudo elevation to start NGINX?"
12
- cmd = sudoify("nginx", sudo, sudo_reason)
13
12
 
14
- super(cmd:, raise: Ruby::Nginx::StartError)
13
+ super(cmd: sudoify("nginx", sudo, sudo_reason), raise: Ruby::Nginx::StartError)
15
14
  end
16
15
 
17
16
  def run
@@ -9,9 +9,8 @@ module Ruby
9
9
  def initialize(sudo: false)
10
10
  @sudo = sudo
11
11
  sudo_reason = "Allow sudo elevation to stop NGINX?"
12
- cmd = sudoify("nginx -s stop", sudo, sudo_reason)
13
12
 
14
- super(cmd:, raise: Ruby::Nginx::StopError)
13
+ super(cmd: sudoify("nginx -s stop", sudo, sudo_reason), raise: Ruby::Nginx::StopError)
15
14
  end
16
15
 
17
16
  def run
@@ -9,9 +9,8 @@ module Ruby
9
9
  def initialize(sudo: false)
10
10
  @sudo = sudo
11
11
  sudo_reason = "Allow sudo elevation to validate NGINX config?"
12
- cmd = sudoify("nginx -t", sudo, sudo_reason)
13
12
 
14
- super(cmd:, raise: Ruby::Nginx::ConfigError)
13
+ super(cmd: sudoify("nginx -t", sudo, sudo_reason), raise: Ruby::Nginx::ConfigError)
15
14
  end
16
15
 
17
16
  def run
@@ -9,12 +9,12 @@ module Ruby
9
9
  class Hosts
10
10
  class << self
11
11
  def add(host, ip)
12
- remove(host)
13
- Commands::AddHostMapping.new(host, ip).run.success?
12
+ remove(host, ignore_ip: ip)
13
+ Commands::AddHostMapping.new(host, ip).run&.success?
14
14
  end
15
15
 
16
- def remove(host)
17
- Commands::RemoveHostMapping.new(host).run.success?
16
+ def remove(host, ignore_ip: nil)
17
+ Commands::RemoveHostMapping.new(host, ignore_ip:).run&.success?
18
18
  end
19
19
  end
20
20
  end
@@ -32,11 +32,11 @@ module Ruby
32
32
  end
33
33
 
34
34
  def add_server_config(name, config)
35
- Commands::AddNginxConfig.new.run(name, config)
35
+ Commands::AddNginxConfig.new(name, config).run
36
36
  end
37
37
 
38
38
  def remove_server_config(name)
39
- Commands::RemoveNginxConfig.new.run(name)
39
+ Commands::RemoveNginxConfig.new(name).run
40
40
  end
41
41
 
42
42
  def validate_config!
@@ -10,11 +10,15 @@ module Ruby
10
10
  def mkdir(dir_path)
11
11
  safe_path = File.expand_path(dir_path)
12
12
 
13
- FileUtils.mkdir_p(dir_path)
13
+ FileUtils.mkdir_p(safe_path)
14
14
 
15
15
  safe_path
16
16
  end
17
17
 
18
+ def exist?(file_path)
19
+ File.exist?(File.expand_path(file_path))
20
+ end
21
+
18
22
  def touch(file_path)
19
23
  safe_path = File.expand_path(file_path)
20
24
 
@@ -25,9 +29,7 @@ module Ruby
25
29
  end
26
30
 
27
31
  def read(file_path)
28
- safe_path = File.expand_path(file_path)
29
-
30
- File.read(safe_path)
32
+ File.read(File.expand_path(file_path))
31
33
  end
32
34
 
33
35
  def write(file_path, content)
@@ -39,9 +41,7 @@ module Ruby
39
41
  end
40
42
 
41
43
  def rm(file_path)
42
- safe_path = File.expand_path(file_path)
43
-
44
- FileUtils.rm_f(safe_path)
44
+ FileUtils.rm_f(File.expand_path(file_path))
45
45
  end
46
46
  end
47
47
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Ruby
4
4
  module Nginx
5
- VERSION = "1.0.0"
5
+ VERSION = "1.0.2"
6
6
 
7
7
  Version = Gem::Version
8
8
  end
data/lib/ruby/nginx.rb CHANGED
@@ -14,9 +14,11 @@ module Ruby
14
14
  setup!
15
15
  conf = config(options, &block)
16
16
 
17
- System::Nginx.add_server_config(conf.name, conf.generate!)
18
- System::Nginx.validate_config!
19
- System::Nginx.restart!
17
+ if System::Nginx.add_server_config(conf.name, conf.generate!)
18
+ System::Nginx.validate_config!
19
+ System::Nginx.restart!
20
+ end
21
+
20
22
  System::Hosts.add(conf.domain, conf.host)
21
23
 
22
24
  conf
@@ -30,8 +32,7 @@ module Ruby
30
32
  def self.remove!(options = {}, &block)
31
33
  conf = config(options, &block)
32
34
 
33
- System::Nginx.remove_server_config(conf.name)
34
- System::Nginx.restart!
35
+ System::Nginx.restart! if System::Nginx.remove_server_config(conf.name)
35
36
  System::Hosts.remove(conf.domain)
36
37
 
37
38
  conf
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-nginx
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bert McCutchen
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2025-03-22 00:00:00.000000000 Z
11
+ date: 2025-04-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: erb