class Provisioner

Attributes

config[R]
config_to_provision[R]
hosts[R]

Public Class Methods

new(config) click to toggle source
# File lib/provisioner.rb, line 7
def initialize(config)
  @config = config
  @config_to_provision = @config.config_tree
  @hosts = {}
end

Public Instance Methods

create_host(host_name, host_config) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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
print_provisioning_plan(host_name) click to toggle source
provision_hosts() click to toggle source
# 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) click to toggle source
# 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