Class: BoxGrinder::VMwarePlugin

Inherits:
BasePlugin show all
Defined in:
lib/boxgrinder-build/plugins/platform/vmware/vmware-plugin.rb

Instance Attribute Summary

Attributes inherited from BasePlugin

#deliverables, #plugin_info

Instance Method Summary collapse

Methods inherited from BasePlugin

#current_platform, #deliverables_exists?, #init, #initialize, #is_supported_os?, #is_supported_platform?, #merge_plugin_config, #read_plugin_config, #register_deliverable, #register_supported_os, #register_supported_platform, #run, #set_default_config_value, #subtype, #supported_oses, #validate_plugin_config

Methods included from Plugins

#plugin

Constructor Details

This class inherits a constructor from BoxGrinder::BasePlugin

Instance Method Details

#after_initObject



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/boxgrinder-build/plugins/platform/vmware/vmware-plugin.rb', line 25

def after_init
  register_deliverable(:vmx => "#{@appliance_config.name}.vmx",
                       :readme => "README")

  if @plugin_config['type'].eql?('personal') and @plugin_config['thin_disk']
    register_deliverable(:disk => "#{@appliance_config.name}.vmdk")
  else
    register_deliverable(:disk => "#{@appliance_config.name}.raw",
                         :vmdk => "#{@appliance_config.name}.vmdk")
  end
end

#build_vmware_enterpriseObject



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/boxgrinder-build/plugins/platform/vmware/vmware-plugin.rb', line 173

def build_vmware_enterprise
  @log.debug "Building VMware enterprise image."

  copy_raw_image

  # defaults for ESXi (maybe for others too)
  @appliance_config.hardware.network = "VM Network" if @appliance_config.hardware.network.eql?("NAT")

  # create .vmx file
  vmx_data = change_common_vmx_values
  vmx_data += "ethernet0.networkName = \"#{@appliance_config.hardware.network}\""

  File.open(@deliverables.vmx, "w") { |f| f.write(vmx_data) }

  # create disk descriptor file
  File.open(@deliverables.vmdk, "w") { |f| f.write(change_vmdk_values("vmfs")) }

  @log.debug "VMware enterprise image was built."
end

#build_vmware_personalObject



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/boxgrinder-build/plugins/platform/vmware/vmware-plugin.rb', line 153

def build_vmware_personal
  @log.debug "Building VMware personal image."

  if @plugin_config['thin_disk']
    @log.debug "Using qemu-img to convert the image..."
    @image_helper.convert_disk(@previous_deliverables.disk, :vmdk, @deliverables.disk)
    @log.debug "Conversion done."
  else
    copy_raw_image

    # create disk descriptor file
    File.open(@deliverables.vmdk, "w") { |f| f.write(change_vmdk_values("monolithicFlat")) }
  end

  # create .vmx file
  File.open(@deliverables.vmx, "w") { |f| f.write(change_common_vmx_values) }

  @log.debug "VMware personal image was built."
end

#change_common_vmx_valuesObject



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/boxgrinder-build/plugins/platform/vmware/vmware-plugin.rb', line 113

