class Nginx::Manager

Public Class Methods

add(ports) click to toggle source
# File lib/nginx/manager.rb, line 17
def add(ports)
  ports = [*ports]
  $logger.info "adding #{ports} to nginx"
  modify_nginx do |ofile, iline|
    ofile.puts(iline)
    if iline =~ /upstream phantomjs/
      ports.each do |port|
        ofile.puts(phantom_upstream(port)) unless port_defined?(port)
      end
    end
  end
end
remove(ports) click to toggle source
# File lib/nginx/manager.rb, line 9
def remove(ports)
  ports = [*ports]
  $logger.info "removing #{ports} from nginx"
  modify_nginx do |ofile, iline|
    ofile.puts(iline) if !line_matches_ports(iline, ports)
  end
end

Private Class Methods

line_matches_ports(line, ports) click to toggle source
# File lib/nginx/manager.rb, line 60
def line_matches_ports(line, ports)
  line =~ ports_regexp(ports)
end
modify_nginx() { |ofile, iline| ... } click to toggle source
# File lib/nginx/manager.rb, line 50
def modify_nginx
  File.open(Cfg.new_nginx_conf, "w") do |ofile|
    File.foreach(Cfg.nginx_conf) do |iline|
      yield ofile, iline
    end
  end
  switch_nginx_configs
  reload_nginx
end
phantom_upstream(port) click to toggle source
# File lib/nginx/manager.rb, line 32
def phantom_upstream(port)
  "  server 127.0.0.1:#{port} fail_timeout=0; # #{Time.now}"
end
port_defined?(port) click to toggle source
# File lib/nginx/manager.rb, line 36
def port_defined?(port)
  File.readlines(Cfg.nginx_conf).grep(/#{port}/).size > 0
end
ports_regexp(ports) click to toggle source
# File lib/nginx/manager.rb, line 64
def ports_regexp(ports)
  /#{ports.join("|")}/
end
reload_nginx() click to toggle source
# File lib/nginx/manager.rb, line 45
def reload_nginx
  $logger.info "reloading nginx"
  Utils::Shell.execute "nginx -s reload"
end
switch_nginx_configs() click to toggle source
# File lib/nginx/manager.rb, line 40
def switch_nginx_configs
  $logger.info "switching nginx configurations"
  `mv #{Cfg.new_nginx_conf} #{Cfg.nginx_conf}`
end