packer-cn/builder/ucloud/common/utils.go

75 lines
1.5 KiB
Go
Raw Normal View History

2019-10-12 04:46:21 -04:00
package common
2019-06-13 03:16:49 -04:00
import (
"fmt"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
"github.com/ucloud/ucloud-sdk-go/services/uhost"
"strings"
)
2019-10-12 04:46:21 -04:00
func CheckStringIn(val string, available []string) error {
2019-06-13 08:17:08 -04:00
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-10-12 04:46:21 -04:00
func CheckIntIn(val int, available []int) error {
2019-06-13 08:17:08 -04:00
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-10-12 04:46:21 -04:00
func IsStringIn(val string, available []string) bool {
2019-06-13 08:17:08 -04:00
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-10-12 04:46:21 -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
}
}
2019-10-12 04:46:21 -04:00
func Halt(state multistep.StateBag, err error, prefix string) multistep.StepAction {
2019-06-13 03:16:49 -04:00
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
}