Class: Ruby::Nginx::Commands::SetupNginx

Inherits:
TerminalCommand show all
Includes:
Ruby::Nginx::Constants
Defined in:
lib/ruby/nginx/commands/setup_nginx.rb

Constant Summary collapse

INCLUDE_STATEMENT =
"include #{File.expand_path(SERVERS_PATH)}/*;"
EXTERNAL_INCLUDE_STATEMENTS =
[
  "include /etc/nginx/sites-enabled/\\*;",
  "include servers/\\*;"
]

Constants included from Ruby::Nginx::Constants

Ruby::Nginx::Constants::CONFIG_PATH, Ruby::Nginx::Constants::SERVERS_PATH

Instance Attribute Summary

Attributes inherited from TerminalCommand

#cmd, #error_type, #printer, #result, #user

Instance Method Summary collapse

Methods included from Helpers::SudoHelper

#sudoify

Methods included from Helpers::PromptHelper

#yes?

Constructor Details

#initialize(sudo: false) ⇒ SetupNginx

Returns a new instance of SetupNginx.



20
21
22
23
24
25
26
27
28
# File 'lib/ruby/nginx/commands/setup_nginx.rb', line 20

def initialize(sudo: false)
  @sudo = sudo
  sudo_reason = "Allow sudo elevation to add \"#{INCLUDE_STATEMENT}\" to NGINX configuration file?"

  super(
    cmd: "echo \"#{new_config}\" | #{sudoify("tee #{config_file_path}", sudo, sudo_reason)}",
    raise: Ruby::Nginx::SetupError
  )
end

Instance Method Details

#runObject



30
31
32
33
34
35
36
37
# File 'lib/ruby/nginx/commands/setup_nginx.rb', line 30

def run
  super unless setup?
rescue Ruby::Nginx::SetupError
  raise if @sudo

  # Elevate to sudo and try again.
  self.class.new(sudo: true).run
end