From 71c3f9d97870324e952fdac606e3a53ca72d047a Mon Sep 17 00:00:00 2001 From: Nico Schottelius Date: Tue, 31 Dec 2019 13:13:19 +0100 Subject: [PATCH] begin adding port support, catch OSError from Flask --- scripts/uncloud | 2 ++ uncloud/api/main.py | 55 ++++++++++++++++++++++++++++----------------- 2 files changed, 36 insertions(+), 21 deletions(-) diff --git a/scripts/uncloud b/scripts/uncloud index 4625164..28d8344 100755 --- a/scripts/uncloud +++ b/scripts/uncloud @@ -34,6 +34,8 @@ if __name__ == '__main__': subparsers = arg_parser.add_subparsers(dest="command") api_parser = subparsers.add_parser("api", parents=[parent_parser]) + api_parser.add_argument("--port", "-p") + host_parser = subparsers.add_parser("host") host_parser.add_argument("--hostname", required=True) diff --git a/uncloud/api/main.py b/uncloud/api/main.py index 37c6c5b..861c1bc 100644 --- a/uncloud/api/main.py +++ b/uncloud/api/main.py @@ -17,7 +17,7 @@ from uncloud.shared import shared from . import schemas from .helper import generate_mac, mac2ipv6 - +from uncloud import UncloudException logger = logging.getLogger(__name__) @@ -561,29 +561,42 @@ api.add_resource(ListHost, "/host/list") api.add_resource(CreateNetwork, "/network/create") -def main(debug=False): - image_stores = list( - shared.etcd_client.get_prefix( - settings["etcd"]["image_store_prefix"], value_in_json=True +def main(debug=False, port=None): + try: + image_stores = list( + shared.etcd_client.get_prefix( + settings["etcd"]["image_store_prefix"], value_in_json=True + ) ) - ) - if not image_stores: - data = { - "is_public": True, - "type": "ceph", - "name": "images", - "description": "first ever public image-store", - "attributes": {"list": [], "key": [], "pool": "images"}, - } + except KeyError: + image_stores = False - shared.etcd_client.put( - join_path( - settings["etcd"]["image_store_prefix"], uuid4().hex - ), - json.dumps(data), - ) + # Do not inject default values that might be very wrong + # fail when required, not before + # + # if not image_stores: + # data = { + # "is_public": True, + # "type": "ceph", + # "name": "images", + # "description": "first ever public image-store", + # "attributes": {"list": [], "key": [], "pool": "images"}, + # } - app.run(host="::", debug=False) + # shared.etcd_client.put( + # join_path( + # settings["etcd"]["image_store_prefix"], uuid4().hex + # ), + # json.dumps(data), + # ) + + if port: + app_port = port + + try: + app.run(host="::", debug=False) + except OSError as e: + raise UncloudException("Failed to start Flask: {}".format(e)) if __name__ == "__main__":