1 # Copyright (c) 2015 SONATA-NFV and Paderborn University
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
8 # http://www.apache.org/licenses/LICENSE-2.0
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.
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
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 from flask
import Flask
, request
27 from flask_restful
import Api
, Resource
28 from gevent
.pywsgi
import WSGIServer
31 LOG
= logging
.getLogger("api.openstack.base")
34 class BaseOpenstackDummy(Resource
):
36 This class is the base class for all openstack entrypoints of son-emu.
39 def __init__(self
, listenip
, port
):
44 self
.http_server
= None
45 self
.server_thread
= None
46 self
.playbook_file
= '/tmp/son-emu-requests.log'
47 with
open(self
.playbook_file
, 'w'):
51 self
.app
= Flask(__name__
)
52 self
.api
= Api(self
.app
)
56 self
.http_server
.stop(timeout
=1.0)
58 def _start_flask(self
):
59 LOG
.info("Starting %s endpoint @ http://%s:%d" % (
60 self
.__class
__.__name
__, self
.ip
, self
.port
))
61 self
.http_server
= WSGIServer(
64 log
=open("/dev/null", "w") # don't show http logs
66 self
.http_server
.serve_forever(stop_timeout
=1.0)
68 def dump_playbook(self
):
69 with self
.manage
.lock
:
70 with
open(self
.playbook_file
, 'a') as logfile
:
71 if len(request
.data
) > 0:
72 data
= "# %s API\n" % str(
73 self
.__class
__).split('.')[-1].rstrip('\'>')
74 data
+= "curl -X {type} -H \"Content-type: application/json\" -d '{data}' {url}".format(type=request
.method
,
77 logfile
.write(data
+ "\n")