Change type of nova_api_version from float to str
[doctor.git] / tests / inspector.py
1 ##############################################################################
2 # Copyright (c) 2016 NEC Corporation and others.
3 #
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 ##############################################################################
9
10 import argparse
11 from flask import Flask
12 from flask import request
13 import json
14 import os
15
16 import novaclient.client as novaclient
17
18 import nova_force_down
19
20
21 class DoctorInspectorSample(object):
22
23     nova_api_version = '2.11'
24
25     def __init__(self):
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'],
31                                       connection_pool=True)
32         # check nova is available
33         self.nova.servers.list(detailed=False)
34
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')
39
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:
44         #
45         # self.nova.services.force_down(hostname, 'nova-compute', True)
46         #
47         nova_force_down.force_down(hostname)
48
49
50 app = Flask(__name__)
51 inspector = DoctorInspectorSample()
52
53
54 @app.route('/events', methods=['POST'])
55 def event_posted():
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)
64     return "OK"
65
66
67 def get_args():
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()
72
73
74 def main():
75     args = get_args()
76     app.run(port=args.port, debug=True)
77
78
79 if __name__ == '__main__':
80     main()