Browse Source

Add xenserver-os-template parameter, to use an arbitrary template instead of boot2docker

any_template_pr
Fábio Kaiser Rauber 6 years ago
parent
commit
6260f87198
  1. 192
      xenserver/xenserver.go

192
xenserver/xenserver.go

@ -30,7 +30,6 @@ import (
const ( const (
isoFilename = "boot2docker.iso" isoFilename = "boot2docker.iso"
tarFilename = "boot2docker.tar" tarFilename = "boot2docker.tar"
osTemplateLabelName = "Other install media"
B2D_USER = "docker" B2D_USER = "docker"
B2D_PASS = "tcuser" B2D_PASS = "tcuser"
) )
@ -38,22 +37,23 @@ const (
type Driver struct { type Driver struct {
*drivers.BaseDriver *drivers.BaseDriver
Server string Server string
Username string Username string
Password string Password string
Boot2DockerURL string Boot2DockerURL string
CPU uint CPU uint
Memory uint Memory uint
DiskSize uint DiskSize uint
SR string SR string
Network string Network string
Host string Host string
ISO string ISO string
TAR string TAR string
UploadTimeout uint UploadTimeout uint
WaitTimeout uint WaitTimeout uint
CaCertPath string CaCertPath string
PrivateKeyPath string PrivateKeyPath string
osTemplateLabelName string
xenAPIClient *XenAPIClient xenAPIClient *XenAPIClient
} }
@ -127,6 +127,13 @@ func (d *Driver) GetCreateFlags() []mcnflag.Flag {
Usage: "XenServer wait VM start timeout(seconds)", Usage: "XenServer wait VM start timeout(seconds)",
Value: 30 * 60, Value: 30 * 60,
}, },
mcnflag.StringFlag{
EnvVar: "XENSERVER_OS_TEMPLATE",
Name: "xenserver-os-template",
Usage: "XenServer OS Template Label Name",
Value: "Other install media",
},
} }
} }
@ -186,6 +193,7 @@ func (d *Driver) SetConfigFromFlags(flags drivers.DriverOptions) error {
d.SwarmDiscovery = flags.String("swarm-discovery") d.SwarmDiscovery = flags.String("swarm-discovery")
d.ISO = d.ResolveStorePath(isoFilename) d.ISO = d.ResolveStorePath(isoFilename)
d.TAR = d.ResolveStorePath(tarFilename) d.TAR = d.ResolveStorePath(tarFilename)
d.osTemplateLabelName = flags.String("xenserver-os-template")
return nil return nil
} }
@ -320,72 +328,80 @@ func (d *Driver) Create() error {
return err return err
} }
log.Infof("Creating ISO VDI...") // Only upload ISO image if using Other install media, otherwise use existing VM template
// Get the SR
var sr *xsclient.SR var sr *xsclient.SR
if d.SR == "" { var isoVdi,diskVdi *xsclient.VDI
sr, err = c.GetDefaultSR() var isoVdiUuid,diskVdiUuid string
if sr.Ref == "OpaqueRef:NULL" {
err := errors.New("No default SR found. Please configure a " + if d.osTemplateLabelName == "Other install media" {
"default or specify the SR explicitly using " +
"--xenserver-sr-label.") log.Infof("Creating ISO VDI...")
log.Errorf("%v", err)
// Get the SR
if d.SR == "" {
sr, err = c.GetDefaultSR()
if sr.Ref == "OpaqueRef:NULL" {
err := errors.New("No default SR found. Please configure a " +
"default or specify the SR explicitly using " +
"--xenserver-sr-label.")
log.Errorf("%v", err)
return err
}
} else {
sr, err = c.GetUniqueSRByNameLabel(d.SR)
}
if err != nil {
return err return err
} }
} else {
sr, err = c.GetUniqueSRByNameLabel(d.SR)
}
if err != nil {
return err
}
isoFileInfo, err := os.Stat(d.ISO) isoFileInfo, err := os.Stat(d.ISO)
if err != nil { if err != nil {
return err return err
} }
// Create the VDI // Create the VDI
isoVdi, err := sr.CreateVdi(isoFilename, isoFileInfo.Size()) isoVdi, err = sr.CreateVdi(isoFilename, isoFileInfo.Size())
if err != nil { if err != nil {
log.Errorf("Unable to create ISO VDI '%s': %v", isoFilename, err) log.Errorf("Unable to create ISO VDI '%s': %v", isoFilename, err)
return err return err
} }
// Import the VDI // Import the VDI
if err = d.importVdi(isoVdi, d.ISO, time.Duration(d.UploadTimeout)*time.Second); err != nil { if err = d.importVdi(isoVdi, d.ISO, time.Duration(d.UploadTimeout)*time.Second); err != nil {
return err return err
} }
isoVdiUuid, err := isoVdi.GetUuid() isoVdiUuid, err = isoVdi.GetUuid()
if err != nil { if err != nil {
return err return err
} }
log.Infof("Creating Disk VDI...") log.Infof("Creating Disk VDI...")
err = d.generateDiskImage() err = d.generateDiskImage()
if err != nil { if err != nil {
return err return err
} }
// Create the VDI // Create the VDI
diskVdi, err := sr.CreateVdi("bootdocker disk", int64(d.DiskSize)*1024*1024) diskVdi, err = sr.CreateVdi("bootdocker disk", int64(d.DiskSize)*1024*1024)
if err != nil { if err != nil {
log.Errorf("Unable to create ISO VDI '%s': %v", "bootdocker disk", err) log.Errorf("Unable to create ISO VDI '%s': %v", "bootdocker disk", err)
return err return err
} }
if err = d.importVdi(diskVdi, d.TAR, time.Duration(d.UploadTimeout)*time.Second); err != nil { if err = d.importVdi(diskVdi, d.TAR, time.Duration(d.UploadTimeout)*time.Second); err != nil {
return err return err
} }
diskVdiUuid, err = diskVdi.GetUuid()
if err != nil {
return err
}
diskVdiUuid, err := diskVdi.GetUuid()
if err != nil {
return err
} }
log.Infof("Creating VM...") log.Infof("Creating VM...")
vm0, err := c.GetUniqueVMByNameLabel(osTemplateLabelName) vm0, err := c.GetUniqueVMByNameLabel(d.osTemplateLabelName)
if err != nil { if err != nil {
return err return err
} }
@ -407,7 +423,7 @@ func (d *Driver) Create() error {
} }
otherConfig := map[string]string{ otherConfig := map[string]string{
"base_template_name": osTemplateLabelName, "base_template_name": d.osTemplateLabelName,
"install-methods": "cdrom,nfs,http,ftp", "install-methods": "cdrom,nfs,http,ftp",
"linux_template": "true", "linux_template": "true",
"mac_seed": vmMacSeed, "mac_seed": vmMacSeed,
@ -463,26 +479,28 @@ func (d *Driver) Create() error {
return err return err
} }
log.Infof("Add ISO VDI to VM...") if d.osTemplateLabelName == "Other install media" {
diskVdi, err = c.GetVdiByUuid(isoVdiUuid) log.Infof("Add ISO VDI to VM...")
if err != nil { diskVdi, err = c.GetVdiByUuid(isoVdiUuid)
return err if err != nil {
} return err
}
err = vm.ConnectVdi(diskVdi, xsclient.Disk, "0") err = vm.ConnectVdi(diskVdi, xsclient.Disk, "0")
if err != nil { if err != nil {
return err return err
} }
log.Infof("Add Disk VDI to VM...") log.Infof("Add Disk VDI to VM...")
diskVdi, err = c.GetVdiByUuid(diskVdiUuid) diskVdi, err = c.GetVdiByUuid(diskVdiUuid)
if err != nil { if err != nil {
return err return err
} }
err = vm.ConnectVdi(diskVdi, xsclient.Disk, "1") err = vm.ConnectVdi(diskVdi, xsclient.Disk, "1")
if err != nil { if err != nil {
return err return err
}
} }
log.Infof("Add Network to VM...") log.Infof("Add Network to VM...")

Loading…
Cancel
Save