Merge branch 'master' into scrape_doc_to_builder_struct_config

This commit is contained in:
Adrien Delorme 2019-06-19 11:21:06 +02:00 committed by GitHub
commit 4c1abf828c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 130 additions and 17 deletions

View File

@ -28,14 +28,14 @@ release: install-build-deps test releasebin package ## Build a release build
bin: install-build-deps ## Build debug/test build
@echo "WARN: 'make bin' is for debug / test builds only. Use 'make release' for release builds."
@GO111MODULE=off sh -c "$(CURDIR)/scripts/build.sh"
@GO111MODULE=auto sh -c "$(CURDIR)/scripts/build.sh"
releasebin: install-build-deps
@grep 'const VersionPrerelease = "dev"' version/version.go > /dev/null ; if [ $$? -eq 0 ]; then \
echo "ERROR: You must remove prerelease tags from version/version.go prior to release."; \
exit 1; \
fi
@GO111MODULE=off sh -c "$(CURDIR)/scripts/build.sh"
@GO111MODULE=auto sh -c "$(CURDIR)/scripts/build.sh"
package:
$(if $(VERSION),,@echo 'VERSION= needed to release; Use make package skip compilation'; exit 1)
@ -118,7 +118,7 @@ testrace: mode-check vet ## Test with race detection enabled
@GO111MODULE=off go test -race $(TEST) $(TESTARGS) -timeout=3m -p=8
check-vendor-vs-mod: ## Check that go modules and vendored code are on par
@GO111MODULE=on go mod vendor
@GO111MODULE=on go mod vendor
@git diff --exit-code --ignore-space-change --ignore-space-at-eol -- vendor ; if [ $$? -eq 1 ]; then \
echo "ERROR: vendor dir is not on par with go modules definition." && \
exit 1; \

View File

