IMPR: Cleaned up source from puts and debug output.

BUG: Moved source for attaching disk from config.rb to import.rb which makes it much more stable when halting and uping the vm several times.
IMPR: Implemented all I18n messages in English for this feature.
FEAT: Improved the documentation and added an example.
This commit is contained in:
John Ericson 2018-07-19 23:49:44 +02:00
parent bdde324158
commit 7429e861e7
7 changed files with 122 additions and 118 deletions

View File

@ -35,57 +35,6 @@ module VagrantPlugins
end
end
# TODO: Should be controllers as we want to be able to have several lines.
#if !env[:machine].provider_config.controllers.empty?
# puts "hej: controller not empty"
# #env[:ui].detail("Controllers set")
# #controller.each do [key, value]
# # puts "key: #{id} value: #{value}"
# # if [type].include?(key)
# # puts "key matches type"
# # end
# #end
#
# env[:machine].provider_config.controller.each do |key, value|
# env[:ui].output("#{key} is #{value}")
# end
#
# #env[:machine].provider.driver.set_vm_integration_services(
# # env[:machine].provider_config.vm_integration_services)
#end
disks_to_create = []
env[:machine].provider_config.controllers.each { |controller|
#puts "configure.rb: controller: #{controller}"
next_is_size = false
disk_name = ''
controller[:disks].each { |i|
#puts "configure.rb: i: #{i} disk_name: #{disk_name}"
if !next_is_size
if File.file?(i)
create_disk = false
filename_for_disk = i
next_is_size = false
@logger.error("Attaching disks is not implemented yet")
else
create_disk = true
disk_name = i
next_is_size = true
#puts "configure.rb: disk_name set to: #{disk_name}"
end
else
#puts "configure.rb: Adding disk to create. name: #{disk_name}"
disks_to_create << { name: "\"#{disk_name}\"", size: i}
end
}
}
#puts "configure.rb: disks_to_create:#{disks_to_create}"
disks_to_create_json = disks_to_create.to_json
puts "configure.rb: json: #{disks_to_create_json}"
# If we already configured previously don't prompt for switch
sentinel = env[:machine].data_dir.join("action_configure")
@ -122,7 +71,6 @@ module VagrantPlugins
"AutoStopAction" => env[:machine].provider_config.auto_stop_action,
"EnableCheckpoints" => env[:machine].provider_config.enable_checkpoints,
"VirtualizationExtensions" => !!env[:machine].provider_config.enable_virtualization_extensions,
"DisksToCreate" => disks_to_create_json
}
options.delete_if{|_,v| v.nil? }

View File

@ -17,7 +17,7 @@ module VagrantPlugins
def call(env)
vm_dir = env[:machine].box.directory.join("Virtual Machines")
hd_dir = env[:machine].box.directory.join("Virtual Hard Disks")
controller = env[:machine].provider_config.controller
controllers = env[:machine].provider_config.controllers
if !vm_dir.directory? || !hd_dir.directory?
@logger.error("Required virtual machine directory not found!")
raise Errors::BoxInvalid, name: env[:machine].name
@ -61,6 +61,35 @@ module VagrantPlugins
env[:ui].output("Importing a Hyper-V instance")
dest_path = env[:machine].data_dir.join("Virtual Hard Disks").join(image_path.basename).to_s
disks_to_create = []
data_dir = env[:machine].data_dir
# This can happen when creating new on up.
controllers.delete_if &:empty?
controllers.each { |controller|
next_is_size = false
disk_name = ''
controller[:disks].each { |i|
if !next_is_size
if File.file?(i)
create_disk = false
filename_for_disk = i
next_is_size = false
@logger.error("Attaching disks is not implemented yet")
else
create_disk = true
disk_name = i
disk_name = data_dir.join("#{disk_name}.vhdx").to_s.gsub("/", "\\")
next_is_size = true
end
else
disks_to_create << { name: disk_name, size: i}
end
}
}
disks_to_create_json = disks_to_create.to_json.to_s.gsub('"', '"""')
options = {
"VMConfigFile" => Vagrant::Util::Platform.wsl_to_windows_path(config_path).gsub("/", "\\"),
"DestinationPath" => Vagrant::Util::Platform.wsl_to_windows_path(dest_path).gsub("/", "\\"),
@ -68,9 +97,8 @@ module VagrantPlugins
"LinkedClone" => !!env[:machine].provider_config.linked_clone,
"SourcePath" => Vagrant::Util::Platform.wsl_to_windows_path(image_path).gsub("/", "\\"),
"VMName" => env[:machine].provider_config.vmname,
"DisksToCreate" => disks_to_create_json,
}
#options[:DisksConfig] = add_abs_path(controller, env[:machine].data_dir).to_json.to_s.gsub('"', '"""') if controller
env[:ui].detail("Creating and registering the VM...")
server = env[:machine].provider.driver.import(options)
@ -80,10 +108,6 @@ module VagrantPlugins
@app.call(env)
end
private
def add_abs_path(disks_config, data_dir)
disks_config.each {|controller| controller.each {|disk| disk['name'] = data_dir.join("#{disk['name']}.vhdx").to_s.gsub("/", "\\") if disk['name'] }}
end
end
end
end

