// Execute executes a raw Vagrant command. func (v *Vagrant) Execute(commandRaw ...string) error { vagrantMutex.Lock() defer vagrantMutex.Unlock() if v.Env == nil { v.Env = make(map[string]string) } // Where to store data v.Env[vagrantDataDirEnvVar] = v.DataDir // Make sure we use our cwd properly v.Env[vagrantCwdEnvVar] = v.Dir // Build up the environment env := os.Environ() for k, v := range v.Env { env = append(env, fmt.Sprintf("%s=%s", k, v)) } // Build the args command := make([]string, len(commandRaw)+1) command[0] = "--machine-readable" copy(command[1:], commandRaw) // Build the command to execute cmd := exec.Command("vagrant", command...) cmd.Dir = v.Dir cmd.Env = env // Build our custom UI that we'll use that'll call the registered // callbacks as well as streaming data to the UI. callbacks := make(map[string]OutputCallback) callbacks["invalid"] = v.uiCallback callbacks["ui"] = v.uiCallback for n, cb := range v.Callbacks { callbacks[n] = cb } ui := &vagrantUi{Callbacks: callbacks} // Run it with the execHelper err := execHelper.Run(ui, cmd) ui.Finish() if err != nil { return fmt.Errorf( "Error executing Vagrant: %s\n\n"+ "The error messages from Vagrant are usually very informative.\n"+ "Please read it carefully and fix any issues it mentions. If\n"+ "the message isn't clear, please report this to the Otto project.", err) } return nil }
// Execute executes a raw Packer command. func (p *Packer) Execute(commandRaw ...string) error { varfile, err := p.varfile() if err != nil { return err } if execHelper.ShouldCleanup() { defer os.Remove(varfile) } // The command must always be machine-readable. We use this // exclusively to mirror the UI output. command := make([]string, len(commandRaw)+3) command[0] = commandRaw[0] command[1] = "-machine-readable" command[2] = "-var-file" command[3] = varfile copy(command[4:], commandRaw[1:]) // Build the command to execute path := "packer" if p.Path != "" { path = p.Path } cmd := exec.Command(path, command...) cmd.Dir = p.Dir // Build our custom UI that we'll use that'll call the registered // callbacks as well as streaming data to the UI. callbacks := make(map[string]OutputCallback) callbacks["ui"] = p.uiCallback for n, cb := range p.Callbacks { callbacks[n] = cb } ui := &packerUi{Callbacks: callbacks} // Execute! err = execHelper.Run(ui, cmd) ui.Finish() if err != nil { return fmt.Errorf( "Error executing Packer: %s\n\n"+ "The error messages from Packer are usually very informative.\n"+ "Please read it carefully and fix any issues it mentions. If\n"+ "the message isn't clear, please report this to the Otto project.", err) } return nil }