def poll(): """ Callback function that polls for new tasks based on a schedule. """ logging.info("Polling for new task.") deployment_id = helper.get_deployment_id() # If the deployment is not registered, skip. if not deployment_id: return # Send request to AppScale Portal. url = "{0}{1}".format(hermes_constants.PORTAL_URL, hermes_constants.PORTAL_POLL_PATH) data = json.dumps({ JSONTags.DEPLOYMENT_ID: deployment_id }) request = helper.create_request(url=url, method='POST', body=data) response = helper.urlfetch(request) try: data = json.loads(response.body) except (TypeError, ValueError) as error: logging.error("Cannot parse response from url '{0}'. Error: {1}". format(url, str(error))) return # Verify all necessary fields are present in the request. if not set(data.keys()).issuperset(set(hermes_constants.REQUIRED_KEYS)) or \ None in data.values(): logging.error("Missing args in response: {0}".format(response)) return logging.debug("Task to run: {0}".format(data)) logging.info("Redirecting task request to TaskHandler.") url = "{0}{1}".format(hermes_constants.HERMES_URL, TaskHandler.PATH) request = helper.create_request(url, method='POST', body=data) # The poller can move forward without waiting for a response here. helper.urlfetch_async(request)
def poll(): """ Callback function that polls for new tasks based on a schedule. """ deployment_id = helper.get_deployment_id() # If the deployment is not registered, skip. if not deployment_id: return # If we can't reach the backup and recovery services, skip. nodes = helper.get_node_info() http_client = tornado.httpclient.HTTPClient() for node in nodes: br_host = node[helper.NodeInfoTags.HOST] request = tornado.httpclient.HTTPRequest(br_host) try: response = http_client.fetch(request) if json.loads(response.body)['status'] != 'up': logging.warn('Backup and Recovery service at {} is not up.' .format(br_host)) return except (socket.error, ValueError): logging.exception('Backup and Recovery service at {} is not up.' .format(br_host)) return logging.info("Polling for new task.") # Send request to AppScale Portal. url = "{0}{1}".format(hermes_constants.PORTAL_URL, hermes_constants.PORTAL_POLL_PATH) data = urllib.urlencode({JSONTags.DEPLOYMENT_ID: deployment_id}) request = helper.create_request(url=url, method='POST', body=data) response = helper.urlfetch(request) if not response[JSONTags.SUCCESS]: logging.error("Inaccessible resource: {}".format(url)) return try: data = json.loads(response[JSONTags.BODY]) except (TypeError, ValueError) as error: logging.error("Cannot parse response from url '{0}'. Error: {1}". format(url, str(error))) return if data == {}: # If there's no task to perform. return # Verify all necessary fields are present in the request. if not set(data.keys()).issuperset(set(hermes_constants.REQUIRED_KEYS)): logging.error("Missing args in response: {0}".format(response)) return logging.debug("Task to run: {0}".format(data)) logging.info("Redirecting task request to TaskHandler.") url = "{0}{1}".format(hermes_constants.HERMES_URL, TaskHandler.PATH) request = helper.create_request(url, method='POST', body=json.dumps(data)) # The poller can move forward without waiting for a response here. helper.urlfetch_async(request)
def send_all_stats(): """ Calls get_all_stats and sends the deployment monitoring stats to the AppScale Portal. """ deployment_id = helper.get_deployment_id() # If the deployment is not registered, skip. if not deployment_id: return # Get all stats from this deployment. logging.debug("Getting all stats from every deployment node.") all_stats = helper.get_all_stats() # Send request to AppScale Portal. portal_path = hermes_constants.PORTAL_STATS_PATH.format(deployment_id) url = "{0}{1}".format(hermes_constants.PORTAL_URL, portal_path) data = { JSONTags.DEPLOYMENT_ID: deployment_id, JSONTags.TIMESTAMP: datetime.datetime.utcnow(), JSONTags.ALL_STATS: json.dumps(all_stats) } logging.debug("Sending all stats to the AppScale Portal. Data: \n{}". format(data)) request = helper.create_request(url=url, method='POST', body=urllib.urlencode(data)) response = helper.urlfetch(request) if not response[JSONTags.SUCCESS]: logging.error("Inaccessible resource: {}".format(url)) return
def send_cluster_stats(): """ Calls get_cluster_stats and sends the deployment monitoring stats to the AppScale Portal. """ deployment_id = helper.get_deployment_id() # If the deployment is not registered, skip. if not deployment_id: return # Get all stats from this deployment. logging.debug("Getting all stats from every deployment node.") cluster_stats = helper.get_cluster_stats() # Send request to AppScale Portal. portal_path = hermes_constants.PORTAL_STATS_PATH.format(deployment_id) url = "{0}{1}".format(hermes_constants.PORTAL_URL, portal_path) data = { JSONTags.DEPLOYMENT_ID: deployment_id, JSONTags.TIMESTAMP: datetime.datetime.utcnow(), JSONTags.ALL_STATS: json.dumps(cluster_stats) } logging.debug("Sending all stats to the AppScale Portal. Data: \n{}". format(data)) request = helper.create_request(url=url, method='POST', body=urllib.urlencode(data)) response = helper.urlfetch(request) if not response[JSONTags.SUCCESS]: logging.error("Inaccessible resource: {}".format(url)) return
def api_notify(event, context): validation = helper.validate_response(event) if validation['statusCode']!=200: return validation validatedrequest = helper.create_request(event) sns_status = sns_publisher.publish_sns_message(SNS_ARN,validatedrequest) dynamoDbStatus = data_writer.write_data(validatedrequest) return helper.boolean_based_response(sns_status, dynamoDbStatus)
def contact(self): dsl = { 'uri': "/WEBAPI/appserver/org/contact", } params = helper.create_request(**dsl) @timer(uri=dsl['uri'], params=str(params)) def request(): self.client.get(dsl['uri'], headers=Mobile.header, params=params) request()
def app_account(self): dsl = { 'uri': "/WEBAPI/appserver/app-account/", } params = helper.create_request(**dsl) @timer(uri=dsl['uri'], params=str(params)) def request(): self.client.get(dsl['uri'], headers=Mobile.header, params=params, hooks=dict(response=helper.print_result)) request()
def on_start(self): """ on_start is called when a Locust start before any task is scheduled """ dsl = { 'uri': "/WEBAPI/auth/accessToken/", 'required': { 'user':'******','password':'******','domain':'987654321','platform':'mobile' }, } if not Web.header.has_key('SESSION-TOKEN'): params = helper.create_request(**dsl) r = self.client.post(dsl['uri'], data=params) self.header['SESSION-TOKEN'] = json.loads(r.text)['result']['token']
def field_values(self): dsl = { 'uri': "/WEBAPI/webserver/view/fieldvalues", 'optional': { 'id': 1 } } params = helper.create_request(**dsl) @timer(uri=dsl['uri'], params=str(params)) def request(): self.client.get(dsl['uri'], headers=Web.header, params=params) request()
def info(self): dsl = { 'uri': "/WEBAPI/webserver/view/info", 'optional': { 'xt_view_id': [1, 2], } } params = helper.create_request(**dsl) @timer(uri=dsl['uri'], params=str(params)) def request(): self.client.get(dsl['uri'], headers=Web.header, params=params) request()
def view_list(self): dsl = { 'uri': "/WEBAPI/appserver/view/list", 'optional': { 'view_type': 'Sales' } } params = helper.create_request(**dsl) @timer(uri=dsl['uri'], params=str(params)) def request(): self.client.get(dsl['uri'], headers=Mobile.header, params=params) request()
def filter(self): dsl = { 'uri': "/WEBAPI/appserver/view/filter", 'optional': { 'xt_view_id': 1 } } params = helper.create_request(**dsl) @timer(uri=dsl['uri'], params=str(params)) def request(): self.client.get(dsl['uri'], headers=Mobile.header, params=params) request()
def connect(self): dsl = { 'uri': "/WEBAPI/webserver/extensions/connect", 'optional': { 'ap_id': [1, 2], } } params = helper.create_request(**dsl) @timer(uri=dsl['uri'], params=str(params)) def request(): self.client.get(dsl['uri'], headers=Web.header, params=params) request()
def datasource(self): dsl = { 'uri': "/WEBAPI/webserver/insights/datasource", 'optional': { 'view_id': [1, 2], } } params = helper.create_request(**dsl) @timer(uri=dsl['uri'], params=str(params)) def request(): self.client.get(dsl['uri'], headers=Web.header, params=params) request()
def poll(): """ Callback function that polls for new tasks based on a schedule. """ logging.info("Polling for new task.") deployment_id = helper.get_deployment_id() # If the deployment is not registered, skip. if not deployment_id: return # Send request to AppScale Portal. url = "{0}{1}".format(hermes_constants.PORTAL_URL, hermes_constants.PORTAL_POLL_PATH) data = json.dumps({JSONTags.DEPLOYMENT_ID: deployment_id}) request = helper.create_request(url=url, method='POST', body=data) response = helper.urlfetch(request) try: data = json.loads(response.body) except (TypeError, ValueError) as error: logging.error( "Cannot parse response from url '{0}'. Error: {1}".format( url, str(error))) return # Verify all necessary fields are present in the request. if not set(data.keys()).issuperset(set(hermes_constants.REQUIRED_KEYS)) or \ None in data.values(): logging.error("Missing args in response: {0}".format(response)) return logging.debug("Task to run: {0}".format(data)) logging.info("Redirecting task request to TaskHandler.") url = "{0}{1}".format(hermes_constants.HERMES_URL, TaskHandler.PATH) request = helper.create_request(url, method='POST', body=data) # The poller can move forward without waiting for a response here. helper.urlfetch_async(request)
def filed_values(self): dsl = { 'uri': "/WEBAPI/acs/data/analysis/fieldvalues", 'required': { 'uid': [4, 10, 119], 'id': [1,2,4,6,8,10,13,14,15,16,17,18,19,22,23] }, } params = helper.create_request(**dsl) @timer(uri=dsl['uri'], params=str(params)) def request(): self.client.get(dsl['uri'], params=params) request()
def forecast(self): dsl = { 'uri': "/WEBAPI/webserver/data/analyses/forecast", 'optional': { 'group_field': ['LeadSource','kh_Type','kh_Industry','kh_Rating','Type','kh_Name'], 'tab' : [0, 1] } } params = helper.create_request(**dsl) @timer(uri=dsl['uri'], params=str(params)) def request(): self.client.get(dsl['uri'], headers=Web.header, params=params) request()
def notice(self): dsl = { 'uri': "/WEBAPI/acs/data/analysis/notice", 'required': { 'uid': [4, 10, 119] }, 'optional': { 'ap_id': [101, 1] } } params = helper.create_request(**dsl) @timer(uri=dsl['uri'], params=str(params)) def request(): self.client.post(dsl['uri'], data=params) request()
def forecast(self): dsl = { 'uri': "/WEBAPI/acs/data/analysis/forecast", 'required': { 'uid': [4, 10, 119] }, 'optional': { 'group_field': ['LeadSource','kh_Type','kh_Industry','kh_Rating','Type','kh_Name'], 'tab' : [0, 1] } } params = helper.create_request(**dsl) @timer(uri=dsl['uri'], params=str(params)) def request(): self.client.get(dsl['uri'], params=params) request()
def performance(self): dsl = { 'uri': "/WEBAPI/webserver/data/analyses/performance", 'optional': { 'group_field': ['LeadSource','kh_Type','kh_Industry','kh_Rating','Type','kh_Name'], 'tab' : [0, 1], 'page' : (20, 200), 'num' : (20, 40), 'time_granule' : ['month','quanter'] } } params = helper.create_request(**dsl) @timer(uri=dsl['uri'], params=str(params)) def request(): self.client.get(dsl['uri'], headers=Web.header, params=params) request()
def post(self): """ POST method that sends a request for action to the corresponding deployment components. """ logging.info("Task request received: {0}, {1}".format(str(self.request), str(self.request.body))) try: data = json.loads(self.request.body) except (TypeError, ValueError) as error: logging.exception(error) logging.error("Unable to parse: {0}".format(self.request.body)) self.set_status(hermes_constants.HTTP_Codes.HTTP_BAD_REQUEST) return # Verify all necessary fields are present in request.body. logging.info("Verifying all necessary parameters are present.") if not set(data.keys()).issuperset(set(hermes_constants.REQUIRED_KEYS)): logging.error("Missing args in request: " + self.request.body) self.set_status(hermes_constants.HTTP_Codes.HTTP_BAD_REQUEST) return # Gather information for sending the requests to start off the current # task at hand. nodes = helper.get_node_info() if data[JSONTags.TYPE] == 'backup' or data[JSONTags.TYPE] == 'restore': tasks = [data[JSONTags.TYPE]] else: logging.error("Unsupported task type: '{0}'".format(data[JSONTags.TYPE])) self.set_status(hermes_constants.HTTP_Codes.HTTP_BAD_REQUEST) return logging.debug("Tasks to execute: {0}".format(tasks)) for task in tasks: # Initiate the task as pending. TASK_STATUS_LOCK.acquire(True) TASK_STATUS[data[JSONTags.TASK_ID]] = { JSONTags.TYPE: task, NodeInfoTags.NUM_NODES: len(nodes), JSONTags.STATUS: TaskStatus.PENDING } TASK_STATUS_LOCK.release() result_queue = Queue.Queue() threads = [] for node in nodes: # Create a br_service compatible JSON object. json_data = helper.create_br_json_data( node[NodeInfoTags.ROLE], task, data[JSONTags.BUCKET_NAME], node[NodeInfoTags.INDEX], data[JSONTags.STORAGE]) request = helper.create_request(url=node[NodeInfoTags.HOST], method='POST', body=json_data) # Start a thread for the request. thread = threading.Thread( target=helper.send_remote_request, name='{0}{1}'. format(data[JSONTags.TYPE], node[NodeInfoTags.HOST]), args=(request, result_queue,)) threads.append(thread) thread.start() # Wait for threads to finish. for thread in threads: thread.join() # Harvest results. results = [result_queue.get() for _ in xrange(len(nodes))] logging.debug("Task: {0}. Results: {1}.".format(task, results)) # Backup source code. app_success = False if task == 'backup': app_success = helper.\ backup_apps(data[JSONTags.STORAGE], data[JSONTags.BUCKET_NAME]) elif task == 'restore': app_success = helper.\ restore_apps(data[JSONTags.STORAGE], data[JSONTags.BUCKET_NAME]) # Update TASK_STATUS. successful_nodes = 0 for result in results: if result[JSONTags.SUCCESS]: successful_nodes += 1 TASK_STATUS_LOCK.acquire(True) all_nodes = TASK_STATUS[data[JSONTags.TASK_ID]]\ [NodeInfoTags.NUM_NODES] if successful_nodes < all_nodes: TASK_STATUS[data[JSONTags.TASK_ID]][JSONTags.STATUS] = \ TaskStatus.FAILED else: TASK_STATUS[data[JSONTags.TASK_ID]][JSONTags.STATUS] = \ TaskStatus.COMPLETE logging.info("Task: {0}. Status: {1}.".format(task, TASK_STATUS[data[JSONTags.TASK_ID]][JSONTags.STATUS])) IOLoop.instance().add_callback(callback=lambda: helper.report_status(task, data[JSONTags.TASK_ID], TASK_STATUS[data[JSONTags.TASK_ID]][JSONTags.STATUS] )) TASK_STATUS_LOCK.release() self.set_status(hermes_constants.HTTP_Codes.HTTP_OK)
def test_create_wrong_bear_type(): req = create_request('brown', 'test', 10) assert req.status_code == 500 # by design or bug ?
async def demo(): # connect() creates a client connection and receives a WebSocketClientProtocol object to send/receive WS messages async with websockets.connect("ws://localhost:8765") as ws: # navigation menu choice = '0' subchoice = '0' subchoice2 = '0' while choice != '9': # Greeting and format print("Client %s runs" % client_id) #testet client id # Menu print(config['menu']['greet'].value) print(config['menu']['data'].value) print(config['menu']['book'].value) print(config['menu']['my_books'].value) print(config['menu']['menu_choose_format'].value) print(config['menu']['end'].value) choice = input (config['menu']['choice'].value) # Exit program if choice == '9': print(config['menu']['bye'].value) time.sleep(0.1) os.remove(config_path) os._exit(1) #Quelle: https://stackoverflow.com/questions/173278/is-there-a-way-to-prevent-a-systemexit-exception-raised-from-sys-exit-from-bei bypasses exceptions elif choice == "5": print("Do Something 4") elif choice == "4": helper.config_switcher(config, config_path) # Show my booking elif choice == "3": # Read config file to get actual format value and make query config.read(config_path) calltype = config['calltype']['show_my_courses'].value # build request request = helper.create_request(config, calltype, client_id) await ws.send(et.tostring(request, encoding='utf8', method='xml')) # recv() receives data from the server response = await ws.recv() print("\n%s\n" % config['misc']['my_courses'].value + response) time.sleep(2) #Booking courses elif choice == "2": #print submenu3 print(config['submenu3']['title'].value) print(config['submenu3']['daten'].value) print(config['submenu3']['guid'].value) print(config['submenu3']['back'].value) subchoice2 = input(config['submenu3']['choice'].value) if subchoice2 == '1': print(config['submenu3']['choice_daten'].value) vorname = input("Ihr Vorname: ") nachname = input("Ihr Nachname: ") strasse = input("Ihre Strasse: ") plz = input("Ihre Postleitzahl: ") ort = input("Ihr Ort: ") land = input("Ihr Land: ") nummer = input("Ihre Telefonnummer: ") mail = input("Ihre E-Mail-Adresse: ") helper.create_kundenxml(str(client_id),vorname,nachname,strasse,plz,ort,land,nummer,mail) time.sleep(1) print("Zurueck zum Hauptmenu..") choice = '0' elif subchoice2 == '2': guid = input("Bitte die GUID des Kurses eingeben: ") config.read(config_path) calltype = config['calltype']['book_with_guid'].value format = config['misc']['format'].value request = helper.create_request(config, calltype, client_id, guid) print("Datei wird validiert...") await ws.send(et.tostring(request, encoding='utf8', method='xml')) response = await ws.recv() print(response) time.sleep(1) elif subchoice2 == '3': print("Zurueck zum Hauptmenu...") time.sleep(1) else: choice = '0' # show data elif choice == "1": #Print submenu for data print(config['submenu2']['title'].value) print(config['submenu2']['alle'].value) print(config['submenu2']['guid'].value) print(config['submenu2']['nummer'].value) print(config['submenu2']['name'].value) print(config['submenu2']['attribute'].value) print(config['submenu2']['back'].value) time.sleep(0.5) subchoice = input(config['submenu2']['choice'].value) print(subchoice) #show ALL courses if subchoice == "1": # Read config file to get actual format value and make query config.read(config_path) calltype = config['calltype']['show_all_courses'].value format = config['misc']['format'].value # build request request = helper.create_request(config, calltype, client_id) await ws.send(et.tostring(request, encoding='utf8', method='xml')) if (format == 'xml'): for i in range(3): # recv() receives data from the server response = await ws.recv() print(response) #print("\n%s\n" % config['misc']['my_courses'].value + response) else: response = await ws.recv() print("\n%s\n" % config['misc']['my_courses'].value + response) time.sleep(2) #sort with guid elif subchoice == "2": # Read config file to get query config.read(config_path) calltype = config['calltype']['show_some_elems'].value value = input(config['submenu2']['choice_guid'].value) # build request request = helper.create_elem_request(config, "guid", calltype, value, client_id) await ws.send(et.tostring(request, encoding='utf8', method='xml')) # recv() receives data from the server response = await ws.recv() print("\n%s\n" % config['misc']['searched'].value + response) time.sleep(0.1) #sort with nummer elif subchoice == "3": # Read config file to get query config.read(config_path) calltype = config['calltype']['show_some_elems'].value value = input(config['submenu2']['choice_nummer'].value) # build request request = helper.create_elem_request(config, "nummer", calltype, value, client_id) await ws.send(et.tostring(request, encoding='utf8', method='xml')) # recv() receives data from the server response = await ws.recv() print("\n%s\n" % config['misc']['searched'].value + response) time.sleep(0.1) #sort with names elif subchoice == "4": # Read config file to get query config.read(config_path) calltype = config['calltype']['show_some_elems'].value value = input(config['submenu2']['choice_name'].value) # build request request = helper.create_elem_request(config, "name", calltype, value, client_id) await ws.send(et.tostring(request, encoding='utf8', method='xml')) # recv() receives data from the server response = await ws.recv() print("\n%s\n" % config['misc']['searched'].value + response) time.sleep(0.1) #sort with stichwort elif subchoice == "5": # Read config file to get query config.read(config_path) calltype = config['calltype']['show_some_elems'].value value = input(config['submenu2']['choice_divers'].value) # build request request = helper.create_elem_request(config, "divers", calltype, value, client_id) await ws.send(et.tostring(request, encoding='utf8', method='xml')) # recv() receives data from the server response = await ws.recv() print("\n%s\n" % config['misc']['searched'].value + response) time.sleep(0.1) elif subchoice == "6": print("Hauptmenu....") time.sleep(1) choice = 0 else: print(config['menu']['repeat'].value) time.sleep(2) else: print(config['menu']['repeat'].value) time.sleep(2)
def post(self): """ POST method that sends a request for action to the corresponding deployment components. """ logging.info("Task request received: {0}, {1}".format( str(self.request), str(self.request.body))) try: data = json.loads(self.request.body) except (TypeError, ValueError) as error: logging.exception(error) logging.error("Unable to parse: {0}".format(self.request.body)) self.set_status(hermes_constants.HTTP_Codes.HTTP_BAD_REQUEST) return # Verify all necessary fields are present in request.body. logging.info("Verifying all necessary parameters are present.") if not set(data.keys()).issuperset(set( hermes_constants.REQUIRED_KEYS)): logging.error("Missing args in request: " + self.request.body) self.set_status(hermes_constants.HTTP_Codes.HTTP_BAD_REQUEST) return # Gather information for sending the requests to start off the current # task at hand. nodes = helper.get_node_info() # Ensure that we bring down affected nodes before any action while doing a # restore. if data[JSONTags.TYPE] == 'backup': tasks = [data[JSONTags.TYPE]] elif data[JSONTags.TYPE] == 'restore': tasks = ['restore'] else: logging.error("Unsupported task type: '{0}'".format( data[JSONTags.TYPE])) self.set_status(hermes_constants.HTTP_Codes.HTTP_BAD_REQUEST) return logging.info("Tasks to execute: {0}".format(tasks)) for task in tasks: # Initiate the task as pending. TASK_STATUS_LOCK.acquire(True) TASK_STATUS[data[JSONTags.TASK_ID]] = { JSONTags.TYPE: task, NodeInfoTags.NUM_NODES: len(nodes), JSONTags.STATUS: TaskStatus.PENDING } TASK_STATUS_LOCK.release() result_queue = Queue.Queue() threads = [] for node in nodes: # Create a br_service compatible JSON object. json_data = helper.create_br_json_data( node[NodeInfoTags.ROLE], task, data[JSONTags.BUCKET_NAME], node[NodeInfoTags.INDEX], data[JSONTags.STORAGE]) request = helper.create_request(url=node[NodeInfoTags.HOST], method='POST', body=json_data) # Start a thread for the request. thread = threading.Thread(target=helper.send_remote_request, name='{0}{1}'.format( data[JSONTags.TYPE], node[NodeInfoTags.HOST]), args=( request, result_queue, )) threads.append(thread) thread.start() # Wait for threads to finish. for thread in threads: thread.join() # Harvest results. results = [result_queue.get() for _ in xrange(len(nodes))] logging.warn("Task: {0}. Results: {1}.".format(task, results)) # Update TASK_STATUS. successful_nodes = 0 for result in results: if result[JSONTags.SUCCESS]: successful_nodes += 1 TASK_STATUS_LOCK.acquire(True) all_nodes = TASK_STATUS[data[JSONTags.TASK_ID]]\ [NodeInfoTags.NUM_NODES] if successful_nodes < all_nodes: TASK_STATUS[data[JSONTags.TASK_ID]][JSONTags.STATUS] = \ TaskStatus.FAILED else: TASK_STATUS[data[JSONTags.TASK_ID]][JSONTags.STATUS] = \ TaskStatus.COMPLETE logging.info("Task: {0}. Status: {1}.".format( task, TASK_STATUS[data[JSONTags.TASK_ID]][JSONTags.STATUS])) IOLoop.instance().add_callback( callback=lambda: helper.report_status( task, data[JSONTags.TASK_ID], TASK_STATUS[data[ JSONTags.TASK_ID]][JSONTags.STATUS])) TASK_STATUS_LOCK.release() self.set_status(hermes_constants.HTTP_Codes.HTTP_OK)