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 ##############################################################################
11 from flask import Flask
12 from flask import request
16 import novaclient.client as novaclient
18 import nova_force_down
21 class DoctorInspectorSample(object):
23 nova_api_version = 2.11
26 self.nova = novaclient.Client(self.nova_api_version,
27 os.environ['OS_USERNAME'],
28 os.environ['OS_PASSWORD'],
29 os.environ['OS_TENANT_NAME'],
30 os.environ['OS_AUTH_URL'],
32 # check nova is available
33 self.nova.servers.list(detailed=False)
35 def disable_compute_host(self, hostname):
36 opts = {'all_tenants': True, 'host': hostname}
37 for server in self.nova.servers.list(detailed=False, search_opts=opts):
38 self.nova.servers.reset_state(server, 'error')
40 # NOTE: We use our own client here instead of this novaclient for a
41 # workaround. Once keystone provides v2.1 nova api endpoint
42 # in the service catalog which is configured by OpenStack
43 # installer, we can use this:
45 # self.nova.services.force_down(hostname, 'nova-compute', True)
47 nova_force_down.force_down(hostname)
51 inspector = DoctorInspectorSample()
54 @app.route('/events', methods=['POST'])
56 app.logger.debug('event posted')
57 app.logger.debug('inspector = %s' % inspector)
58 app.logger.debug('received data = %s' % request.data)
59 d = json.loads(request.data)
60 hostname = d['hostname']
61 event_type = d['type']
62 if event_type == 'compute.host.down':
63 inspector.disable_compute_host(hostname)
68 parser = argparse.ArgumentParser(description='Doctor Sample Inspector')
69 parser.add_argument('port', metavar='PORT', type=int, nargs='?',
70 help='a port for inspector')
71 return parser.parse_args()
76 app.run(port=args.port, debug=True)
79 if __name__ == '__main__':