@ -73,6 +73,8 @@ type Driver interface {
DeleteVirtualSwitch(string) error
CheckVMName(string) error
CreateVirtualMachine(string, string, string, int64, int64, int64, string, uint, bool, bool, string) error
AddVirtualMachineHardDrive(string, string, string, int64, int64, string) error

View File

@ -110,6 +110,9 @@ type DriverMock struct {
DeleteVirtualSwitch_SwitchName string
DeleteVirtualSwitch_Err error
CheckVMName_Called bool
CheckVMName_Err error
CreateVirtualSwitch_Called bool
CreateVirtualSwitch_SwitchName string
CreateVirtualSwitch_SwitchType string
@ -421,6 +424,11 @@ func (d *DriverMock) AddVirtualMachineHardDrive(vmName string, vhdFile string, v
return d.AddVirtualMachineHardDrive_Err
}
func (d *DriverMock) CheckVMName(vmName string) error {
d.CheckVMName_Called = true
return d.CheckVMName_Err
}
func (d *DriverMock) CreateVirtualMachine(vmName string, path string, harddrivePath string,
ram int64, diskSize int64, diskBlockSize int64, switchName string, generation uint,
diffDisks bool, fixedVHD bool, version string) error {

View File

@ -186,6 +186,10 @@ func (d *HypervPS4Driver) AddVirtualMachineHardDrive(vmName string, vhdFile stri
diskBlockSize, controllerType)
}
func (d *HypervPS4Driver) CheckVMName(vmName string) error {
return hyperv.CheckVMName(vmName)
}
func (d *HypervPS4Driver) CreateVirtualMachine(vmName string, path string, harddrivePath string, ram int64,
diskSize int64, diskBlockSize int64, switchName string, generation uint, diffDisks bool,
fixedVHD bool, version string) error {

View File

@ -48,6 +48,14 @@ func (s *StepCreateVM) Run(ctx context.Context, state multistep.StateBag) multis
path = v.(string)
}
err := driver.CheckVMName(s.VMName)
if err != nil {
s.KeepRegistered = true
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
// Determine if we even have an existing virtual harddrive to attach
harddrivePath := ""
if harddrivePathRaw, ok := state.GetOk("iso_path"); ok {
@ -66,7 +74,7 @@ func (s *StepCreateVM) Run(ctx context.Context, state multistep.StateBag) multis
diskSize := int64(s.DiskSize) * 1024 * 1024
diskBlockSize := int64(s.DiskBlockSize) * 1024 * 1024
err := driver.CreateVirtualMachine(s.VMName, path, harddrivePath, ramSize, diskSize, diskBlockSize,
err = driver.CreateVirtualMachine(s.VMName, path, harddrivePath, ramSize, diskSize, diskBlockSize,
s.SwitchName, s.Generation, s.DifferencingDisk, s.FixedVHD, s.Version)
if err != nil {
err := fmt.Errorf("Error creating virtual machine: %s", err)

View File

@ -0,0 +1,58 @@
package common
import (
"context"
"fmt"
"testing"
"github.com/hashicorp/packer/helper/multistep"
)
func TestStepCreateVM_impl(t *testing.T) {
var _ multistep.Step = new(StepCreateVM)
}
func TestStepCreateVM(t *testing.T) {
state := testState(t)
step := new(StepCreateVM)
step.VMName = "test-VM-Name"
driver := state.Get("driver").(*DriverMock)
// Test the run
if action := step.Run(context.Background(), state); action != multistep.ActionContinue {
t.Fatalf("Bad action: %v", action)
}
if _, ok := state.GetOk("error"); ok {
t.Fatal("Should NOT have error")
}
// Test the driver
if !driver.CheckVMName_Called {
t.Fatal("Should have called CheckVMName")
}
}
func TestStepCreateVM_CheckVMNameErr(t *testing.T) {
state := testState(t)
step := new(StepCreateVM)
step.VMName = "test-VM-Name"
driver := state.Get("driver").(*DriverMock)
driver.CheckVMName_Err = fmt.Errorf("A virtual machine with the name is already" +
" defined in Hyper-V. To avoid a name collision, please set your " +
"vm_name to a unique value")
// Test the run
if action := step.Run(context.Background(), state); action != multistep.ActionHalt {
t.Fatalf("Bad action: %v", action)
}
if _, ok := state.GetOk("error"); !ok {
t.Fatal("Should have error")
}
// Test the driver
if !driver.CheckVMName_Called {
t.Fatal("Should have called CheckVMName")
}
}

View File

@ -21,9 +21,9 @@ func (s *stepStartVM) Run(ctx context.Context, state multistep.StateBag) multist
client := state.Get("proxmoxClient").(*proxmox.Client)
c := state.Get("config").(*Config)
agent := "1"
agent := 1
if c.Agent == false {
agent = "0"
agent = 0
}
ui.Say("Creating VM")

View File

@ -283,10 +283,23 @@ Hyper-V\New-VM -Name "{{ .VMName }}" -Path "{{ .Path }}" -MemoryStartupBytes {{
return final, nil
}
func CheckVMName(vmName string) error {
// Check that no vm with the same name is registered, to prevent
// namespace collisions
var gs powershell.PowerShellCmd
getVMCmd := fmt.Sprintf(`Hyper-V\Get-VM -Name "%s"`, vmName)
if err := gs.Run(getVMCmd); err == nil {
return fmt.Errorf("A virtual machine with the name %s is already"+
" defined in Hyper-V. To avoid a name collision, please set your "+
"vm_name to a unique value", vmName)
}
return nil
}
func CreateVirtualMachine(vmName string, path string, harddrivePath string, ram int64,
diskSize int64, diskBlockSize int64, switchName string, generation uint,
diffDisks bool, fixedVHD bool, version string) error {
opts := scriptOptions{
Version: version,
VMName: vmName,

3
go.mod
View File

@ -9,7 +9,7 @@ require (
github.com/ChrisTrenkamp/goxpath v0.0.0-20170625215350-4fe035839290
github.com/NaverCloudPlatform/ncloud-sdk-go v0.0.0-20180110055012-c2e73f942591
github.com/PuerkitoBio/goquery v1.5.0 // indirect
github.com/Telmate/proxmox-api-go v0.0.0-20190410200643-f08824d5082d
github.com/Telmate/proxmox-api-go v0.0.0-20190614181158-26cd147831a4
github.com/abdullin/seq v0.0.0-20160510034733-d5467c17e7af // indirect
github.com/aliyun/alibaba-cloud-sdk-go v0.0.0-20190418113227-25233c783f4e
github.com/aliyun/aliyun-oss-go-sdk v0.0.0-20170113022742-e6dbea820a9f
@ -84,6 +84,7 @@ require (
github.com/mitchellh/go-fs v0.0.0-20180402234041-7b48fa161ea7
github.com/mitchellh/go-homedir v1.0.0
github.com/mitchellh/go-vnc v0.0.0-20150629162542-723ed9867aed
github.com/mitchellh/gox v1.0.1 // indirect
github.com/mitchellh/iochan v1.0.0
github.com/mitchellh/mapstructure v0.0.0-20180111000720-b4575eea38cc
github.com/mitchellh/panicwrap v0.0.0-20170106182340-fce601fe5557

5
go.sum
View File

@ -27,6 +27,8 @@ github.com/PuerkitoBio/goquery v1.5.0 h1:uGvmFXOA73IKluu/F84Xd1tt/z07GYm8X49XKHP
github.com/PuerkitoBio/goquery v1.5.0/go.mod h1:qD2PgZ9lccMbQlc7eEOjaeRlFQON7xY8kdmcsrnKqMg=
github.com/Telmate/proxmox-api-go v0.0.0-20190410200643-f08824d5082d h1:igrCnHheXb+lZ1bW9Ths8JZZIjh9D4Vi/49JqiHE+cI=
github.com/Telmate/proxmox-api-go v0.0.0-20190410200643-f08824d5082d/go.mod h1:OGWyIMJ87/k/GCz8CGiWB2HOXsOVDM6Lpe/nFPkC4IQ=
github.com/Telmate/proxmox-api-go v0.0.0-20190614181158-26cd147831a4 h1:o//09WenT9BNcQypCYfOBfRe5gtLUvUfTPq0xQqPMEI=
github.com/Telmate/proxmox-api-go v0.0.0-20190614181158-26cd147831a4/go.mod h1:OGWyIMJ87/k/GCz8CGiWB2HOXsOVDM6Lpe/nFPkC4IQ=
github.com/abdullin/seq v0.0.0-20160510034733-d5467c17e7af h1:DBNMBMuMiWYu0b+8KMJuWmfCkcxl09JwdlqwDZZ6U14=
github.com/abdullin/seq v0.0.0-20160510034733-d5467c17e7af/go.mod h1:5Jv4cbFiHJMsVxt52+i0Ha45fjshj6wxYr1r19tB9bw=
github.com/aliyun/alibaba-cloud-sdk-go v0.0.0-20190418113227-25233c783f4e h1:/8wOj52pewmIX/8d5eVO3t7Rr3astkBI/ruyg4WNqRo=
@ -187,6 +189,7 @@ github.com/hashicorp/go-syslog v1.0.0/go.mod h1:qPfqrKkXGihmCqbJM2mZgkZGvKG1dFdv
github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
github.com/hashicorp/go-uuid v1.0.1 h1:fv1ep09latC32wFoVwnqcnKJGnMSdBanPczbHAYm1BE=
github.com/hashicorp/go-uuid v1.0.1/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
github.com/hashicorp/go-version v1.0.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/hashicorp/go-version v1.1.0 h1:bPIoEKD27tNdebFGGxxYwcL4nepeY4j1QP23PFRGzg0=
github.com/hashicorp/go-version v1.1.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/hashicorp/go-version v1.2.0 h1:3vNe/fWF5CBgRIguda1meWhsZHy3m8gCJ5wx+dIzX/E=
@ -280,6 +283,8 @@ github.com/mitchellh/go-testing-interface v1.0.0/go.mod h1:kRemZodwjscx+RGhAo8eI
github.com/mitchellh/go-vnc v0.0.0-20150629162542-723ed9867aed h1:FI2NIv6fpef6BQl2u3IZX/Cj20tfypRF4yd+uaHOMtI=
github.com/mitchellh/go-vnc v0.0.0-20150629162542-723ed9867aed/go.mod h1:3rdaFaCv4AyBgu5ALFM0+tSuHrBh6v692nyQe3ikrq0=
github.com/mitchellh/gox v0.4.0/go.mod h1:Sd9lOJ0+aimLBi73mGofS1ycjY8lL3uZM3JPS42BGNg=
github.com/mitchellh/gox v1.0.1 h1:x0jD3dcHk9a9xPSDN6YEL4xL6Qz0dvNYm8yZqui5chI=
github.com/mitchellh/gox v1.0.1/go.mod h1:ED6BioOGXMswlXa2zxfh/xdd5QhwYliBFn9V18Ap4z4=
github.com/mitchellh/iochan v1.0.0 h1:C+X3KsSTLFVBr/tK1eYN/vs4rJcvsiLU338UhYPJWeY=
github.com/mitchellh/iochan v1.0.0/go.mod h1:JwYml1nuB7xOzsp52dPpHFffvOCDupsG0QubkSMEySY=
github.com/mitchellh/mapstructure v0.0.0-20160808181253-ca63d7c062ee/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=

View File

@ -144,7 +144,7 @@ IFS="${OLDIFS}"
# Copy our OS/Arch to the bin/ directory
echo "==> Copying binaries for this platform..."
DEV_PLATFORM="./pkg/${XC_OS}_${XC_ARCH}"
DEV_PLATFORM="./pkg/$(go env GOOS)_$(go env GOARCH)"
for F in $(find ${DEV_PLATFORM} -mindepth 1 -maxdepth 1 -type f); do
cp -v ${F} bin/
cp -v ${F} "${MAIN_GOPATH}/bin/"

View File

@ -497,6 +497,8 @@ func (c *Client) GetNextID(currentID int) (nextID int, err error) {
}
}
nextID, err = strconv.Atoi(data["data"].(string))
} else if strings.HasPrefix(err.Error(), "400 ") {
return c.GetNextID(currentID + 1)
}
return
}

View File

@ -26,7 +26,7 @@ type ConfigQemu struct {
Name string `json:"name"`
Description string `json:"desc"`
Onboot bool `json:"onboot"`
Agent string `json:"agent"`
Agent int `json:"agent"`
Memory int `json:"memory"`
QemuOs string `json:"os"`
QemuCores int `json:"cores"`
@ -251,9 +251,16 @@ func NewConfigQemuFromApi(vmr *VmRef, client *Client) (config *ConfigQemu, err e
if _, isSet := vmConfig["onboot"]; isSet {
onboot = Itob(int(vmConfig["onboot"].(float64)))
}
agent := "1"
agent := 0
if _, isSet := vmConfig["agent"]; isSet {
agent = vmConfig["agent"].(string)
switch vmConfig["agent"].(type) {
case float64:
agent = int(vmConfig["agent"].(float64))
case string:
agent, _ = strconv.Atoi(vmConfig["agent"].(string))
}
}
ostype := "other"
if _, isSet := vmConfig["ostype"]; isSet {
@ -299,6 +306,9 @@ func NewConfigQemuFromApi(vmr *VmRef, client *Client) (config *ConfigQemu, err e
if _, isSet := vmConfig["searchdomain"]; isSet {
config.Searchdomain = vmConfig["searchdomain"].(string)
}
if _, isSet := vmConfig["nameserver"]; isSet {
config.Nameserver = vmConfig["nameserver"].(string)
}
if _, isSet := vmConfig["sshkeys"]; isSet {
config.Sshkeys, _ = url.PathUnescape(vmConfig["sshkeys"].(string))
}
@ -330,6 +340,7 @@ func NewConfigQemuFromApi(vmr *VmRef, client *Client) (config *ConfigQemu, err e
//
diskConfMap := QemuDevice{
"id": diskID,
"type": diskType,
"storage": storageName,
"file": fileName,
@ -345,11 +356,10 @@ func NewConfigQemuFromApi(vmr *VmRef, client *Client) (config *ConfigQemu, err e
}
// Add networks.
nicNameRe := regexp.MustCompile(`net\d+`)
nicNames := []string{}
for k, _ := range vmConfig {
if nicName := nicNameRe.FindStringSubmatch(k); len(nicName) > 0 {
if nicName := rxNicName.FindStringSubmatch(k); len(nicName) > 0 {
nicNames = append(nicNames, nicName[0])
}
}
@ -358,13 +368,13 @@ func NewConfigQemuFromApi(vmr *VmRef, client *Client) (config *ConfigQemu, err e
nicConfStr := vmConfig[nicName]
nicConfList := strings.Split(nicConfStr.(string), ",")
//
id := rxDeviceID.FindStringSubmatch(nicName)
nicID, _ := strconv.Atoi(id[0])
model, macaddr := ParseSubConf(nicConfList[0], "=")
// Add model and MAC address.
nicConfMap := QemuDevice{
"id": nicID,
"model": model,
"macaddr": macaddr,
}
@ -441,7 +451,7 @@ func RemoveSshForwardUsernet(vmr *VmRef, client *Client) (err error) {
func MaxVmId(client *Client) (max int, err error) {
resp, err := client.GetVmList()
vms := resp["data"].([]interface{})
max = 0
max = 100
for vmii := range vms {
vm := vms[vmii].(map[string]interface{})
vmid := int(vm["vmid"].(float64))
@ -524,6 +534,7 @@ func (c ConfigQemu) CreateQemuNetworksParams(vmID int, params map[string]interfa
// For backward compatibility.
if len(c.QemuNetworks) == 0 && len(c.QemuNicModel) > 0 {
deprecatedStyleMap := QemuDevice{
"id": 0,
"model": c.QemuNicModel,
"bridge": c.QemuBrige,
"macaddr": c.QemuMacAddr,
@ -602,6 +613,7 @@ func (c ConfigQemu) CreateQemuDisksParams(
}
}
deprecatedStyleMap := QemuDevice{
"id": 0,
"type": dType,
"storage": c.Storage,
"size": c.DiskSize,

2
vendor/modules.txt vendored
View File

@ -51,7 +51,7 @@ github.com/NaverCloudPlatform/ncloud-sdk-go/request
github.com/NaverCloudPlatform/ncloud-sdk-go/oauth
# github.com/PuerkitoBio/goquery v1.5.0
github.com/PuerkitoBio/goquery
# github.com/Telmate/proxmox-api-go v0.0.0-20190410200643-f08824d5082d
# github.com/Telmate/proxmox-api-go v0.0.0-20190614181158-26cd147831a4
github.com/Telmate/proxmox-api-go/proxmox
# github.com/aliyun/alibaba-cloud-sdk-go v0.0.0-20190418113227-25233c783f4e
github.com/aliyun/alibaba-cloud-sdk-go/sdk/errors