@@ -13,16 +13,21 @@ import (
13
13
"gopkg.in/yaml.v2"
14
14
)
15
15
16
- type SaltServerSetupRequest struct {
17
- Password string `json:"password,omitempty"`
18
- }
19
-
20
16
type SaltActionRequest struct {
17
+ Master SaltMaster `json:"master,omitempty"`
21
18
Minions []SaltMinion `json:"minions,omitempty"`
22
- Server string `json:"server,omitempty"`
23
19
Action string `json:"action"`
24
20
}
25
21
22
+ type SaltAuth struct {
23
+ Password string `json:"password,omitempty"`
24
+ }
25
+
26
+ type SaltMaster struct {
27
+ Address string `json:"address"`
28
+ Auth SaltAuth `json:"auth,omitempty"`
29
+ }
30
+
26
31
type SaltMinion struct {
27
32
Address string `json:"address"`
28
33
Roles []string `json:"roles,omitempty"`
@@ -40,6 +45,11 @@ func (saltMinion SaltMinion) AsByteArray() []byte {
40
45
return b
41
46
}
42
47
48
+ func (saltMaster SaltMaster ) AsByteArray () []byte {
49
+ b , _ := json .Marshal (saltMaster )
50
+ return b
51
+ }
52
+
43
53
type GrainConfig struct {
44
54
HostGroup string `json:"hostgroup" yaml:"hostgroup"`
45
55
Roles []string `json:"roles" yaml:"roles"`
@@ -52,21 +62,22 @@ func (r SaltActionRequest) String() string {
52
62
53
63
func (r SaltActionRequest ) distributeAction (user string , pass string ) (result []model.Response ) {
54
64
log .Printf ("[distributeAction] distribute salt state command to targets: %s" , r .String ())
65
+
55
66
var targets []string
56
- var payloads []Payload
67
+ var minionPayload []Payload
57
68
for _ , minion := range r .Minions {
58
69
targets = append (targets , minion .Address )
59
70
if minion .Server == "" {
60
- minion .Server = r .Server
71
+ minion .Server = r .Master . Address
61
72
}
62
- payloads = append (payloads , minion )
73
+ minionPayload = append (minionPayload , minion )
63
74
}
64
75
65
- for res := range DistributePayload (targets , payloads , SaltMinionEp + "/" + r .Action , user , pass ) {
76
+ for res := range DistributePayload (targets , minionPayload , SaltMinionEp + "/" + r .Action , user , pass ) {
66
77
result = append (result , res )
67
78
}
68
- if len (r .Server ) > 0 {
69
- result = append (result , <- Distribute ([]string {r .Server }, nil , SaltServerEp + "/" + r .Action , user , pass ))
79
+ if len (r .Master . Address ) > 0 {
80
+ result = append (result , <- DistributePayload ([]string {r .Master . Address }, [] Payload { r . Master } , SaltServerEp + "/" + r .Action , user , pass ))
70
81
}
71
82
return result
72
83
}
@@ -135,11 +146,29 @@ func SaltMinionStopRequestHandler(w http.ResponseWriter, req *http.Request) {
135
146
136
147
func SaltServerRunRequestHandler (w http.ResponseWriter , req * http.Request ) {
137
148
log .Printf ("[SaltServerRunRequestHandler] execute salt run request" )
138
- resp , err := LaunchService ("salt-master" )
149
+
150
+ decoder := json .NewDecoder (req .Body )
151
+ var saltMaster SaltMaster
152
+ err := decoder .Decode (& saltMaster )
153
+ if err != nil {
154
+ log .Printf ("[SaltServerRunRequestHandler] [ERROR] couldn't decode json: %s" , err )
155
+ model.Response {Status : err .Error ()}.WriteBadRequestHttp (w )
156
+ return
157
+ }
158
+
159
+ resp , err := CreateUser (saltMaster )
160
+ resp .WriteHttp (w )
161
+ if err != nil {
162
+ return
163
+ }
164
+
165
+ resp , err = LaunchService ("salt-master" )
139
166
resp .WriteHttp (w )
167
+
140
168
if err != nil {
141
169
return
142
170
}
171
+
143
172
resp , _ = LaunchService ("salt-api" )
144
173
resp .WriteHttp (w )
145
174
}
@@ -155,10 +184,6 @@ func SaltServerStopRequestHandler(w http.ResponseWriter, req *http.Request) {
155
184
resp .WriteHttp (w )
156
185
}
157
186
158
- func SaltServerSetupRequestHandler (w http.ResponseWriter , req * http.Request ) {
159
-
160
- }
161
-
162
187
func (pillar SaltPillar ) WritePillar () (outStr string , err error ) {
163
188
file := "/srv/pillar" + pillar .Path
164
189
dir := file [0 :strings .LastIndex (file , "/" )]
0 commit comments