View File

@ -68,7 +68,6 @@ module VagrantPlugins
end
def controller(controller={})
#puts "hej: controller called in config.rb"
@controllers << controller
end
@ -133,7 +132,6 @@ module VagrantPlugins
controllers.delete_if &:empty?
controllers.each { |controller|
#puts "controller: #{controller}"
if ![:ide, :scsi].include?(controller[:type])
errors << I18n.t("vagrant_hyperv.config.invalid_controller_type",
@ -141,13 +139,12 @@ module VagrantPlugins
end
if [:ide].include?(controller[:type])
errors << I18n.t("vagrant_hyperv.config.invalid_controller_type_ide_not_implemeented_yet",
type: controller[:type])
errors << I18n.t("vagrant_hyperv.config.invalid_controller_type_ide_not_implemeented_yet")
end
if !controller[:disks].is_a?(Array)
errors << I18n.t("vagrant_hyperv.config.invalid_controller_disks_is_not_an_array",
disks: controller[:disks])
received: controller[:disks].class)
next
end
@ -166,13 +163,12 @@ module VagrantPlugins
end
else
errors << I18n.t("vagrant_hyperv.config.invalid_controller_disks_element_is_not_a_string",
element: i)
element: i, element_class: i.class)
end
else
#puts "next_is_size: true"
if !i.is_a?(Integer)
errors << I18n.t("vagrant_hyperv.config.invalid_controller_disks_element_is_not_an_integer",
element: i)
element: i, element_class: i.class)
end
end
}

View File

@ -18,9 +18,7 @@ param(
[parameter (Mandatory=$false)]
[switch] $VirtualizationExtensions,
[parameter (Mandatory=$false)]
[switch] $EnableCheckpoints,
[parameter (Mandatory=$false)]
[string] $DisksToCreate=$null
[switch] $EnableCheckpoints
)
$ErrorActionPreference = "Stop"
@ -95,49 +93,3 @@ try {
Write-ErrorMessage "Failed to ${CheckpointAction} checkpoints on VM: ${PSItem}"
exit 1
}
#controller - path (for existent)
# path,
# sizeMB, name (for new)
function AddDisks($vm, $controller) {
#get controller
$contNumber = ($vm | Add-VMScsiController -PassThru).ControllerNumber
foreach($disk in $controller) {
#get vhd
$vhd = $null
if($disk.Path) {
if (Test-Path -Path $disk.Path) {
$vhd = Resolve-Path -Path $disk.Path
}
}
else {
$vhd = "$($disk.Name).vhdx"
Add-Content "c:/ps_debug.log" -value "vhd: $vhd"
if (!(Test-Path -Path $vhd)) {
New-VHD -Path $vhd -SizeBytes ([UInt64]$disk.Size * 1MB) -Dynamic
}
}
if (!(Test-Path -Path $vhd)) {
Write-Error "There is error in virtual disk (VHD) configuration"
break
}
$driveParam = @{
ControllerNumber = $contNumber
Path = $vhd
ControllerType = "SCSI"
}
Add-Content "c:/ps_debug.log" -value "$vm | Add-VMHardDiskDrive @driveParam"
Add-Content "c:/ps_debug.log" -value "vhd: $vhd"
$vm | Add-VMHardDiskDrive @driveParam
}
}
if ($DisksToCreate) {
Add-Content "c:/ps_debug.log" -value "DisksToCreate: $DisksToCreate"
$ParsedDisksToCreate = $DisksToCreate | ConvertFrom-Json
Add-Content "c:/ps_debug.log" -value "ParsedDisksToCreate: $ParsedDisksToCreate"
$ParsedDisksToCreate | ForEach-Object { AddDisks -vm $VM -controller $_ }
}

