Class: Chef::Knife::VcVmNetwork

Inherits:
Chef::Knife show all
Includes:
VcCommon, VcNetworkCommon, VcVmCommon
Defined in:
lib/chef/knife/vm/vc_vm_network.rb

Instance Method Summary collapse

Methods included from VcNetworkCommon

#get_network

Methods included from VcVmCommon

#get_vm, included, #sanitize_guest_name, #stop_if_running

Methods included from VcCommon

#connection, #deprecation_msg, #generate_key, #get_password, included, #locate_config_value, #locate_org_option, #notice_msg, #out_msg, #pretty_symbol, #sort_by_key, #store_config, #store_password, #wait_task

Instance Method Details

#runObject



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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/chef/knife/vm/vc_vm_network.rb', line 47

def run
  $stdout.sync = true

  command_arg = @name_args.shift
  vm_arg = @name_args.shift
  network_arg = @name_args.shift

  unless command_arg =~ /add|delete|edit/
    raise ArgumentError, "Invalid command #{command_arg} supplied. Only add, delete and edit are allowed."
  end

  command = command_arg.to_sym

  config = {
    :network_index => locate_config_value(:vm_net_index),
    :ip => locate_config_value(:vm_net_ip),
    :is_connected => locate_config_value(:vm_net_is_connected),
    :ip_allocation_mode => locate_config_value(:vm_ip_allocation_mode),
    :retain_network => locate_config_value(:retain_network)
  }

  connection.

  vm = get_vm(vm_arg)
  network = get_network network_arg

  unless network
    raise new ArgumentError, "Network #{network_arg} not found in vDC."
  end

  unless command == :delete
    parent_network_arg = locate_config_value(:parent_network)
    if parent_network_arg
      ui.msg "Retrieving parent network details"
      parent_network = get_network parent_network_arg
      config[:parent_network] =  { :id => parent_network[:id],
                                   :name => parent_network[:name] }
    else
      ui.msg "Forcing parent network to itself"
      config[:parent_network] = { :id => network[:id],
                                  :name => network[:name] }
    end
  end

  ui.msg "VM network configuration..."
  stop_if_running(connection, vm)

  case command
    when :add
      ui.msg "Adding #{network[:name]} to VM..."
      task_id, response = connection.add_vm_network vm[:id], network, config
      result = wait_task(connection, task_id)
    when :delete
      ui.msg "Removing #{network[:name]} from VM..."
      task_id, response = connection.delete_vm_network vm[:id], network
      result = wait_task(connection, task_id)
    when :edit
      ui.msg "VM network configuration for #{network[:name]}..."
      task_id, response = connection.edit_vm_network vm[:id], network, config
      result = wait_task(connection, task_id)
  end

  if result
    unless vm[:guest_customizations][:enabled]
      config = {
        :enabled => true,
        :admin_passwd_enabled => vm[:guest_customizations][:admin_passwd_enabled],
        :admin_passwd => vm[:guest_customizations][:admin_passwd],
        :customization_script => script
      }

      ui.msg "Enabling Guest Customization to apply changes..."
      task_id, response = connection.set_vm_guest_customization vm[:id], guest_name, config
      wait_task(connection, task_id)
    end

    ui.msg "Forcing Guest Customization to apply changes..."
    task_id = connection.force_customization_vm vm[:id]
    wait_task(connection, task_id)
  end

  connection.logout
end