class Configurator::Config

Attributes

config_tree[R]
driver[R]
loaded_configuration[R]

Public Class Methods

new(config_file_path) click to toggle source
# File lib/configurator.rb, line 11
def initialize(config_file_path)
  abort("Docker Machine configuration file not found: #{config_file_path}") unless File.exist?(config_file_path)
  abort("Can't read Docker Machine configuration file: #{config_file_path}") unless File.readable?(config_file_path)

  template = ERB.new File.new(config_file_path).read
  @loaded_configuration = YAML.load template.result(binding)

  # Checking 'options' section from Docker Machine config file
  @driver = get_option('driver')
  abort(build_wrong_config_format_error('options => driver parameter not found')) unless @driver
  abort(build_invalid_config_error("unknown driver 'aws'")) unless @@known_drivers.include?(@driver)
  abort(build_invalid_config_error("driver set as 'amazonec2', but 'aws-credentials-profile' parameter not set")) \
    unless get_option('aws-credentials-profile')

  # Loading 'hosts' section from Docker Machine config file
  hosts = @loaded_configuration['hosts']
  abort(build_invalid_config_error('hosts are not defined')) unless hosts

  hosts.each do |host|
    host_name = host.first.to_s
    host_config = get_host_config(host_name)
    host_type = host_config['host-type']

    # Checking 'host' section from Docker Machine config file
    abort(build_invalid_config_error("host-type is not defined for host '#{host_name}'")) unless host_type
    abort(build_invalid_config_error("unknown host-type '#{host_type}' for host '#{host_name}'")) \
      unless @@known_host_types.include?(host_type)
    abort(build_invalid_config_error("hosts-amount not defined for host '#{host_name}'")) \
      unless host_config['hosts-amount']

    is_hosts_amount_int = Integer(host_config['hosts-amount']) rescue false
    abort(build_invalid_config_error("hosts-amount must be integer. Host: '#{host_name}'")) \
      unless is_hosts_amount_int

    engine_opts = host_config['engine-opts']
    abort(build_invalid_config_error("engine-opts must be an array in yml notation. Host: '#{host_name}'")) \
      unless engine_opts.nil? || engine_opts.kind_of?(Array)

    commands_to_execute = host_config['commands-to-execute']
    abort(build_invalid_config_error("commands-to-execute must be an array in yml notation. Host: '#{host_name}'")) \
      unless commands_to_execute.nil? || commands_to_execute.kind_of?(Array)

    engine_install_url = host_config['engine-install-url']
    abort(build_invalid_config_error("engine-install-url must be a string. Host: '#{host_name}'")) \
      unless engine_install_url.nil? || engine_install_url.kind_of?(String)

    # Checking 'host' section from Docker Machine config file for 'swarm-node' host type
    if (host_type == 'swarm-node')
      abort(build_invalid_config_error("host-type is swarm-node, but swarm-discovery is not set. Host: '#{host_name}'")) \
      unless host_config['swarm-discovery']
      abort(build_invalid_config_error("swarm-discovery must be a string. Host: '#{host_name}'")) \
      unless host_config['swarm-discovery'].kind_of?(String)
    end

    # Checking 'host' section from Docker Machine config file for 'amazonec2' driver
    validate_amazonec2_options(host_config, host_name)

    @config_tree ||= {}
    hosts_amount = host_config['hosts-amount']
    if hosts_amount > 1
      for i in 1..hosts_amount
        create_host_in_config_tree(host_name+"-#{i}", host_config, host_type, @driver)
      end
    else
      create_host_in_config_tree(host_name, host_config, host_type, @driver)
    end
  end
end

Public Instance Methods

validate_amazonec2_options(host_config, host_name) click to toggle source
# File lib/configurator.rb, line 80
def validate_amazonec2_options(host_config, host_name)
  abort(build_invalid_config_error("amazonec2-region not defined for host '#{host_name}'")) unless host_config['amazonec2-region']

  amazonec2_region = host_config['amazonec2-region']
  abort(build_invalid_config_error("amazonec2-region must be a string. Host: '#{host_name}'")) \
      unless amazonec2_region.kind_of?(String)

  abort(build_invalid_config_error("amazonec2-zone not defined for host '#{host_name}'")) unless host_config['amazonec2-zone']

  amazonec2_zone = host_config['amazonec2-zone']
  abort(build_invalid_config_error("amazonec2-zone must be a string. Host: '#{host_name}'")) \
      unless amazonec2_zone.kind_of?(String)

  abort(build_invalid_config_error("amazonec2-vpc-id not defined for host '#{host_name}'")) unless host_config['amazonec2-vpc-id']

  amazonec2_vpc_id = host_config['amazonec2-vpc-id']
  abort(build_invalid_config_error("amazonec2-vpc-id must be a string. Host: '#{host_name}'")) \
      unless amazonec2_vpc_id.kind_of?(String)

  abort(build_invalid_config_error("amazonec2-subnet-id not defined for host '#{host_name}'")) unless host_config['amazonec2-subnet-id']

  amazonec2_subnet_id = host_config['amazonec2-subnet-id']
  abort(build_invalid_config_error("amazonec2-subnet-id must be a string. Host: '#{host_name}'")) \
      unless amazonec2_subnet_id.kind_of?(String)

  abort(build_invalid_config_error("amazonec2-security-group not defined for host '#{host_name}'")) unless host_config['amazonec2-security-group']

  amazonec2_security_group = host_config['amazonec2-security-group']
  abort(build_invalid_config_error("amazonec2-security-group must be a string. Host: '#{host_name}'")) \
      unless amazonec2_security_group.kind_of?(String)

  abort(build_invalid_config_error("amazonec2-instance-type not defined for host '#{host_name}'")) unless host_config['amazonec2-instance-type']

  amazonec2_instance_type = host_config['amazonec2-instance-type']
  abort(build_invalid_config_error("amazonec2-instance-type must be a string. Host: '#{host_name}'")) \
      unless amazonec2_instance_type.kind_of?(String)

  amazonec2_ami = host_config['amazonec2-ami']
  abort(build_invalid_config_error("amazonec2-ami must be a string. Host: '#{host_name}'")) \
      unless amazonec2_ami.nil? || amazonec2_ami.kind_of?(String)
end

Private Instance Methods

build_invalid_config_error(reason) click to toggle source
# File lib/configurator.rb, line 132
def build_invalid_config_error(reason)
  "Invalid Docker Machine config: #{reason}."
end
build_wrong_config_format_error(reason) click to toggle source
# File lib/configurator.rb, line 128
def build_wrong_config_format_error(reason)
  "Wrong Docker Machine config format: #{reason}."
end
create_host_in_config_tree(host_name, host_config, host_type, driver) click to toggle source
# File lib/configurator.rb, line 141
def create_host_in_config_tree(host_name, host_config, host_type, driver)
  case driver
    when 'amazonec2'
      @config_tree[host_name] = Hosts::AmazonHost.new(host_config, host_type)
  end
end
get_host_config(host_node_name) click to toggle source
# File lib/configurator.rb, line 136
def get_host_config(host_node_name)
  hosts_hash = @loaded_configuration['hosts']
  hosts_hash ? hosts_hash[host_node_name] : nil
end
get_option(key) click to toggle source
# File lib/configurator.rb, line 123
def get_option(key)
  options_hash = @loaded_configuration['options']
  options_hash ? options_hash[key] : nil
end