1 ##############################################################################
2 # Copyright (c) 2016 NEC 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 ##############################################################################
12 from flask import Flask
13 from flask import request
15 import logger as doctor_log
19 from keystoneauth1 import session
20 import novaclient.client as novaclient
22 import doctor_tests.identity_auth
24 LOG = doctor_log.Logger('doctor_inspector').getLogger()
27 class ThreadedResetState(threading.Thread):
29 def __init__(self, nova, state, server):
30 threading.Thread.__init__(self)
36 self.nova.servers.reset_state(self.server, self.state)
37 LOG.info('doctor mark vm(%s) error at %s' % (self.server, time.time()))
40 class DoctorInspectorSample(object):
42 NOVA_API_VERSION = '2.34'
43 NUMBER_OF_CLIENTS = 50
44 # TODO(tojuvone): This could be enhanced in future with dynamic
45 # reuse of self.novaclients when all threads in use and
46 # self.NUMBER_OF_CLIENTS based on amount of cores or overriden by input
50 self.servers = collections.defaultdict(list)
51 self.novaclients = list()
52 auth=identity_auth.get_identity_auth()
53 sess=session.Session(auth=auth)
54 # Pool of novaclients for redundant usage
55 for i in range(self.NUMBER_OF_CLIENTS):
56 self.novaclients.append(
57 novaclient.Client(self.NOVA_API_VERSION, session=sess))
58 # Normally we use this client for non redundant API calls
59 self.nova=self.novaclients[0]
60 self.nova.servers.list(detailed=False)
61 self.init_servers_list()
63 def init_servers_list(self):
64 opts = {'all_tenants': True}
65 servers=self.nova.servers.list(search_opts=opts)
67 for server in servers:
69 host=server.__dict__.get('OS-EXT-SRV-ATTR:host')
70 self.servers[host].append(server)
71 LOG.debug('get hostname=%s from server=%s' % (host, server))
72 except Exception as e:
73 LOG.error('can not get hostname from server=%s' % server)
75 def disable_compute_host(self, hostname):
77 if len(self.servers[hostname]) > self.NUMBER_OF_CLIENTS:
78 # TODO(tojuvone): This could be enhanced in future with dynamic
79 # reuse of self.novaclients when all threads in use
80 LOG.error('%d servers in %s. Can handle only %d'%(
81 self.servers[hostname], hostname, self.NUMBER_OF_CLIENTS))
82 for nova, server in zip(self.novaclients, self.servers[hostname]):
83 t = ThreadedResetState(nova, "error", server)
88 self.nova.services.force_down(hostname, 'nova-compute', True)
89 LOG.info('doctor mark host(%s) down at %s' % (hostname, time.time()))
93 inspector = DoctorInspectorSample()
96 @app.route('/events', methods=['POST'])
98 LOG.info('event posted at %s' % time.time())
99 LOG.info('inspector = %s' % inspector)
100 LOG.info('received data = %s' % request.data)
101 d = json.loads(request.data)
103 hostname = event['details']['hostname']
104 event_type = event['type']
105 if event_type == 'compute.host.down':
106 inspector.disable_compute_host(hostname)
111 parser = argparse.ArgumentParser(description='Doctor Sample Inspector')
112 parser.add_argument('port', metavar='PORT', type=int, nargs='?',
113 help='a port for inspector')
114 return parser.parse_args()
119 app.run(host='0.0.0.0', port=args.port)
121 if __name__ == '__main__':