How to use execute_task method in locust

Best Python code snippet using locust

api_tengu.py

Source:api_tengu.py Github

copy

Full Screen

...42@TENGU.route('/controllers', methods=['GET'])43def get_all_controllers():44 try:45 LOGGER.info('/TENGU/controllers [GET] => receiving call')46 token = execute_task(juju.authenticate, request.headers['api-key'], request.authorization)47 LOGGER.info('/TENGU/controllers [GET] => Authenticated!')48 if token.is_admin:49 code, response = 200, juju.get_all_controllers()50 LOGGER.info('/TENGU/controllers [GET] => Succesfully retrieved all controllers!')51 else:52 code, response = errors.no_permission()53 LOGGER.error('/TENGU/controllers [GET] => No Permission to perform action!')54 except KeyError:55 code, response = errors.invalid_data()56 error_log()57 except HTTPException:58 ers = error_log()59 raise60 except Exception:61 ers = error_log()62 code, response = errors.cmd_error(ers)63 return juju.create_response(code, response)64@TENGU.route('/controllers', methods=['POST'])65def create_controller():66 try:67 if request.json is None:68 data = request.form69 else:70 data = request.json71 url = request.url_rule72 LOGGER.info('%s [POST] => receiving call', url)73 token = execute_task(juju.authenticate, request.headers['api-key'], request.authorization)74 LOGGER.info('%s [POST] => Authenticated', url)75 if token.is_admin:76 valid, controller = juju.check_input(data['controller'], 'controller')77 LOGGER.info('%s [POST] => Creating Controller %s', url, controller)78 if valid:79 c_type = juju.check_c_type(data['type'])80 if juju.controller_exists(controller):81 code, response = errors.already_exists('controller')82 LOGGER.error('%s [POST] => Controller %s already exists', url, controller)83 else:84 sup_clouds = juju.get_supported_regions(c_type)85 if data['region'] in sup_clouds:86 if juju.credential_exists(token.username, data['credential']):87 code, response = juju.create_controller(c_type,88 controller, data['region'], data['credential'])89 LOGGER.info('%s [POST] => Creating Controller %s, check add_controller.log for more details! ', url, controller)90 else:91 code, response = 400, 'Credential {} not found for user {}'.format(data['credential'], token.username)92 else:93 code, response = 400, 'Region not supported for cloud {}. Please choose one of the following: {}'.format(c_type, sup_clouds)94 LOGGER.error('%s [POST] => %s', url, response)95 else:96 code, response = 400, controller97 else:98 code, response = errors.no_permission()99 LOGGER.error('%s [POST] => No Permission to perform action!', url)100 except KeyError:101 code, response = errors.invalid_data()102 error_log()103 except HTTPException:104 ers = error_log()105 raise106 except Exception:107 ers = error_log()108 code, response = errors.cmd_error(ers)109 return juju.create_response(code, response)110@TENGU.route('/controllers/<controller>', methods=['GET'])111def get_controller_info(controller):112 try:113 LOGGER.info('/TENGU/controllers/%s [GET] => receiving call', controller)114 token = execute_task(juju.authenticate, request.headers['api-key'], request.authorization)115 LOGGER.info('/TENGU/controllers/%s [GET] => Authenticated!', controller)116 con = juju.authorize( token, controller)117 LOGGER.info('/TENGU/controllers/%s [GET] => Authorized!', controller)118 code, response = 200, juju.get_controller_info(token, con)119 LOGGER.info('/TENGU/controllers/%s [GET] => Succesfully retrieved controller information!', controller)120 except KeyError:121 code, response = errors.invalid_data()122 error_log()123 except HTTPException:124 ers = error_log()125 raise126 except Exception:127 ers = error_log()128 code, response = errors.cmd_error(ers)129 return juju.create_response(code, response)130@TENGU.route('/controllers/<controller>', methods=['DELETE'])131def delete_controller(controller):132 try:133 LOGGER.info('/TENGU/controllers/%s [DELETE] => receiving call', controller)134 token = execute_task(juju.authenticate, request.headers['api-key'], request.authorization)135 LOGGER.info('/TENGU/controllers/%s [DELETE] => Authenticated!', controller)136 con = juju.authorize( token, controller)137 LOGGER.info('/TENGU/controllers/%s [DELETE] => Authorized!', controller)138 if con.c_access == 'superuser':139 LOGGER.info('/TENGU/controllers/%s [DELETE] => Deleting Controller!', controller)140 juju.delete_controller(con)141 code, response = 202, 'Controller {} is being deleted'.format(controller)142 LOGGER.info('/TENGU/controllers/%s [DELETE] => Succesfully deleted controller!', controller)143 else:144 code, response = errors.no_permission()145 LOGGER.error('/TENGU/controllers/%s [DELETE] => No Permission to perform this action!', controller)146 except KeyError:147 code, response = errors.invalid_data()148 error_log()149 except HTTPException:150 ers = error_log()151 raise152 except Exception:153 ers = error_log()154 code, response = errors.cmd_error(ers)155 return juju.create_response(code, response)156@TENGU.route('/controllers/<controller>/models', methods=['POST'])157def create_model(controller):158 try:159 LOGGER.info('/TENGU/controllers/%s/models [POST] => receiving call', controller)160 data = request.json161 token = execute_task(juju.authenticate, request.headers['api-key'], request.authorization)162 LOGGER.info('/TENGU/controllers/%s/models [POST] => Authenticated!', controller)163 con = juju.authorize( token, controller)164 LOGGER.info('/TENGU/controllers/%s/models [POST] => Authorized!', controller)165 valid, model = juju.check_input(data['model'], "model")166 if con.c_access == 'add-model' or con.c_access == 'superuser':167 if juju.credential_exists(token.username, data['credential']):168 credentials = data['credential']169 if valid:170 LOGGER.info('/TENGU/controllers/%s/models [POST] => Creating model, check add_model.log for more details', controller)171 code, response = juju.create_model(token, con.c_name, model, credentials)172 else:173 code, response = 400, model174 else:175 code, response = 400, 'Credential {} not found for user {}'.format(data['credential'], token.username)176 else:177 code, response = errors.no_permission()178 LOGGER.error('/TENGU/controllers/%s/models [POST] => No Permission to perform this action!', controller)179 except KeyError:180 code, response = errors.invalid_data()181 error_log()182 except HTTPException:183 ers = error_log()184 raise185 except Exception:186 ers = error_log()187 code, response = errors.cmd_error(ers)188 return juju.create_response(code, response)189@TENGU.route('/controllers/<controller>/models', methods=['GET'])190def get_models_info(controller):191 try:192 LOGGER.info('/TENGU/controllers/%s/models [GET] => receiving call', controller)193 token = execute_task(juju.authenticate, request.headers['api-key'], request.authorization)194 LOGGER.info('/TENGU/controllers/%s/models [GET] => Authenticated!', controller)195 con = juju.authorize( token, controller)196 LOGGER.info('/TENGU/controllers/%s/models [GET] => Authorized!', controller)197 code, response = 200, juju.get_models_info(token, con)198 LOGGER.info('/TENGU/controllers/%s/models [GET] => modelinfo retieved for all models!', controller)199 except KeyError:200 code, response = errors.invalid_data()201 error_log()202 except HTTPException:203 ers = error_log()204 raise205 except Exception:206 ers = error_log()207 code, response = errors.cmd_error(ers)208 return juju.create_response(code, response)209@TENGU.route('/controllers/<controller>/models/<model>', methods=['GET'])210def get_model_info(controller, model):211 try:212 LOGGER.info('/TENGU/controllers/%s/models/%s [GET] => receiving call', controller, model)213 token = execute_task(juju.authenticate, request.headers['api-key'], request.authorization)214 LOGGER.info('/TENGU/controllers/%s/models/%s [GET] => Authenticated!', controller, model)215 con, mod = juju.authorize( token, controller, model)216 LOGGER.info('/TENGU/controllers/%s/models/%s [GET] => Authorized!', controller, model)217 code, response = 200, execute_task(juju.get_model_info, token, con, mod)218 LOGGER.info('/TENGU/controllers/%s/models/%s [GET] => model information retrieved!', controller, model)219 except KeyError:220 code, response = errors.invalid_data()221 error_log()222 except HTTPException:223 ers = error_log()224 raise225 except Exception:226 ers = error_log()227 code, response = errors.cmd_error(ers)228 return juju.create_response(code, response)229@TENGU.route('/controllers/<controller>/models/<model>', methods=['POST'])230def add_bundle(controller, model):231 try:232 LOGGER.info('/TENGU/controllers/%s/models/%s [POST] => receiving call', controller, model)233 data = request.json234 token = execute_task(juju.authenticate, request.headers['api-key'], request.authorization)235 LOGGER.info('/TENGU/controllers/%s/models/%s [POST] => Authenticated!', controller, model)236 con, mod = juju.authorize( token, controller, model)237 LOGGER.info('/TENGU/controllers/%s/models/%s [POST] => Authorized!', controller, model)238 if mod.m_access == 'admin' or mod.m_access == 'write':239 bundle = data['bundle']240 if 'applications' in bundle:241 bundle['services'] = bundle['applications']242 bundle.pop('applications')243 LOGGER.info('/TENGU/controllers/%s/models/%s [POST] => Bundle is being deployed, check bundle_deployment.log for more information!', controller, model)244 juju.add_bundle(token, con.c_name, mod.m_name, bundle)245 code, response = 202, "Bundle is being deployed"246 else:247 code, response = errors.no_permission()248 LOGGER.error('/TENGU/controllers/%s/models/%s [POST] => No Permission to perform action!', controller, model)249 except KeyError:250 code, response = errors.invalid_data()251 error_log()252 except HTTPException:253 ers = error_log()254 raise255 except Exception:256 ers = error_log()257 code, response = errors.cmd_error(ers)258 return juju.create_response(code, response)259@TENGU.route('/controllers/<controller>/models/<model>', methods=['DELETE'])260def delete_model(controller, model):261 try:262 LOGGER.info('/TENGU/controllers/%s/models/%s [DELETE] => receiving call', controller, model)263 token = execute_task(juju.authenticate, request.headers['api-key'], request.authorization)264 LOGGER.info('/TENGU/controllers/%s/models/%s [DELETE] => Authenticated!', controller, model)265 con, mod = juju.authorize( token, controller, model)266 LOGGER.info('/TENGU/controllers/%s/models/%s [DELETE] => Authorized!', controller, model)267 if mod.m_access == 'admin':268 code, response = 202, execute_task(juju.delete_model, token, con, mod)269 LOGGER.info('/TENGU/controllers/%s/models/%s [DELETE] => Model succesfully deleted!', controller, model)270 else:271 code, response = errors.no_permission()272 LOGGER.error('/TENGU/controllers/%s/models/%s [DELETE] => No Permission to perform this action!', controller, model)273 except KeyError:274 code, response = errors.invalid_data()275 error_log()276 except HTTPException:277 ers = error_log()278 raise279 except Exception:280 ers = error_log()281 code, response = errors.cmd_error(ers)282 return juju.create_response(code, response)283@TENGU.route('/controllers/<controller>/models/<model>/applications', methods=['GET'])284def get_applications_info(controller, model):285 try:286 LOGGER.info('/TENGU/controllers/%s/models/%s/applications [GET] => receiving call', controller, model)287 token = execute_task(juju.authenticate, request.headers['api-key'], request.authorization)288 LOGGER.info('/TENGU/controllers/%s/models/%s/applications [GET] => Authenticated!', controller, model)289 con, mod = juju.authorize( token, controller, model)290 LOGGER.info('/TENGU/controllers/%s/models/%s/applications [GET] => Authorized!', controller, model)291 code, response = 200, execute_task(juju.get_applications_info, token, mod)292 LOGGER.info('/TENGU/controllers/%s/models/%s/applications [GET] => succesfully retieved applications info!', controller, model)293 except KeyError:294 code, response = errors.invalid_data()295 error_log()296 except HTTPException:297 ers = error_log()298 raise299 except Exception:300 ers = error_log()301 code, response = errors.cmd_error(ers)302 return juju.create_response(code, response)303@TENGU.route('/controllers/<controller>/models/<model>/applications', methods=['POST'])304def add_application(controller, model):305 try:306 LOGGER.info('/TENGU/controllers/%s/models/%s/applications [POST] => receiving call', controller, model)307 data = request.json308 token = execute_task(juju.authenticate, request.headers['api-key'], request.authorization)309 LOGGER.info('/TENGU/controllers/%s/models/%s/applications [POST] => Authenticated!', controller, model)310 con, mod = juju.authorize( token, controller, model)311 LOGGER.info('/TENGU/controllers/%s/models/%s/applications [POST] => Authorized!', controller, model)312 app = data['application']313 if execute_task(juju.app_exists, token, con, mod, app):314 code, response = errors.already_exists('application')315 LOGGER.error('/TENGU/controllers/%s/models/%s/applications [POST] => Application already exists!', controller, model)316 else:317 if mod.m_access == 'write' or mod.m_access == 'admin':318 execute_task(juju.deploy_app, token, con, mod, app, data)319 code, response = 200, execute_task(juju.get_application_info, token, mod, app)320 LOGGER.info('/TENGU/controllers/%s/models/%s/applications [POST] => succesfully deployed application!', controller, model)321 else:322 code, response = errors.no_permission()323 LOGGER.error('/TENGU/controllers/%s/models/%s [DELETE] => No Permission to perform this action!', controller, model)324 except KeyError:325 code, response = errors.invalid_data()326 error_log()327 except HTTPException:328 ers = error_log()329 raise330 except Exception:331 ers = error_log()332 code, response = errors.cmd_error(ers)333 return juju.create_response(code, response)334@TENGU.route('/controllers/<controller>/models/<model>/applications/<application>', methods=['GET'])335def get_application_info(controller, model, application):336 try:337 LOGGER.info('/TENGU/controllers/%s/models/%s/applications/%s [GET] => receiving call', controller, model, application)338 token = execute_task(juju.authenticate, request.headers['api-key'], request.authorization)339 LOGGER.info('/TENGU/controllers/%s/models/%s/applications/%s [GET] => Authenticated!', controller, model, application)340 con, mod = juju.authorize( token, controller, model)341 LOGGER.info('/TENGU/controllers/%s/models/%s/applications/%s [GET] => authorized!', controller, model, application)342 if execute_task(juju.app_exists, token, con, mod, application):343 code, response = 200, execute_task(juju.get_application_info, token, mod, application)344 LOGGER.info('/TENGU/controllers/%s/models/%s/applications/%s [GET] => Succesfully retrieved application info!', controller, model, application)345 else:346 code, response = errors.does_not_exist('application')347 LOGGER.error('/TENGU/controllers/%s/models/%s/applications/%s [GET] => Application does not exist!', controller, model, application)348 except KeyError:349 code, response = errors.invalid_data()350 error_log()351 except HTTPException:352 ers = error_log()353 raise354 except Exception:355 ers = error_log()356 code, response = errors.cmd_error(ers)357 return juju.create_response(code, response)358@TENGU.route('/controllers/<controller>/models/<model>/applications/<application>', methods=['PUT'])359def expose_application(controller, model, application):360 data = request.json361 try:362 LOGGER.info('/TENGU/controllers/%s/models/%s/applications/%s [PUT] => receiving call', controller, model, application)363 token = execute_task(juju.authenticate, request.headers['api-key'], request.authorization)364 LOGGER.info('/TENGU/controllers/%s/models/%s/applications/%s [PUT] => Authenticated!', controller, model, application)365 con, mod = juju.authorize( token, controller, model)366 LOGGER.info('/TENGU/controllers/%s/models/%s/applications/%s [PUT] => Authorized!', controller, model, application)367 exposed = True if data['expose'] == "True" else False368 if execute_task(juju.check_if_exposed, token, mod, application) == exposed:369 code, response = 200, execute_task(juju.get_application_info, token, mod, application)370 LOGGER.info('/TENGU/controllers/%s/models/%s/applications/%s [PUT] => Application already exposed!', controller, model, application)371 else:372 if exposed:373 execute_task(juju.expose_app, token, mod, application)374 LOGGER.info('/TENGU/controllers/%s/models/%s/applications/%s [PUT] => Application exposed!', controller, model, application)375 else:376 execute_task(juju.unexpose_app, token, mod, application)377 LOGGER.info('/TENGU/controllers/%s/models/%s/applications/%s [PUT] => Application unexposed!', controller, model, application)378 code, response = 200, execute_task(juju.get_application_info, token, mod, application)379 except KeyError:380 code, response = errors.invalid_data()381 error_log()382 except HTTPException:383 ers = error_log()384 raise385 except Exception:386 ers = error_log()387 code, response = errors.cmd_error(ers)388 return juju.create_response(code, response)389@TENGU.route('/controllers/<controller>/models/<model>/applications/<application>', methods=['DELETE'])390def remove_app(controller, model, application):391 try:392 LOGGER.info('/TENGU/controllers/%s/models/%s/applications/%s [DELETE] => receiving call', controller, model, application)393 token = execute_task(juju.authenticate, request.headers['api-key'], request.authorization)394 LOGGER.info('/TENGU/controllers/%s/models/%s/applications/%s [DELETE] => Authenticated!', controller, model, application)395 con, mod = juju.authorize( token, controller, model)396 LOGGER.info('/TENGU/controllers/%s/models/%s/applications/%s [DELETE] => Authorized!', controller, model, application)397 if mod.m_access == 'write' or mod.m_access == 'admin':398 if execute_task(juju.app_exists, token, con, mod, application):399 execute_task(juju.remove_app, token, mod, application)400 code, response = 202, "The application is being removed"401 LOGGER.info('/TENGU/controllers/%s/models/%s/applications/%s [DELETE] => Removing application!', controller, model, application)402 else:403 code, response = errors.does_not_exist('application')404 LOGGER.error('/TENGU/controllers/%s/models/%s/applications/%s [DELETE] => Application does not exist!', controller, model, application)405 else:406 code, response = errors.no_permission()407 LOGGER.error('/TENGU/controllers/%s/models/%s/applications/%s [DELETE] => No Permission to perform this action!', controller, model, application)408 except KeyError:409 code, response = errors.invalid_data()410 error_log()411 except HTTPException:412 ers = error_log()413 raise414 except Exception:415 ers = error_log()416 code, response = errors.cmd_error(ers)417 return juju.create_response(code, response)418@TENGU.route('/controllers/<controller>/models/<model>/applications/<application>/config', methods=['GET'])419def get_application_config(controller, model, application):420 try:421 LOGGER.info('/TENGU/controllers/%s/models/%s/applications/%s/config [GET] => receiving call', controller, model, application)422 token = execute_task(juju.authenticate, request.headers['api-key'], request.authorization)423 LOGGER.info('/TENGU/controllers/%s/models/%s/applications/%s/config [GET] => Authenticated!', controller, model, application)424 con, mod = juju.authorize( token, controller, model)425 LOGGER.info('/TENGU/controllers/%s/models/%s/applications/%s/config [GET] => Authorized!', controller, model, application)426 code, response = 200, execute_task(juju.get_application_config, token, mod, application)427 LOGGER.info('/TENGU/controllers/%s/models/%s/applications/%s/config [GET] => Succesfully retrieved application config!', controller, model, application)428 except KeyError:429 code, response = errors.invalid_data()430 error_log()431 except HTTPException:432 ers = error_log()433 raise434 except Exception:435 ers = error_log()436 code, response = errors.cmd_error(ers)437 return juju.create_response(code, response)438@TENGU.route('/controllers/<controller>/models/<model>/applications/<application>/config', methods=['PUT'])439def set_application_config(controller, model, application):440 try:441 LOGGER.info('/TENGU/controllers/%s/models/%s/applications/%s/config [PUT] => receiving call', controller, model, application)442 data = request.json443 token = execute_task(juju.authenticate, request.headers['api-key'], request.authorization)444 LOGGER.info('/TENGU/controllers/%s/models/%s/applications/%s/config [PUT] => Authenticated!', controller, model, application)445 con, mod = juju.authorize( token, controller, model)446 LOGGER.info('/TENGU/controllers/%s/models/%s/applications/%s/config [PUT] => Authorized!', controller, model, application)447 if mod.m_access == 'write' or mod.m_access == 'admin':448 execute_task(juju.set_application_config, token, mod, application, data.get('config', None))449 LOGGER.info('/TENGU/controllers/%s/models/%s/applications/%s/config [PUT] => Config parameter is being changed!', controller, model, application)450 code, response = 202, "The config parameter is being changed"451 else:452 code, response = errors.no_permission()453 LOGGER.error('/TENGU/controllers/%s/models/%s/applications/%s/config [PUT] => No Permission to perform this action!', controller, model, application)454 except KeyError:455 code, response = errors.invalid_data()456 error_log()457 except HTTPException:458 ers = error_log()459 raise460 except Exception:461 ers = error_log()462 code, response = errors.cmd_error(ers)463 return juju.create_response(code, response)464@TENGU.route('/controllers/<controller>/models/<model>/machines', methods=['GET'])465def get_machines_info(controller, model):466 try:467 LOGGER.info('/TENGU/controllers/%s/models/%s/machines [GET] => receiving call', controller, model)468 token = execute_task(juju.authenticate, request.headers['api-key'], request.authorization)469 LOGGER.info('/TENGU/controllers/%s/models/%s/machines [GET] => Authenticated!', controller, model)470 con, mod = juju.authorize( token, controller, model)471 LOGGER.info('/TENGU/controllers/%s/models/%s/machines [GET] => Authorized!', controller, model)472 code, response = 200, execute_task(juju.get_machines_info, token, mod)473 LOGGER.info('/TENGU/controllers/%s/models/%s/machines [GET] => Succesfully retrieved machine information!', controller, model)474 except KeyError:475 code, response = errors.invalid_data()476 error_log()477 except HTTPException:478 ers = error_log()479 raise480 except Exception:481 ers = error_log()482 code, response = errors.cmd_error(ers)483 return juju.create_response(code, response)484@TENGU.route('/controllers/<controller>/models/<model>/machines', methods=['POST'])485def add_machine(controller, model):486 try:487 LOGGER.info('/TENGU/controllers/%s/models/%s/machines [POST] => receiving call', controller, model)488 data = request.json489 token = execute_task(juju.authenticate, request.headers['api-key'], request.authorization)490 LOGGER.info('/TENGU/controllers/%s/models/%s/machines [POST] => Authenticated!', controller, model)491 con, mod = juju.authorize( token, controller, model)492 LOGGER.info('/TENGU/controllers/%s/models/%s/machines [POST] => Authorized!', controller, model)493 if mod.m_access == 'write' or mod.m_access == 'admin':494 constraints = data.get('constraints', None)495 if constraints:496 juju.check_constraints(data.get(constraints, True))497 series = data.get('series', None)498 if juju.cloud_supports_series(con, series):499 execute_task(juju.add_machine, token, mod, series, constraints)500 LOGGER.info('/TENGU/controllers/%s/models/%s/machines [POST] => Creating Machine!', controller, model)501 code, response = 202, 'Machine is being deployed!'502 else:503 code, response = 400, 'This cloud does not support this version of Ubuntu'504 LOGGER.error('/TENGU/controllers/%s/models/%s/machines [POST] => This cloud does not support this version of Ubuntu!', controller, model)505 else:506 code, response = errors.no_permission()507 LOGGER.error('/TENGU/controllers/%s/models/%s/machines [POST] => No Permission to perform this action!', controller, model)508 except KeyError:509 code, response = errors.invalid_data()510 error_log()511 except HTTPException:512 ers = error_log()513 raise514 except Exception:515 ers = error_log()516 code, response = errors.cmd_error(ers)517 return juju.create_response(code, response)518@TENGU.route('/controllers/<controller>/models/<model>/machines/<machine>', methods=['GET'])519def get_machine_info(controller, model, machine):520 try:521 LOGGER.info('/TENGU/controllers/%s/models/%s/machines/%s [GET] => receiving call', controller, model, machine)522 token = execute_task(juju.authenticate, request.headers['api-key'], request.authorization)523 LOGGER.info('/TENGU/controllers/%s/models/%s/machines/%s [GET] => Authenticated!', controller, model, machine)524 con, mod = juju.authorize( token, controller, model)525 LOGGER.info('/TENGU/controllers/%s/models/%s/machines/%s [GET] => Authorized!', controller, model, machine)526 if execute_task(juju.machine_exists, token, mod, machine):527 code, response = 200, execute_task(juju.get_machine_info, token, mod, machine)528 LOGGER.info('/TENGU/controllers/%s/models/%s/machines/%s [GET] => Succesfully retrieved machine information!', controller, model, machine)529 else:530 code, response = errors.does_not_exist('machine')531 LOGGER.error('/TENGU/controllers/%s/models/%s/machines/%s [GET] => Machine does not exist!', controller, model, machine)532 except KeyError:533 code, response = errors.invalid_data()534 error_log()535 except HTTPException:536 ers = error_log()537 raise538 except Exception:539 ers = error_log()540 code, response = errors.cmd_error(ers)541 return juju.create_response(code, response)542@TENGU.route('/controllers/<controller>/models/<model>/machines/<machine>', methods=['DELETE'])543def remove_machine(controller, model, machine):544 try:545 LOGGER.info('/TENGU/controllers/%s/models/%s/machines/%s [DELETE] => receiving call', controller, model, machine)546 token = execute_task(juju.authenticate, request.headers['api-key'], request.authorization)547 LOGGER.info('/TENGU/controllers/%s/models/%s/machines/%s [DELETE] => Authenticated!', controller, model, machine)548 con, mod = juju.authorize( token, controller, model)549 LOGGER.info('/TENGU/controllers/%s/models/%s/machines/%s [DELETE] => Authorized!', controller, model, machine)550 if execute_task(juju.machine_exists, token, mod, machine):551 if mod.m_access == 'write' or mod.m_access == 'admin':552 juju.remove_machine(token, con, mod, machine)553 code, response = 202, 'Machine being removed'554 LOGGER.info('/TENGU/controllers/%s/models/%s/machines/%s [GET] => Destroying machine, check remove_machine.log for more information!', controller, model, machine)555 else:556 code, response = errors.no_permission()557 LOGGER.error('/TENGU/controllers/%s/models/%s/machines/%s [DELETE] => No Permission to perform this action!', controller, model, machine)558 else:559 code, response = errors.does_not_exist('machine')560 LOGGER.error('/TENGU/controllers/%s/models/%s/machines/%s [DELETE] => Machine does not exist!', controller, model, machine)561 except KeyError:562 code, response = errors.invalid_data()563 error_log()564 except HTTPException:565 ers = error_log()566 raise567 except Exception:568 ers = error_log()569 code, response = errors.cmd_error(ers)570 return juju.create_response(code, response)571@TENGU.route('/controllers/<controller>/models/<model>/applications/<application>/units', methods=['GET'])572def get_units_info(controller, model, application):573 try:574 LOGGER.info('/TENGU/controllers/%s/models/%s/applications/%s/units [GET] => receiving call', controller, model, application)575 token = execute_task(juju.authenticate, request.headers['api-key'], request.authorization)576 LOGGER.info('/TENGU/controllers/%s/models/%s/applications/%s/units [GET] => Auhtenticated!', controller, model, application)577 con, mod = juju.authorize( token, controller, model)578 LOGGER.info('/TENGU/controllers/%s/models/%s/applications/%s/units [GET] => Authorized!', controller, model, application)579 if execute_task(juju.app_exists, token, con, mod, application):580 code, response = 200, execute_task(juju.get_units_info, token, mod, application)581 LOGGER.info('/TENGU/controllers/%s/models/%s/applications/%s/units [GET] => Succesfully retrieved units info!', controller, model, application)582 else:583 code, response = errors.does_not_exist('application')584 LOGGER.error('/TENGU/controllers/%s/models/%s/applications/%s/units [GET] => Application does not exist!', controller, model, application)585 except KeyError:586 code, response = errors.invalid_data()587 error_log()588 except HTTPException:589 ers = error_log()590 raise591 except Exception:592 ers = error_log()593 code, response = errors.cmd_error(ers)594 return juju.create_response(code, response)595@TENGU.route('/controllers/<controller>/models/<model>/applications/<application>/units', methods=['POST'])596def add_unit(controller, model, application):597 try:598 LOGGER.info('/TENGU/controllers/%s/models/%s/applications/%s/units [POST] => receiving call', controller, model, application)599 data = request.json600 token = execute_task(juju.authenticate, request.headers['api-key'], request.authorization)601 LOGGER.info('/TENGU/controllers/%s/models/%s/applications/%s/units [POST] => Authenticated!', controller, model, application)602 con, mod = juju.authorize( token, controller, model)603 LOGGER.info('/TENGU/controllers/%s/models/%s/applications/%s/units [POST] => Authorized!', controller, model, application)604 if execute_task(juju.app_exists, token, con, mod, application):605 if mod.m_access == 'write' or mod.m_access == 'admin':606 juju.add_unit(token, con, mod, application, data.get('amount', 1), data.get('target', 'None'))607 code, response = 202, "Unit is being created"608 LOGGER.info('/TENGU/controllers/%s/models/%s/applications/%s/units [POST] => Unit is being created, check add_unit.log for more information!', controller, model, application)609 else:610 code, response = errors.no_permission()611 LOGGER.error('/TENGU/controllers/%s/models/%s/applications/%s/units [POST] => No Permission to perform this action!', controller, model, application)612 else:613 code, response = errors.does_not_exist('application')614 LOGGER.error('/TENGU/controllers/%s/models/%s/applications/%s/units [POST] => Application does not exist!', controller, model, application)615 except KeyError:616 code, response = errors.invalid_data()617 error_log()618 except HTTPException:619 ers = error_log()620 raise621 except Exception:622 ers = error_log()623 code, response = errors.cmd_error(ers)624 return juju.create_response(code, response)625@TENGU.route('/controllers/<controller>/models/<model>/applications/<application>/units/<unitnumber>', methods=['GET'])626def get_unit_info(controller, model, application, unitnumber):627 try:628 LOGGER.info('/TENGU/controllers/%s/models/%s/applications/%s/units/%s [GET] => receiving call', controller, model, application, unitnumber)629 token = execute_task(juju.authenticate, request.headers['api-key'], request.authorization)630 LOGGER.info('/TENGU/controllers/%s/models/%s/applications/%s/units/%s [GET] => Authenticated!', controller, model, application, unitnumber)631 con, mod = juju.authorize( token, controller, model)632 LOGGER.info('/TENGU/controllers/%s/models/%s/applications/%s/units/%s [GET] => Authorized!', controller, model, application, unitnumber)633 unit = execute_task(juju.get_unit_info, token, mod, application, unitnumber)634 if unit is not {}:635 code, response = 200, unit636 LOGGER.info('/TENGU/controllers/%s/models/%s/applications/%s/units/%s [GET] => Succesfully retrieved Unit information!', controller, model, application, unitnumber)637 else:638 code, response = errors.does_not_exist('unit')639 LOGGER.error('/TENGU/controllers/%s/models/%s/applications/%s/units/%s [GET] => Unit does not exist!', controller, model, application, unitnumber)640 except KeyError:641 code, response = errors.invalid_data()642 error_log()643 except HTTPException:644 ers = error_log()645 raise646 except Exception:647 ers = error_log()648 code, response = errors.cmd_error(ers)649 return juju.create_response(code, response)650@TENGU.route('/controllers/<controller>/models/<model>/applications/<application>/units/<unitnumber>', methods=['DELETE'])651def remove_unit(controller, model, application, unitnumber):652 try:653 LOGGER.info('/TENGU/controllers/%s/models/%s/applications/%s/units/%s [DELETE] => receiving call', controller, model, application, unitnumber)654 token = execute_task(juju.authenticate, request.headers['api-key'], request.authorization)655 LOGGER.info('/TENGU/controllers/%s/models/%s/applications/%s/units/%s [DELETE] => Authenticated!', controller, model, application, unitnumber)656 con, mod = juju.authorize( token, controller, model)657 LOGGER.info('/TENGU/controllers/%s/models/%s/applications/%s/units/%s [DELETE] => Authorized!', controller, model, application, unitnumber)658 if execute_task(juju.get_unit_info, token, mod, application, unitnumber) is not {}:659 if mod.m_access == 'write' or mod.m_access == 'admin':660 execute_task(juju.remove_unit, token, mod, application, unitnumber)661 LOGGER.info('/TENGU/controllers/%s/models/%s/applications/%s/units/%s [DELETE] => Unit is being removed!', controller, model, application, unitnumber)662 code, response = 202, "Unit is being removed"663 else:664 code, response = errors.no_permission()665 LOGGER.error('/TENGU/controllers/%s/models/%s/applications/%s/units/%s [DELETE] => No Permission to perform this action!', controller, model, application, unitnumber)666 else:667 code, response = errors.does_not_exist('unit')668 LOGGER.error('/TENGU/controllers/%s/models/%s/applications/%s/units/%s [DELETE] => Unit does not exist!', controller, model, application, unitnumber)669 except KeyError:670 code, response = errors.invalid_data()671 error_log()672 except HTTPException:673 ers = error_log()674 raise675 except Exception:676 ers = error_log()677 code, response = errors.cmd_error(ers)678 return juju.create_response(code, response)679@TENGU.route('/controllers/<controller>/models/<model>/relations', methods=['GET'])680def get_relations_info(controller, model):681 try:682 LOGGER.info('/TENGU/controllers/%s/models/%s/relations [GET] => receiving call', controller, model)683 token = execute_task(juju.authenticate, request.headers['api-key'], request.authorization)684 LOGGER.info('/TENGU/controllers/%s/models/%s/relations [GET] => Authenticated!', controller, model)685 con, mod = juju.authorize( token, controller, model)686 LOGGER.info('/TENGU/controllers/%s/models/%s/relations [GET] => Authorized!', controller, model)687 code, response = 200, execute_task(juju.get_relations_info, token, mod)688 LOGGER.info('/TENGU/controllers/%s/models/%s/relations [GET] => Succesfully retrieved relation info!', controller, model)689 except KeyError:690 code, response = errors.invalid_data()691 error_log()692 except HTTPException:693 ers = error_log()694 raise695 except Exception:696 ers = error_log()697 code, response = errors.cmd_error(ers)698 return juju.create_response(code, response)699@TENGU.route('/controllers/<controller>/models/<model>/relations', methods=['PUT'])700def add_relation(controller, model):701 try:702 LOGGER.info('/TENGU/controllers/%s/models/%s/relations [PUT] => receiving call', controller, model)703 data = request.json704 token = execute_task(juju.authenticate, request.headers['api-key'], request.authorization)705 LOGGER.info('/TENGU/controllers/%s/models/%s/relations [PUT] => Authenticated!', controller, model)706 con, mod = juju.authorize( token, controller, model)707 LOGGER.info('/TENGU/controllers/%s/models/%s/relations [PUT] => Authorized!', controller, model)708 #proper check will have to be implemented709 app1, app2 = data['app1'], data['app2']710 if execute_task(juju.app_exists, token, con, mod, app1) and execute_task(juju.app_exists, token, con, mod, app2):711 if mod.m_access == 'write' or mod.m_access == 'admin':712 execute_task(juju.add_relation, token, mod, app1, app2)713 code, response = 200, execute_task(juju.get_relations_info, token, mod)714 LOGGER.info('/TENGU/controllers/%s/models/%s/relations [PUT] => Relationship succesfully created.', controller, model)715 else:716 code, response = errors.no_permission()717 LOGGER.error('/TENGU/controllers/%s/models/%s/relations [PUT] => No permission to perform this acion!', controller, model)718 else:719 code, response = errors.does_not_exist('application')720 LOGGER.error('/TENGU/controllers/%s/models/%s/relations [PUT] => Application does not exist!', controller, model)721 except KeyError:722 code, response = errors.invalid_data()723 error_log()724 except HTTPException:725 ers = error_log()726 raise727 except Exception:728 ers = error_log()729 code, response = errors.cmd_error(ers)730 return juju.create_response(code, response)731@TENGU.route('/controllers/<controller>/models/<model>/relations/<application>', methods=['GET'])732def get_relations(controller, model, application):733 try:734 LOGGER.info('/TENGU/controllers/%s/models/%s/relations/%s [GET] => receiving call', controller, model, application)735 token = execute_task(juju.authenticate, request.headers['api-key'], request.authorization)736 LOGGER.info('/TENGU/controllers/%s/models/%s/relations/%s [GET] => Authenticated!', controller, model, application)737 con, mod = juju.authorize( token, controller, model)738 LOGGER.info('/TENGU/controllers/%s/models/%s/relations/%s [GET] => Authorized!', controller, model, application)739 if execute_task(juju.app_exists, token, con, mod, application):740 code, response = 200, execute_task(juju.get_application_info, token, mod, application)['relations']741 LOGGER.info('/TENGU/controllers/%s/models/%s/relations/%s [GET] => Succesfully retrieved application info!', controller, model, application)742 else:743 code, response = errors.does_not_exist('application')744 LOGGER.error('/TENGU/controllers/%s/models/%s/relations/%s [GET] => Application does not exist!', controller, model, application)745 except KeyError:746 code, response = errors.invalid_data()747 error_log()748 except HTTPException:749 ers = error_log()750 raise751 except Exception:752 ers = error_log()753 code, response = errors.cmd_error(ers)754 return juju.create_response(code, response)755@TENGU.route('/controllers/<controller>/models/<model>/relations/<app1>/<app2>', methods=['DELETE'])756def remove_relation(controller, model, app1, app2):757 try:758 LOGGER.info('/TENGU/controllers/%s/models/%s/relations/%s/%s [DELETE] => receiving call', controller, model, app1, app2)759 token = execute_task(juju.authenticate, request.headers['api-key'], request.authorization)760 LOGGER.info('/TENGU/controllers/%s/models/%s/relations/%s/%s [DELETE] => Authenticated!', controller, model, app1, app2)761 con, mod = juju.authorize( token, controller, model)762 LOGGER.info('/TENGU/controllers/%s/models/%s/relations/%s/%s [DELETE] => Authorized!', controller, model, app1, app2)763 if execute_task(juju.app_exists, token, con, mod, app1) and execute_task(juju.app_exists, token, con, mod, app2):764 if mod.m_access == 'write' or mod.m_access == 'admin':765 execute_task(juju.remove_relation, token, mod, app1, app2)766 code, response = 202, 'The relation is being removed'767 LOGGER.info('/TENGU/controllers/%s/models/%s/relations/%s/%s [DELETE] => Relation is being removed!', controller, model, app1, app2)768 else:769 code, response = errors.no_permission()770 LOGGER.error('/TENGU/controllers/%s/models/%s/relations/%s/%s [DELETE] => No Permission to perform this action!', controller, model, app1, app2)771 else:772 code, response = errors.does_not_exist('application')773 LOGGER.error('/TENGU/controllers/%s/models/%s/relations/%s/%s [DELETE] => Application does not exist!', controller, model, app1, app2)774 except KeyError:775 code, response = errors.invalid_data()776 error_log()777 except HTTPException:778 ers = error_log()779 raise780 except Exception:781 ers = error_log()782 code, response = errors.cmd_error(ers)783 return juju.create_response(code, response)784# On hold785# TO DO: Backup and restore calls786@TENGU.route('/backup', methods=['GET'])787def backup_controllers():788 try:789 LOGGER.info('/TENGU/backup [GET] => receiving call')790 token = execute_task(juju.authenticate, request.headers['api-key'], request.authorization)791 if token.is_admin:792 apidir = juju.get_api_dir()793 homedir = '/home/{}/.local/share/juju'.format(juju.get_api_user())794 try:795 shutil.copytree('/home/{}/credentials'.format(juju.get_api_user()), '{}/backup/credentials'.format(apidir))796 shutil.copytree(homedir, '{}/backup/juju'.format(apidir))797 except Exception: # FileExistsError798 os.rmdir('{}/backup/credentials'.format(apidir))799 os.rmdir(homedir)800 shutil.copytree('/home/{}/credentials'.format(juju.get_api_user()), '{}/backup/credentials'.format(apidir))801 shutil.copytree(homedir, '{}/backup/juju'.format(apidir))802 except Exception: # FileNotFoundError803 pass804 shutil.make_archive('{}/backup'.format(apidir), 'zip', '{}/backup/'.format(apidir))805 return send_file('{}/backup.zip'.format(apidir))806 else:807 code, response = errors.no_permission()808 except KeyError:809 code, response = errors.invalid_data()810 error_log()811 except HTTPException:812 ers = error_log()813 raise814 except Exception:815 ers = error_log()816 code, response = errors.cmd_error(ers)817 return juju.create_response(code, response)818# On hold819@TENGU.route('/restore', methods=['POST'])820def restore_controllers():821 try:822 LOGGER.info('/TENGU/restore [POST] => receiving call')823 token = execute_task(juju.authenticate, request.headers['api-key'], request.authorization)824 if token.is_admin:825 homedir = '/home/{}/.local/share/juju'.format(juju.get_api_user())826 if 'backup' in request.files:827 file = request.files['backup']828 filename = file.filename829 tmpdir = tempfile.mkdtemp()830 saved_loc = os.path.join(tmpdir, filename)831 file.save(saved_loc)832 zip_ref = zipfile.ZipFile(saved_loc, 'r')833 zip_ref.extractall(tmpdir)834 zip_ref.close()835 shutil.rmtree(homedir)836 shutil.rmtree('/home/{}/credentials'.format(juju.get_api_user()))837 shutil.copytree('{}/juju'.format(tmpdir), homedir)...

