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 Method Summary collapse

Constructor Details

#initialize(root, procfile = nil) ⇒ Config

Returns a new instance of Config.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/procodile/config.rb', line 11

def initialize(root, procfile = nil)
  @root = root
  @procfile_path = procfile

  unless File.file?(procfile_path)
    raise Error, "Procfile not found at #{procfile_path}"
  end

  # We need to check to see if the local or options
  # configuration will override the root that we've been given.
  # If they do, we can throw away any reference to the one that the
  # configuration was initialized with and start using that immediately.
  if new_root = (local_options['root'] || options['root'])
    @root = new_root
  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

  @loaded_at = Time.now
end

Instance Method Details

#app_nameObject



84
85
86
# File 'lib/procodile/config.rb', line 84

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

#console_commandObject



88
89
90
# File 'lib/procodile/config.rb', line 88

def console_command
  local_options['console_command'] || options['console_command']
end

#environment_variablesObject



120
121
122
# File 'lib/procodile/config.rb', line 120

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

#loaded_atObject



76
77
78
# File 'lib/procodile/config.rb', line 76

def loaded_at
  @loaded_at
end

#local_optionsObject



108
109
110
# File 'lib/procodile/config.rb', line 108

def local_options
  @local_options ||= load_local_options_from_file
end

#local_options_pathObject



163
164
165
# File 'lib/procodile/config.rb', line 163

def local_options_path
  procfile_path + ".local"
end

#local_process_optionsObject



112
113
114
# File 'lib/procodile/config.rb', line 112

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

#log_pathObject



132
133
134
135
136
137
138
139
140
141
# File 'lib/procodile/config.rb', line 132

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

#log_rootObject



143
144
145
146
147
148
149
# File 'lib/procodile/config.rb', line 143

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

#optionsObject



100
101
102
# File 'lib/procodile/config.rb', line 100

def options
  @options ||= load_options_from_file
end

#options_for_process(name) ⇒ Object



116
117
118
# File 'lib/procodile/config.rb', line 116

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

#options_pathObject



159
160
161
# File 'lib/procodile/config.rb', line 159

def options_path
  procfile_path + ".options"
end

#pid_rootObject



124
125
126
# File 'lib/procodile/config.rb', line 124

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

#process_listObject



96
97
98
# File 'lib/procodile/config.rb', line 96

def process_list
  @process_list ||= load_process_list_from_file
end

#process_optionsObject



104
105
106
# File 'lib/procodile/config.rb', line 104

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

#processesObject



92
93
94
# File 'lib/procodile/config.rb', line 92

def processes
  @processes ||= {}
end

#procfile_pathObject



155
156
157
# File 'lib/procodile/config.rb', line 155

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

#reloadObject



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

def reload
  @process_list = nil
  @options = nil
  @process_options = nil
  @local_options = nil
  @local_process_options = nil
  @loaded_at = 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
  @loaded_at = Time.now
end

#rootObject



72
73
74
# File 'lib/procodile/config.rb', line 72

def root
  @root
end

#sock_pathObject



151
152
153
# File 'lib/procodile/config.rb', line 151

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

#supervisor_pid_pathObject



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

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

#userObject



80
81
82
# File 'lib/procodile/config.rb', line 80

def user
  local_options['user'] || options['user']
end