Skip to content

Sample plugin to do unattended Windows installations

Sean Walberg edited this page Mar 2, 2018 · 2 revisions
require 'rbvmomi'

class KnifeVspherePlugin
  # If you pass `--cplugin-data` on the CLI then when this class is instantiated we'll set the
  # `data` variable with that value (e.g. yourclass.data = whatyoupassed. It can be whatever you want.
  #  A password, JSON, path to a file, base64 encoded XML. You're just going to be responsible for 
  # decoding and making sense of it. In this example it's a path to a file containing JSON that the plugin will
  # parse and then use to fill out the new specification.
  attr_accessor :data # rather than defining a data= method

  def customize_clone_spec(src_config, clone_spec)

    if File.exists? data
      customization_data = JSON.parse(IO.read(data)) # see example below
    else
      abort "Customization plugin data file #{data} not accessible"
    end

    cust_guiUnattended = RbVmomi::VIM.CustomizationGuiUnattended(
      :autoLogon => false,
      :autoLogonCount => 1,
      :password => nil,
      :timeZone => customization_data['timeZone']
    )
    cust_identification = RbVmomi::VIM.CustomizationIdentification(
      :domainAdmin => nil,
      :domainAdminPassword => nil,
      :joinDomain => nil
    )
    cust_name = RbVmomi::VIM.CustomizationFixedName(
      :name => customization_data['host_name']
    )
    cust_user_data = RbVmomi::VIM.CustomizationUserData(
      :computerName => cust_name,
      :fullName => customization_data['fullName'],
      :orgName => customization_data['orgName'],
      :productId => customization_data['windows_key']
    )
    cust_sysprep = RbVmomi::VIM.CustomizationSysprep(
      :guiUnattended => cust_guiUnattended,
      :identification => cust_identification,
      :userData => cust_user_data
    )
    dhcp_ip = RbVmomi::VIM.CustomizationDhcpIpGenerator
    cust_ip = RbVmomi::VIM.CustomizationIPSettings(
      :ip => dhcp_ip
    )
    cust_adapter_mapping = RbVmomi::VIM.CustomizationAdapterMapping(
      :adapter => cust_ip
    )
    cust_adapter_mapping_list = [cust_adapter_mapping]
    global_ip = RbVmomi::VIM.CustomizationGlobalIPSettings
    customization_spec = RbVmomi::VIM.CustomizationSpec(
      :identity => cust_sysprep,
      :globalIPSettings => global_ip,
      :nicSettingMap => cust_adapter_mapping_list
    )
    clone_spec.customization = customization_spec
    puts "New clone_spec object :\n #{YAML::dump(clone_spec)}"
    clone_spec
  end

  def reconfig_vm (target_vm)
    puts "In reconfig_vm method.  No actions implemented.."
  end
end

json data file for the above example

{
  "fullName": "Your Company Inc.",
  "orgName": "Your Company Inc.",
  "windows_key": "xxxxx-xxxxx-xxxxx-xxxxx-xxxxx",
  "host_name": "foo_host",
  "timeZone": "100"
}