begin adding port support, catch OSError from Flask

This commit is contained in:
Nico Schottelius 2019-12-31 13:13:19 +01:00
parent 29dfacfadb
commit 71c3f9d978
2 changed files with 36 additions and 21 deletions

View file

@ -34,6 +34,8 @@ if __name__ == '__main__':
subparsers = arg_parser.add_subparsers(dest="command") subparsers = arg_parser.add_subparsers(dest="command")
api_parser = subparsers.add_parser("api", parents=[parent_parser]) api_parser = subparsers.add_parser("api", parents=[parent_parser])
api_parser.add_argument("--port", "-p")
host_parser = subparsers.add_parser("host") host_parser = subparsers.add_parser("host")
host_parser.add_argument("--hostname", required=True) host_parser.add_argument("--hostname", required=True)

View file

@ -17,7 +17,7 @@ from uncloud.shared import shared
from . import schemas from . import schemas
from .helper import generate_mac, mac2ipv6 from .helper import generate_mac, mac2ipv6
from uncloud import UncloudException
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -561,29 +561,42 @@ api.add_resource(ListHost, "/host/list")
api.add_resource(CreateNetwork, "/network/create") api.add_resource(CreateNetwork, "/network/create")
def main(debug=False): def main(debug=False, port=None):
image_stores = list( try:
shared.etcd_client.get_prefix( image_stores = list(
settings["etcd"]["image_store_prefix"], value_in_json=True shared.etcd_client.get_prefix(
settings["etcd"]["image_store_prefix"], value_in_json=True
)
) )
) except KeyError:
if not image_stores: image_stores = False
data = {
"is_public": True,
"type": "ceph",
"name": "images",
"description": "first ever public image-store",
"attributes": {"list": [], "key": [], "pool": "images"},
}
shared.etcd_client.put( # Do not inject default values that might be very wrong
join_path( # fail when required, not before
settings["etcd"]["image_store_prefix"], uuid4().hex #
), # if not image_stores:
json.dumps(data), # 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__": if __name__ == "__main__":