Fix inspections

This commit is contained in:
Vladislav Rassokhin 2019-07-08 17:48:37 +03:00
parent 1147536646
commit 94a036038b
10 changed files with 42 additions and 43 deletions

View File

@ -57,7 +57,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
SetOrder: false,
},
&common.StepWaitForIp{
&b.config.WaitIpConfig,
Config: &b.config.WaitIpConfig,
},
&communicator.StepConnect{
Config: &b.config.Comm,

View File

@ -41,14 +41,14 @@ func (s *StepCloneVM) Run(ctx context.Context, state multistep.StateBag) multist
ui := state.Get("ui").(packer.Ui)
d := state.Get("driver").(*driver.Driver)
find_vm, err := d.FindVM(s.Location.VMName)
vm, err := d.FindVM(s.Location.VMName)
if s.Force == false && err == nil {
state.Put("error", fmt.Errorf("%s already exists, you can use -force flag to destroy it: %v", s.Location.VMName, err))
return multistep.ActionHalt
} else if s.Force == true && err == nil {
ui.Say(fmt.Sprintf("the vm/template %s already exists, but deleting it due to -force flag", s.Location.VMName))
err := find_vm.Destroy()
err := vm.Destroy()
if err != nil {
state.Put("error", fmt.Errorf("error destroying %s: %v", s.Location.VMName, err))
}
@ -61,7 +61,7 @@ func (s *StepCloneVM) Run(ctx context.Context, state multistep.StateBag) multist
return multistep.ActionHalt
}
vm, err := template.Clone(ctx, &driver.CloneConfig{
vm, err = template.Clone(ctx, &driver.CloneConfig{
Name: s.Location.VMName,
Folder: s.Location.Folder,
Cluster: s.Location.Cluster,

View File

@ -8,6 +8,9 @@ func main() {
if err != nil {
panic(err)
}
server.RegisterBuilder(new(clone.Builder))
err = server.RegisterBuilder(new(clone.Builder))
if err != nil {
panic(err)
}
server.Serve()
}

View File

@ -8,6 +8,9 @@ func main() {
if err != nil {
panic(err)
}
server.RegisterBuilder(new(iso.Builder))
err = server.RegisterBuilder(new(iso.Builder))
if err != nil {
panic(err)
}
server.Serve()
}

View File

@ -39,6 +39,9 @@ func (d *Driver) FindDatastore(name string, host string) (*Datastore, error) {
ds := d.NewDatastore(&i.Datastore[0])
inf, err := ds.Info("name")
if err != nil {
return nil, err
}
name = inf.Name
}

View File

@ -31,14 +31,14 @@ type ConnectConfig struct {
func NewDriver(config *ConnectConfig) (*Driver, error) {
ctx := context.TODO()
vcenter_url, err := url.Parse(fmt.Sprintf("https://%v/sdk", config.VCenterServer))
vcenterUrl, err := url.Parse(fmt.Sprintf("https://%v/sdk", config.VCenterServer))
if err != nil {
return nil, err
}
credentials := url.UserPassword(config.Username, config.Password)
vcenter_url.User = credentials
vcenterUrl.User = credentials
soapClient := soap.NewClient(vcenter_url, config.InsecureConnection)
soapClient := soap.NewClient(vcenterUrl, config.InsecureConnection)
vimClient, err := vim25.NewClient(ctx, soapClient)
if err != nil {
return nil, err

View File

@ -183,22 +183,22 @@ func (vm *VirtualMachine) Devices() (object.VirtualDeviceList, error) {
return vmInfo.Config.Hardware.Device, nil
}
func (template *VirtualMachine) Clone(ctx context.Context, config *CloneConfig) (*VirtualMachine, error) {
folder, err := template.driver.FindFolder(config.Folder)
func (vm *VirtualMachine) Clone(ctx context.Context, config *CloneConfig) (*VirtualMachine, error) {
folder, err := vm.driver.FindFolder(config.Folder)
if err != nil {
return nil, err
}
var relocateSpec types.VirtualMachineRelocateSpec
pool, err := template.driver.FindResourcePool(config.Cluster, config.Host, config.ResourcePool)
pool, err := vm.driver.FindResourcePool(config.Cluster, config.Host, config.ResourcePool)
if err != nil {
return nil, err
}
poolRef := pool.pool.Reference()
relocateSpec.Pool = &poolRef
datastore, err := template.driver.FindDatastore(config.Datastore, config.Host)
datastore, err := vm.driver.FindDatastore(config.Datastore, config.Host)
if err != nil {
return nil, err
}
@ -212,7 +212,7 @@ func (template *VirtualMachine) Clone(ctx context.Context, config *CloneConfig)
if config.LinkedClone == true {
cloneSpec.Location.DiskMoveType = "createNewChildDiskBacking"
tpl, err := template.Info("snapshot")
tpl, err := vm.Info("snapshot")
if err != nil {
return nil, err
}
@ -231,7 +231,7 @@ func (template *VirtualMachine) Clone(ctx context.Context, config *CloneConfig)
}
if config.Network != "" {
net, err := template.driver.FindNetwork(config.Network)
net, err := vm.driver.FindNetwork(config.Network)
if err != nil {
return nil, err
}
@ -240,7 +240,7 @@ func (template *VirtualMachine) Clone(ctx context.Context, config *CloneConfig)
return nil, err
}
devices, err := template.vm.Device(ctx)
devices, err := vm.vm.Device(ctx)
if err != nil {
return nil, err
}
@ -260,7 +260,7 @@ func (template *VirtualMachine) Clone(ctx context.Context, config *CloneConfig)
configSpec.DeviceChange = append(configSpec.DeviceChange, config)
}
task, err := template.vm.Clone(template.driver.ctx, folder.folder, config.Name, cloneSpec)
task, err := vm.vm.Clone(vm.driver.ctx, folder.folder, config.Name, cloneSpec)
if err != nil {
return nil, err
}
@ -276,8 +276,8 @@ func (template *VirtualMachine) Clone(ctx context.Context, config *CloneConfig)
}
vmRef := info.Result.(types.ManagedObjectReference)
vm := template.driver.NewVM(&vmRef)
return vm, nil
created := vm.driver.NewVM(&vmRef)
return created, nil
}
func (vm *VirtualMachine) Destroy() error {
@ -545,16 +545,6 @@ func addNetwork(d *Driver, devices object.VirtualDeviceList, config *CreateConfi
return append(devices, device), nil
}
func addIDE(devices object.VirtualDeviceList) (object.VirtualDeviceList, error) {
ideDevice, err := devices.CreateIDEController()
if err != nil {
return nil, err
}
devices = append(devices, ideDevice)
return devices, nil
}
func (vm *VirtualMachine) AddCdrom(controllerType string, isoPath string) error {
devices, err := vm.vm.Device(vm.driver.ctx)
if err != nil {

View File

@ -86,8 +86,8 @@ func (s *StepAddFloppy) Cleanup(state multistep.StateBag) {
return
}
err_del := ds.Delete(UploadedFloppyPath.(string))
if err_del != nil {
err = ds.Delete(UploadedFloppyPath.(string))
if err != nil {
state.Put("error", err)
return
}

View File

@ -53,21 +53,21 @@ func (s *StepCreateVM) Run(_ context.Context, state multistep.StateBag) multiste
ui := state.Get("ui").(packer.Ui)
d := state.Get("driver").(*driver.Driver)
find_vm, err := d.FindVM(s.Location.VMName)
vm, err := d.FindVM(s.Location.VMName)
if s.Force == false && err == nil {
state.Put("error", fmt.Errorf("%s already exists, you can use -force flag to destroy it: %v", s.Location.VMName, err))
return multistep.ActionHalt
} else if s.Force == true && err == nil {
ui.Say(fmt.Sprintf("the vm/template %s already exists, but deleting it due to -force flag", s.Location.VMName))
err := find_vm.Destroy()
err := vm.Destroy()
if err != nil {
state.Put("error", fmt.Errorf("error destroying %s: %v", s.Location.VMName, err))
}
}
ui.Say("Creating VM...")
vm, err := d.CreateVM(&driver.CreateConfig{
vm, err = d.CreateVM(&driver.CreateConfig{
DiskThinProvisioned: s.Config.DiskThinProvisioned,
DiskControllerType: s.Config.DiskControllerType,
DiskSize: s.Config.DiskSize,

View File

@ -27,28 +27,28 @@ func (s *StepRemoteUpload) Run(_ context.Context, state multistep.StateBag) mult
return multistep.ActionHalt
}
remotepath := fmt.Sprintf("packer_cache/%s", filename)
remotedirectory := fmt.Sprintf("[%s] packer_cache/", ds.Name())
final_remotepath := fmt.Sprintf("%s/%s", remotedirectory, filename)
remotePath := fmt.Sprintf("packer_cache/%s", filename)
remoteDirectory := fmt.Sprintf("[%s] packer_cache/", ds.Name())
fullRemotePath := fmt.Sprintf("%s/%s", remoteDirectory, filename)
ui.Say(fmt.Sprintf("Uploading %s to %s", filename, remotepath))
ui.Say(fmt.Sprintf("Uploading %s to %s", filename, remotePath))
if exists := ds.FileExists(remotepath); exists == true {
if exists := ds.FileExists(remotePath); exists == true {
ui.Say("File already upload")
state.Put("iso_remote_path", final_remotepath)
state.Put("iso_remote_path", fullRemotePath)
return multistep.ActionContinue
}
if err := ds.MakeDirectory(remotedirectory); err != nil {
if err := ds.MakeDirectory(remoteDirectory); err != nil {
state.Put("error", err)
return multistep.ActionHalt
}
if err := ds.UploadFile(path.(string), remotepath, s.Host); err != nil {
if err := ds.UploadFile(path.(string), remotePath, s.Host); err != nil {
state.Put("error", err)
return multistep.ActionHalt
}
state.Put("iso_remote_path", final_remotepath)
state.Put("iso_remote_path", fullRemotePath)
}
return multistep.ActionContinue