2013-12-24 00:58:41 -05:00
|
|
|
package iso
|
2013-09-19 20:07:04 -04:00
|
|
|
|
|
|
|
import (
|
2013-11-12 15:49:57 -05:00
|
|
|
"bufio"
|
2013-09-19 20:07:04 -04:00
|
|
|
"bytes"
|
2014-04-26 15:25:18 -04:00
|
|
|
gossh "code.google.com/p/go.crypto/ssh"
|
2013-09-19 20:07:04 -04:00
|
|
|
"encoding/csv"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
"github.com/mitchellh/packer/communicator/ssh"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2013-11-08 00:18:25 -05:00
|
|
|
// ESX5 driver talks to an ESXi5 hypervisor remotely over SSH to build
|
|
|
|
// virtual machines. This driver can only manage one machine at a time.
|
2013-09-19 20:07:04 -04:00
|
|
|
type ESX5Driver struct {
|
2013-11-07 15:00:27 -05:00
|
|
|
Host string
|
|
|
|
Port uint
|
|
|
|
Username string
|
|
|
|
Password string
|
|
|
|
Datastore string
|
|
|
|
|
2013-11-08 00:18:25 -05:00
|
|
|
comm packer.Communicator
|
|
|
|
outputDir string
|
2014-04-21 11:25:32 -04:00
|
|
|
vmId string
|
2013-09-19 20:07:04 -04:00
|
|
|
}
|
|
|
|
|
2013-12-26 17:03:12 -05:00
|
|
|
func (d *ESX5Driver) Clone(dst, src string) error {
|
|
|
|
return errors.New("Cloning is not supported with the ESX driver.")
|
|
|
|
}
|
|
|
|
|
2013-09-19 20:07:04 -04:00
|
|
|
func (d *ESX5Driver) CompactDisk(diskPathLocal string) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *ESX5Driver) CreateDisk(diskPathLocal string, size string, typeId string) error {
|
|
|
|
diskPath := d.datastorePath(diskPathLocal)
|
|
|
|
return d.sh("vmkfstools", "-c", size, "-d", typeId, "-a", "lsilogic", diskPath)
|
|
|
|
}
|
|
|
|
|
2014-04-21 11:25:32 -04:00
|
|
|
func (d *ESX5Driver) IsRunning(string) (bool, error) {
|
|
|
|
state, err := d.run(nil, "vim-cmd", "vmsvc/power.getstate", d.vmId)
|
2013-09-19 20:07:04 -04:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return strings.Contains(state, "Powered on"), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *ESX5Driver) Start(vmxPathLocal string, headless bool) error {
|
2014-04-21 11:25:32 -04:00
|
|
|
return d.sh("vim-cmd", "vmsvc/power.on", d.vmId)
|
2013-09-19 20:07:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *ESX5Driver) Stop(vmxPathLocal string) error {
|
2014-04-21 11:25:32 -04:00
|
|
|
return d.sh("vim-cmd", "vmsvc/power.off", d.vmId)
|
2013-09-19 20:07:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *ESX5Driver) Register(vmxPathLocal string) error {
|
2013-11-08 00:18:25 -05:00
|
|
|
vmxPath := filepath.Join(d.outputDir, filepath.Base(vmxPathLocal))
|
2013-11-07 23:47:15 -05:00
|
|
|
if err := d.upload(vmxPath, vmxPathLocal); err != nil {
|
2013-09-19 20:07:04 -04:00
|
|
|
return err
|
|
|
|
}
|
2014-04-21 11:25:32 -04:00
|
|
|
r, err := d.run(nil, "vim-cmd", "solo/registervm", vmxPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
d.vmId = strings.TrimRight(r, "\n")
|
|
|
|
return nil
|
2013-09-19 20:07:04 -04:00
|
|
|
}
|
|
|
|
|
2013-11-08 15:19:09 -05:00
|
|
|
func (d *ESX5Driver) SuppressMessages(vmxPath string) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-09-19 20:07:04 -04:00
|
|
|
func (d *ESX5Driver) Unregister(vmxPathLocal string) error {
|
2014-04-21 11:25:32 -04:00
|
|
|
return d.sh("vim-cmd", "vmsvc/unregister", d.vmId)
|
2013-09-19 20:07:04 -04:00
|
|
|
}
|
|
|
|
|
2013-11-07 15:28:41 -05:00
|
|
|
func (d *ESX5Driver) UploadISO(localPath string) (string, error) {
|
|
|
|
cacheRoot, _ := filepath.Abs(".")
|
|
|
|
targetFile, err := filepath.Rel(cacheRoot, localPath)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2013-11-11 18:51:30 -05:00
|
|
|
finalPath := d.datastorePath(targetFile)
|
|
|
|
if err := d.mkdir(filepath.Dir(finalPath)); err != nil {
|
2013-11-07 15:28:41 -05:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := d.upload(finalPath, localPath); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return finalPath, nil
|
|
|
|
}
|
|
|
|
|
2013-09-19 20:07:04 -04:00
|
|
|
func (d *ESX5Driver) ToolsIsoPath(string) string {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2014-04-21 11:25:32 -04:00
|
|
|
func (d *ESX5Driver) ToolsInstall() error {
|
|
|
|
return d.sh("vim-cmd", "vmsvc/tools.install", d.vmId)
|
|
|
|
}
|
|
|
|
|
2013-09-19 20:07:04 -04:00
|
|
|
func (d *ESX5Driver) DhcpLeasesPath(string) string {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *ESX5Driver) Verify() error {
|
|
|
|
checks := []func() error{
|
|
|
|
d.connect,
|
|
|
|
d.checkSystemVersion,
|
|
|
|
d.checkGuestIPHackEnabled,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, check := range checks {
|
2013-11-07 15:00:27 -05:00
|
|
|
if err := check(); err != nil {
|
2013-09-19 20:07:04 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *ESX5Driver) HostIP() (string, error) {
|
2014-02-13 05:53:47 -05:00
|
|
|
conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", d.Host, d.Port))
|
|
|
|
defer conn.Close()
|
2013-09-19 20:07:04 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2014-02-13 05:53:47 -05:00
|
|
|
host, _, err := net.SplitHostPort(conn.LocalAddr().String())
|
|
|
|
return host, err
|
2013-09-19 20:07:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *ESX5Driver) VNCAddress(portMin, portMax uint) (string, uint) {
|
|
|
|
var vncPort uint
|
|
|
|
// TODO(dougm) use esxcli network ip connection list
|
|
|
|
for port := portMin; port <= portMax; port++ {
|
2013-11-07 15:00:27 -05:00
|
|
|
address := fmt.Sprintf("%s:%d", d.Host, port)
|
2013-09-19 20:07:04 -04:00
|
|
|
log.Printf("Trying address: %s...", address)
|
|
|
|
l, err := net.DialTimeout("tcp", address, 1*time.Second)
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
log.Printf("%s in use", address)
|
|
|
|
l.Close()
|
|
|
|
} else if e, ok := err.(*net.OpError); ok {
|
|
|
|
if e.Err == syscall.ECONNREFUSED {
|
|
|
|
// then port should be available for listening
|
|
|
|
vncPort = port
|
|
|
|
break
|
|
|
|
} else if e.Timeout() {
|
|
|
|
log.Printf("Timeout connecting to: %s (check firewall rules)", address)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-07 15:00:27 -05:00
|
|
|
return d.Host, vncPort
|
2013-09-19 20:07:04 -04:00
|
|
|
}
|
|
|
|
|
2013-11-07 15:00:27 -05:00
|
|
|
func (d *ESX5Driver) SSHAddress(state multistep.StateBag) (string, error) {
|
|
|
|
config := state.Get("config").(*config)
|
|
|
|
|
|
|
|
if address, ok := state.GetOk("vm_address"); ok {
|
|
|
|
return address.(string), nil
|
|
|
|
}
|
|
|
|
|
2014-05-02 11:25:58 -04:00
|
|
|
r, err := d.esxcli("network", "vm", "list")
|
2013-11-07 15:00:27 -05:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2014-05-02 11:25:58 -04:00
|
|
|
record, err := r.find("Name", config.VMName)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
wid := record["WorldID"]
|
|
|
|
if wid == "" {
|
|
|
|
return "", errors.New("VM WorldID not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
r, err = d.esxcli("network", "vm", "port", "list", "-w", wid)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
record, err = r.read()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2013-11-07 15:00:27 -05:00
|
|
|
|
2014-05-02 11:25:58 -04:00
|
|
|
if record["IPAddress"] == "0.0.0.0" {
|
2013-11-07 15:00:27 -05:00
|
|
|
return "", errors.New("VM network port found, but no IP address")
|
|
|
|
}
|
2013-09-19 20:07:04 -04:00
|
|
|
|
2014-05-02 11:25:58 -04:00
|
|
|
address := fmt.Sprintf("%s:%d", record["IPAddress"], config.SSHPort)
|
2013-11-07 15:00:27 -05:00
|
|
|
state.Put("vm_address", address)
|
|
|
|
return address, nil
|
2013-09-19 20:07:04 -04:00
|
|
|
}
|
|
|
|
|
2014-01-01 23:55:08 -05:00
|
|
|
//-------------------------------------------------------------------
|
|
|
|
// OutputDir implementation
|
|
|
|
//-------------------------------------------------------------------
|
|
|
|
|
2013-11-08 00:18:25 -05:00
|
|
|
func (d *ESX5Driver) DirExists() (bool, error) {
|
|
|
|
err := d.sh("test", "-e", d.outputDir)
|
2013-11-07 23:46:34 -05:00
|
|
|
return err == nil, nil
|
2013-09-19 20:07:04 -04:00
|
|
|
}
|
|
|
|
|
2013-11-12 15:49:57 -05:00
|
|
|
func (d *ESX5Driver) ListFiles() ([]string, error) {
|
|
|
|
stdout, err := d.ssh("ls -1p "+d.outputDir, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
files := make([]string, 0, 10)
|
|
|
|
reader := bufio.NewReader(stdout)
|
|
|
|
for {
|
|
|
|
line, _, err := reader.ReadLine()
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if line[len(line)-1] == '/' {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
files = append(files, filepath.Join(d.outputDir, string(line)))
|
|
|
|
}
|
|
|
|
|
|
|
|
return files, nil
|
|
|
|
}
|
|
|
|
|
2013-11-08 00:18:25 -05:00
|
|
|
func (d *ESX5Driver) MkdirAll() error {
|
|
|
|
return d.mkdir(d.outputDir)
|
|
|
|
}
|
|
|
|
|
2013-11-12 15:49:57 -05:00
|
|
|
func (d *ESX5Driver) Remove(path string) error {
|
|
|
|
return d.sh("rm", path)
|
|
|
|
}
|
|
|
|
|
2013-11-08 00:18:25 -05:00
|
|
|
func (d *ESX5Driver) RemoveAll() error {
|
|
|
|
return d.sh("rm", "-rf", d.outputDir)
|
2013-09-19 20:07:04 -04:00
|
|
|
}
|
|
|
|
|
2013-11-08 00:18:25 -05:00
|
|
|
func (d *ESX5Driver) SetOutputDir(path string) {
|
|
|
|
d.outputDir = d.datastorePath(path)
|
2013-09-19 20:07:04 -04:00
|
|
|
}
|
|
|
|
|
2014-01-01 23:55:08 -05:00
|
|
|
func (d *ESX5Driver) String() string {
|
|
|
|
return d.outputDir
|
|
|
|
}
|
|
|
|
|
2013-09-19 20:07:04 -04:00
|
|
|
func (d *ESX5Driver) datastorePath(path string) string {
|
2014-05-09 00:16:18 -04:00
|
|
|
baseDir := filepath.Base(filepath.Dir(path))
|
|
|
|
return filepath.Join("/vmfs/volumes", d.Datastore, baseDir, filepath.Base(path))
|
2013-09-19 20:07:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *ESX5Driver) connect() error {
|
2013-11-07 15:00:27 -05:00
|
|
|
address := fmt.Sprintf("%s:%d", d.Host, d.Port)
|
|
|
|
|
2014-04-10 04:48:55 -04:00
|
|
|
auth := []gossh.AuthMethod{
|
|
|
|
gossh.Password(d.Password),
|
|
|
|
gossh.KeyboardInteractive(
|
2013-11-07 15:00:27 -05:00
|
|
|
ssh.PasswordKeyboardInteractive(d.Password)),
|
2013-09-19 20:07:04 -04:00
|
|
|
}
|
2013-11-07 15:00:27 -05:00
|
|
|
|
2013-09-19 20:07:04 -04:00
|
|
|
// TODO(dougm) KeyPath support
|
|
|
|
sshConfig := &ssh.Config{
|
|
|
|
Connection: ssh.ConnectFunc("tcp", address),
|
|
|
|
SSHConfig: &gossh.ClientConfig{
|
2013-11-07 15:00:27 -05:00
|
|
|
User: d.Username,
|
2013-09-19 20:07:04 -04:00
|
|
|
Auth: auth,
|
|
|
|
},
|
|
|
|
NoPty: true,
|
|
|
|
}
|
|
|
|
|
2014-04-10 04:48:55 -04:00
|
|
|
comm, err := ssh.New(address, sshConfig)
|
2013-09-19 20:07:04 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
d.comm = comm
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *ESX5Driver) checkSystemVersion() error {
|
|
|
|
r, err := d.esxcli("system", "version", "get")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
record, err := r.read()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-11-07 15:00:27 -05:00
|
|
|
log.Printf("Connected to %s %s %s", record["Product"],
|
|
|
|
record["Version"], record["Build"])
|
2013-09-19 20:07:04 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *ESX5Driver) checkGuestIPHackEnabled() error {
|
|
|
|
r, err := d.esxcli("system", "settings", "advanced", "list", "-o", "/Net/GuestIPHack")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
record, err := r.read()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if record["IntValue"] != "1" {
|
2013-12-07 13:31:57 -05:00
|
|
|
return errors.New(
|
|
|
|
"GuestIPHack is required, enable by running this on the ESX machine:\n" +
|
2013-12-11 14:19:36 -05:00
|
|
|
"esxcli system settings advanced set -o /Net/GuestIPHack -i 1")
|
2013-09-19 20:07:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-11-08 00:18:25 -05:00
|
|
|
func (d *ESX5Driver) mkdir(path string) error {
|
|
|
|
return d.sh("mkdir", "-p", path)
|
|
|
|
}
|
|
|
|
|
2013-11-07 23:47:15 -05:00
|
|
|
func (d *ESX5Driver) upload(dst, src string) error {
|
2013-09-19 20:07:04 -04:00
|
|
|
f, err := os.Open(src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
return d.comm.Upload(dst, f)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *ESX5Driver) verifyChecksum(ctype string, hash string, file string) bool {
|
|
|
|
stdin := bytes.NewBufferString(fmt.Sprintf("%s %s", hash, file))
|
|
|
|
_, err := d.run(stdin, fmt.Sprintf("%ssum", ctype), "-c")
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *ESX5Driver) ssh(command string, stdin io.Reader) (*bytes.Buffer, error) {
|
|
|
|
var stdout, stderr bytes.Buffer
|
|
|
|
|
|
|
|
cmd := &packer.RemoteCmd{
|
|
|
|
Command: command,
|
|
|
|
Stdout: &stdout,
|
|
|
|
Stderr: &stderr,
|
|
|
|
Stdin: stdin,
|
|
|
|
}
|
|
|
|
|
|
|
|
err := d.comm.Start(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.Wait()
|
|
|
|
|
|
|
|
if cmd.ExitStatus != 0 {
|
|
|
|
err = fmt.Errorf("'%s'\n\nStdout: %s\n\nStderr: %s",
|
|
|
|
cmd.Command, stdout.String(), stderr.String())
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &stdout, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *ESX5Driver) run(stdin io.Reader, args ...string) (string, error) {
|
|
|
|
stdout, err := d.ssh(strings.Join(args, " "), stdin)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return stdout.String(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *ESX5Driver) sh(args ...string) error {
|
|
|
|
_, err := d.run(nil, args...)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *ESX5Driver) esxcli(args ...string) (*esxcliReader, error) {
|
|
|
|
stdout, err := d.ssh("esxcli --formatter csv "+strings.Join(args, " "), nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
r := csv.NewReader(bytes.NewReader(stdout.Bytes()))
|
|
|
|
r.TrailingComma = true
|
|
|
|
header, err := r.Read()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &esxcliReader{r, header}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type esxcliReader struct {
|
|
|
|
cr *csv.Reader
|
|
|
|
header []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *esxcliReader) read() (map[string]string, error) {
|
|
|
|
fields, err := r.cr.Read()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
record := map[string]string{}
|
|
|
|
for i, v := range fields {
|
|
|
|
record[r.header[i]] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
return record, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *esxcliReader) find(key, val string) (map[string]string, error) {
|
|
|
|
for {
|
|
|
|
record, err := r.read()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if record[key] == val {
|
|
|
|
return record, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|