def start_app(name, num): logger.info(" start service") print "start_app()" time1 = time() (code, text) = apicall_claas_service('start_service', name) logger.info(" apicall returns (%d, %s)" % (code, text)) if code < 200 or code >= 300: msg = "Error in calling start API {}:{}".format(code, text) return {"success": False, "total": msg} target = {"status": "Running", "num": num} if not get_events(name, "start"): return {"success": False, "total": "this action do not have events"} isdeploying = is_deploying(name, target, 180, settings.CLAAS_NAMESPACE) time2 = time() if isdeploying: (code, text, obj) = get_service_info(name, 0, settings.CLAAS_NAMESPACE) if 'current_status' in obj: if obj['current_status'].find('Error') >= 0: return { "success": False, "total": "restart {} service status is Error".format(name) } return { "success": False, "total": "Timeout in starting {}".format(name) } logger.info(" ==> Starting service uses {}s".format(time2 - time1)) obj = {"success": True, "total": time2 - time1} return obj
def create_service(name, payload, service_detail='Hello', region='STAGING'): delete_app(name, region) print "create claas service" logger.info(region + " Start creating service") time1 = time() print payload (code, text) = apicall_claas_service('create_service', name, payload) logger.info("apicall returns (%d, %s)" % (code, text)) print("apicall returns (%d, %s)" % (code, text)) if code < 200 or code >= 300: msg = "Error in calling create Claas API {}:{}".format(code, text) return {"success": False, "total": msg} if not get_events(name, "create"): return {"success": False, "total": "this action do not have events"} # test if at least 1 instance is running target = {"status": "Running", "text": service_detail} isdeploying = is_deploying(name, target, 200, settings.CLAAS_NAMESPACE) time2 = time() logger.info(region + " is_deploying check use {}s".format(time2 - time1)) if isdeploying: (code, text, obj) = get_service_info(name, 0, settings.CLAAS_NAMESPACE) if 'current_status' in obj: if obj['current_status'].find('Error') >= 0: return { "success": False, "total": "create service {} status is Error".format(name) } if "instance_ports" in json.loads(text) and len( json.loads(text)['instance_ports']): if "endpoint_type" in json.loads(text)['instance_ports'][ 0] and json.loads(text)['instance_ports'][0][ 'endpoint_type'] == "internal-endpoint": (code, html) = get_service_result( json.loads(text)["instance_ports"][0] ["default_domain"]) if code > 300 or code < 200: return {"success": True, "total": time2 - time1} else: return { "success": False, "total": "internal haproxy service should not can visit" } flag = can_visit(target, text) if not flag and obj['current_status'] == 'Running': return { "success": False, "total": "create service {} can not visit".format(name) } msg = "Timeout in creating {} service".format(name) return {"success": False, "total": msg} logger.info(region + " create use {}s".format(time2 - time1)) return {"success": True, "total": time2 - time1}
def update_service(name, num, size, service_detail='Hello'): print "scale_app()" logger.info(" Start scaling service {}".format(name)) time1 = time() payload = { # "image_tag": "latest", "target_num_instances": num, "instance_size": size } (code, text) = apicall_claas_service('modify_service', name, payload) logger.info(" apicall returns (%d, %s)" % (code, text)) if code < 200 or code >= 300: msg = "Error in calling scale API {}:{}".format(code, text) return {"success": False, "total": msg} if not get_events(name, "update"): return {"success": False, "total": "this action do not have events"} target = { "status": "Running", "num": num, "size": size, "text": service_detail } isdeploying = is_deploying(name, target, 240, settings.CLAAS_NAMESPACE) time2 = time() if isdeploying: (code, text, obj) = get_service_info(name, 0, settings.CLAAS_NAMESPACE) if 'current_status' in obj: if obj['current_status'].find('Error') >= 0: return { "success": False, "total": "update service {} status is Error".format(name) } flag = can_visit(target, text) if not flag and obj['current_status'] == 'Running': return { "success": False, "total": "update service {} can not visit".format(name) } msg = "Timeout in scaling {}".format(name) return {"success": False, "total": msg} logger.info(" ==> Scaling uses {}s".format(time2 - time1)) return {"success": True, "total": time2 - time1}
def stop_app(name): logger.info(" Stopping service") print "stop_app()" time1 = time() (code, text) = apicall_claas_service('stop_service', name) logger.info(" apicall returns (%d, %s)" % (code, text)) if code < 200 or code >= 300: msg = "Error in calling stop API {}:{}".format(code, text) return {"success": False, "total": msg} if not get_events(name, "stop"): return {"success": False, "total": "this action do not have events"} target = {"status": "Stopped", "num": 0} isdeploying = is_deploying(name, target, 120, settings.CLAAS_NAMESPACE) time2 = time() if isdeploying: return { "success": False, "total": "Timeout in stopping {}".format(name) } logger.info(" ==> Stopping service uses {}s".format(time2 - time1)) obj = {"success": True, "total": time2 - time1} return obj