Skip to content

Commit

Permalink
Add checks for salt id being set
Browse files Browse the repository at this point in the history
  • Loading branch information
CameronRP committed Sep 28, 2024
1 parent 967a0c3 commit 3867330
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions saltutil/saltutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package saltutil
import (
"bufio"
"encoding/json"
"errors"
"fmt"
"os"
"os/exec"
Expand All @@ -23,7 +24,12 @@ type Grains struct {
Group string `json:"group,omitempty"`
}

var ErrNoSaltMinionID = errors.New("no salt minion ID")

func SetGrains(grains Grains, log *logging.Logger) error {
if !IsSaltIdSet() {
return ErrNoSaltMinionID
}
// Setup logger if none is provided
if log == nil {
log = logging.NewLogger("info")
Expand All @@ -48,6 +54,9 @@ func SetGrains(grains Grains, log *logging.Logger) error {

// GetSaltGrains returns the salt grains. Optional to pass a logger, pass nil to use default.
func GetSaltGrains(log *logging.Logger) (*Grains, error) {
if !IsSaltIdSet() {
return nil, ErrNoSaltMinionID
}
// Setup logger if none is provided
if log == nil {
log = logging.NewLogger("info")
Expand Down Expand Up @@ -106,6 +115,9 @@ func GetSaltGrains(log *logging.Logger) (*Grains, error) {
}

func GetNodegroupFromFile() (string, error) {
if !IsSaltIdSet() {
return "", ErrNoSaltMinionID
}
nodegroup, err := os.ReadFile(nodegroupFile)
if err != nil {
return "", err
Expand All @@ -114,6 +126,9 @@ func GetNodegroupFromFile() (string, error) {
}

func GetMinionID(log *logging.Logger) (string, error) {
if !IsSaltIdSet() {
return "", ErrNoSaltMinionID
}
// Setup logger if none is provided
if log == nil {
log = logging.NewLogger("info")
Expand All @@ -129,3 +144,8 @@ func GetMinionID(log *logging.Logger) (string, error) {
log.Debugf("Minion ID: '%s'", id)
return id, nil
}

func IsSaltIdSet() bool {
_, err := os.Stat(minionIdFile)
return err == nil
}

0 comments on commit 3867330

Please sign in to comment.