Class: Eye::Config

Inherits:
Object show all
Defined in:
lib/eye/config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(settings = {}, applications = {}) ⇒ Config

Returns a new instance of Config.



5
6
7
8
# File 'lib/eye/config.rb', line 5

def initialize(settings = {}, applications = {})
  @settings = settings
  @applications = applications
end

Instance Attribute Details

#applicationsObject (readonly)

Returns the value of attribute applications.



3
4
5
# File 'lib/eye/config.rb', line 3

def applications
  @applications
end

#settingsObject (readonly)

Returns the value of attribute settings.



3
4
5
# File 'lib/eye/config.rb', line 3

def settings
  @settings
end

Instance Method Details

#application_namesObject



105
106
107
# File 'lib/eye/config.rb', line 105

def application_names
  applications.keys
end

#delete_app(name) ⇒ Object



109
110
111
# File 'lib/eye/config.rb', line 109

def delete_app(name)
  applications.delete(name)
end

#delete_group(name) ⇒ Object



113
114
115
116
117
# File 'lib/eye/config.rb', line 113

def delete_group(name)
  applications.each do |_, app_cfg|
    (app_cfg[:groups] || {}).delete(name)
  end
end

#delete_process(name) ⇒ Object



119
120
121
122
123
124
125
# File 'lib/eye/config.rb', line 119

def delete_process(name)
  applications.each do |_, app_cfg|
    (app_cfg[:groups] || {}).each do |_, gr_cfg|
      (gr_cfg[:processes] || {}).delete(name)
    end
  end
end

#each_process(&block) ⇒ Object



97
98
99
100
101
102
103
# File 'lib/eye/config.rb', line 97

def each_process(&block)
  applications.each do |_, app_cfg|
    (app_cfg[:groups] || {}).each do |_, gr_cfg|
      (gr_cfg[:processes] || {}).each_value(&block)
    end
  end
end

#merge(other_config) ⇒ Object



10
11
12
13
14
15
# File 'lib/eye/config.rb', line 10

def merge(other_config)
  new_settings = {}
  Eye::Utils.deep_merge!(new_settings, @settings)
  Eye::Utils.deep_merge!(new_settings, other_config.settings)
  Eye::Config.new(new_settings, @applications.merge(other_config.applications))
end

#merge!(other_config) ⇒ Object



17
18
19
20
# File 'lib/eye/config.rb', line 17

def merge!(other_config)
  Eye::Utils.deep_merge!(@settings, other_config.settings)
  @applications.merge!(other_config.applications)
end

#to_hObject



22
23
24
25
26
27
28
29
30
31
# File 'lib/eye/config.rb', line 22

def to_h
  h = {}
  h[:settings] = @settings
  if Eye.respond_to?(:parsed_default_app)
    d = Eye.parsed_default_app
    h[:defaults] = d ? d.config : {}
  end
  h[:applications] = @applications
  h
end

#transform!Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/eye/config.rb', line 76

def transform!
  # transform syslog option
  each_process do |process|
    out = process[:stdout] && process[:stdout].start_with?(':syslog')
    err = process[:stderr] && process[:stderr].start_with?(':syslog')
    next unless err || out

    redir = err ? '2>&1' : ''
    process[:stdout] = nil if out
    process[:stderr] = nil if err

    escaped_start_command = process[:start_command].to_s.gsub(%{"}, %{\\"})

    names = [process[:application], process[:group] == '__default__' ? nil : process[:group], process[:name]].compact
    logger = "logger -t \"#{names.join(':')}\""

    process[:start_command] = %{sh -c "#{escaped_start_command} #{redir} | #{logger}"}
    process[:use_leaf_child] = true if process[:daemonize]
  end
end

#validate!(validate_apps = []) ⇒ Object

raise an error if config wrong



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
# File 'lib/eye/config.rb', line 34

def validate!(validate_apps = [])
  # Check blank pid_files
  no_pid_file = []
  each_process { |c| no_pid_file << c if c[:pid_file].blank? }
  if no_pid_file.any?
    raise Eye::Dsl::Error, "blank pid_file for: #{no_pid_file.map { |c| c[:name] } * ', '}"
  end

  # Check duplicates of the full pid_file

  dupl_pids = Hash.new(0)
  each_process do |o|
    ex_pid_file = Eye::System.normalized_file(o[:pid_file], o[:working_dir])
    dupl_pids[ex_pid_file] += 1
  end
  dupl_pids = dupl_pids.select { |_, v| v > 1 }

  if dupl_pids.any?
    raise Eye::Dsl::Error, "duplicate pid_files: #{dupl_pids.inspect}"
  end

  # Check duplicates of the full_name
  dupl_names = Hash.new(0)
  each_process do |o|
    full_name = "#{o[:application]}:#{o[:group]}:#{o[:name]}"
    dupl_names[full_name] += 1
  end
  dupl_names = dupl_names.select { |_, v| v > 1 }

  if dupl_names.any?
    raise Eye::Dsl::Error, "duplicate names: #{dupl_names.inspect}"
  end

  # validate processes with their own validate
  each_process do |process_cfg|
    Eye::Process.validate process_cfg, validate_apps.include?(process_cfg[:application])
  end

  # just to be sure ENV was not removed
  ENV[''] rescue raise Eye::Dsl::Error, "ENV is not a hash '#{ENV.inspect}'"
end