@@ -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
3359func 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.
105133func 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