def change_common_vmx_values
  vmx_data = File.open("#{File.dirname(__FILE__)}/src/base.vmx").read

  # replace version with current appliance version
  vmx_data.gsub!(/#VERSION#/, "#{@appliance_config.version}.#{@appliance_config.release}")
  # change name
  vmx_data.gsub!(/#NAME#/, @appliance_config.name.to_s)
  # and summary
  vmx_data.gsub!(/#SUMMARY#/, @appliance_config.summary.to_s)
  # replace guestOS informations to: linux or otherlinux-64, this seems to be the savests values
  vmx_data.gsub!(/#GUESTOS#/, "#{@appliance_config.hardware.arch == "x86_64" ? "otherlinux-64" : "linux"}")
  # memory size
  vmx_data.gsub!(/#MEM_SIZE#/, @appliance_config.hardware.memory.to_s)
  # memory size
  vmx_data.gsub!(/#VCPU#/, @appliance_config.hardware.cpus.to_s)
  # network name
  # vmx_data.gsub!( /#NETWORK_NAME#/, @image_config.network_name )

  vmx_data
end

#change_vmdk_values(type) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/boxgrinder-build/plugins/platform/vmware/vmware-plugin.rb', line 92

def change_vmdk_values(type)
  vmdk_data = File.open("#{File.dirname(__FILE__)}/src/base.vmdk").read

  c, h, s, total_sectors = generate_scsi_chs

  is_enterprise = type.eql?("vmfs")

  vmdk_data.gsub!(/#NAME#/, @appliance_config.name)
  vmdk_data.gsub!(/#TYPE#/, type)
  vmdk_data.gsub!(/#EXTENT_TYPE#/, is_enterprise ? "VMFS" : "FLAT")
  vmdk_data.gsub!(/#NUMBER#/, is_enterprise ? "" : "0")
  vmdk_data.gsub!(/#HW_VERSION#/, "7")
  vmdk_data.gsub!(/#CYLINDERS#/, c.to_s)
  vmdk_data.gsub!(/#HEADS#/, h.to_s)
  vmdk_data.gsub!(/#SECTORS#/, s.to_s)
  vmdk_data.gsub!(/#TOTAL_SECTORS#/, total_sectors.to_s)
  vmdk_data.gsub!(/#THIN_PROVISIONED#/, @plugin_config['thin_disk'] ? "1" : "0")

  vmdk_data
end

#copy_raw_imageObject



147
148
149
150
151
# File 'lib/boxgrinder-build/plugins/platform/vmware/vmware-plugin.rb', line 147

def copy_raw_image
  @log.debug "Copying VMware image file, this may take several minutes..."
  @exec_helper.execute "cp '#{@previous_deliverables.disk}' '#{@deliverables.disk}'"
  @log.debug "VMware image copied."
end

#create_readmeObject



61
62
63
64
65
66
# File 'lib/boxgrinder-build/plugins/platform/vmware/vmware-plugin.rb', line 61

def create_readme
  readme = File.open("#{File.dirname(__FILE__)}/src/README-#{@plugin_config['type']}").read
  readme.gsub!(/#APPLIANCE_NAME#/, @appliance_config.name)

  readme
end

#customize_imageObject



134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/boxgrinder-build/plugins/platform/vmware/vmware-plugin.rb', line 134

def customize_image
  unless @appliance_config.post['vmware'].nil? or @appliance_config.post['vmware'].empty?
    @image_helper.customize(@deliverables.disk) do |guestfs, guestfs_helper|
      @appliance_config.post['vmware'].each do |cmd|
        guestfs_helper.sh(cmd, :arch => @appliance_config.hardware.arch)
      end
      @log.debug "Post commands from appliance definition file executed."
    end
  else
    @log.debug "No commands specified, skipping."
  end
end

#executeObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/boxgrinder-build/plugins/platform/vmware/vmware-plugin.rb', line 42

def execute
  @log.info "Converting image to VMware #{@plugin_config['type']} format..."

  case @plugin_config['type']
    when 'personal'
      build_vmware_personal
    when 'enterprise'
      build_vmware_enterprise
    else
      raise "Not known VMware format specified. Available are: personal and enterprise. See http://boxgrinder.org/tutorials/boxgrinder-build-plugins/#VMware_Platform_Plugin for more info."
  end

  customize_image

  File.open(@deliverables.readme, "w") { |f| f.write(create_readme) }

  @log.info "Image converted to VMware format."
end

#generate_scsi_chsObject

returns value of cylinders, heads and sector for selected disk size (in GB) http://kb.vmware.com/kb/1026254



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/boxgrinder-build/plugins/platform/vmware/vmware-plugin.rb', line 70

def generate_scsi_chs
  disk_size = File.stat(@previous_deliverables.disk).size

  if disk_size < 1073741824
    h = 64
    s = 32
  else
    if disk_size < 2147483648
      h = 128
      s = 32
    else
      h = 255
      s = 63
    end
  end

  c = disk_size / (h*s*512)
  total_sectors = disk_size / 512

  [c.to_i, h.to_i, s.to_i, total_sectors.to_i]
end

#validateObject



37
38
39
40
# File 'lib/boxgrinder-build/plugins/platform/vmware/vmware-plugin.rb', line 37

def validate
  set_default_config_value('thin_disk', false)
  validate_plugin_config(['type'], 'http://boxgrinder.org/tutorials/boxgrinder-build-plugins/#VMware_Platform_Plugin')
end