@@ -136,14 +136,6 @@ func SaltMinionRunRequestHandler(w http.ResponseWriter, req *http.Request) {
136
136
log .Printf ("[SaltMinionRunRequestHandler] [ERROR] while hostfile update: %s" , err )
137
137
}
138
138
139
- grainConfig := GrainConfig {Roles : saltMinion .Roles , HostGroup : saltMinion .HostGroup }
140
- grainYaml , err := yaml .Marshal (grainConfig )
141
- if err != nil {
142
- resp = model.Response {ErrorText : err .Error (), StatusCode : http .StatusInternalServerError }
143
- resp .WriteHttp (w )
144
- return
145
- }
146
-
147
139
baseDir := req .Header .Get ("salt-minion-base-dir" )
148
140
149
141
err = os .MkdirAll (baseDir + "/etc/salt/minion.d" , 0755 )
@@ -173,11 +165,21 @@ func SaltMinionRunRequestHandler(w http.ResponseWriter, req *http.Request) {
173
165
return
174
166
}
175
167
176
- err = ioutil .WriteFile (baseDir + "/etc/salt/grains" , grainYaml , 0644 )
177
- if err != nil {
178
- resp = model.Response {ErrorText : err .Error (), StatusCode : http .StatusInternalServerError }
179
- resp .WriteHttp (w )
180
- return
168
+ grainConfigPath := baseDir + "/etc/salt/grains"
169
+ if isGrainsConfigNeeded (grainConfigPath ) {
170
+ grainConfig := GrainConfig {Roles : saltMinion .Roles , HostGroup : saltMinion .HostGroup }
171
+ grainYaml , err := yaml .Marshal (grainConfig )
172
+ if err != nil {
173
+ resp = model.Response {ErrorText : err .Error (), StatusCode : http .StatusInternalServerError }
174
+ resp .WriteHttp (w )
175
+ return
176
+ }
177
+ err = ioutil .WriteFile (grainConfigPath , grainYaml , 0644 )
178
+ if err != nil {
179
+ resp = model.Response {ErrorText : err .Error (), StatusCode : http .StatusInternalServerError }
180
+ resp .WriteHttp (w )
181
+ return
182
+ }
181
183
}
182
184
183
185
log .Println ("[SaltMinionRunRequestHandler] execute salt-minion run request" )
@@ -381,6 +383,24 @@ func distributePillarImpl(distributeActionRequest func([]string, string, string,
381
383
return result
382
384
}
383
385
386
+ func isGrainsConfigNeeded (grainConfigLocation string ) bool {
387
+ log .Println ("[isGrainsConfigNeeded] check whether salt grains are empty, config file: " + grainConfigLocation )
388
+ b , err := ioutil .ReadFile (grainConfigLocation )
389
+ if err == nil && len (b ) > 0 {
390
+ var grains GrainConfig = GrainConfig {}
391
+ if err := yaml .Unmarshal (b , & grains ); err != nil {
392
+ log .Printf ("[isGrainsConfigNeeded] failed to unmarshal grain config file: %s" , err .Error ())
393
+ return true
394
+ }
395
+ if grains .Roles != nil && len (grains .Roles ) > 0 {
396
+ log .Printf ("[isGrainsConfigNeeded] there are roles already defined: %s, no need to create new config" , grains .Roles )
397
+ return false
398
+ }
399
+ }
400
+ log .Println ("[isGrainsConfigNeeded] there is no grain config present at the moment, config is required" )
401
+ return true
402
+ }
403
+
384
404
func isSaltMinionRestartNeeded (servers []string ) bool {
385
405
log .Println ("[isSaltMinionRestartNeeded] check whether salt-minion requires restart" )
386
406
masterConfFile := "/etc/salt/minion.d/master.conf"
0 commit comments