Documentation
¶
Index ¶
- Variables
- func BoolVar(d map[string]any, args map[string]any, key string) (*bool, error)
- func DurationVar(d map[string]any, args map[string]any, key string) (time.Duration, error)
- func Extension2Slice(d map[string]any, ext runtime.RawExtension) []any
- func Extension2String(d map[string]any, ext runtime.RawExtension) (string, error)
- func Extension2Variables(ext runtime.RawExtension) map[string]any
- func IntVar(d map[string]any, vars map[string]any, key string) (*int, error)
- func StringSliceVar(d map[string]any, vars map[string]any, key string) ([]string, error)
- func StringVar(d map[string]any, args map[string]any, key string) (string, error)
- type GetFunc
- type MergeFunc
- type Variable
Constants ¶
This section is empty.
Variables ¶
var GetAllVariable = func(hostName string) GetFunc { return func(v Variable) (any, error) { vv, ok := v.(*variable) if !ok { return nil, errors.New("variable type error") } result := make(map[string]any) result = combineVariables(result, vv.value.Hosts[hostName].RuntimeVars) result = combineVariables(result, vv.value.Hosts[hostName].RemoteVars) if vv, ok := vv.value.getParameterVariable()[hostName]; ok { if vvd, ok := vv.(map[string]any); ok { result = combineVariables(result, vvd) } } return result, nil } }
GetAllVariable get all variable for a given host
var GetHostMaxLength = func() GetFunc { return func(v Variable) (any, error) { vv, ok := v.(*variable) if !ok { return nil, errors.New("variable type error") } var hostNameMaxLen int for k := range vv.value.Hosts { hostNameMaxLen = max(len(k), hostNameMaxLen) } return hostNameMaxLen, nil } }
GetHostMaxLength get the max length for all hosts
var GetHostnames = func(name []string) GetFunc { if len(name) == 0 { return emptyGetFunc } return func(v Variable) (any, error) { vv, ok := v.(*variable) if !ok { return nil, errors.New("variable type error") } var hs []string for _, n := range name { if _, ok := vv.value.Hosts[n]; ok { hs = append(hs, n) } for gn, gv := range convertGroup(vv.value.Inventory) { if gn == n { if gvd, ok := gv.([]string); ok { hs = mergeSlice(hs, gvd) } break } } regexForIndex := regexp.MustCompile(`^(.*)\[\d\]$`) if match := regexForIndex.FindStringSubmatch(strings.TrimSpace(n)); match != nil { index, err := strconv.Atoi(match[2]) if err != nil { klog.V(4).ErrorS(err, "convert index to int error", "index", match[2]) return nil, err } if group, ok := convertGroup(vv.value.Inventory)[match[1]].([]string); ok { if index >= len(group) { return nil, fmt.Errorf("index %v out of range for group %s", index, group) } hs = append(hs, group[index]) } } regexForRandom := regexp.MustCompile(`^(.+?)\s*\|\s*random$`) if match := regexForRandom.FindStringSubmatch(strings.TrimSpace(n)); match != nil { if group, ok := convertGroup(vv.value.Inventory)[match[1]].([]string); ok { hs = append(hs, group[rand.Intn(len(group))]) } } } return hs, nil } }
GetHostnames get all hostnames from a group or host
var GetParamVariable = func(hostname string) GetFunc { return func(v Variable) (any, error) { vv, ok := v.(*variable) if !ok { return nil, errors.New("variable type error") } if hostname == "" { return vv.value.getParameterVariable(), nil } return vv.value.getParameterVariable()[hostname], nil } }
GetParamVariable get param variable which is combination of inventory, config. if hostname is empty, return all host's param variable.
var MergeAllRuntimeVariable = func(data map[string]any, hostName string) MergeFunc { return func(v Variable) error { vv, ok := v.(*variable) if !ok { return errors.New("variable type error") } curVariable, err := v.Get(GetAllVariable(hostName)) if err != nil { return err } if err := parseVariable(data, func(s string) (string, error) { cv, ok := curVariable.(map[string]any) if !ok { return "", errors.New("variable type error") } return tmpl.ParseString(combineVariables(data, cv), s) }); err != nil { return err } for h := range vv.value.Hosts { if _, ok := v.(*variable); !ok { return errors.New("variable type error") } hv := vv.value.Hosts[h] hv.RuntimeVars = combineVariables(hv.RuntimeVars, data) vv.value.Hosts[h] = hv } return nil } }
MergeAllRuntimeVariable parse variable by specific host and merge to all hosts.
var MergeRemoteVariable = func(data map[string]any, hostname string) MergeFunc { return func(v Variable) error { vv, ok := v.(*variable) if !ok { return errors.New("variable type error") } if hostname == "" { return errors.New("when merge source is remote. HostName cannot be empty") } if _, ok := vv.value.Hosts[hostname]; !ok { return fmt.Errorf("when merge source is remote. HostName %s not exist", hostname) } if hv := vv.value.Hosts[hostname]; len(hv.RemoteVars) == 0 { hv.RemoteVars = data vv.value.Hosts[hostname] = hv } return nil } }
MergeRemoteVariable merge variable to remote.
var MergeRuntimeVariable = func(data map[string]any, hosts ...string) MergeFunc { if len(data) == 0 || len(hosts) == 0 { return emptyMergeFunc } return func(v Variable) error { for _, hostName := range hosts { vv, ok := v.(*variable) if !ok { return errors.New("variable type error") } curVariable, err := v.Get(GetAllVariable(hostName)) if err != nil { return err } if err := parseVariable(data, func(s string) (string, error) { cv, ok := curVariable.(map[string]any) if !ok { return "", errors.New("variable type error") } return tmpl.ParseString(combineVariables(data, cv), s) }); err != nil { return err } if _, ok := v.(*variable); !ok { return errors.New("variable type error") } hv := vv.value.Hosts[hostName] hv.RuntimeVars = combineVariables(hv.RuntimeVars, data) vv.value.Hosts[hostName] = hv } return nil } }
MergeRuntimeVariable parse variable by specific host and merge to the host.
Functions ¶
func DurationVar ¶
DurationVar get time.Duration value by key
func Extension2Slice ¶
func Extension2Slice(d map[string]any, ext runtime.RawExtension) []any
Extension2Slice convert runtime.RawExtension to slice if runtime.RawExtension contains tmpl syntax, parse it.
func Extension2String ¶
Extension2String convert runtime.RawExtension to string. if runtime.RawExtension contains tmpl syntax, parse it.
func Extension2Variables ¶
func Extension2Variables(ext runtime.RawExtension) map[string]any
Extension2Variables convert runtime.RawExtension to variables
func StringSliceVar ¶
StringSliceVar get string slice value by key