Files
Packer-Cn/builder/ucloud/common/utils.go
T

76 lines
1.5 KiB
Go
Raw Normal View History

2019-10-12 16:46:21 +08:00
package common
2019-06-13 15:16:49 +08:00
import (
"fmt"
2019-12-17 11:25:56 +01:00
"strings"
2019-06-13 15:16:49 +08:00
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
"github.com/ucloud/ucloud-sdk-go/services/uhost"
)
2019-10-12 16:46:21 +08:00
func CheckStringIn(val string, available []string) error {
2019-06-13 20:17:08 +08:00
for _, choice := range available {
2019-06-13 15:16:49 +08:00
if val == choice {
return nil
}
}
2019-06-13 20:17:08 +08:00
return fmt.Errorf("should be one of %q, got %q", strings.Join(available, ","), val)
2019-06-13 15:16:49 +08:00
}
2019-10-12 16:46:21 +08:00
func CheckIntIn(val int, available []int) error {
2019-06-13 20:17:08 +08:00
for _, choice := range available {
2019-06-13 15:16:49 +08:00
if val == choice {
return nil
}
}
2019-06-13 20:17:08 +08:00
return fmt.Errorf("should be one of %v, got %d", available, val)
2019-06-13 15:16:49 +08:00
}
2019-10-12 16:46:21 +08:00
func IsStringIn(val string, available []string) bool {
2019-06-13 20:17:08 +08:00
for _, choice := range available {
2019-06-13 15:16:49 +08: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 16:46:21 +08:00
if v.Type == IpTypePrivate {
2019-06-13 15:16:49 +08:00
privateIp = v.IP
} else {
publicIp = v.IP
}
}
2019-06-19 21:32:33 +08:00
2019-06-13 15:16:49 +08:00
if usePrivateIp {
return privateIp, nil
}
2019-06-19 21:32:33 +08:00
return publicIp, nil
2019-06-13 15:16:49 +08:00
}
}
2019-10-12 16:46:21 +08:00
func Halt(state multistep.StateBag, err error, prefix string) multistep.StepAction {
2019-06-13 15:16:49 +08: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
}