Class: KnifeESX::DeployJob

Inherits:
Object
  • Object
show all
Defined in:
lib/chef/knife/esx_vm_create.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ DeployJob

Returns a new instance of DeployJob.



63
64
65
66
# File 'lib/chef/knife/esx_vm_create.rb', line 63

def initialize(options)
  @name, @options = options
  validate
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



61
62
63
# File 'lib/chef/knife/esx_vm_create.rb', line 61

def name
  @name
end

Instance Method Details

#runObject

returns [status, stdout, stderr]



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/chef/knife/esx_vm_create.rb', line 78

def run
  args = ""
  extra_args = ""
  @options.each do |k, v|
    if k == 'extra-args'
      extra_args << v
    else
      args << "--#{k} #{v} " unless k == 'extra-args'
    end
  end

  @out = ""
  @err = ""
  optstring = []
  @options.each do |k,v|
    optstring << "   - #{k}:".ljust(25) +  "#{v}\n" unless k =~ /password/
  end
  log_file = "/tmp/knife_esx_vm_create_#{@name.to_s.strip.chomp.gsub(/\s/,'_')}.log"
  Chef::Log.info "Bootstrapping VM #{@name} \n#{optstring.join}"
  Chef::Log.info "VM #{@name} bootstrap log: #{log_file}"
  @status = Open4.popen4("knife esx vm create --vm-name #{@name} #{args} #{extra_args} > #{log_file} 2>&1") do |pid, stdin, stdout, stderr|
    @out << stdout.read.strip
    @err << stderr.read.strip
  end
  if @status == 0
    Chef::Log.info "[#{@name}] deployment finished OK"
  else
    Chef::Log.error "[#{@name}] deployment FAILED"
    @err.each_line do |l|
      Chef::Log.error "[#{@name}] #{l.chomp}"
    end
  end
  return @status, @out, @err
end

#validateObject



68
69
70
71
72
73
74
75
# File 'lib/chef/knife/esx_vm_create.rb', line 68

def validate
  if @name.nil? or @name.empty?
    raise Exception.new("Invalid job name")
  end
  if not @options['vm-disk'] or !File.exist?(@options['vm-disk'])
    raise Exception.new("Invalid VM disk for job #{@name}.")
  end
end