Class: Provisioner

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Provisioner

Returns a new instance of Provisioner.



7
8
9
10
11
# File 'lib/provisioner.rb', line 7

def initialize(config)
  @config = config
  @config_to_provision = @config.config_tree
  @hosts = {}
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



6
7
8
# File 'lib/provisioner.rb', line 6

def config
  @config
end

#config_to_provisionObject (readonly)

Returns the value of attribute config_to_provision.



6
7
8
# File 'lib/provisioner.rb', line 6

def config_to_provision
  @config_to_provision
end

#hostsObject (readonly)

Returns the value of attribute hosts.



6
7
8
# File 'lib/provisioner.rb', line 6

def hosts
  @hosts
end

Instance Method Details

#create_host(host_name, host_config) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/provisioner.rb', line 54

def create_host(host_name, host_config)
  host_creation_string = host_config.get_creation_string
  thread = Thread.new do
    process = run_os_command(host_name, "docker-machine create #{host_creation_string} #{host_name}")
    if process[:process].exitstatus != 0
      abort("#{host_name} creation failed. Check log for details.")
    end
    @hosts[host_name] = {:ip => get_host_ip(host_name)}
    commands = @config.loaded_configuration['hosts'][host_name]['commands-to-execute']
    if commands
      commands.each do |command|
        process = run_os_command(host_name, "docker-machine ssh #{host_name} #{command}")
        if process[:process].exitstatus != 0
          abort("#{host_name} provisioning failed. Check log for details.")
        end
      end
    end
  end
  thread
end

#formatted_log_to_stderr(prefix, string, timestamp = true) ⇒ Object



81
82
83
84
85
# File 'lib/provisioner.rb', line 81

def formatted_log_to_stderr(prefix, string, timestamp=true)
  time = Time.new
  time_string = time.strftime("%H:%M:%S")
  STDERR.puts "#{time_string if timestamp} #{prefix} #{string}"
end

#formatted_log_to_stdout(prefix, string, timestamp = true) ⇒ Object



75
76
77
78
79
# File 'lib/provisioner.rb', line 75

def formatted_log_to_stdout(prefix, string, timestamp=true)
  time = Time.new
  time_string = time.strftime("%H:%M:%S")
  puts "#{time_string if timestamp} #{prefix} #{string}"
end

#get_host_ip(host_name) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/provisioner.rb', line 87

def get_host_ip(host_name)
  while true
    process = run_os_command host_name, "docker-machine ip #{host_name}"
    if process[:process].exitstatus == 0
      ip = process[:stdout][/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/,0]
      if !ip.nil?
        formatted_log_to_stdout("OUT [#{host_name}]", "Got IP: #{ip}")
        return ip
      end
    else
      formatted_log_to_stdout("OUT [#{host_name}]", 'Waiting 10 seconds for IP.')
      sleep 10
    end
  end
end


103
104
105
106
# File 'lib/provisioner.rb', line 103

def print_provisioning_plan(host_name)
  host_config = @config_to_provision[host_name]
  formatted_log_to_stdout "PLAN [#{host_name}]", "docker machine create #{host_config.get_creation_string} #{host_name}"
end

#provision_hostsObject



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
# File 'lib/provisioner.rb', line 13

def provision_hosts
  threads = []
  @config_to_provision.each do |host|
    host_name = host[0]
    host_config = host[1]

    host_node_name = host_name.sub /\-\d*$/, ''
    host_config_node = @config.loaded_configuration['hosts'][host_node_name]
    placeholders = host_config_node['placeholders']
    if placeholders
      placeholders.each do |key, value|
        if key=='host_ip' && @hosts[value] && @hosts[value][:ip]
          replacement = @hosts[value][:ip]
        else
          abort("Cant find IP for host '#{value}' to use in placeholder")
        end
        host_config.creation_string.each do |parameter|
          if parameter.respond_to?(:each)
            parameter.each do |subparam|
              if subparam.respond_to?(:sub!)
                subparam.sub! "$#{key}$", replacement
              end
            end
          else
            parameter.sub! "$#{key}$", replacement
          end
        end
      end
    end
    print_provisioning_plan host_name
    synchron = @config.loaded_configuration['hosts'][host_node_name]['synchronous']
    if synchron == true
      create_host(host_name, host_config).join()
      next
    end
    threads.push(create_host host_name, host_config)
  end
  threads.each {|thread| thread.join}
  puts @hosts.to_s
end

#run_os_command(host_name, command, stream_stdout = true, stream_stderr = true) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/provisioner.rb', line 108

def run_os_command(host_name, command, stream_stdout=true, stream_stderr=true)
  process_hash = {}
  process = Open4::popen4('sh') do |pid, stdin, stdout, stderr|
    stdin.puts command
    stdin.close
    if stream_stdout
      stdout.each do |line|
        formatted_log_to_stdout("OUT [#{host_name}]", line)
        process_hash[:stdout] ||= ''
        process_hash[:stdout] += line
      end
    end
    if stream_stderr
      stderr.each do |line|
        formatted_log_to_stderr("ERROR [#{host_name}]", line)
        process_hash[:stderr] ||= ''
        process_hash[:stderr] += line
      end
    end
    process_hash[:pid] = pid
  end
  process_hash[:process] = process
  process_hash
end