Full Screen

Full Screen

slot_table.py

Source:slot_table.py Github

copy

Full Screen

1#-*- coding:utf8 -*-2# Copyright (c) 2021 barriery3# Python release: 3.7.04# Create time: 2021-07-255import multiprocessing6from readerwriterlock import rwlock7import logging8import os9from typing import List, Dict, Union, Optional10import stream_lite.proto.common_pb2 as common_pb211from stream_lite.proto import task_manager_pb212import stream_lite.config13from stream_lite.network import serializator14from stream_lite.server.subtask_server import SubTaskServer15_LOGGER = logging.getLogger(__name__)16class Slot(object):17 def __init__(self, 18 tm_name: str,19 jobid: str,20 job_manager_enpoint: str,21 execute_task: serializator.SerializableExectueTask,22 state: Union[None, task_manager_pb2.DeployTaskRequest.State]):23 self.tm_name = tm_name24 self.jobid = jobid25 self.job_manager_enpoint = job_manager_enpoint26 self.subtask = SubTaskServer(27 tm_name=self.tm_name, 28 jobid=self.jobid, 29 job_manager_enpoint=self.job_manager_enpoint, 30 execute_task=execute_task,31 state=state)32 self.state = state33 self.status = "DEPLOYED"34 def start(self):35 self.status = "RUNNING"36 self.subtask.run_on_standalone_process(37 is_process=stream_lite.config.IS_PROCESS)38 def __str__(self):39 return "[{}] subtask_name: {}, status: {}".format(40 self.cls_name, 41 self.execute_task.subtask_name, 42 self.status)43class SlotTable(object):44 def __init__(self, tm_name: str, job_manager_enpoint: str, capacity: int):45 self.rw_lock_pair = rwlock.RWLockFair()46 self.tm_name = tm_name47 self.capacity = capacity48 self.job_manager_enpoint = job_manager_enpoint49 self.table = {} # subtask_name -> Slot50 def deployExecuteTask(self, 51 jobid: str,52 proto: common_pb2.ExecuteTask,53 state: Union[None, task_manager_pb2.DeployTaskRequest.State]) -> None:54 """55 add a slot by execute_task56 """57 execute_task = serializator.SerializableExectueTask.from_proto(proto)58 with self.rw_lock_pair.gen_wlock():59 if len(self.table) >= self.capacity:60 raise RuntimeError(61 "Failed to deploy task: out of slot_table capacity")62 subtask_name = execute_task.subtask_name63 if subtask_name in self.table:64 raise KeyError(65 "Failed to deploy task: subtask_name({}) already exists"66 .format(subtask_name))67 self.table[subtask_name] = Slot(68 tm_name=self.tm_name, 69 jobid=jobid,70 job_manager_enpoint=self.job_manager_enpoint, 71 execute_task=execute_task,72 state=state)73 _LOGGER.debug("Succ deploy task: {}".format(subtask_name))74 def startExecuteTask(self, subtask_name: str) -> None:75 slot = self.getSlot(subtask_name)76 slot.start()77 _LOGGER.debug("Succ start task: {}".format(subtask_name))78 def hasSlot(self, name: str) -> bool:79 with self.rw_lock_pair.gen_rlock():80 return name in self.table81 def getSlot(self, name: str) -> Slot:82 with self.rw_lock_pair.gen_rlock():83 if name not in self.table:84 raise KeyError(85 "Failed: task(subtask_name={}) not deployed".format(name))...

Full Screen

Full Screen

docker_once.py

Source:docker_once.py Github

copy

Full Screen

1import asyncio2import base643import traceback4import typing5import interactive_widgets.backend.executors.docker_executor6class DockerOnce(interactive_widgets.backend.executors.docker_executor.DockerExecutor):7 def __init__(self, *args, **kwargs):8 super().__init__(*args, **kwargs)9 self.execute_task: typing.Optional[asyncio.Task] = None10 async def _execute(self):11 try:12 await self.send_message({13 'type': 'started',14 })15 await self._run_once()16 await self.send_message({17 'type': 'finished',18 })19 except:20 await self.send_message({21 'type': 'errored',22 'message': base64.b64encode(traceback.format_exc().encode('utf-8')).decode('utf-8'),23 })24 finally:25 self.execute_task = None26 async def handle_message(self, message: typing.Any):27 # TODO: enhance what happens if a task already executes28 if self.execute_task is None:29 self.execute_task = asyncio.create_task(self._execute())30 async def tear_down(self):31 if self.execute_task is not None:32 self.execute_task.cancel()33 try:34 await self.execute_task35 except asyncio.CancelledError:...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run locust automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful