added RAM ocnfiguration

This commit is contained in:
LizaTretyakova 2017-04-16 23:47:01 +03:00
parent 3b612373e9
commit 3232c52682
1 changed files with 24 additions and 9 deletions

View File

@ -16,6 +16,11 @@ import (
"strconv" "strconv"
) )
type HwConfig struct {
Cpu_sockets int
Ram int
}
type Config struct { type Config struct {
common.PackerConfig `mapstructure:",squash"` common.PackerConfig `mapstructure:",squash"`
@ -27,8 +32,9 @@ type Config struct {
Vm_source_name string `mapstructure:"vm_source_name"` Vm_source_name string `mapstructure:"vm_source_name"`
Vm_target_name string `mapstructure:"vm_target_name"` Vm_target_name string `mapstructure:"vm_target_name"`
Cpu_sockets string `mapstructure:"cpus"` Cpu_sockets string `mapstructure:"cpus"`
Ram string `mapstructure:"RAM"`
cpus int hwConfig HwConfig
ctx interpolate.Context ctx interpolate.Context
} }
@ -53,9 +59,16 @@ func (p *PostProcessor) Configure(raws ...interface{}) error {
if p.config.Vm_target_name == "" { if p.config.Vm_target_name == "" {
p.config.Vm_target_name = p.config.Vm_source_name + "_cloned" p.config.Vm_target_name = p.config.Vm_source_name + "_cloned"
} }
p.config.cpus = -1 p.config.hwConfig.Cpu_sockets = -1
if p.config.Cpu_sockets != "" { if p.config.Cpu_sockets != "" {
p.config.cpus, err = strconv.Atoi(p.config.Cpu_sockets) p.config.hwConfig.Cpu_sockets, err = strconv.Atoi(p.config.Cpu_sockets)
if err != nil {
panic(err)
}
}
p.config.hwConfig.Ram = -1
if p.config.Ram != "" {
p.config.hwConfig.Ram, err = strconv.Atoi(p.config.Ram)
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -85,8 +98,7 @@ func (p *PostProcessor) Configure(raws ...interface{}) error {
return nil return nil
} }
// TODO: replace `cpus` with a more generic hw config structure func CloneVM(URL, username, password, dc_name, folder_name, source_name, target_name string, hwConfig HwConfig) {
func CloneVM(URL, username, password, dc_name, folder_name, source_name, target_name string, cpus int) {
// Prepare entities: client (authentification), finder, folder, virtual machine // Prepare entities: client (authentification), finder, folder, virtual machine
client, ctx := createClient(URL, username, password) client, ctx := createClient(URL, username, password)
finder, ctx := createFinder(ctx, client, dc_name) finder, ctx := createFinder(ctx, client, dc_name)
@ -103,9 +115,12 @@ func CloneVM(URL, username, password, dc_name, folder_name, source_name, target_
var relocateSpec types.VirtualMachineRelocateSpec var relocateSpec types.VirtualMachineRelocateSpec
var confSpec types.VirtualMachineConfigSpec var confSpec types.VirtualMachineConfigSpec
// configure CPUs // configure HW
if cpus != -1 { if hwConfig.Cpu_sockets != -1 {
confSpec.NumCPUs = int32(cpus) confSpec.NumCPUs = int32(hwConfig.Cpu_sockets)
}
if hwConfig.Ram != -1 {
confSpec.MemoryMB = int64(hwConfig.Ram)
} }
cloneSpec := types.VirtualMachineCloneSpec{ cloneSpec := types.VirtualMachineCloneSpec{
@ -126,7 +141,7 @@ func CloneVM(URL, username, password, dc_name, folder_name, source_name, target_
} }
func (p *PostProcessor) PostProcess(ui packer.Ui, source packer.Artifact) (packer.Artifact, bool, error) { func (p *PostProcessor) PostProcess(ui packer.Ui, source packer.Artifact) (packer.Artifact, bool, error) {
CloneVM(p.config.Url, p.config.Username, p.config.Password, p.config.Dc_name, p.config.Folder_name, p.config.Vm_source_name, p.config.Vm_target_name, p.config.cpus) CloneVM(p.config.Url, p.config.Username, p.config.Password, p.config.Dc_name, p.config.Folder_name, p.config.Vm_source_name, p.config.Vm_target_name, p.config.hwConfig)
return source, true, nil return source, true, nil
} }