@@ -45,84 +45,96 @@ def list_all_key(self):
4545 params = {'client' : 'wheel' , 'fun' : 'key.list_all' }
4646 content = self .post_request (params )
4747 if isinstance (content , dict ):
48- minions = content ['return' ][0 ]['data' ]['return' ]
49- return minions
48+ return content ['return' ][0 ]['data' ]['return' ]
5049 else :
5150 return {"status" : False , "message" : "Salt API Error : " + content }
5251
5352 def delete_key (self , node_name ):
5453 params = {'client' : 'wheel' , 'fun' : 'key.delete' , 'match' : node_name }
5554 content = self .post_request (params )
5655 if isinstance (content , dict ):
57- ret = content ['return' ][0 ]['data' ]['success' ]
58- return ret
56+ return content ['return' ][0 ]['data' ]['success' ]
5957 else :
6058 return {"status" : False , "message" : "salt api error : " + content }
6159
6260 def accept_key (self , node_name ):
6361 params = {'client' : 'wheel' , 'fun' : 'key.accept' , 'match' : node_name }
6462 content = self .post_request (params )
6563 if isinstance (content , dict ):
66- ret = content ['return' ][0 ]['data' ]['success' ]
67- return ret
64+ return content ['return' ][0 ]['data' ]['success' ]
6865 else :
6966 return {"status" : False , "message" : "Salt API Error : " + content }
7067
7168 def reject_key (self , node_name ):
7269 params = {'client' : 'wheel' , 'fun' : 'key.reject' , 'match' : node_name }
7370 content = self .post_request (params )
7471 if isinstance (content , dict ):
75- ret = content ['return' ][0 ]['data' ]['success' ]
76- return ret
72+ return content ['return' ][0 ]['data' ]['success' ]
7773 else :
7874 return {"status" : False , "message" : "Salt API Error : " + content }
7975
80- def remote_noarg_execution (self , tgt , fun ):
76+ def remote_noarg_execution (self , tgt , fun , types = "tgt_type" ):
8177 # 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' }
8379 content = self .post_request (params )
8480 if isinstance (content , dict ):
8581 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" )
8685 return ret
8786 else :
8887 return {"status" : False , "message" : "Salt API Error : " + content }
8988
90- def remote_noarg_execution_notgt (self , tgt , fun ):
89+ def remote_noarg_execution_notgt (self , tgt , fun , types = "tgt_type" ):
9190 # 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' }
9392 content = self .post_request (params )
9493 if isinstance (content , dict ):
9594 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" )
9698 return ret
9799 else :
98100 return {"status" : False , "message" : "Salt API Error : " + content }
99101
100- def remote_execution (self , tgt , fun , arg ):
102+ def remote_execution (self , tgt , fun , arg , types = "tgt_type" ):
101103 # 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' }
103105 content = self .post_request (params )
104106 if isinstance (content , dict ):
105107 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" )
106111 return ret
107112 else :
108113 return {"status" : False , "message" : "Salt API Error : " + content }
109114
110- def remote_execution_notgt (self , tgt , fun , arg ):
115+ def remote_execution_notgt (self , tgt , fun , arg , types = "tgt_type" ):
111116 # 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' }
113118 content = self .post_request (params )
114119 if isinstance (content , dict ):
115120 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" )
116124 return ret
117125 else :
118126 return {"status" : False , "message" : "Salt API Error : " + content }
119127
120- def shell_remote_execution (self , tgt , arg ):
128+ def shell_remote_execution (self , tgt , arg , types = "tgt_type" ):
121129 # 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' }
123132 content = self .post_request (params )
124133 if isinstance (content , dict ):
125134 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" )
126138 return ret
127139 else :
128140 return {"status" : False , "message" : "Salt API Error : " + content }
@@ -132,8 +144,7 @@ def grain(self, tgt, arg):
132144 params = {'client' : 'local' , 'tgt' : tgt , 'fun' : 'grains.item' , 'arg' : arg }
133145 content = self .post_request (params )
134146 if isinstance (content , dict ):
135- ret = content ['return' ][0 ]
136- return ret
147+ return content ['return' ][0 ]
137148 else :
138149 return {"status" : False , "message" : "Salt API Error : " + content }
139150
@@ -142,59 +153,64 @@ def grains(self, tgt):
142153 params = {'client' : 'local' , 'tgt' : tgt , 'fun' : 'grains.items' }
143154 content = self .post_request (params )
144155 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 ]}
147157 else :
148158 return {"status" : False , "message" : "Salt API Error : " + content }
149159
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}
159169
160170 def deploy (self , tgt , arg ):
161171 # Module deployment
162172 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 )
165174
166175 def async_deploy (self , tgt , arg ):
167176 # Asynchronously send a command to connected minions
168177 params = {'client' : 'local_async' , 'tgt' : tgt , 'fun' : 'state.sls' , 'arg' : arg }
169178 content = self .post_request (params )
170179 if isinstance (content , dict ):
171- jid = content ['return' ][0 ]['jid' ]
172- return jid
180+ return content ['return' ][0 ]['jid' ]
173181 else :
174182 return {"status" : False , "message" : "salt api error : " + content }
175183
176- def target_deploy (self , tgt , arg ):
184+ def target_deploy (self , tgt , arg , types = "tgt_type" ):
177185 # 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' }
179187 content = self .post_request (params )
180188 if isinstance (content , dict ):
181189 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
183195 except Exception as e :
184196 return {"status" : False , "message" : str (e )}
185197 else :
186198 return {"status" : False , "message" : "Salt API Error : " + content }
187199
188- def pillar_items (self , tgt , arg = []):
200+ def pillar_items (self , tgt , arg = [], types = "tgt_type" ):
189201 # Get pillar item
190202 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' }
192204 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' }
194206 content = self .post_request (params )
195207 if isinstance (content , dict ):
196208 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
198214 except Exception as e :
199215 return {"status" : False , "message" : str (e )}
200216 else :
0 commit comments