Class: Elevage::Provisioner

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

Overview

Provisioner class

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, component, instance, environment, options) ⇒ Provisioner

Set us up to build the specified instance of component



17
18
19
20
21
22
23
24
# File 'lib/elevage/provisioner.rb', line 17

def initialize(name, component, instance, environment, options)
  @name = name
  @component = component
  @instance = instance
  @environment = environment
  @options = options
  @vcenter = @environment.vcenter
end

Instance Attribute Details

#componentObject

Returns the value of attribute component.



11
12
13
# File 'lib/elevage/provisioner.rb', line 11

def component
  @component
end

#environmentObject

Returns the value of attribute environment.



13
14
15
# File 'lib/elevage/provisioner.rb', line 13

def environment
  @environment
end

#instanceObject

Returns the value of attribute instance.



12
13
14
# File 'lib/elevage/provisioner.rb', line 12

def instance
  @instance
end

#nameObject

Returns the value of attribute name.



10
11
12
# File 'lib/elevage/provisioner.rb', line 10

def name
  @name
end

#vcenterObject

Returns the value of attribute vcenter.



14
15
16
# File 'lib/elevage/provisioner.rb', line 14

def vcenter
  @vcenter
end

Instance Method Details

#buildObject

Public: Build the node rubocop:disable MethodLength, LineLength, GlobalVars, CyclomaticComplexity



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
79
80
81
82
# File 'lib/elevage/provisioner.rb', line 37

def build
  knife_cmd = generate_knife_cmd

  # Modify behavior for dry-run
  # Echo command to stdout and logfile instead of executing command.
  if @options['dry-run']
    puts knife_cmd
    knife_cmd = "echo #{knife_cmd}"
  end

  # Open the logfile for writing
  logfile = File.new("#{@options[:logfiles]}/#{@name}.log", 'w')

  stamp = @options['dry-run'] ? '' : "#{Time.now} [#{$$}]: "
  puts "#{stamp}#{@name}: logging to #{logfile.path}"
  logfile.puts "#{stamp}#{@name}: Provisioning."

  # Execute the knife command, capturing stderr and stdout as they
  # produce anything, and push it all into a Queue object, which we then
  # write to the log file as things come available.
  status = Open4.popen4(knife_cmd) do |_pid, _stdin, stdout, stderr|
    sem = Mutex.new
    # Set and forget the thread for stderr...
    # err_thread = Thread.new do
    Thread.new do
      while (line = stderr.gets)
        sem.synchronize { logfile.puts line }
      end
    end
    out_thread = Thread.new do
      while (line = stdout.gets)
        sem.synchronize { logfile.puts line }
      end
    end
    out_thread.join
    # err_thread.exit
  end

  stamp = @options['dry-run'] ? '' : "#{Time.now} [#{$$}]: "
  logfile.puts "#{stamp}#{@name}: exit status: #{status.exitstatus}"
  logfile.close

  # Inform our master whether we succeeded or failed. Any non-zero
  # exit status is a failure, and the details will be in the logfile
  status.exitstatus == 0 ? true : false
end

#to_sObject



26
27
28
29
30
31
32
33
# File 'lib/elevage/provisioner.rb', line 26

def to_s
  puts "Name: #{@name}"
  puts "Instance: #{@instance}"
  puts "Component: #{@component}"
  puts @component.to_yaml
  puts 'Environment:'
  puts @environment.to_yaml
end