Class: Procodile::Config

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

Constant Summary collapse

COLORS =
[35, 31, 36, 32, 33, 34]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root, environment = nil, procfile = nil) ⇒ Config

Returns a new instance of Config.



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/procodile/config.rb', line 14

def initialize(root, environment = nil, procfile = nil)
  @root = root
  @environment = environment || 'production'
  @procfile_path = procfile
  unless File.file?(procfile_path)
    raise Error, "Procfile not found at #{procfile_path}"
  end
  FileUtils.mkdir_p(pid_root)

  @processes = process_list.each_with_index.each_with_object({}) do |((name, command), index), hash|
    hash[name] = create_process(name, command, COLORS[index.divmod(COLORS.size)[1]])
  end
end

Instance Attribute Details

#environmentObject (readonly)

Returns the value of attribute environment.



12
13
14
# File 'lib/procodile/config.rb', line 12

def environment
  @environment
end

#rootObject (readonly)

Returns the value of attribute root.



11
12
13
# File 'lib/procodile/config.rb', line 11

def root
  @root
end

Instance Method Details

#app_nameObject



62
63
64
# File 'lib/procodile/config.rb', line 62

def app_name
  @app_name ||= fetch(local_options['app_name']) || fetch(options['app_name']) || 'Procodile'
end

#console_commandObject



66
67
68
# File 'lib/procodile/config.rb', line 66

def console_command
  fetch(local_options['console_command']) || fetch(options['console_command'])
end

#environment_variablesObject



98
99
100
# File 'lib/procodile/config.rb', line 98

def environment_variables
  fetch_hash_values(options['env'] || {}).merge(fetch_hash_values(local_options['env'] || {}))
end

#local_optionsObject



86
87
88
# File 'lib/procodile/config.rb', line 86

def local_options
  @local_options ||= load_local_options_from_file
end

#local_options_pathObject



141
142
143
# File 'lib/procodile/config.rb', line 141

def local_options_path
  procfile_path + ".local"
end

#local_process_optionsObject



90
91
92
# File 'lib/procodile/config.rb', line 90

def local_process_options
  @local_process_options ||= local_options['processes'] || {}
end

#log_pathObject



110
111
112
113
114
115
116
117
118
119
# File 'lib/procodile/config.rb', line 110

def log_path
  log_path = fetch(local_options['log_path']) || fetch(options['log_path'])
  if log_path
    File.expand_path(log_path, @root)
  elsif log_path.nil? && self.log_root
    File.join(self.log_root, 'procodile.log')
  else
    File.expand_path("procodile.log", @root)
  end
end

#log_rootObject



121
122
123
124
125
126
127
# File 'lib/procodile/config.rb', line 121

def log_root
  if log_root = (fetch(local_options['log_root']) || fetch(options['log_root']))
    File.expand_path(log_root, @root)
  else
    nil
  end
end

#optionsObject



78
79
80
# File 'lib/procodile/config.rb', line 78

def options
  @options ||= load_options_from_file
end

#options_for_process(name) ⇒ Object



94
95
96
# File 'lib/procodile/config.rb', line 94

def options_for_process(name)
  (process_options[name] || {}).merge(local_process_options[name] || {})
end

#options_pathObject



137
138
139
# File 'lib/procodile/config.rb', line 137

def options_path
  procfile_path + ".options"
end

#pid_rootObject



102
103
104
# File 'lib/procodile/config.rb', line 102

def pid_root
  @pid_root ||= File.expand_path(fetch(local_options['pid_root']) || fetch(options['pid_root']) || 'pids', @root)
end

#process_listObject



74
75
76
# File 'lib/procodile/config.rb', line 74

def process_list
  @process_list ||= load_process_list_from_file
end

#process_optionsObject



82
83
84
# File 'lib/procodile/config.rb', line 82

def process_options
  @process_options ||= options['processes'] || {}
end

#processesObject



70
71
72
# File 'lib/procodile/config.rb', line 70

def processes
  @processes ||= {}
end

#procfile_pathObject



133
134
135
# File 'lib/procodile/config.rb', line 133

def procfile_path
  @procfile_path || File.join(@root, 'Procfile')
end

#reloadObject



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

def reload
  @process_list = nil
  @options = nil
  @process_options = nil
  @local_options = nil
  @local_process_options = nil

  if @processes
    process_list.each do |name, command|
      if process = @processes[name]
        process.removed = false
        # This command is already in our list. Add it.
        if process.command != command
          process.command = command
          Procodile.log nil, 'system', "#{name} command has changed. Updated."
        end
        process.options = options_for_process(name)
      else
        Procodile.log nil, 'system', "#{name} has been added to the Procfile. Adding it."
        @processes[name] = create_process(name, command, COLORS[@processes.size.divmod(COLORS.size)[1]])
      end
    end

    removed_processes = @processes.keys - process_list.keys
    removed_processes.each do |process_name|
      if p = @processes[process_name]
        p.removed = true
        @processes.delete(process_name)
        Procodile.log nil, 'system', "#{process_name} has been removed to the Procfile. It will be removed when it is stopped."
      end
    end
  end
end

#sock_pathObject



129
130
131
# File 'lib/procodile/config.rb', line 129

def sock_path
  File.join(pid_root, 'procodile.sock')
end

#supervisor_pid_pathObject



106
107
108
# File 'lib/procodile/config.rb', line 106

def supervisor_pid_path
  File.join(pid_root, 'procodile.pid')
end