Class: Provisioner
- Inherits:
-
Object
- Object
- Provisioner
- Defined in:
- lib/provisioner.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#config_to_provision ⇒ Object
readonly
Returns the value of attribute config_to_provision.
-
#hosts ⇒ Object
readonly
Returns the value of attribute hosts.
Instance Method Summary collapse
- #create_host(host_name, host_config) ⇒ Object
- #formatted_log_to_stderr(prefix, string, timestamp = true) ⇒ Object
- #formatted_log_to_stdout(prefix, string, timestamp = true) ⇒ Object
- #get_host_ip(host_name) ⇒ Object
-
#initialize(config) ⇒ Provisioner
constructor
A new instance of Provisioner.
- #print_provisioning_plan(host_name) ⇒ Object
- #provision_hosts ⇒ Object
- #run_os_command(host_name, command, stream_stdout = true, stream_stderr = true) ⇒ Object
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
#config ⇒ Object (readonly)
Returns the value of attribute config.
6 7 8 |
# File 'lib/provisioner.rb', line 6 def config @config end |
#config_to_provision ⇒ Object (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 |
#hosts ⇒ Object (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, =true) time = Time.new time_string = time.strftime("%H:%M:%S") STDERR.puts "#{time_string if } #{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, =true) time = Time.new time_string = time.strftime("%H:%M:%S") puts "#{time_string if } #{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 |
#print_provisioning_plan(host_name) ⇒ Object
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_hosts ⇒ Object
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 |