Class: Ringleader::Config

Inherits:
Object
  • Object
show all
Includes:
Celluloid::Logger
Defined in:
lib/ringleader/config.rb

Constant Summary collapse

DEFAULT_IDLE_TIMEOUT =
1800
DEFAULT_STARTUP_TIMEOUT =
30
DEFAULT_HOST =
"127.0.0.1"
REQUIRED_KEYS =
%w(dir command app_port server_port)
TERMINAL_COLORS =
[:red, :green, :yellow, :blue, :magenta, :cyan]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, boring = false) ⇒ Config

Public: Load the configs from a file

file - the yml file to load the config from boring - use terminal colors instead of a rainbow for app colors



18
19
20
21
22
# File 'lib/ringleader/config.rb', line 18

def initialize(file, boring=false)
  config_data = YAML.load(File.read(file))
  configs = convert_and_validate config_data, boring
  @apps = Hash[*configs.flatten]
end

Instance Attribute Details

#appsObject (readonly)

Returns the value of attribute apps.



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

def apps
  @apps
end

Instance Method Details

#assign_colors(configs, boring) ⇒ Object

Internal: assign a color to each application configuration.

configs - the config data to modify boring - use boring standard terminal colors instead of a rainbow.



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/ringleader/config.rb', line 79

def assign_colors(configs, boring)
  sorted = configs.sort_by(&:first).map(&:last)
  if boring
    sorted.each.with_index do |config, i|
      config["color"] = TERMINAL_COLORS[ i % TERMINAL_COLORS.length ]
    end
  else
    offset = 360/configs.size
    sorted.each.with_index do |config, i|
      config["color"] = Color::HSL.new(offset * i, 100, 50).html
    end
  end
end

#convert_and_validate(configs, boring) ⇒ Object

Internal: convert a YML hash to an array of name/OpenStruct pairs

Does validation for each app config and raises an error if anything is wrong. Sets default values for missing options, and assigns colors to each app config.

configs - a hash of config data boring - whether or not to use a rainbow of colors for the apps

Returns [ [app_name, OpenStruct], … ]



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

def convert_and_validate(configs, boring)
  assign_colors configs, boring
  configs.map do |name, options|
    options["name"] = name
    options["host"] ||= DEFAULT_HOST
    options["idle_timeout"] ||= DEFAULT_IDLE_TIMEOUT
    options["startup_timeout"] ||= DEFAULT_STARTUP_TIMEOUT
    options["kill_with"] ||= "INT"
    options["env"] ||= {}

    options["dir"] = File.expand_path options["dir"]
    unless File.directory?(options["dir"]) || options["disabled"]
      warn "#{options["dir"]} does not exist!"
    end

    if command = options.delete("rvm")
      options["command"] = "source ~/.rvm/scripts/rvm && rvm in #{options["dir"]} do #{command}"
    elsif command = options.delete("chruby")
      options["command"] = "source /usr/local/share/chruby/chruby.sh ; source /usr/local/share/chruby/auto.sh ; cd #{options["dir"]} ; #{command}"
    elsif command = options.delete("rbenv")
      options["command"] = "rbenv exec #{command}"
      options["env"]["RBENV_VERSION"] = nil
      options["env"]["RBENV_DIR"] = nil
      options["env"]["GEM_HOME"] = nil
    end

    validate name, options

    [name, OpenStruct.new(options)]
  end
end

#validate(name, options) ⇒ Object

Internal: validate that the options have all of the required keys



67
68
69
70
71
72
73
# File 'lib/ringleader/config.rb', line 67

def validate(name, options)
  REQUIRED_KEYS.each do |key|
    unless options.has_key?(key)
      raise ArgumentError, "#{key} missing in #{name} config"
    end
  end
end