@@ -45,84 +45,96 @@ def list_all_key(self):
45
45
params = {'client' : 'wheel' , 'fun' : 'key.list_all' }
46
46
content = self .post_request (params )
47
47
if isinstance (content , dict ):
48
- minions = content ['return' ][0 ]['data' ]['return' ]
49
- return minions
48
+ return content ['return' ][0 ]['data' ]['return' ]
50
49
else :
51
50
return {"status" : False , "message" : "Salt API Error : " + content }
52
51
53
52
def delete_key (self , node_name ):
54
53
params = {'client' : 'wheel' , 'fun' : 'key.delete' , 'match' : node_name }
55
54
content = self .post_request (params )
56
55
if isinstance (content , dict ):
57
- ret = content ['return' ][0 ]['data' ]['success' ]
58
- return ret
56
+ return content ['return' ][0 ]['data' ]['success' ]
59
57
else :
60
58
return {"status" : False , "message" : "salt api error : " + content }
61
59
62
60
def accept_key (self , node_name ):
63
61
params = {'client' : 'wheel' , 'fun' : 'key.accept' , 'match' : node_name }
64
62
content = self .post_request (params )
65
63
if isinstance (content , dict ):
66
- ret = content ['return' ][0 ]['data' ]['success' ]
67
- return ret
64
+ return content ['return' ][0 ]['data' ]['success' ]
68
65
else :
69
66
return {"status" : False , "message" : "Salt API Error : " + content }
70
67
71
68
def reject_key (self , node_name ):
72
69
params = {'client' : 'wheel' , 'fun' : 'key.reject' , 'match' : node_name }
73
70
content = self .post_request (params )
74
71
if isinstance (content , dict ):
75
- ret = content ['return' ][0 ]['data' ]['success' ]
76
- return ret
72
+ return content ['return' ][0 ]['data' ]['success' ]
77
73
else :
78
74
return {"status" : False , "message" : "Salt API Error : " + content }
79
75
80
- def remote_noarg_execution (self , tgt , fun ):
76
+ def remote_noarg_execution (self , tgt , fun , types = "tgt_type" ):
81
77
# Execute commands without parameters
82
- params = {'client' : 'local' , 'tgt' : tgt , 'fun' : fun , 'expr_form' : 'list' }
78
+ params = {'client' : 'local' , 'tgt' : tgt , 'fun' : fun , types : 'list' }
83
79
content = self .post_request (params )
84
80
if isinstance (content , dict ):
85
81
ret = content ['return' ][0 ][tgt ]
82
+ # 如果返回结果是空,说明salt可能版本小于2017.7.0的版本,使用次方法处理
83
+ if not ret and types != "expr_form" : # types != "expr_form" 很重要确保最多执行两次,否则肯能发送死循环
84
+ return self .remote_noarg_execution (tgt , fun , types = "expr_form" )
86
85
return ret
87
86
else :
88
87
return {"status" : False , "message" : "Salt API Error : " + content }
89
88
90
- def remote_noarg_execution_notgt (self , tgt , fun ):
89
+ def remote_noarg_execution_notgt (self , tgt , fun , types = "tgt_type" ):
91
90
# Execute commands without parameters
92
- params = {'client' : 'local' , 'tgt' : tgt , 'fun' : fun , 'expr_form' : 'list' }
91
+ params = {'client' : 'local' , 'tgt' : tgt , 'fun' : fun , types : 'list' }
93
92
content = self .post_request (params )
94
93
if isinstance (content , dict ):
95
94
ret = content ['return' ][0 ]
95
+ # 如果返回结果是空,说明salt可能版本小于2017.7.0的版本,使用次方法处理
96
+ if not ret and types != "expr_form" : # types != "expr_form" 很重要确保最多执行两次,否则肯能发送死循环
97
+ return self .remote_noarg_execution_notgt (tgt , fun , types = "expr_form" )
96
98
return ret
97
99
else :
98
100
return {"status" : False , "message" : "Salt API Error : " + content }
99
101
100
- def remote_execution (self , tgt , fun , arg ):
102
+ def remote_execution (self , tgt , fun , arg , types = "tgt_type" ):
101
103
# Command execution with parameters
102
- params = {'client' : 'local' , 'tgt' : tgt , 'fun' : fun , 'arg' : arg , 'expr_form' : 'list' }
104
+ params = {'client' : 'local' , 'tgt' : tgt , 'fun' : fun , 'arg' : arg , types : 'list' }
103
105
content = self .post_request (params )
104
106
if isinstance (content , dict ):
105
107
ret = content ['return' ][0 ][tgt ]
108
+ # 如果返回结果是空,说明salt可能版本小于2017.7.0的版本,使用次方法处理
109
+ if not ret and types != "expr_form" : # types != "expr_form" 很重要确保最多执行两次,否则肯能发送死循环
110
+ return self .remote_execution (tgt , fun , arg , types = "expr_form" )
106
111
return ret
107
112
else :
108
113
return {"status" : False , "message" : "Salt API Error : " + content }
109
114
110
- def remote_execution_notgt (self , tgt , fun , arg ):
115
+ def remote_execution_notgt (self , tgt , fun , arg , types = "tgt_type" ):
111
116
# Command execution with parameters
112
- params = {'client' : 'local' , 'tgt' : tgt , 'fun' : fun , 'arg' : arg , 'expr_form' : 'list' }
117
+ params = {'client' : 'local' , 'tgt' : tgt , 'fun' : fun , 'arg' : arg , types : 'list' }
113
118
content = self .post_request (params )
114
119
if isinstance (content , dict ):
115
120
ret = content ['return' ][0 ]
121
+ # 如果返回结果是空,说明salt可能版本小于2017.7.0的版本,使用次方法处理
122
+ if not ret and types != "expr_form" : # types != "expr_form" 很重要确保最多执行两次,否则肯能发送死循环
123
+ return self .remote_execution_notgt (tgt , fun , arg , types = "expr_form" )
116
124
return ret
117
125
else :
118
126
return {"status" : False , "message" : "Salt API Error : " + content }
119
127
120
- def shell_remote_execution (self , tgt , arg ):
128
+ def shell_remote_execution (self , tgt , arg , types = "tgt_type" ):
121
129
# Shell command execution with parameters
122
- params = {'client' : 'local' , 'tgt' : tgt , 'fun' : 'cmd.run' , 'arg' : arg , 'expr_form' : 'list' }
130
+ # Changed in version 2017.7.0: Renamed from expr_form to tgt_type
131
+ params = {'client' : 'local' , 'tgt' : tgt , 'fun' : 'cmd.run' , 'arg' : arg , types : 'list' }
123
132
content = self .post_request (params )
124
133
if isinstance (content , dict ):
125
134
ret = content ['return' ][0 ]
135
+ # 如果返回结果是空,说明salt可能版本小于2017.7.0的版本,使用次方法处理
136
+ if not ret and types != "expr_form" : # types != "expr_form" 很重要确保最多执行两次,否则肯能发送死循环
137
+ return self .shell_remote_execution (tgt , arg , types = "expr_form" )
126
138
return ret
127
139
else :
128
140
return {"status" : False , "message" : "Salt API Error : " + content }
@@ -132,8 +144,7 @@ def grain(self, tgt, arg):
132
144
params = {'client' : 'local' , 'tgt' : tgt , 'fun' : 'grains.item' , 'arg' : arg }
133
145
content = self .post_request (params )
134
146
if isinstance (content , dict ):
135
- ret = content ['return' ][0 ]
136
- return ret
147
+ return content ['return' ][0 ]
137
148
else :
138
149
return {"status" : False , "message" : "Salt API Error : " + content }
139
150
@@ -142,59 +153,64 @@ def grains(self, tgt):
142
153
params = {'client' : 'local' , 'tgt' : tgt , 'fun' : 'grains.items' }
143
154
content = self .post_request (params )
144
155
if isinstance (content , dict ):
145
- ret = content ['return' ][0 ]
146
- return {"status" : True , "message" : "" , "data" : ret }
156
+ return {"status" : True , "message" : "" , "data" : content ['return' ][0 ]}
147
157
else :
148
158
return {"status" : False , "message" : "Salt API Error : " + content }
149
159
150
- def target_remote_execution (self , tgt , fun , arg ):
151
- # Use targeting for remote execution
152
- params = {'client' : 'local' , 'tgt' : tgt , 'fun' : fun , 'arg' : arg , 'expr_form' : 'nodegroup' }
153
- content = self .post_request (params )
154
- if isinstance (content , dict ):
155
- jid = content ['return' ][0 ]['jid' ]
156
- return jid
157
- else :
158
- return {"status" : False , "message" : "Salt API Error : " + content }
160
+ # def target_remote_execution(self, tgt, fun, arg):
161
+ # # Use targeting for remote execution
162
+ # params = {'client': 'local', 'tgt': tgt, 'fun': fun, 'arg': arg, 'expr_form': 'nodegroup'}
163
+ # content = self.post_request(params)
164
+ # if isinstance(content, dict):
165
+ # jid = content['return'][0]['jid']
166
+ # return jid
167
+ # else:
168
+ # return {"status": False, "message": "Salt API Error : " + content}
159
169
160
170
def deploy (self , tgt , arg ):
161
171
# Module deployment
162
172
params = {'client' : 'local' , 'tgt' : tgt , 'fun' : 'state.sls' , 'arg' : arg }
163
- content = self .post_request (params )
164
- return content
173
+ return self .post_request (params )
165
174
166
175
def async_deploy (self , tgt , arg ):
167
176
# Asynchronously send a command to connected minions
168
177
params = {'client' : 'local_async' , 'tgt' : tgt , 'fun' : 'state.sls' , 'arg' : arg }
169
178
content = self .post_request (params )
170
179
if isinstance (content , dict ):
171
- jid = content ['return' ][0 ]['jid' ]
172
- return jid
180
+ return content ['return' ][0 ]['jid' ]
173
181
else :
174
182
return {"status" : False , "message" : "salt api error : " + content }
175
183
176
- def target_deploy (self , tgt , arg ):
184
+ def target_deploy (self , tgt , arg , types = "tgt_type" ):
177
185
# Based on the list forms deployment
178
- params = {'client' : 'local' , 'tgt' : tgt , 'fun' : 'state.sls' , 'arg' : arg , 'expr_form' : 'list' }
186
+ params = {'client' : 'local' , 'tgt' : tgt , 'fun' : 'state.sls' , 'arg' : arg , types : 'list' }
179
187
content = self .post_request (params )
180
188
if isinstance (content , dict ):
181
189
try :
182
- return content .get ("return" )[0 ]
190
+ ret = content .get ("return" )[0 ]
191
+ # 如果返回结果是空,说明salt可能版本小于2017.7.0的版本,使用次方法处理
192
+ if not ret and types != "expr_form" : # types != "expr_form" 很重要确保最多执行两次,否则肯能发送死循环
193
+ return self .target_deploy (tgt , arg , types = "expr_form" )
194
+ return ret
183
195
except Exception as e :
184
196
return {"status" : False , "message" : str (e )}
185
197
else :
186
198
return {"status" : False , "message" : "Salt API Error : " + content }
187
199
188
- def pillar_items (self , tgt , arg = []):
200
+ def pillar_items (self , tgt , arg = [], types = "tgt_type" ):
189
201
# Get pillar item
190
202
if arg :
191
- params = {'client' : 'local' , 'tgt' : tgt , 'fun' : 'pillar.item' , 'arg' : arg , 'expr_form' : 'list' }
203
+ params = {'client' : 'local' , 'tgt' : tgt , 'fun' : 'pillar.item' , 'arg' : arg , types : 'list' }
192
204
else :
193
- params = {'client' : 'local' , 'tgt' : tgt , 'fun' : 'pillar.items' , 'arg' : arg , 'expr_form' : 'list' }
205
+ params = {'client' : 'local' , 'tgt' : tgt , 'fun' : 'pillar.items' , 'arg' : arg , types : 'list' }
194
206
content = self .post_request (params )
195
207
if isinstance (content , dict ):
196
208
try :
197
- return content .get ("return" )[0 ]
209
+ ret = content .get ("return" )[0 ]
210
+ # 如果返回结果是空,说明salt可能版本小于2017.7.0的版本,使用次方法处理
211
+ if not ret and types != "expr_form" : # types != "expr_form" 很重要确保最多执行两次,否则肯能发送死循环
212
+ return self .pillar_items (tgt , arg = [], types = "expr_form" )
213
+ return ret
198
214
except Exception as e :
199
215
return {"status" : False , "message" : str (e )}
200
216
else :
0 commit comments