github.com/sneal/packer@v0.5.2/builder/virtualbox/common/driver_4_2.go (about)

     1  package common
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"log"
     7  	"os/exec"
     8  	"regexp"
     9  	"strings"
    10  	"time"
    11  )
    12  
    13  type VBox42Driver struct {
    14  	// This is the path to the "VBoxManage" application.
    15  	VBoxManagePath string
    16  }
    17  
    18  func (d *VBox42Driver) CreateSATAController(vmName string, name string) error {
    19  	version, err := d.Version()
    20  	if err != nil {
    21  		return err
    22  	}
    23  
    24  	portCountArg := "--sataportcount"
    25  	if strings.HasPrefix(version, "4.3") {
    26  		portCountArg = "--portcount"
    27  	}
    28  
    29  	command := []string{
    30  		"storagectl", vmName,
    31  		"--name", name,
    32  		"--add", "sata",
    33  		portCountArg, "1",
    34  	}
    35  
    36  	return d.VBoxManage(command...)
    37  }
    38  
    39  func (d *VBox42Driver) Delete(name string) error {
    40  	return d.VBoxManage("unregistervm", name, "--delete")
    41  }
    42  
    43  func (d *VBox42Driver) Import(name, path, opts string) error {
    44  	args := []string{
    45  		"import", path,
    46  		"--vsys", "0",
    47  		"--vmname", name,
    48  		"--options", opts,
    49  	}
    50  
    51  	return d.VBoxManage(args...)
    52  }
    53  
    54  func (d *VBox42Driver) IsRunning(name string) (bool, error) {
    55  	var stdout bytes.Buffer
    56  
    57  	cmd := exec.Command(d.VBoxManagePath, "showvminfo", name, "--machinereadable")
    58  	cmd.Stdout = &stdout
    59  	if err := cmd.Run(); err != nil {
    60  		return false, err
    61  	}
    62  
    63  	for _, line := range strings.Split(stdout.String(), "\n") {
    64  		// Need to trim off CR character when running in windows
    65  		line = strings.TrimRight(line, "\r")
    66  
    67  		if line == `VMState="running"` {
    68  			return true, nil
    69  		}
    70  
    71  		// We consider "stopping" to still be running. We wait for it to
    72  		// be completely stopped or some other state.
    73  		if line == `VMState="stopping"` {
    74  			return true, nil
    75  		}
    76  
    77  		// We consider "paused" to still be running. We wait for it to
    78  		// be completely stopped or some other state.
    79  		if line == `VMState="paused"` {
    80  			return true, nil
    81  		}
    82  	}
    83  
    84  	return false, nil
    85  }
    86  
    87  func (d *VBox42Driver) Stop(name string) error {
    88  	if err := d.VBoxManage("controlvm", name, "poweroff"); err != nil {
    89  		return err
    90  	}
    91  
    92  	// We sleep here for a little bit to let the session "unlock"
    93  	time.Sleep(2 * time.Second)
    94  
    95  	return nil
    96  }
    97  
    98  func (d *VBox42Driver) SuppressMessages() error {
    99  	extraData := map[string]string{
   100  		"GUI/RegistrationData": "triesLeft=0",
   101  		"GUI/SuppressMessages": "confirmInputCapture,remindAboutAutoCapture,remindAboutMouseIntegrationOff,remindAboutMouseIntegrationOn,remindAboutWrongColorDepth",
   102  		"GUI/UpdateDate":       fmt.Sprintf("1 d, %d-01-01, stable", time.Now().Year()+1),
   103  		"GUI/UpdateCheckCount": "60",
   104  	}
   105  
   106  	for k, v := range extraData {
   107  		if err := d.VBoxManage("setextradata", "global", k, v); err != nil {
   108  			return err
   109  		}
   110  	}
   111  
   112  	return nil
   113  }
   114  
   115  func (d *VBox42Driver) VBoxManage(args ...string) error {
   116  	var stdout, stderr bytes.Buffer
   117  
   118  	log.Printf("Executing VBoxManage: %#v", args)
   119  	cmd := exec.Command(d.VBoxManagePath, args...)
   120  	cmd.Stdout = &stdout
   121  	cmd.Stderr = &stderr
   122  	err := cmd.Run()
   123  
   124  	stdoutString := strings.TrimSpace(stdout.String())
   125  	stderrString := strings.TrimSpace(stderr.String())
   126  
   127  	if _, ok := err.(*exec.ExitError); ok {
   128  		err = fmt.Errorf("VBoxManage error: %s", stderrString)
   129  	}
   130  
   131  	log.Printf("stdout: %s", stdoutString)
   132  	log.Printf("stderr: %s", stderrString)
   133  
   134  	return err
   135  }
   136  
   137  func (d *VBox42Driver) Verify() error {
   138  	return nil
   139  }
   140  
   141  func (d *VBox42Driver) Version() (string, error) {
   142  	var stdout bytes.Buffer
   143  
   144  	cmd := exec.Command(d.VBoxManagePath, "--version")
   145  	cmd.Stdout = &stdout
   146  	if err := cmd.Run(); err != nil {
   147  		return "", err
   148  	}
   149  
   150  	versionOutput := strings.TrimSpace(stdout.String())
   151  	log.Printf("VBoxManage --version output: %s", versionOutput)
   152  
   153  	// If the "--version" output contains vboxdrv, then this is indicative
   154  	// of problems with the VirtualBox setup and we shouldn't really continue,
   155  	// whether or not we can read the version.
   156  	if strings.Contains(versionOutput, "vboxdrv") {
   157  		return "", fmt.Errorf("VirtualBox is not properly setup: %s", versionOutput)
   158  	}
   159  
   160  	versionRe := regexp.MustCompile("[^.0-9]")
   161  	matches := versionRe.Split(versionOutput, 2)
   162  	if len(matches) == 0 || matches[0] == "" {
   163  		return "", fmt.Errorf("No version found: %s", versionOutput)
   164  	}
   165  
   166  	log.Printf("VirtualBox version: %s", matches[0])
   167  	return matches[0], nil
   168  }