Class: Cheese::Nginx::Config
- Inherits:
-
Object
- Object
- Cheese::Nginx::Config
- Defined in:
- lib/web/nginx.rb
Overview
Represents the Nginx config file, giving the ability to manipulate the VHosts within it, and save changes.
Constant Summary collapse
- TEMPLATES =
The values will be replaced in load_templates
{ :header => "header.inc", :footer => "footer.inc", :vhost => "vhost.inc", :proxy => "proxy.inc" }
- VHOST_BEGIN =
/#### VHOST (.*?) BEGIN ####/
- VHOST_END =
/#### VHOST (.*?) END ####/
- PROXY_BEGIN =
/#### PROXY (.*?) BEGIN ####/
- PROXY_END =
/#### PROXY (.*?) END ####/
- PROXY_THREAD =
/:(\d.*);/
Instance Attribute Summary collapse
-
#domains ⇒ Object
Returns the value of attribute domains.
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
Instance Method Summary collapse
-
#add(options = {}) ⇒ Object
Add a virtual host to the Nginx config file.
-
#domain(url) ⇒ Object
Find a domain which matches the url given.
-
#initialize(file) ⇒ Config
constructor
A new instance of Config.
-
#remove(object) ⇒ Object
Remove a virtual host from the Nginx config file.
-
#replace(options = {}) ⇒ Object
Replace a virtual host in the Nginx config file.
-
#restart ⇒ Object
Restart the Nginx server.
-
#save ⇒ Object
Save any changes made to the Nginx config file, this has to specifically called to be sure it’s not accidentally called.
-
#start ⇒ Object
Start the Nginx server.
-
#stop ⇒ Object
Stop the Nginx server.
Constructor Details
#initialize(file) ⇒ Config
Returns a new instance of Config.
30 31 32 33 34 |
# File 'lib/web/nginx.rb', line 30 def initialize(file) @filename = file load_templates find_domains end |
Instance Attribute Details
#domains ⇒ Object
Returns the value of attribute domains.
13 14 15 |
# File 'lib/web/nginx.rb', line 13 def domains @domains end |
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
12 13 14 |
# File 'lib/web/nginx.rb', line 12 def filename @filename end |
Instance Method Details
#add(options = {}) ⇒ Object
Add a virtual host to the Nginx config file
42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/web/nginx.rb', line 42 def add(={}) domain = [:name] return false if domain.nil? domain = clean_domain(domain) threads = [:thread_count].to_i vhost_proxy = Cheese::Nginx::Proxy.new(domain, new_ports(threads), self) vhost = Cheese::Nginx::VirtualHost.new(domain, self) new_domain = Cheese::Nginx::Domain.new(vhost, vhost_proxy) @domains << new_domain return new_domain end |
#domain(url) ⇒ Object
Find a domain which matches the url given
37 38 39 |
# File 'lib/web/nginx.rb', line 37 def domain(url) @domains.detect {|domain| domain.vhost.domain == url} end |
#remove(object) ⇒ Object
Remove a virtual host from the Nginx config file
57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/web/nginx.rb', line 57 def remove(object) if object.is_a? Cheese::Nginx::VirtualHost @vhosts.delete(object) elsif object.is_a? Cheese::Nginx::Proxy @proxies.delete(object) elsif object.is_a? Cheese::Nginx::Domain @domains.delete(object) elsif object.is_a? Hash @domains.delete_if {|domain| domain.host == object[:name] } return object[:name] end end |
#replace(options = {}) ⇒ Object
Replace a virtual host in the Nginx config file
71 72 73 74 |
# File 'lib/web/nginx.rb', line 71 def replace(={}) self.remove [:name] self.add end |
#restart ⇒ Object
Restart the Nginx server
121 122 123 124 125 |
# File 'lib/web/nginx.rb', line 121 def restart Cheese::Verbose.log_task "Restart nginx server" do %x{ /etc/init.d/nginx restart } end end |
#save ⇒ Object
Save any changes made to the Nginx config file, this has to specifically called to be sure it’s not accidentally called
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/web/nginx.rb', line 78 def save # self.as_www do Cheese::Verbose.log_task "Saving #{@domains.size} domains" File.open(@filename, "w+") do |file| file.puts TEMPLATES[:header] file.puts @domains.each do |domain| Cheese::Verbose.log_task "Adding proxy for #{domain.host}" file.puts "#### PROXY #{domain.host} BEGIN ####" file.puts proxy_text(domain) file.puts "#### PROXY #{domain.host} END ####" end file.puts @domains.each do |domain| Cheese::Verbose.log_task "Adding vhost for #{domain.host}" file.puts "#### VHOST #{domain.host} BEGIN ####" file.puts vhost_text(domain) file.puts "#### VHOST #{domain.host} END ####" end file.puts TEMPLATES[:footer] end # end Cheese::Verbose.log_task "Saved #{@filename}" end |