From fa11d78e728029bcefa0ad6608d422715ae8f0be Mon Sep 17 00:00:00 2001 From: Thomas Duval Date: Fri, 3 Nov 2017 15:07:12 +0100 Subject: Fix the Moon platform startup and fix some bugs in startup. Change-Id: I9014071c755ec5ef0c9eb0d575af29d5f3ad3533 --- moonv4/moon_manager/moon_manager/api/pdp.py | 26 ++++++++++++++++++++++--- moonv4/moon_manager/moon_manager/http_server.py | 22 ++++++++++++++++++++- 2 files changed, 44 insertions(+), 4 deletions(-) (limited to 'moonv4/moon_manager') diff --git a/moonv4/moon_manager/moon_manager/api/pdp.py b/moonv4/moon_manager/moon_manager/api/pdp.py index 823055a2..ffc91fba 100644 --- a/moonv4/moon_manager/moon_manager/api/pdp.py +++ b/moonv4/moon_manager/moon_manager/api/pdp.py @@ -11,6 +11,7 @@ from flask import request from flask_restful import Resource import logging import requests +import time from moon_utilities.security_functions import check_auth from moon_db.core import PDPManager from moon_utilities import configuration @@ -25,12 +26,20 @@ def delete_pod(uuid): def add_pod(uuid, data): + LOG.info("Add a new pod {}".format(data)) conf = configuration.get_configuration("components/orchestrator") hostname = conf["components/orchestrator"].get("hostname", "orchestrator") port = conf["components/orchestrator"].get("port", 80) proto = conf["components/orchestrator"].get("protocol", "http") - req = requests.post("{}://{}:{}/pods".format(proto, hostname, port), - data=data) + while True: + try: + req = requests.post("{}://{}:{}/pods".format(proto, hostname, port), + data=data) + except requests.exceptions.ConnectionError: + LOG.warning("Orchestrator is not ready, standby...") + time.sleep(1) + else: + break LOG.info(req.text) @@ -93,8 +102,14 @@ class PDP(Resource): :internal_api: add_pdp """ try: + data = dict(request.json) + if not data.get("keystone_project_id"): + data["keystone_project_id"] = None data = PDPManager.add_pdp( user_id=user_id, pdp_id=None, value=request.json) + uuid = list(data.keys())[0] + LOG.info("data={}".format(data)) + LOG.info("uuid={}".format(uuid)) add_pod(uuid=uuid, data=data[uuid]) except Exception as e: LOG.error(e, exc_info=True) @@ -140,8 +155,13 @@ class PDP(Resource): :internal_api: update_pdp """ try: + _data = dict(request.json) + if not _data.get("keystone_project_id"): + _data["keystone_project_id"] = None data = PDPManager.update_pdp( - user_id=user_id, pdp_id=uuid, value=request.json) + user_id=user_id, pdp_id=uuid, value=_data) + LOG.info("data={}".format(data)) + LOG.info("uuid={}".format(uuid)) add_pod(uuid=uuid, data=data[uuid]) except Exception as e: LOG.error(e, exc_info=True) diff --git a/moonv4/moon_manager/moon_manager/http_server.py b/moonv4/moon_manager/moon_manager/http_server.py index dfaa23ae..c671ed6f 100644 --- a/moonv4/moon_manager/moon_manager/http_server.py +++ b/moonv4/moon_manager/moon_manager/http_server.py @@ -7,6 +7,8 @@ from flask import Flask, jsonify from flask_cors import CORS, cross_origin from flask_restful import Resource, Api import logging +import sqlalchemy.exc +import time from moon_manager import __version__ from moon_manager.api.generic import Status, Logs, API from moon_manager.api.models import Models @@ -20,8 +22,10 @@ from moon_manager.api.assignments import SubjectAssignments, ObjectAssignments, from moon_manager.api.rules import Rules # from moon_manager.api.containers import Container from moon_utilities import configuration, exceptions +from moon_db.core import PDPManager -logger = logging.getLogger("moon.manager.http") + +LOG = logging.getLogger("moon.manager.http") class Server: @@ -132,6 +136,22 @@ class HTTPServer(Server): for api in __API__: self.api.add_resource(api, *api.__urls__) + @staticmethod + def __check_if_db_is_up(): + first = True + while True: + try: + PDPManager.get_pdp(user_id="admin", pdp_id=None) + except sqlalchemy.exc.ProgrammingError: + time.sleep(1) + if first: + LOG.warning("Waiting for the database...") + first = False + else: + LOG.warning("Database is up, resuming operations...") + break + def run(self): + self.__check_if_db_is_up() self.app.run(debug=True, host=self._host, port=self._port) # nosec -- cgit 1.2.3-korg