Class: Elevage::Provisioner

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

Overview

Provisioner is responsible for the actual execution of the commands to create the requested virtual machine.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Create the Provisioner object for the requested virtual machine ‘component` this node is

Parameters:

  • name (String)

    Name of the node to create

  • component (String)

    Name of the platform component node is part of

  • instance (String)

    Number representing which instance of

  • environment (String)

    Name of the environnment we’re provisioning to

  • options (String)

    Thor ‘options` hash



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

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.



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

def component
  @component
end

#environmentObject

Returns the value of attribute environment.



16
17
18
# File 'lib/elevage/provisioner.rb', line 16

def environment
  @environment
end

#instanceObject

Returns the value of attribute instance.



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

def instance
  @instance
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#vcenterObject

Returns the value of attribute vcenter.



17
18
19
# File 'lib/elevage/provisioner.rb', line 17

def vcenter
  @vcenter
end

Instance Method Details

#buildObject

Build the the virtual machine



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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/elevage/provisioner.rb', line 50

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 do
          logfile.puts line
          logfile.sync
        end
      end
    end
    out_thread = Thread.new do
      while (line = stdout.gets)
        sem.synchronize do
          logfile.puts line
          logfile.sync
        end
      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_sString

Stringify the Provisioner

Returns:

  • (String)


38
39
40
41
42
43
44
45
# File 'lib/elevage/provisioner.rb', line 38

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