2015-06-19 15:06:06 -07:00
|
|
|
package shell
|
|
|
|
|
|
|
|
import (
|
2018-02-23 13:26:31 -08:00
|
|
|
sl "github.com/hashicorp/packer/common/shell-local"
|
2017-04-04 13:39:01 -07:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2015-06-19 15:06:06 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
type Provisioner struct {
|
2018-02-27 12:50:42 -08:00
|
|
|
config sl.Config
|
2015-06-19 15:06:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Provisioner) Prepare(raws ...interface{}) error {
|
2018-08-03 10:45:57 -07:00
|
|
|
// This is a bit of a hack. For provisioners that need access to
|
|
|
|
// auto-generated WinRMPasswords, the mechanism of keeping provisioner data
|
|
|
|
// and build data totally segregated breaks down. We get around this by
|
|
|
|
// having the builder stash the WinRMPassword in the state bag, then
|
|
|
|
// grabbing it out of the statebag inside of StepProvision. Then, when
|
|
|
|
// the time comes to provision for real, we run the prepare step one more
|
|
|
|
// time, now with WinRMPassword defined in the raws, and can store the
|
|
|
|
// password on the provisioner config without overwriting the rest of the
|
|
|
|
// work we've already done in the first prepare run.
|
|
|
|
if len(raws) == 1 {
|
|
|
|
for k, v := range raws[0].(map[interface{}]interface{}) {
|
|
|
|
if k.(string) == "WinRMPassword" {
|
|
|
|
p.config.WinRMPassword = v.(string)
|
|
|
|
// Even if WinRMPassword is not gonna be used, we've stored the
|
|
|
|
// key and pointed it to an empty string. That means we'll
|
|
|
|
// always reach this on our second-run of Prepare()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-02 12:32:34 -08:00
|
|
|
err := sl.Decode(&p.config, raws...)
|
2015-06-19 15:06:06 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-03-02 12:32:34 -08:00
|
|
|
err = sl.Validate(&p.config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-06-19 15:06:06 -07:00
|
|
|
func (p *Provisioner) Provision(ui packer.Ui, _ packer.Communicator) error {
|
2018-02-28 11:53:53 -08:00
|
|
|
_, retErr := sl.Run(ui, &p.config)
|
|
|
|
if retErr != nil {
|
|
|
|
return retErr
|
2015-06-19 15:06:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Provisioner) Cancel() {
|
|
|
|
// Just do nothing. When the process ends, so will our provisioner
|
|
|
|
}
|