Class: NginxUtils::VirtualHost

Inherits:
Object
  • Object
show all
Defined in:
lib/nginx_utils/virtual_host.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ VirtualHost

Returns a new instance of VirtualHost.



7
8
9
10
11
12
13
# File 'lib/nginx_utils/virtual_host.rb', line 7

def initialize(options={})
  options = options.inject({}){|r,(k,v)| r.store(k.to_sym, v); r}
  set_vhost_type(options)
  set_common_params(options)
  set_protocols(options)
  set_log_params(options)
end

Instance Attribute Details

#access_log_formatObject

Returns the value of attribute access_log_format.



5
6
7
# File 'lib/nginx_utils/virtual_host.rb', line 5

def access_log_format
  @access_log_format
end

#auth_basicObject

Returns the value of attribute auth_basic.



5
6
7
# File 'lib/nginx_utils/virtual_host.rb', line 5

def auth_basic
  @auth_basic
end

#auth_basic_user_fileObject

Returns the value of attribute auth_basic_user_file.



5
6
7
# File 'lib/nginx_utils/virtual_host.rb', line 5

def auth_basic_user_file
  @auth_basic_user_file
end

#destinationObject

Returns the value of attribute destination.



5
6
7
# File 'lib/nginx_utils/virtual_host.rb', line 5

def destination
  @destination
end

#error_log_levelObject

Returns the value of attribute error_log_level.



5
6
7
# File 'lib/nginx_utils/virtual_host.rb', line 5

def error_log_level
  @error_log_level
end

#httpObject

Returns the value of attribute http.



5
6
7
# File 'lib/nginx_utils/virtual_host.rb', line 5

def http
  @http
end

#httpsObject

Returns the value of attribute https.



5
6
7
# File 'lib/nginx_utils/virtual_host.rb', line 5

def https
  @https
end

#indexObject

Returns the value of attribute index.



5
6
7
# File 'lib/nginx_utils/virtual_host.rb', line 5

def index
  @index
end

#log_dirObject

Returns the value of attribute log_dir.



5
6
7
# File 'lib/nginx_utils/virtual_host.rb', line 5

def log_dir
  @log_dir
end

#prefixObject

Returns the value of attribute prefix.



5
6
7
# File 'lib/nginx_utils/virtual_host.rb', line 5

def prefix
  @prefix
end

#rootObject

Returns the value of attribute root.



5
6
7
# File 'lib/nginx_utils/virtual_host.rb', line 5

def root
  @root
end

#server_nameObject

Returns the value of attribute server_name.



5
6
7
# File 'lib/nginx_utils/virtual_host.rb', line 5

def server_name
  @server_name
end

#ssl_certificateObject

Returns the value of attribute ssl_certificate.



5
6
7
# File 'lib/nginx_utils/virtual_host.rb', line 5

def ssl_certificate
  @ssl_certificate
end

#ssl_certificate_keyObject

Returns the value of attribute ssl_certificate_key.



5
6
7
# File 'lib/nginx_utils/virtual_host.rb', line 5

def ssl_certificate_key
  @ssl_certificate_key
end

#vhost_typeObject

Returns the value of attribute vhost_type.



5
6
7
# File 'lib/nginx_utils/virtual_host.rb', line 5

def vhost_type
  @vhost_type
end

Instance Method Details

#configObject



70
71
72
73
# File 'lib/nginx_utils/virtual_host.rb', line 70

def config
  content = open(File.expand_path("../../../template/virtual_host.erb", __FILE__)).read
  ERB.new(content).result(binding).gsub(/^\s+$/, "").gsub(/\n+/, "\n")
end

#set_common_params(options) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/nginx_utils/virtual_host.rb', line 39

def set_common_params(options)
  # Arguments: prefix, server_name, root, index, auth_basic, auth_basic_user_file in options
  @prefix = options.fetch(:prefix, "/usr/local/nginx")
  @server_name = options.fetch(:server_name, "example.com")
  @root = options.fetch(:root, File.join(@prefix, "vhosts", @server_name, "html"))
  @index = options.fetch(:index, ["index.html", "index.htm"].join(" "))
  @auth_basic = options[:auth_basic]
  if @auth_basic
    @auth_basic_user_file = options.fetch(:auth_basic_user_file, File.join(@prefix, "vhosts", @server_name, "etc", "users"))
  end
end

#set_log_params(options) ⇒ Object



63
64
65
66
67
68
# File 'lib/nginx_utils/virtual_host.rb', line 63

def set_log_params(options)
  # Arguments: log_dir, access_log_format, error_log_level in options
  @log_dir = options.fetch(:log_dir, File.join(@prefix, "vhosts", @server_name, "logs"))
  @access_log_format = options.fetch(:access_log_format, :ltsv)
  @error_log_level = options.fetch(:error_log_level, :info)
end

#set_protocols(options) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/nginx_utils/virtual_host.rb', line 51

def set_protocols(options)
  # Arguments: http, https, ssl_certificate, ssl_certificate_key in options
  @http = options[:http].nil? ? true : options[:http]
  @https = options[:https].nil? ? true : options[:https]
  @http = false if options[:only_https]
  @https = false if options[:only_http]
  if @https
    @ssl_certificate = options.fetch(:ssl_certificate, File.join(@prefix, "vhosts", @server_name, "ssl.crt", "server.crt"))
    @ssl_certificate_key = options.fetch(:ssl_certificate_key, File.join(@prefix, "vhosts", @server_name, "ssl.key", "server.key"))
  end
end

#set_vhost_type(options) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/nginx_utils/virtual_host.rb', line 15

def set_vhost_type(options)
  # Arguments: vhost_type, destination in options
  if options[:vhost_type].nil?
    @vhost_type = :normal
  else
    case options[:vhost_type].to_sym
    when :unicorn then
      @vhost_type = :unicorn
      @destination = options.fetch(:destination, "127.0.0.1:8080")
    when :proxy then
      @vhost_type = :proxy
      @destination = options.fetch(:destination, "127.0.0.1:8080")
    when :passenger then
      @vhost_type = :passenger
    else
      @vhost_type = :normal
    end
  end

  if @destination =~ /\.sock$/ && @destination !~ /^unix:/
    @destination = "unix:#{@destination}"
  end
end