class Percheron::Config

Constants

DEFAULT_CONFIG_FILE

Public Class Methods

docker() click to toggle source
# File lib/percheron/config.rb, line 58
def self.docker
  instance.docker
end
file_base_path() click to toggle source
# File lib/percheron/config.rb, line 34
def self.file_base_path
  instance.file_base_path
end
load!(file) click to toggle source

rubocop:enable Style/ClassVars

# File lib/percheron/config.rb, line 14
def self.load!(file)
  instance.load!(file)
end
secrets() click to toggle source
# File lib/percheron/config.rb, line 42
def self.secrets
  instance.secrets
end
stacks() click to toggle source

rubocop:enable Style/ClassVars

# File lib/percheron/config.rb, line 26
def self.stacks
  instance.stacks
end
userdata() click to toggle source
# File lib/percheron/config.rb, line 50
def self.userdata
  instance.userdata
end

Public Instance Methods

docker() click to toggle source
# File lib/percheron/config.rb, line 62
def docker
  contents.docker
end
file_base_path() click to toggle source
# File lib/percheron/config.rb, line 38
def file_base_path
  file.dirname
end
load!(file) click to toggle source

rubocop:disable Style/ClassVars

# File lib/percheron/config.rb, line 19
def load!(file)
  @@file = Pathname.new(file).expand_path
  invalidate_memoised_values!
  self
end
secrets() click to toggle source
# File lib/percheron/config.rb, line 46
def secrets
  secrets_file ? Hashie::Mash.new(YAML.load_file(secrets_file)) : {}
end
stacks() click to toggle source
# File lib/percheron/config.rb, line 30
def stacks
  @stacks ||= process_stacks!
end
userdata() click to toggle source
# File lib/percheron/config.rb, line 54
def userdata
  contents.userdata || {}
end

Private Instance Methods

contents() click to toggle source
# File lib/percheron/config.rb, line 160
def contents
  @contents ||= begin
    parsed_contents.tap do |c|
      c.docker ||= {}
      c.docker.host ||= env_docker_host
      c.docker.cert_path ||= env_cert_path
      c.docker.ssl_verify_peer ||= env_ssl_verify_peer
    end
  end
end
env_cert_path() click to toggle source
# File lib/percheron/config.rb, line 175
def env_cert_path
  ENV['DOCKER_CERT_PATH'] ? File.expand_path(ENV['DOCKER_CERT_PATH']) : nil
end
env_docker_host() click to toggle source
# File lib/percheron/config.rb, line 171
def env_docker_host
  ENV['DOCKER_HOST'] || fail("Docker host not defined in '#{file}' or ENV['DOCKER_HOST']")
end
env_ssl_verify_peer() click to toggle source
# File lib/percheron/config.rb, line 179
def env_ssl_verify_peer
  (ENV['DOCKER_TLS_VERIFY'] == 1) || true
end
eval_unit_config(unit_config) click to toggle source
# File lib/percheron/config.rb, line 139
def eval_unit_config(unit_config)
  template = Liquid::Template.parse(unit_config.to_h.to_yaml.to_s)
  YAML.load(template.render(unit_config))
end
expand_unit_config(unit_config, new_unit_names) click to toggle source

FIXME

# File lib/percheron/config.rb, line 129
def expand_unit_config(unit_config, new_unit_names)
  new_unit_names.each_with_object({}) do |new_name, all|
    temp_unit_config = unit_config.dup
    temp_unit_config.delete(:instances)
    temp_unit_config.pseudo_name = unit_config.name
    temp_unit_config.name = new_name
    all[new_name] = eval_unit_config(temp_unit_config)
  end
end
file() click to toggle source
# File lib/percheron/config.rb, line 68
def file
  @@file
end
invalidate_memoised_values!() click to toggle source
# File lib/percheron/config.rb, line 77
def invalidate_memoised_values!
  @stacks = @yaml_contents = @raw_contents = @contents = nil
end
merge_or_replace(all, config, scanned) click to toggle source
# File lib/percheron/config.rb, line 94
def merge_or_replace(all, config, scanned)
  if scanned[config.name]
    merge_scanned(all, config, scanned)
  else
    replace_scanned(all, config, scanned)
  end
end
merge_scanned(all, config, scanned) click to toggle source
# File lib/percheron/config.rb, line 102
def merge_scanned(all, config, scanned)
  all.merge!(expand_unit_config(config, scanned[config.name]))
end
parsed_contents() click to toggle source
# File lib/percheron/config.rb, line 156
def parsed_contents
  Hashie::Mash.new(YAML.load(templated_contents))
end
process_stacks!() click to toggle source

FIXME: bugs here :(

# File lib/percheron/config.rb, line 82
def process_stacks!
  stacks_by_name = contents.stacks.to_hash_by_key(:name)
  scanned = scan_unit_configs(stacks_by_name)
  stacks_by_name.each do |_, stack|
    stack_units = stack.fetch(:units, []).each_with_object({}) do |unit_config, all|
      merge_or_replace(all, unit_config, scanned)
    end
    $logger.warn "No units defined for '%s' stack" % stack.name if stack_units.empty?
    stack.units = stack_units
  end
end
raw_contents() click to toggle source
# File lib/percheron/config.rb, line 144
def raw_contents
  @raw_contents ||= file.read
end
replace_scanned(all, config, scanned) click to toggle source
# File lib/percheron/config.rb, line 106
def replace_scanned(all, config, scanned)
  match = config.fetch(:needed_unit_names, [])
  unless (match & scanned.keys).empty?
    config.needed_unit_names = match.map { |v| scanned[v] }.flatten
  end
  all[config.name] = config
end
scan_unit_configs(stacks_by_name) click to toggle source

FIXME

# File lib/percheron/config.rb, line 115
def scan_unit_configs(stacks_by_name)
  all = {}
  stacks_by_name.each do |_, stack|
    stack.fetch(:units, []).each do |unit_config|
      next if unit_config.fetch(:instances, 1) == 1
      all[unit_config.name] = 1.upto(unit_config.instances).map do |number|
        "#{unit_config.name}#{number}"
      end
    end
  end
  all
end
secrets_file() click to toggle source
# File lib/percheron/config.rb, line 72
def secrets_file
  return unless yaml_contents.secrets_file
  File.expand_path(yaml_contents.secrets_file, file_base_path)
end
templated_contents() click to toggle source
# File lib/percheron/config.rb, line 152
def templated_contents
  Liquid::Template.parse(raw_contents).render('secrets' => secrets)
end
yaml_contents() click to toggle source
# File lib/percheron/config.rb, line 148
def yaml_contents
  @yaml_contents ||= Hashie::Mash.new(YAML.load(raw_contents))
end