rdudyala | 996d70b | 2016-10-13 17:40:55 +0000 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | from flask import request, Request, jsonify |
| 3 | from flask import Flask |
| 4 | from flask import make_response |
| 5 | import logging |
| 6 | import logging.handlers |
| 7 | import logging.config |
| 8 | import subprocess |
| 9 | import ConfigParser |
| 10 | import generate_pipeline |
| 11 | app = Flask(__name__) |
| 12 | |
| 13 | |
| 14 | @app.route('/monitoring/agent/openstack/start',methods=['POST']) |
| 15 | def openstack_start(): |
| 16 | try: |
| 17 | # To do validation of user inputs for all the functions |
| 18 | target = request.json['target'] |
| 19 | logging.debug("target:%s",target) |
| 20 | if not generate_pipeline.onos_service_info: |
| 21 | logging.debug (" ONOS Service is not enalble,Only openstack need to be enabled ") |
| 22 | generate_pipeline.generate_pipeline_yaml_for_openstack(target,True) |
| 23 | else: |
| 24 | logging.debug(" ONOS Service is also enabled ,please generate yaml file for both onos and openstack") |
| 25 | generate_pipeline.generate_pipeline_yaml_for_openstack_onos(target,True) |
| 26 | restart_ceilometer_services() |
| 27 | return "Openstack start service called \n" |
| 28 | except Exception as e: |
| 29 | return e.__str__() |
| 30 | |
| 31 | @app.route('/monitoring/agent/onos/start',methods=['POST']) |
| 32 | def onos_start(): |
| 33 | try: |
| 34 | target = request.json['target'] |
| 35 | logging.debug("target:%s",target) |
| 36 | metadata = request.json['meta_data'] |
| 37 | logging.debug("metadata:%s",metadata) |
| 38 | logging.debug(type(target)) |
| 39 | resources = metadata['resources'] |
| 40 | logging.debug("resources:%s",resources) |
| 41 | if not generate_pipeline.openstack_service_info: |
| 42 | logging.debug("Openstak Service is not enabled,Only ONOS need to be enabled") |
| 43 | generate_pipeline.generate_pipeline_yaml_for_onos(target,resources,True) |
| 44 | else: |
| 45 | logging.debug(" Openstack Service is also enabled ,please generate yaml file for both onos and openstack") |
| 46 | generate_pipeline.generate_pipeline_yaml_for_onos_openstack(target,resources,True) |
| 47 | |
| 48 | restart_ceilometer_services() |
| 49 | return "ONOS start service called \n" |
| 50 | except Exception as e: |
| 51 | return e.__str__() |
| 52 | |
| 53 | @app.route('/monitoring/agent/vsg/start',methods=['POST']) |
| 54 | def vsg_start(): |
| 55 | try: |
| 56 | target = request.json['target'] |
| 57 | logging.debug("target:%s",target) |
| 58 | return "vsg start service called \n" |
| 59 | except Exception as e: |
| 60 | return e.__str__() |
| 61 | |
| 62 | |
| 63 | @app.route('/monitoring/agent/openstack/stop',methods=['POST']) |
| 64 | def openstack_stop(): |
| 65 | try: |
| 66 | target = request.json['target'] |
| 67 | logging.debug("target:%s",target) |
| 68 | if not generate_pipeline.onos_service_info: |
| 69 | generate_pipeline.generate_pipeline_yaml_for_openstack(target,False) |
| 70 | else: |
| 71 | generate_pipeline.generate_pipeline_yaml_for_onos(generate_pipeline.onos_service_info[0].target,generate_pipeline.onos_service_info[0].resources,True) |
| 72 | logging.debug("Delete Openstack object") |
| 73 | for obj in generate_pipeline.openstack_service_info: |
| 74 | generate_pipeline.openstack_service_info.remove(obj) |
| 75 | |
| 76 | restart_ceilometer_services() |
| 77 | return "Openstack stop service called \n" |
| 78 | |
| 79 | except Exception as e: |
| 80 | return e.__str__() |
| 81 | |
| 82 | @app.route('/monitoring/agent/onos/stop',methods=['POST']) |
| 83 | def onos_stop(): |
| 84 | try: |
| 85 | target = request.json['target'] |
| 86 | logging.debug("target:%s",target) |
| 87 | metadata = request.json['meta_data'] |
| 88 | logging.debug("metadata:%s",metadata) |
| 89 | resources = metadata['resources'] |
| 90 | logging.debug("resources:%s",resources) |
| 91 | |
| 92 | if not generate_pipeline.openstack_service_info: |
| 93 | generate_pipeline.generate_pipeline_yaml_for_onos(target,resources,False) |
| 94 | else: |
| 95 | generate_pipeline.generate_pipeline_yaml_for_openstack(generate_pipeline.openstack_service_info[0].target,True) |
| 96 | |
| 97 | logging.debug("Delete ONOS Object") |
| 98 | for obj in generate_pipeline.onos_service_info: |
| 99 | generate_pipeline.onos_service_info.remove(obj) |
| 100 | |
| 101 | restart_ceilometer_services() |
| 102 | return "ONOS stop service called \n" |
| 103 | except Exception as e: |
| 104 | return e.__str__() |
| 105 | |
| 106 | @app.route('/monitoring/agent/vsg/stop',methods=['POST']) |
| 107 | def vsg_stop(): |
| 108 | try: |
| 109 | target = request.json['target'] |
| 110 | logging.debug("target:%s",target) |
| 111 | return "vsg stop service called \n" |
| 112 | except Exception as e: |
| 113 | return e.__str__() |
| 114 | |
| 115 | |
| 116 | def restart_ceilometer_services(): |
| 117 | try : |
| 118 | config = ConfigParser.ConfigParser() |
| 119 | config.read('monitoring_agent.conf') |
| 120 | services = config.get('SERVICE','Ceilometer_service') |
| 121 | service = services.split(",") |
| 122 | subprocess.call("sudo cp pipeline.yaml /etc/ceilometer/pipeline.yaml",shell=True) |
| 123 | except Exception as e: |
| 124 | logging.error("* Error in confing file:%s",e.__str__()) |
| 125 | return False |
| 126 | else : |
| 127 | for service_name in service: |
| 128 | command = ['service',service_name, 'restart']; |
| 129 | logging.debug("Executing: %s command",command) |
| 130 | #shell=FALSE for sudo to work. |
| 131 | try : |
| 132 | subprocess.call(command, shell=False) |
| 133 | except Exception as e: |
| 134 | logging.error("* %s command execution failed with error %s",command,e.__str__()) |
| 135 | return False |
| 136 | return True |
| 137 | |
| 138 | if __name__ == "__main__": |
| 139 | logging.config.fileConfig('monitoring_agent.conf', disable_existing_loggers=False) |
| 140 | app.run(host="0.0.0.0",port=5004,debug=False) |