17
17
import ansible_runner
18
18
19
19
from jinja2 import Environment , meta
20
+
20
21
from tornado .web import RequestHandler , HTTPError
22
+ from tornado .concurrent import run_on_executor
23
+ from concurrent .futures import ThreadPoolExecutor
24
+
25
+ import tornado .gen
21
26
22
27
from .tool import Tool
23
28
from .config import Config
@@ -60,24 +65,31 @@ def get(self):
60
65
{'message' : "Hello, I am Ansible Api" , 'rc' : ErrorCode .ERRCODE_NONE }))
61
66
62
67
63
- class AsyncTest (Controller ):
68
+ class NonBlockTest (Controller ):
69
+ executor = ThreadPoolExecutor (4 )
64
70
71
+ @tornado .gen .coroutine
65
72
def get (self ):
66
- self .finish (Tool .jsonal (
67
- {'message' : 'hi test' , 'rc' : ErrorCode .ERRCODE_NONE }))
73
+ start = time .time ()
74
+ msg = yield self .run_block ()
75
+ end = time .time () - start
76
+ self .write (Tool .jsonal ({'message' : msg + 'and total cost time: %s' % end , 'rc' : ErrorCode .ERRCODE_NONE }))
68
77
69
- async def test (self ):
78
+ @run_on_executor
79
+ def run_block (self ):
70
80
time .sleep (10 )
71
81
return 'i have slept 10 s'
72
82
73
83
74
84
class Command (Controller ):
85
+ executor = ThreadPoolExecutor (Config .get ('thread_pool_size' ))
75
86
76
87
def get (self ):
77
88
self .finish (Tool .jsonal (
78
89
{'error' : "Forbidden in get method" , 'rc' : ErrorCode .ERRCODE_SYS }))
79
90
80
- async def post (self ): # Change the async method to python3 async, this performance better than gen.coroutine
91
+ @tornado .gen .coroutine
92
+ def post (self ): # Change the async method to python3 async, this performance better than gen.coroutine
81
93
data = Tool .parsejson (self .request .body )
82
94
bad_cmd = ['reboot' , 'su' , 'sudo' , 'dd' ,
83
95
'mkfs' , 'shutdown' , 'half' , 'top' ]
@@ -106,27 +118,33 @@ async def post(self): # Change the async method to python3 async, this performa
106
118
cb .status_drawer (dict (status = 'starting' , raw = lambda : dict (
107
119
task_name = module , event = 'playbook_on_play_start' , runner_ident = name ,
108
120
event_data = dict (pattern = target , name = module )),
109
- after = lambda : dict (task_list = [module ])))
110
- response = ansible_runner .interface .run (
111
- host_pattern = target , inventory = '/etc/ansible/hosts' ,
112
- envvars = dict (PATH = os .environ .get ('PATH' )+ ':' + sys .path [0 ]),
113
- ident = name , module = module , module_args = arg ,
114
- event_handler = cb .event_handler , status_handler = cb .status_handler
115
- )
116
- # pp = pprint.PrettyPrinter(indent=4)
117
- # print('*' * 20)
118
- # pp.pprint(cb.get_summary())
119
- # print('+' * 20)
120
- self .finish (Tool .jsonal (dict (rc = response .rc , detail = cb .get_summary ())))
121
+ after = lambda : dict (task_list = [module ])))
122
+ response = yield self .run (target , name , module , arg , cb )
123
+ self .write (Tool .jsonal (dict (rc = response .rc , detail = cb .get_summary ())))
121
124
except Exception as e :
122
125
Tool .LOGGER .exception (e )
123
- self .finish (Tool .jsonal (
126
+ self .write (Tool .jsonal (
124
127
{'error' : str (e ), 'rc' : ErrorCode .ERRCODE_BIZ }))
125
128
129
+ @run_on_executor
130
+ def run (self , target , name , module , arg , cb ):
131
+ return ansible_runner .interface .run (
132
+ host_pattern = target , inventory = '/etc/ansible/hosts' ,
133
+ envvars = dict (PATH = os .environ .get ('PATH' ) + ':' + sys .path [0 ]),
134
+ ident = name , module = module , module_args = arg ,
135
+ event_handler = cb .event_handler , status_handler = cb .status_handler
136
+ )
137
+
126
138
127
139
class Playbook (Controller ):
140
+ executor = ThreadPoolExecutor (Config .get ('thread_pool_size' ))
128
141
129
- async def post (self ):
142
+ def get (self ):
143
+ self .finish (Tool .jsonal (
144
+ {'error' : "Forbidden in get method" , 'rc' : ErrorCode .ERRCODE_SYS }))
145
+
146
+ @tornado .gen .coroutine
147
+ def post (self ):
130
148
data = Tool .parsejson (self .request .body )
131
149
Tool .LOGGER .debug ("MORE DETAIL: data %s" % data )
132
150
name = data ['n' ].encode ('utf-8' ).decode ()
@@ -165,16 +183,7 @@ async def post(self):
165
183
try :
166
184
cb = CallBack ()
167
185
cb .event_pepper ('playbook_on_play_start' , dict (task_list = task_list ))
168
- response = ansible_runner .interface .run (
169
- host_pattern = hosts , inventory = '/etc/ansible/hosts' ,
170
- envvars = dict (PATH = os .environ .get ('PATH' )+ ':' + sys .path [0 ]),
171
- playbook = yml_file , ident = name , extravars = my_vars ,
172
- event_handler = cb .event_handler , status_handler = cb .status_handler
173
- )
174
- # pp = pprint.PrettyPrinter(indent=4)
175
- # print('*' * 20)
176
- # pp.pprint(cb.get_summary())
177
- # print('+' * 20)
186
+ response = yield self .run (hosts , name , yml_file , my_vars , cb )
178
187
self .finish (Tool .jsonal (dict (rc = response .rc , detail = cb .get_summary ())))
179
188
except BaseException as e :
180
189
Tool .LOGGER .exception (e )
@@ -185,6 +194,15 @@ async def post(self):
185
194
self .finish (Tool .jsonal (
186
195
{'error' : "yml file(" + yml_file + ") is not existed" , 'rc' : ErrorCode .ERRCODE_SYS }))
187
196
197
+ @run_on_executor
198
+ def run (self , hosts , name , yml_file , my_vars , cb ):
199
+ return ansible_runner .interface .run (
200
+ host_pattern = hosts , inventory = '/etc/ansible/hosts' ,
201
+ envvars = dict (PATH = os .environ .get ('PATH' ) + ':' + sys .path [0 ]),
202
+ playbook = yml_file , ident = name , extravars = my_vars ,
203
+ event_handler = cb .event_handler , status_handler = cb .status_handler
204
+ )
205
+
188
206
189
207
class FileList (Controller ):
190
208
0 commit comments