1 ##############################################################################
2 # Copyright (c) 2017 ZTE Corporation and others.
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
10 from flask import Flask
11 from flask import request
14 from threading import Thread
17 from doctor_tests.common import utils
18 from doctor_tests.identity_auth import get_identity_auth
19 from doctor_tests.identity_auth import get_session
20 from doctor_tests.os_clients import nova_client
21 from doctor_tests.os_clients import neutron_client
22 from doctor_tests.inspector.base import BaseInspector
25 class SampleInspector(BaseInspector):
26 event_type = 'compute.host.down'
28 def __init__(self, conf, log):
29 super(SampleInspector, self).__init__(conf, log)
30 self.inspector_url = self.get_inspector_url()
31 self.novaclients = list()
32 self._init_novaclients()
33 # Normally we use this client for non redundant API calls
34 self.nova = self.novaclients[0]
36 auth = get_identity_auth(project=self.conf.doctor_project)
37 session = get_session(auth=auth)
38 if self.conf.update_neutron_port_dp_status:
39 self.neutron = neutron_client(session)
41 self.servers = collections.defaultdict(list)
42 self.hostnames = list()
45 def _init_novaclients(self):
46 self.NUMBER_OF_CLIENTS = self.conf.instance_count
47 auth = get_identity_auth(project=self.conf.doctor_project)
48 session = get_session(auth=auth)
49 for i in range(self.NUMBER_OF_CLIENTS):
50 self.novaclients.append(
51 nova_client(self.conf.nova_version, session))
53 def _init_servers_list(self):
55 opts = {'all_tenants': True}
56 servers = self.nova.servers.list(search_opts=opts)
57 for server in servers:
59 host = server.__dict__.get('OS-EXT-SRV-ATTR:host')
60 self.servers[host].append(server)
61 self.log.debug('get hostname=%s from server=%s' % (host, server))
62 except Exception as e:
63 self.log.info('can not get hostname from server=%s' % server)
65 def get_inspector_url(self):
66 return 'http://%s:%s' % (self.conf.inspector.ip, self.conf.inspector.port)
69 self.log.info('sample inspector start......')
70 self._init_servers_list()
71 self.app = InspectorApp(self.conf.inspector.port, self, self.log)
75 self.log.info('sample inspector stop......')
78 for hostname in self.hostnames:
79 self.nova.services.force_down(hostname, 'nova-compute', False)
82 'Content-Type': 'application/json',
83 'Accept': 'application/json',
85 url = '%s%s' % (self.inspector_url, 'shutdown') \
86 if self.inspector_url.endswith('/') else \
87 '%s%s' % (self.inspector_url, '/shutdown')
88 requests.post(url, data='', headers=headers)
90 def handle_events(self, events):
92 hostname = event['details']['hostname']
93 event_type = event['type']
94 if event_type == self.event_type:
95 self.hostnames.append(hostname)
96 thr1 = self._disable_compute_host(hostname)
97 thr2 = self._vms_reset_state('error', hostname)
98 if self.conf.update_neutron_port_dp_status:
99 thr3 = self._set_ports_data_plane_status('DOWN', hostname)
102 if self.conf.update_neutron_port_dp_status:
106 def _disable_compute_host(self, hostname):
107 self.nova.services.force_down(hostname, 'nova-compute', True)
108 self.log.info('doctor mark host(%s) down at %s' % (hostname, time.time()))
111 def _vms_reset_state(self, state, hostname):
114 def _vm_reset_state(nova, server, state):
115 nova.servers.reset_state(server, state)
116 self.log.info('doctor mark vm(%s) error at %s' % (server, time.time()))
119 for nova, server in zip(self.novaclients, self.servers[hostname]):
120 t = _vm_reset_state(nova, server, state)
126 def _set_ports_data_plane_status(self, status, hostname):
127 body = {'data_plane_status': status}
130 def _set_port_data_plane_status(port_id):
131 self.neutron.update_port(port_id, body)
132 self.log.info('doctor set data plane status %s on port %s' % (status, port_id))
135 params = {'binding:host_id': hostname}
136 for port_id in self.neutron.list_ports(**params):
137 t = _set_port_data_plane_status(port_id)
143 class InspectorApp(Thread):
145 def __init__(self, port, inspector, log):
146 Thread.__init__(self)
148 self.inspector = inspector
152 app = Flask('inspector')
154 @app.route('/events', methods=['PUT'])
156 self.log.info('event posted in sample inspector at %s' % time.time())
157 self.log.info('sample inspector = %s' % self.inspector)
158 self.log.info('sample inspector received data = %s' % request.data)
159 events = json.loads(request.data.decode('utf8'))
160 self.inspector.handle_events(events)
163 @app.route('/shutdown', methods=['POST'])
165 self.log.info('shutdown inspector app server at %s' % time.time())
166 func = request.environ.get('werkzeug.server.shutdown')
168 raise RuntimeError('Not running with the Werkzeug Server')
170 return 'inspector app shutting down...'
172 app.run(host="0.0.0.0", port=self.port)