Added possibility to configure memory, cpus count, and vmname in vagrantfile

This commit is contained in:
Volodymyr Babchynskyy 2015-01-14 16:59:01 +02:00
parent ea8de92c59
commit a9ac168b19
4 changed files with 77 additions and 11 deletions

View File

@ -14,6 +14,15 @@ module VagrantPlugins
def call(env)
vm_dir = env[:machine].box.directory.join("Virtual Machines")
hd_dir = env[:machine].box.directory.join("Virtual Hard Disks")
memory = env[:machine].provider_config.memory
maxmemory = env[:machine].provider_config.maxmemory
cpus = env[:machine].provider_config.cpus
vmname = env[:machine].provider_config.vmname
env[:ui].output("Configured Dynamical memory allocation, maxmemory is #{maxmemory}") if maxmemory
env[:ui].output("Configured startup memory is #{memory}") if memory
env[:ui].output("Configured cpus number is #{cpus}") if cpus
env[:ui].output("Configured vmname is #{vmname}") if vmname
if !vm_dir.directory? || !hd_dir.directory?
raise Errors::BoxInvalid
@ -78,6 +87,10 @@ module VagrantPlugins
image_path: image_path.to_s.gsub("/", "\\")
}
options[:switchname] = switch if switch
options[:memory] = memory if memory
options[:maxmemory] = maxmemory if maxmemory
options[:cpus] = cpus if cpus
options[:vmname] = vmname if vmname
env[:ui].detail("Creating and registering the VM...")
server = env[:machine].provider.driver.import(options)

View File

@ -8,15 +8,27 @@ module VagrantPlugins
#
# @return [Integer]
attr_accessor :ip_address_timeout
attr_accessor :memory
attr_accessor :maxmemory
attr_accessor :cpus
attr_accessor :vmname
def initialize
@ip_address_timeout = UNSET_VALUE
@memory = UNSET_VALUE
@maxmemory = UNSET_VALUE
@cpus = UNSET_VALUE
@vmname = UNSET_VALUE
end
def finalize!
if @ip_address_timeout == UNSET_VALUE
@ip_address_timeout = 120
end
@memory = nil if @memory == UNSET_VALUE
@maxmemory = nil if @maxmemory == UNSET_VALUE
@cpus = nil if @cpus == UNSET_VALUE
@vmname = nil if @vmname == UNSET_VALUE
end
def validate(machine)

View File

@ -4,7 +4,11 @@ Param(
[Parameter(Mandatory=$true)]
[string]$image_path,
[string]$switchname=$null
[string]$switchname=$null,
[string]$memory=$null,
[string]$maxmemory=$null,
[string]$cpus=$null,
[string]$vmname=$null
)
# Include the following modules
@ -13,10 +17,22 @@ $Dir = Split-Path $script:MyInvocation.MyCommand.Path
[xml]$vmconfig = Get-Content -Path $vm_xml_config
$vm_name = $vmconfig.configuration.properties.name.'#text'
$processors = $vmconfig.configuration.settings.processors.count.'#text'
$generation = [int]($vmconfig.configuration.properties.subtype.'#text')+1
if (!$vmname) {
# Get the name of the vm
$vm_name = $vmconfig.configuration.properties.name.'#text'
}else {
$vm_name = $vmname
}
if (!$cpus) {
# Get the name of the vm
$processors = $vmconfig.configuration.settings.processors.count.'#text'
}else {
$processors = $cpus
}
function GetUniqueName($name) {
Get-VM | ForEach-Object -Process {
if ($name -eq $_.Name) {
@ -31,18 +47,34 @@ do {
$vm_name = GetUniqueName $name
} while ($vm_name -ne $name)
$memory = (Select-Xml -xml $vmconfig -XPath "//memory").node.Bank
if ($memory.dynamic_memory_enabled."#text" -eq "True") {
$dynamicmemory = $True
if (!$memory) {
$xmlmemory = (Select-Xml -xml $vmconfig -XPath "//memory").node.Bank
if ($xmlmemory.dynamic_memory_enabled."#text" -eq "True") {
$dynamicmemory = $True
}
else {
$dynamicmemory = $False
}
# Memory values need to be in bytes
$MemoryMaximumBytes = ($xmlmemory.limit."#text" -as [int]) * 1MB
$MemoryStartupBytes = ($xmlmemory.size."#text" -as [int]) * 1MB
$MemoryMinimumBytes = ($xmlmemory.reservation."#text" -as [int]) * 1MB
}
else {
$dynamicmemory = $False
if (!$maxmemory){
$dynamicmemory = $False
$MemoryMaximumBytes = ($memory -as [int]) * 1MB
$MemoryStartupBytes = ($memory -as [int]) * 1MB
$MemoryMinimumBytes = ($memory -as [int]) * 1MB
}
else {
$dynamicmemory = $True
$MemoryMaximumBytes = ($maxmemory -as [int]) * 1MB
$MemoryStartupBytes = ($memory -as [int]) * 1MB
$MemoryMinimumBytes = ($memory -as [int]) * 1MB
}
}
# Memory values need to be in bytes
$MemoryMaximumBytes = ($memory.limit."#text" -as [int]) * 1MB
$MemoryStartupBytes = ($memory.size."#text" -as [int]) * 1MB
$MemoryMinimumBytes = ($memory.reservation."#text" -as [int]) * 1MB
if (!$switchname) {
# Get the name of the virtual switch

View File

@ -8,6 +8,15 @@ sidebar_current: "hyperv-configuration"
The Hyper-V provider has some provider-specific configuration options
you may set. A complete reference is shown below:
* `vmname` (string) - Name of virtual mashine as shown in Hyper-V manager.
Defaults is taken from box image XML.
* `cpus` (integer) - Number of virtual CPU given to mashine.
Defaults is taken from box image XML.
* `memory` (integer) - Number of MegaBytes allocated to VM at startup.
Defaults is taken from box image XML.
* `maxmemory` (integer) - Number of MegaBytes maximal allowed to allocate for VM
This parameter is switch on Dynamic Allocation of memory.
Defaults is taken from box image XML.
* `ip_address_timeout` (integer) - The time in seconds to wait for the
virtual machine to report an IP address. This defaults to 120 seconds.
This may have to be increased if your VM takes longer to boot.