mirror of
https://github.com/hashicorp/packer.git
synced 2026-06-14 19:20:04 -04:00
Merge pull request #3096 from grubernaut/f-resource-limits
Prevalidate Hardware Specs on Linux
This commit is contained in:
commit
ffb85f6ea5
8 changed files with 114 additions and 6 deletions
|
|
@ -1,6 +1,10 @@
|
|||
package common
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"github.com/mitchellh/packer/common"
|
||||
"github.com/mitchellh/packer/template/interpolate"
|
||||
)
|
||||
|
||||
|
|
@ -11,7 +15,25 @@ type VBoxManageConfig struct {
|
|||
func (c *VBoxManageConfig) Prepare(ctx *interpolate.Context) []error {
|
||||
if c.VBoxManage == nil {
|
||||
c.VBoxManage = make([][]string, 0)
|
||||
return nil
|
||||
}
|
||||
|
||||
return nil
|
||||
var errs []error
|
||||
var err error
|
||||
var desiredMem uint64
|
||||
|
||||
for _, cmd := range c.VBoxManage {
|
||||
if cmd[2] == "--memory" {
|
||||
desiredMem, err = strconv.ParseUint(cmd[3], 10, 64)
|
||||
if err != nil {
|
||||
errs = append(errs, fmt.Errorf("Error parsing string: %s", err))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if err = common.AvailableMem(desiredMem); err != nil {
|
||||
errs = append(errs, fmt.Errorf("Unavailable Resources: %s", err))
|
||||
}
|
||||
|
||||
return errs
|
||||
}
|
||||
|
|
|
|||
|
|
@ -152,6 +152,12 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
|
|||
b.config.GuestAdditionsSHA256 = strings.ToLower(b.config.GuestAdditionsSHA256)
|
||||
}
|
||||
|
||||
// Determine if DiskSize is able to be allocated
|
||||
if err = common.AvailableDisk(uint64(b.config.DiskSize)); err != nil {
|
||||
errs = packer.MultiErrorAppend(errs,
|
||||
fmt.Errorf("Unavailable Resources: %s", err))
|
||||
}
|
||||
|
||||
// Warnings
|
||||
if b.config.ShutdownCommand == "" {
|
||||
warnings = append(warnings,
|
||||
|
|
|
|||
|
|
@ -1,6 +1,10 @@
|
|||
package common
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"github.com/mitchellh/packer/common"
|
||||
"github.com/mitchellh/packer/template/interpolate"
|
||||
)
|
||||
|
||||
|
|
@ -9,6 +13,26 @@ type VMXConfig struct {
|
|||
VMXDataPost map[string]string `mapstructure:"vmx_data_post"`
|
||||
}
|
||||
|
||||
func (c *VMXConfig) Prepare(ctx *interpolate.Context) []error {
|
||||
return nil
|
||||
func (c *VMXConfig) Prepare(ctx *interpolate.Context, remoteType string) []error {
|
||||
var errs []error
|
||||
var err error
|
||||
var desiredMem uint64
|
||||
|
||||
// Validate memory resources, only on local hosts
|
||||
if remoteType == "" {
|
||||
for k, v := range c.VMXData {
|
||||
if k == "memsize" {
|
||||
desiredMem, err = strconv.ParseUint(v, 10, 64)
|
||||
if err != nil {
|
||||
errs = append(errs, fmt.Errorf("Error parsing string: %s", err))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if err = common.AvailableMem(desiredMem); err != nil {
|
||||
errs = append(errs, fmt.Errorf("Unavailable Resources: %s", err))
|
||||
}
|
||||
|
||||
return errs
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ func TestVMXConfigPrepare(t *testing.T) {
|
|||
"two": "bar",
|
||||
}
|
||||
|
||||
errs := c.Prepare(testConfigTemplate(t))
|
||||
errs := c.Prepare(testConfigTemplate(t), "")
|
||||
if len(errs) > 0 {
|
||||
t.Fatalf("bad: %#v", errs)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -93,7 +93,7 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
|
|||
errs = packer.MultiErrorAppend(errs, b.config.ShutdownConfig.Prepare(&b.config.ctx)...)
|
||||
errs = packer.MultiErrorAppend(errs, b.config.SSHConfig.Prepare(&b.config.ctx)...)
|
||||
errs = packer.MultiErrorAppend(errs, b.config.ToolsConfig.Prepare(&b.config.ctx)...)
|
||||
errs = packer.MultiErrorAppend(errs, b.config.VMXConfig.Prepare(&b.config.ctx)...)
|
||||
errs = packer.MultiErrorAppend(errs, b.config.VMXConfig.Prepare(&b.config.ctx, b.config.RemoteType)...)
|
||||
|
||||
if b.config.DiskName == "" {
|
||||
b.config.DiskName = "disk"
|
||||
|
|
@ -171,6 +171,14 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
|
|||
}
|
||||
}
|
||||
|
||||
// Determine if DiskSize is able to be allocated, only when running locally
|
||||
if b.config.RemoteType == "" {
|
||||
if err = common.AvailableDisk(uint64(b.config.DiskSize)); err != nil {
|
||||
errs = packer.MultiErrorAppend(errs,
|
||||
fmt.Errorf("Unavailable Resources: %s", err))
|
||||
}
|
||||
}
|
||||
|
||||
// Warnings
|
||||
if b.config.ShutdownCommand == "" {
|
||||
warnings = append(warnings,
|
||||
|
|
|
|||
|
|
@ -63,7 +63,7 @@ func NewConfig(raws ...interface{}) (*Config, []string, error) {
|
|||
errs = packer.MultiErrorAppend(errs, c.ShutdownConfig.Prepare(&c.ctx)...)
|
||||
errs = packer.MultiErrorAppend(errs, c.SSHConfig.Prepare(&c.ctx)...)
|
||||
errs = packer.MultiErrorAppend(errs, c.ToolsConfig.Prepare(&c.ctx)...)
|
||||
errs = packer.MultiErrorAppend(errs, c.VMXConfig.Prepare(&c.ctx)...)
|
||||
errs = packer.MultiErrorAppend(errs, c.VMXConfig.Prepare(&c.ctx, c.RemoteType)...)
|
||||
|
||||
if c.SourcePath == "" {
|
||||
errs = packer.MultiErrorAppend(errs, fmt.Errorf("source_path is blank, but is required"))
|
||||
|
|
|
|||
37
common/resources_linux.go
Normal file
37
common/resources_linux.go
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
package common
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
sigar "github.com/cloudfoundry/gosigar"
|
||||
)
|
||||
|
||||
func AvailableMem(desired uint64) error {
|
||||
free := freeMem()
|
||||
if desired > free {
|
||||
return fmt.Errorf("RAM - Requested - %dMB - Available %dMB", desired, free)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func freeMem() uint64 {
|
||||
mem := sigar.Mem{}
|
||||
mem.Get()
|
||||
return (mem.Free / 1024 / 1024)
|
||||
}
|
||||
|
||||
func AvailableDisk(desired uint64) error {
|
||||
free := freeDisk()
|
||||
if desired > free {
|
||||
return fmt.Errorf("Disk - Requested - %dMB - Available %dMB", desired, free)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func freeDisk() uint64 {
|
||||
disk := sigar.FileSystemUsage{}
|
||||
workingDirectory, _ := os.Getwd()
|
||||
disk.Get(workingDirectory)
|
||||
return (disk.Avail / 1024)
|
||||
}
|
||||
11
common/resources_universal.go
Normal file
11
common/resources_universal.go
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
// +build !linux
|
||||
|
||||
package common
|
||||
|
||||
func AvailableMem(desired uint64) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func AvailableDisk(desired uint64) error {
|
||||
return nil
|
||||
}
|
||||
Loading…
Reference in a new issue