Allow simple REST API container deployment

* Add verbs to container entrypoint

* Have REST API serve the schema JSON

* Add documentation explaining how to deploy

Co-Authored-By: Shachar Snapiri <shachar.snapiri@toganetworks.com>
Change-Id: I243130a219d3e9e96bf87d39693e600ea4b627a2
Partially-Implements: blueprint add-dragonflow-api
This commit is contained in:
Omer Anson 2018-11-07 15:28:53 +02:00 committed by Shachar Snapiri
parent bd037320f2
commit 1808a2cbfd
3 changed files with 50 additions and 9 deletions

View File

@ -83,12 +83,25 @@ these files using e.g.
Running the container without the controller service
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The docker entrypoint accepts verbs. To start the container without the
controller service, use the verb `bash`.
.. code-block:: bash
docker run --name dragonflow --net $DRAGONFLOW_NET_NAME --ip ${DRAGONFLOW_ADDRESS} -i -t dragonflow:latest --dragonflow_address ${DRAGONFLOW_ADDRESS} --db_address ${NODE1}:2379 bash
This enables you to run the container without running the container service.
This is useful in order to create a standalone node for Dragonflow API,
separated from the controller node.
* Run the following commands:
Running a REST API Service
~~~~~~~~~~~~~~~~~~~~~~~~~~
To start a REST API service, running on HTTP port 8080, use the verb `rest`.
.. code-block:: bash
docker run --name dragonflow --net $DRAGONFLOW_NET_NAME --ip ${DRAGONFLOW_ADDRESS} -e DF_NO_CONTROLLER=1 -i -t dragonflow:latest --dragonflow_address ${DRAGONFLOW_ADDRESS} --db_address ${NODE1}:2379
docker run --name dragonflow --net $DRAGONFLOW_NET_NAME --ip ${DRAGONFLOW_ADDRESS} -i -t dragonflow:latest --dragonflow_address ${DRAGONFLOW_ADDRESS} --db_address ${NODE1}:2379 rest
The schema would be available on `http://$DRAGONFLOW_ADDRESS:8080/schema.json`.

View File

@ -23,6 +23,9 @@ from dragonflow.db import model_framework as mf
from dragonflow.db.models import all # noqa
schema_file = None
def nbapi_decorator(f):
# f(nbapi, ...) -> f(...)
def wrapper(*args, **kwargs):
@ -111,6 +114,13 @@ def delete(nbapi, model, name, id_):
bottle.response.status = HTTPStatus.NO_CONTENT.value
@bottle.get('/schema.json')
def schema():
if not schema_file:
bottle.abort(HTTPStatus.NOT_FOUND.value)
return bottle.static_file(schema_file, '/')
def main():
parser = argparse.ArgumentParser(description='Dragonflow REST server')
parser.add_argument('--host', type=str, default='127.0.0.1',
@ -121,6 +131,11 @@ def main():
default='/etc/dragonflow/dragonflow.ini',
help=('Dragonflow config file '
'(/etc/dragonflow/dragonflow.ini)'))
parser.add_argument('--json', type=str,
default=None,
help=('JSON schema file (None)'))
args = parser.parse_args()
global schema_file
schema_file = args.json
utils.config_init(None, [args.config])
bottle.run(host=args.host, port=args.port)

View File

@ -1,5 +1,6 @@
#!/bin/bash
VERB=""
# First get all the arguments
while test ${#} -gt 0; do
case $1 in
@ -31,8 +32,12 @@ while test ${#} -gt 0; do
break
;;
*)
echo >&2 "Unknown command line argument: $1"
exit 1
if [ -n "$VERB" ]; then
echo >&2 "Unknown command line argument: $1"
exit 1
fi
VERB=$1
shift
;;
esac
shift
@ -69,8 +74,16 @@ if [ -n "$DB_INIT" ]; then
df-db init
fi
if [ -z "$DF_NO_CONTROLLER" ]; then
/usr/local/bin/df-local-controller --config-file /etc/dragonflow/dragonflow.ini
elif [ -z "$DF_NO_BASH" ]; then
/bin/bash
fi
case "$VERB" in
""|"controller")
/usr/local/bin/df-local-controller --config-file /etc/dragonflow/dragonflow.ini
;;
"bash")
/bin/bash
;;
"rest")
df-model -j -o /var/dragonflow_model.json
pip install bottle
/usr/local/bin/df-rest-service --config /etc/dragonflow/dragonflow.ini --host 0.0.0.0 --json /var/dragonflow_model.json
;;
esac