Class: KnifeWsFusion::WsfusionCreate

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BaseCommand

#clone_vm, #create_vm_directory, #get_exe_from_path, #get_guest_ip_address, #get_tools_state, #get_vmrun_path, #is_fusion?, #locate_config_value, #normalize_vmx_path, #power_on_vm, #run_vmrun, #set_vmx_variable, #wait_for_ssh

Instance Attribute Details

#initial_sleep_delayObject

Returns the value of attribute initial_sleep_delay.



105
106
107
# File 'lib/chef/knife/wsfusion_create.rb', line 105

def initial_sleep_delay
  @initial_sleep_delay
end

Instance Method Details

#bootstrap_for_node(vm_name, ip_address) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/chef/knife/wsfusion_create.rb', line 172

def bootstrap_for_node(vm_name, ip_address)
    knife_config = Chef::Config[:knife]

    bootstrap = Chef::Knife::Bootstrap.new
    bootstrap.name_args = [ip_address]
    bootstrap.config[:run_list] = config[:run_list]
    bootstrap.config[:chef_node_name] = \
        config[:chef_node_name] || vm_name
    bootstrap.config[:first_boot_attributes] = \
        config[:json_attributes] || {}
    bootstrap.config[:bootstrap_version] = \
        locate_config_value(:bootstrap_version)
    bootstrap.config[:distro] = locate_config_value(:distro)
    bootstrap.config[:template_file] = \
        locate_config_value(:template_file)
    bootstrap.config[:environment] = locate_config_value(:environment)
    bootstrap.config[:ssh_user] = config[:ssh_user]
    bootstrap.config[:ssh_password] = config[:ssh_password]
    bootstrap.config[:use_sudo] = (config[:ssh_user] != 'root')
    bootstrap.config[:identity_file] = config[:identity_file]
    bootstrap.config[:no_host_key_verify] = \
        locate_config_value(:no_host_key_verify)
    bootstrap.config[:encrypted_data_bag_secret] = \
        locate_config_value(:encrypted_data_bag_secret)
    bootstrap.config[:encrypted_data_bag_secret_file] = \
        locate_config_value(:encrypted_data_bag_secret_file)

    return bootstrap
end

#runObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/chef/knife/wsfusion_create.rb', line 107

def run
    vm_source_path = config[:vm_source_path]
    src_vmx_path = normalize_vmx_path(vm_source_path)

    if not src_vmx_path
        ui.fatal("No VM was found at #{vm_source_path}")
        exit 1
    end

    vms_dir = File.dirname(File.dirname(src_vmx_path))

    dest_vm_name = config[:vm_name]
    dest_vm_dir = create_vm_directory(vms_dir, dest_vm_name)

    if not dest_vm_dir
        ui.fatal('Unable to find a place to create the new VM')
        exit 1
    end

    # Check for Tools in the source VM.
    tools_state = get_tools_state(src_vmx_path)

    if tools_state != 'installed' and tools_state != 'running'
        ui.fatal("The source VM doesn't appear to have " +
                 "VMware Tools installed")
        exit 1
    end

    dest_vmx_path = File.join(dest_vm_dir, "#{dest_vm_name}.vmx")

    # Create the virtual machine by cloning the existing one.
    puts "Creating VM #{dest_vm_name} from #{vm_source_path}..."

    success = clone_vm(src_vmx_path, dest_vmx_path, config[:clone_type],
                       dest_vm_name, config[:clone_snapshot_name])

    if not success
        ui.fatal("Unable to clone the VM. See the log for details.")
        exit 1
    end

    # Now power on the VM.
    puts "Powering on the new VM..."
    if not power_on_vm(dest_vmx_path)
        ui.fatal("Unable to power on the VM. See the log for details.")
        exit 1
    end

    # Wait for the guest's IP address.
    puts "Waiting for an IP address from the guest..."
    ip_address = get_guest_ip_address(dest_vmx_path)

    # Wait for SSH access.
    puts "Waiting for sshd..."
    print ('.') until wait_for_ssh(ip_address, config[:ssh_port]) do
        sleep @initial_sleep_delay ||= 10;
        print 'done'
    end

    if Chef::Config[:knife][:chef_mode] != "solo"
        puts "Bootstrapping VM"
        bootstrap_for_node(dest_vm_name, ip_address).run()
    end
end