Fix: Do monkey.patch_all() only once.
[osm/vim-emu.git] / src / emuvim / api / openstack / openstack_dummies / base_openstack_dummy.py
1 # Copyright (c) 2015 SONATA-NFV and Paderborn University
2 # ALL RIGHTS RESERVED.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 #
16 # Neither the name of the SONATA-NFV, Paderborn University
17 # nor the names of its contributors may be used to endorse or promote
18 # products derived from this software without specific prior written
19 # permission.
20 #
21 # This work has been performed in the framework of the SONATA project,
22 # funded by the European Commission under Grant number 671517 through
23 # the Horizon 2020 and 5G-PPP programmes. The authors would like to
24 # acknowledge the contributions of their colleagues of the SONATA
25 # partner consortium (www.sonata-nfv.eu).
26 import logging
27
28 import threading
29 from flask import Flask, request
30 from flask_restful import Api, Resource
31 from gevent.pywsgi import WSGIServer
32
33
34 LOG = logging.getLogger("api.openstack.base")
35
36
37 class BaseOpenstackDummy(Resource):
38 """
39 This class is the base class for all openstack entrypoints of son-emu.
40 """
41
42 def __init__(self, listenip, port):
43 self.ip = listenip
44 self.port = port
45 self.compute = None
46 self.manage = None
47 self.http_server = None
48 self.server_thread = None
49 self.playbook_file = '/tmp/son-emu-requests.log'
50 with open(self.playbook_file, 'w'):
51 pass
52
53 # setup Flask
54 self.app = Flask(__name__)
55 self.api = Api(self.app)
56
57 def start(self):
58 self.server_thread = threading.Thread(target=self._start_flask, args=())
59 self.server_thread.name = self.__class__.__name__
60 self.server_thread.start()
61
62 def stop(self):
63 if self.http_server:
64 LOG.info('Stopping %s' % self.__class__.__name__)
65 self.http_server.stop(timeout=1)
66
67 def _start_flask(self):
68 LOG.info("Starting %s endpoint @ http://%s:%d" % (
69 self.__class__.__name__, self.ip, self.port))
70 self.http_server = WSGIServer(
71 (self.ip, self.port),
72 self.app,
73 log=open("/dev/null", "w") # don't show http logs
74 )
75 self.http_server.serve_forever(stop_timeout=1)
76 LOG.info('Stopped %s' % self.__class__.__name__)
77
78 def dump_playbook(self):
79 with self.manage.lock:
80 with open(self.playbook_file, 'a') as logfile:
81 if len(request.data) > 0:
82 data = "# %s API\n" % str(
83 self.__class__).split('.')[-1].rstrip('\'>')
84 data += "curl -X {type} -H \"Content-type: application/json\" -d '{data}' {url}".format(type=request.method,
85 data=request.data,
86 url=request.url)
87 logfile.write(data + "\n")