Class: Configurator::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/configurator.rb

Constant Summary collapse

@@known_drivers =
['amazonec2']
@@known_host_types =
%w(generic swarm-master swarm-node)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_file_path) ⇒ 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
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# 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

Instance Attribute Details

#config_treeObject (readonly)

Returns the value of attribute config_tree.



7
8
9
# File 'lib/configurator.rb', line 7

def config_tree
  @config_tree
end

#driverObject (readonly)

Returns the value of attribute driver.



7
8
9
# File 'lib/configurator.rb', line 7

def driver
  @driver
end

#loaded_configurationObject (readonly)

Returns the value of attribute loaded_configuration.



7
8
9
# File 'lib/configurator.rb', line 7

def loaded_configuration
  @loaded_configuration
end

Instance Method Details

#validate_amazonec2_options(host_config, host_name) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# 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