Detect datastore automatically (#69)

This commit is contained in:
Michael Kuzmin 2018-03-21 02:03:47 +03:00 committed by GitHub
parent 40ec9efdfd
commit 707039eb26
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 57 additions and 12 deletions

View File

@ -27,7 +27,6 @@ func defaultConfig() map[string]interface{} {
"template": "alpine",
"host": "esxi-1.vsphere65.test",
"datastore": "datastore1",
"ssh_username": "root",
"ssh_password": "jetbrains",

View File

@ -5,6 +5,7 @@ import (
"github.com/vmware/govmomi/vim25/types"
"github.com/vmware/govmomi/vim25/mo"
"github.com/vmware/govmomi/vim25/soap"
"fmt"
)
type Datastore struct {
@ -19,12 +20,33 @@ func (d *Driver) NewDatastore(ref *types.ManagedObjectReference) *Datastore {
}
}
// If name is an empty string, returns the default datastore (is exists)
func (d *Driver) FindDatastore(name string) (*Datastore, error) {
ds, err := d.finder.DatastoreOrDefault(d.ctx, name)
// If name is an empty string, then resolve host's one
func (d *Driver) FindDatastore(name string, host string) (*Datastore, error) {
if name == "" {
h, err := d.FindHost(host)
if err != nil {
return nil, err
}
i, err := h.Info("datastore")
if err != nil {
return nil, err
}
if len(i.Datastore) > 1 {
return nil, fmt.Errorf("Host has multiple datastores. Specify it explicitly")
}
ds := d.NewDatastore(&i.Datastore[0])
inf, err := ds.Info("name")
name = inf.Name
}
ds, err := d.finder.Datastore(d.ctx, name)
if err != nil {
return nil, err
}
return &Datastore{
ds: ds,
driver: d,

View File

@ -8,7 +8,7 @@ func TestDatastoreAcc(t *testing.T) {
initDriverAcceptanceTest(t)
d := newTestDriver(t)
ds, err := d.FindDatastore("datastore1")
ds, err := d.FindDatastore("datastore1", "")
if err != nil {
t.Fatalf("Cannot find the default datastore '%v': %v", "datastore1", err)
}

View File

@ -96,7 +96,7 @@ func (d *Driver) CreateVM(config *CreateConfig) (*VirtualMachine, error) {
host = h.host
}
datastore, err := d.FindDatastore(config.Datastore)
datastore, err := d.FindDatastore(config.Datastore, config.Host)
if err != nil {
return nil, err
}
@ -186,7 +186,7 @@ func (template *VirtualMachine) Clone(config *CloneConfig) (*VirtualMachine, err
poolRef := pool.pool.Reference()
relocateSpec.Pool = &poolRef
datastore, err := template.driver.FindDatastore(config.Datastore)
datastore, err := template.driver.FindDatastore(config.Datastore, config.Host)
if err != nil {
return nil, err
}

View File

@ -29,7 +29,6 @@ func TestVMAcc_clone(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
tc.config.Host = TestHostName
tc.config.Datastore = "datastore1"
tc.config.Name = newVMName()
templateName := "alpine"

View File

@ -19,7 +19,6 @@ func TestVMAcc_create(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
tc.config.Host = TestHostName
tc.config.Datastore = "datastore1"
tc.config.Name = newVMName()
d := newTestDriver(t)

25
examples/driver/main.go Normal file
View File

@ -0,0 +1,25 @@
package main
import (
"github.com/jetbrains-infra/packer-builder-vsphere/driver"
"fmt"
)
func main() {
d, err := driver.NewDriver(&driver.ConnectConfig{
VCenterServer: "vcenter.vsphere65.test",
Username: "root",
Password: "jetbrains",
InsecureConnection: true,
})
if err != nil {
panic(err)
}
ds, err := d.FindDatastore("", "esxi-1.vsphere65.test")
if err != nil {
panic(err)
}
fmt.Println(ds.Name())
}

View File

@ -49,6 +49,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
&StepAddFloppy{
Config: &b.config.FloppyConfig,
Datastore: b.config.Datastore,
Host: b.config.Host,
},
&StepConfigParams{
Config: &b.config.ConfigParamsConfig,

View File

@ -27,7 +27,6 @@ func defaultConfig() map[string]interface{} {
"insecure_connection": true,
"host": "esxi-1.vsphere65.test",
"datastore": "datastore1",
"ssh_username": "root",
"ssh_password": "jetbrains",

View File

@ -29,6 +29,7 @@ func (c *FloppyConfig) Prepare() []error {
type StepAddFloppy struct {
Config *FloppyConfig
Datastore string
Host string
uploadedFloppyPath string
}
@ -51,7 +52,7 @@ func (s *StepAddFloppy) runImpl(state multistep.StateBag) error {
if tmpFloppy != nil {
ui.Say("Uploading created floppy image")
ds, err := d.FindDatastore(s.Datastore)
ds, err := d.FindDatastore(s.Datastore, s.Host)
if err != nil {
return err
}
@ -102,7 +103,7 @@ func (s *StepAddFloppy) Cleanup(state multistep.StateBag) {
}
if s.uploadedFloppyPath != "" {
ds, err := d.FindDatastore(s.Datastore)
ds, err := d.FindDatastore(s.Datastore, s.Host)
if err != nil {
ui.Error(err.Error())
return