Class: Spawngebob::Compilers::Nginx
- Inherits:
-
Object
- Object
- Spawngebob::Compilers::Nginx
- Includes:
- Spawngebob::Constants
- Defined in:
- lib/spawngebob/compilers/nginx.rb
Constant Summary
Constants included from Spawngebob::Constants
Spawngebob::Constants::BASE_CONFIG_PATH, Spawngebob::Constants::CONFIG_FILE, Spawngebob::Constants::NGINX_CONF, Spawngebob::Constants::NGINX_CONFD_PATH, Spawngebob::Constants::NGINX_CONF_PATH, Spawngebob::Constants::NGINX_DEFAULTS, Spawngebob::Constants::NGINX_DIRS, Spawngebob::Constants::NGINX_HTTP_LOCATION_TEMPLATE, Spawngebob::Constants::NGINX_PATH, Spawngebob::Constants::NGINX_PID, Spawngebob::Constants::NGINX_SERVER_TEMPLATE, Spawngebob::Constants::NGINX_SSL_LOCATION_TEMPLATE, Spawngebob::Constants::NGINX_SSL_SERVER_TEMPLATE, Spawngebob::Constants::NGINX_UPSTREAM_TEMPLATE, Spawngebob::Constants::SCRIPT_DIR
Instance Method Summary collapse
- #compile! ⇒ Object
-
#initialize ⇒ Nginx
constructor
A new instance of Nginx.
- #prepare ⇒ Object
Constructor Details
#initialize ⇒ Nginx
Returns a new instance of Nginx.
6 7 8 9 |
# File 'lib/spawngebob/compilers/nginx.rb', line 6 def initialize @config_file_path = File.join(BASE_CONFIG_PATH, CONFIG_FILE) @container = '' end |
Instance Method Details
#compile! ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/spawngebob/compilers/nginx.rb', line 22 def compile! location_string = '' if @config['nginx'].include? 'listen' NGINX_DEFAULTS.update(@config['nginx']) end if @config['nginx'].include? 'port' NGINX_DEFAULTS.update(@config['nginx']) end if @config['nginx']['ssl'] location_template = NGINX_SSL_LOCATION_TEMPLATE.chomp! else location_template = NGINX_HTTP_LOCATION_TEMPLATE.chomp! end # apps @config['applications'].each do |k, v| s = NGINX_UPSTREAM_TEMPLATE.gsub('%app%', k) @container.concat(s.gsub('%port%', "#{v["port"]}")) end # hosts @config['hosts'].each do |k, v| v['host'] = [v['host']] unless v['host'].is_a? Array v['host'].each do |h| # routes location_string = '' v['routes'].each do |r| s = location_template.gsub('%route%', r.first) s = s.gsub('%proxy%', r.last) location_string.concat(s + "\n") end if @config['nginx']['ssl_rewrite'] s = NGINX_SSL_SERVER_TEMPLATE.gsub('%host%', h) s = s.gsub('%cert_path%', @config['nginx']['cert_path']) s = s.gsub('%key_path%', @config['nginx']['key_path']) else s = NGINX_SERVER_TEMPLATE.gsub('%host%', h) end s = s.gsub('%ip%', NGINX_DEFAULTS['listen']) s = s.gsub('%port%', NGINX_DEFAULTS['port'].to_s) s = s.gsub('%location%', location_string) @container.concat(s) end end File.open(File.join(NGINX_CONFD_PATH, 'caresharing.conf'), 'w') do |f| Utils.say_with_time "Generating new config file..." do f.write(@container) end end end |
#prepare ⇒ Object
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/spawngebob/compilers/nginx.rb', line 11 def prepare @config = ERB.new(File.read(@config_file_path)).result(binding) @config = YAML.load(@config) if !@config.is_a? Hash Utils.error "Garbage #{CONFIG_FILE}! Fix it!" else compile! end end |