Class: Ringleader::Config
- Inherits:
-
Object
- Object
- Ringleader::Config
- 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
-
#apps ⇒ Object
readonly
Returns the value of attribute apps.
Instance Method Summary collapse
-
#assign_colors(configs, boring) ⇒ Object
Internal: assign a color to each application configuration.
-
#convert_and_validate(configs, boring) ⇒ Object
Internal: convert a YML hash to an array of name/OpenStruct pairs.
-
#initialize(file, boring = false) ⇒ Config
constructor
Public: Load the configs from a file.
-
#validate(name, options) ⇒ Object
Internal: validate that the options have all of the required keys.
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
#apps ⇒ Object (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, | ["name"] = name ["host"] ||= DEFAULT_HOST ["idle_timeout"] ||= DEFAULT_IDLE_TIMEOUT ["startup_timeout"] ||= DEFAULT_STARTUP_TIMEOUT ["kill_with"] ||= "INT" ["env"] ||= {} ["dir"] = File. ["dir"] unless File.directory?(["dir"]) || ["disabled"] warn "#{["dir"]} does not exist!" end if command = .delete("rvm") ["command"] = "source ~/.rvm/scripts/rvm && rvm in #{["dir"]} do #{command}" elsif command = .delete("chruby") ["command"] = "source /usr/local/share/chruby/chruby.sh ; source /usr/local/share/chruby/auto.sh ; cd #{["dir"]} ; #{command}" elsif command = .delete("rbenv") ["command"] = "rbenv exec #{command}" ["env"]["RBENV_VERSION"] = nil ["env"]["RBENV_DIR"] = nil ["env"]["GEM_HOME"] = nil end validate name, [name, OpenStruct.new()] 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, ) REQUIRED_KEYS.each do |key| unless .has_key?(key) raise ArgumentError, "#{key} missing in #{name} config" end end end |