Skip to content

Commit f2f40a4

Browse files
authored
Merge pull request #3 from puppetlabs-operations/fix-test-url
Fix test url and puppet pathing, rework calls to puppet, add .exe to windows files
2 parents 786aaa7 + f69305e commit f2f40a4

File tree

2 files changed

+56
-21
lines changed

2 files changed

+56
-21
lines changed

build.sh

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@ mkdir -p ${release_path}
1313
for target in $targets; do
1414
os="$(echo $target | cut -d '/' -f1)"
1515
arch="$(echo $target | cut -d '/' -f2)"
16-
output="${release_path}/${repo_name}_${os}_${arch}"
16+
17+
if [ "$os" = "windows" ]; then
18+
output="${release_path}/${repo_name}_${os}_${arch}.exe"
19+
else
20+
output="${release_path}/${repo_name}_${os}_${arch}"
21+
fi
1722

1823
echo "----> Building project for: $target"
1924
GOOS=$os GOARCH=$arch CGO_ENABLED=0 go build -o $output

puppet-cron.go

Lines changed: 50 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,57 @@ import (
99
"net/http"
1010
"os"
1111
"os/exec"
12+
"runtime"
1213
"strings"
13-
"syscall"
1414
"time"
1515
)
1616

17-
const Puppet = "/opt/puppetlabs/bin/puppet"
17+
// Call the installed version of Puppet with a given set of arguments.
18+
// It returns the output to the calling function as a byte array.
19+
func callPuppet(puppetArgs []string) []byte {
20+
paths := []string{}
1821

19-
// We use `puppet config` to get and set values in
20-
// /etc/puppetlabs/puppet/puppet.conf. We don't touch it directly at all.
22+
if os.Getenv("PATH") != "" {
23+
paths = append(paths, os.Getenv("PATH"))
24+
}
2125

22-
func puppetConfigGet(section string, key string) string {
23-
args := []string{"config", "print", "--section", section, key}
26+
if runtime.GOOS == "windows" {
27+
defaultPuppetLocation := string("C:\\Program Files\\Puppet Labs\\Puppet\\bin")
28+
paths = append(paths, defaultPuppetLocation)
29+
os.Setenv("PATH", strings.Join(paths, ";"))
30+
} else {
31+
defaultPuppetLocation := string("/opt/puppetlabs/bin")
32+
paths = append(paths, defaultPuppetLocation)
33+
os.Setenv("PATH", strings.Join(paths, ":"))
34+
}
2435

25-
output, err := exec.Command(Puppet, args...).Output()
36+
output, err := exec.Command("puppet", puppetArgs...).Output()
2637
if err != nil {
27-
log.Fatalf("Failed: %s %s", Puppet, strings.Join(args, " "))
38+
log.Fatal(err)
2839
}
2940

30-
return string(output[:len(output)-1])
41+
return output
3142
}
3243

44+
// Use `puppet config print` to retrieve the value of a given key
45+
// from a given section of puppet.conf.
46+
// The value is returned as a string to the callling function.
47+
func puppetConfigGet(section string, key string) string {
48+
args := []string{"config", "print", "--section", section, key}
49+
output := callPuppet(args)
50+
51+
value := string(output[:len(output)-1])
52+
value = strings.ReplaceAll(value, "\r", "")
53+
value = strings.ReplaceAll(value, "\n", "")
54+
return value
55+
}
56+
57+
// use `puppet config set` to set the value of a given key in a given
58+
// section of puppet.conf
3359
func puppetConfigSet(section string, key string, value string) {
3460
args := []string{"config", "set", "--section", section, key, value}
3561

36-
_, err := exec.Command(Puppet, args...).Output()
37-
if err != nil {
38-
log.Fatalf("Failed: %s %s", Puppet, strings.Join(args, " "))
39-
}
62+
callPuppet(args)
4063
}
4164

4265
// Create an http.Client that recognizes the Puppet CA, and authenticates with
@@ -80,7 +103,9 @@ func isValidEnvironment(environment string) bool {
80103
server := puppetConfigGet("agent", "server")
81104
port := puppetConfigGet("agent", "masterport")
82105

83-
url := fmt.Sprintf("https://%s:%s/puppet/v3/status/test?environment=%s", server, port, environment)
106+
log.Printf("Checking if environment '%s' exists on '%s'", environment, server)
107+
108+
url := fmt.Sprintf("https://%s:%s/puppet/v3/file_metadatas/plugins?environment=%s", server, port, environment)
84109
request, err := http.NewRequest("GET", url, nil)
85110
if err != nil {
86111
log.Panic(err)
@@ -102,19 +127,24 @@ func isValidEnvironment(environment string) bool {
102127
return true
103128
}
104129

130+
// Check to see if the locally configured puppet environment still exists.
131+
// If it doesn't, revert to the `production` envionment. Once the check
132+
// and any needed update is complete, run the puppet agent.
105133
func main() {
134+
log.Print("Starting puppet-cron...")
106135
environment := puppetConfigGet("agent", "environment")
136+
puppetArgs := []string{"agent", "--no-daemonize", "--onetime"}
107137

108138
if environment == "" || !isValidEnvironment(environment) {
109139
log.Printf("Environment %q is invalid; resetting", environment)
110140
puppetConfigSet("agent", "environment", "production")
111141
}
112142

113-
err := syscall.Exec(Puppet, []string{"puppet", "agent", "--no-daemonize", "--onetime"}, os.Environ())
114-
if err != nil {
115-
log.Fatal(err)
116-
} else {
117-
// WTF. exec should only return if it fails
118-
log.Panic("syscall.Exec returned with no error")
143+
if os.Getenv("PUPPET_CRON_DEBUG") != "" {
144+
log.Printf("Current value of $PATH: %s", os.Getenv("PATH"))
119145
}
146+
147+
log.Printf("Running 'puppet %s'", strings.Join(puppetArgs, " "))
148+
149+
callPuppet(puppetArgs)
120150
}

0 commit comments

Comments
 (0)