Class: ChatopsDeployer::NginxConfig

Inherits:
Object
  • Object
show all
Includes:
Logger
Defined in:
lib/chatops_deployer/nginx_config.rb

Defined Under Namespace

Classes: Error

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logger

included, #logger, #logger=

Constructor Details

#initialize(project) ⇒ NginxConfig

Returns a new instance of NginxConfig.



15
16
17
18
19
20
21
# File 'lib/chatops_deployer/nginx_config.rb', line 15

def initialize(project)
  @sha1 = project.sha1
  @project = project
  check_sites_enabled_dir_exists!
  @config_path = File.join NGINX_SITES_ENABLED_DIR, @sha1
  @urls = {}
end

Instance Attribute Details

#urlsObject (readonly)

Returns the value of attribute urls.



11
12
13
# File 'lib/chatops_deployer/nginx_config.rb', line 11

def urls
  @urls
end

Instance Method Details

#add_urls(service_urls) ⇒ Object

service_urls is an array in the format: {“web” => [[“10.1.1.2”, “3000”],[“10.1.1.2”, “4000”]] }



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/chatops_deployer/nginx_config.rb', line 29

def add_urls(service_urls)
  return if service_urls.nil?
  remove if exists?

  service_urls.each do |service, internal_urls|
    Array(internal_urls).each do |internal_url|
      expose(service, internal_url)
    end
  end
  logger.info "Reloading nginx"
  nginx_reload = Command.run(command: 'service nginx reload', logger: logger)
  unless nginx_reload.success?
    raise_error("Cannot reload nginx after adding config. Check #{NGINX_SITES_ENABLED_DIR}/#{@sha1} for errors")
  end
end

#exists?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/chatops_deployer/nginx_config.rb', line 23

def exists?
  File.exists? @config_path
end

#prepare_urlsObject



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/chatops_deployer/nginx_config.rb', line 61

def prepare_urls
  @project.env['urls'] = {}
  service_ports_from_config.each do |service, ports|
    @urls[service] = {}
    @project.env['urls'][service] = {}
    ports.each do |port|
      @urls[service][port.to_s] = generate_haikunated_url
      @project.env['urls'][service][port.to_s] = "http://#{@urls[service][port.to_s]}"
    end
  end
end

#readable_urlsObject



51
52
53
54
55
56
57
58
59
# File 'lib/chatops_deployer/nginx_config.rb', line 51

def readable_urls
  urls = {}
  @urls.each do |service, port_exposed_urls|
    urls[service] = port_exposed_urls.collect do |port, exposed_url|
      "http://#{exposed_url}"
    end
  end
  urls.to_json
end

#removeObject



45
46
47
48
49
# File 'lib/chatops_deployer/nginx_config.rb', line 45

def remove
  logger.info "Removing nginx config"
  FileUtils.rm @config_path
  system('service nginx reload')
end