2019-06-13 03:16:49 -04:00
|
|
|
package uhost
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
"github.com/ucloud/ucloud-sdk-go/services/uhost"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2019-06-13 08:17:08 -04:00
|
|
|
func checkStringIn(val string, available []string) error {
|
|
|
|
for _, choice := range available {
|
2019-06-13 03:16:49 -04:00
|
|
|
if val == choice {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-13 08:17:08 -04:00
|
|
|
return fmt.Errorf("should be one of %q, got %q", strings.Join(available, ","), val)
|
2019-06-13 03:16:49 -04:00
|
|
|
}
|
|
|
|
|
2019-06-13 08:17:08 -04:00
|
|
|
func checkIntIn(val int, available []int) error {
|
|
|
|
for _, choice := range available {
|
2019-06-13 03:16:49 -04:00
|
|
|
if val == choice {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-13 08:17:08 -04:00
|
|
|
return fmt.Errorf("should be one of %v, got %d", available, val)
|
2019-06-13 03:16:49 -04:00
|
|
|
}
|
|
|
|
|
2019-06-13 08:17:08 -04:00
|
|
|
func isStringIn(val string, available []string) bool {
|
|
|
|
for _, choice := range available {
|
2019-06-13 03:16:49 -04:00
|
|
|
if val == choice {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// SSHHost returns a function that can be given to the SSH communicator
|
|
|
|
func SSHHost(usePrivateIp bool) func(multistep.StateBag) (string, error) {
|
|
|
|
return func(state multistep.StateBag) (string, error) {
|
|
|
|
|
|
|
|
instance := state.Get("instance").(*uhost.UHostInstanceSet)
|
|
|
|
var privateIp, publicIp string
|
|
|
|
|
|
|
|
for _, v := range instance.IPSet {
|
2019-06-19 09:32:33 -04:00
|
|
|
if v.Type == ipTypePrivate {
|
2019-06-13 03:16:49 -04:00
|
|
|
privateIp = v.IP
|
|
|
|
} else {
|
|
|
|
publicIp = v.IP
|
|
|
|
}
|
|
|
|
}
|
2019-06-19 09:32:33 -04:00
|
|
|
|
2019-06-13 03:16:49 -04:00
|
|
|
if usePrivateIp {
|
|
|
|
return privateIp, nil
|
|
|
|
}
|
2019-06-19 09:32:33 -04:00
|
|
|
|
|
|
|
return publicIp, nil
|
2019-06-13 03:16:49 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func halt(state multistep.StateBag, err error, prefix string) multistep.StepAction {
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
|
|
|
if prefix != "" {
|
|
|
|
err = fmt.Errorf("%s: %s", prefix, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|