Class: ClcMachine::Machine

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Machine

Returns a new instance of Machine.



11
12
13
14
15
16
# File 'lib/clc_machine.rb', line 11

def initialize(options)
  @options = options
  set_chef(options)
  @data_bag = DataBag.new(options[:stamp])
  @secrets = SecretDataBag.new(options[:stamp])
end

Instance Attribute Details

#data_bagObject (readonly)

Returns the value of attribute data_bag.



19
20
21
# File 'lib/clc_machine.rb', line 19

def data_bag
  @data_bag
end

#optionsObject (readonly)

Returns the value of attribute options.



18
19
20
# File 'lib/clc_machine.rb', line 18

def options
  @options
end

#secretsObject (readonly)

Returns the value of attribute secrets.



20
21
22
# File 'lib/clc_machine.rb', line 20

def secrets
  @secrets
end

Instance Method Details

#connectionObject



22
23
24
# File 'lib/clc_machine.rb', line 22

def connection
  RbVmomi::VIM.connect vsphere_options
end

#delete(name) ⇒ Object



36
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
83
84
# File 'lib/clc_machine.rb', line 36

def delete(name)
  if name.is_a?(String)
    vm = get(name)
    if vm
      config = Chef::Config.merge!({ :driver_options => vsphere_options })
      machine_spec = ChefMetal::ChefMachineSpec.new({'name' => name}, Cheffish.default_chef_server(config))
      driver = ChefMetal.driver_for_url("vsphere://#{vsphere_options[:host]}", config)
      action_handler = ChefMetal::ActionHandler.new
      machine_spec.location = { 'driver_url' => driver.driver_url,
                    'server_id' => vm.config.instanceUuid}

      chef_server = Cheffish.default_chef_server(config)
      begin
        driver.destroy_machine(action_handler, machine_spec, :convergence_options => { :chef_server => chef_server })
      rescue Chef::Exceptions::PrivateKeyMissing, Net::HTTPServerException, Errno::ETIMEDOUT
        puts "Unable to delete node on the chef server. See previous message."
      end

      puts "succesfully deleted #{name}"
    else
      puts "VM #{name} does not exist. I shall destroy nothing."
    end
  else
    to_be_deleted = []
    print 'Looking for matching machines '
    get.each do |v|
      check = v['name']
      print '.'
      if name.match(check)
        to_be_deleted.push(check)
      end
    end
    if to_be_deleted.count > 0
      puts "\nReady to delete #{to_be_deleted.inspect}"
      confirm = ask("Continue? (Y/N) ") { |q| q.validate = /\A[yYnN]\Z/ }
      if confirm.downcase == 'y'
        to_be_deleted.each do |m|
          delete m
        end
      else
        puts 'No action taken. Exiting.'
        exit 0
      end
    else
      puts "\nNo matching machines found.  Exiting."
      exit 0
    end
  end
end

#get(name = nil) ⇒ Object



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

def get(name = nil)
  stamp = data_bag.vsphere_datacenter(options[:hypervisor_type])
  if name
    find_vm(stamp, options[:folder], name)
  else
    folder = find_folder(stamp, options[:folder])
    folder.childEntity.each
  end
end