View File

@ -14,7 +14,7 @@ param(
[parameter (Mandatory=$false)]
[string] $VMName=$null,
[parameter (Mandatory=$false)]
[string] $DisksConfig=$null
[string] $DisksToCreate=$null
)
$ErrorActionPreference = "Stop"
@ -37,3 +37,48 @@ try {
Write-ErrorMessage "${PSItem}"
exit 1
}
#controller - path (for existent)
# path,
# sizeMB, name (for new)
function AddDisks($vm, $controller) {
#get controller
$contNumber = ($vm | Add-VMScsiController -PassThru).ControllerNumber
foreach($disk in $controller) {
#get vhd
$vhd = $null
if($disk.Path) {
if (Test-Path -Path $disk.Path) {
$vhd = Resolve-Path -Path $disk.Path
}
}
else {
$vhd = $disk.Name
if (!(Test-Path -Path $vhd)) {
New-VHD -Path $vhd -SizeBytes ([UInt64]$disk.Size * 1MB) -Dynamic
}
}
if (!(Test-Path -Path $vhd)) {
Write-Error "There is error in virtual disk (VHD) configuration"
break
}
$driveParam = @{
ControllerNumber = $contNumber
Path = $vhd
ControllerType = "SCSI"
}
$vm | Add-VMHardDiskDrive @driveParam
}
}
if ($DisksToCreate) {
$ParsedDisksToCreate = $DisksToCreate | ConvertFrom-Json
$ParsedDisksToCreate | ForEach-Object { AddDisks -vm $VMName -controller $_ }
}

View File

@ -39,6 +39,25 @@ en:
The `differencing_disk` configuration option is deprecated and should
no longer be used. The `linked_clone` configuration option should
be used instead.
invalid_controller_type: |-
Invalid controller type provided for :type. Type receiver is
`%{type}` but `:ide` or `:scsi` was expected.
Received: %{type}
Allowed: :ide, :scsi
invalid_controller_type_ide_not_implemeented_yet: |-
Controller type IDE is not implemented yet.
invalid_controller_disks_is_not_an_array: |-
Invalid type provided for `disks` in controller. Type received
is `%{received}` but `Array` was expected.
invalid_controller_disks_attaching_disks_not_implemented_yet: |-
Attaching existing disks is not implemented yet.
invalid_controller_disks_element_is_not_a_string: |-
Invalid type provided for element `%{element}`. Type received is
`%{element_class}` but `String` was expected.
invalid_controller_disks_element_is_not_an_integer: |-
Invalid type provided for element `%{element}`. Type received is
`%{element_class}` but `Integer` was expected.
errors:
admin_required: |-
The Hyper-V provider requires that Vagrant be run with

View File

@ -32,7 +32,9 @@ you may set. A complete reference is shown below:
* `shutdown` (boolean)
* `time_synchronization` (boolean)
* `vss` (boolean)
* `controller` (hash) Config of disks and controllers.
* `controller` (hash) - Config of disks and controllers.
* `type` (:scsi) - Determines type of controller. Only supports SCSI right now. IDE is to be implemented.
* `disks` (array) - Disks to be created on the controller. First element is a string specifying name of disk, example d1. Second is size of disk in MB, example 10 * 1024 is 10 GB. Name of disk comes first and size after on every disk.
## VM Integration Services
@ -55,3 +57,21 @@ end
This example would enable the `GuestServiceInterface` (which Vagrant is aware) and `CustomVMSRV` (which
Vagrant is _not_ aware) VM integration services.
## Attaching extra controllers and disks
The `controller` configuration option is a simple hash that describes what kind of controller it is.
Such as `:scsi` or `:ide`. Right now only `:scsi` is supported. After this the disks that will be
created is specified in the `disks` array. This is an array where several disks to create can be
specified. First comes the name of the disk, for example d1, and after that followes the size of the
disk specified in MB, for example 10 * 1024 for a 10 GB disk. Repeat this for the number of disks you
want to attach on the controller. To attach existing vhd- or vhdx-files is not implemented yet.
```ruby
config.vm.provider "hyperv" do |h|
h.controller type: :scsi, disks: [ "d1", 10 * 1024 ]
end
```
This example would create a SCSI controller and also create a d1.vhdx file and attach it as a dynamic
disk that is 10 * 1024 MB big, in other words 10 GB.