blob: 9500ea2436eabe79d83a67b8bc32d14d62782544 [file] [log] [blame]
tierno1d213f42020-04-24 14:02:51 +00001#!/usr/bin/python3
2# -*- coding: utf-8 -*-
3
4##
5# Copyright 2020 Telefonica Investigacion y Desarrollo, S.A.U.
6#
7# Licensed under the Apache License, Version 2.0 (the "License");
8# you may not use this file except in compliance with the License.
9# You may obtain a copy of the License at
10#
11# http://www.apache.org/licenses/LICENSE-2.0
12#
13# Unless required by applicable law or agreed to in writing, software
14# distributed under the License is distributed on an "AS IS" BASIS,
15# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
16# implied.
17# See the License for the specific language governing permissions and
18# limitations under the License.
19##
20
sousaedu049cbb12022-01-05 11:39:35 +000021
22from codecs import getreader
23import getopt
24from http import HTTPStatus
tierno1d213f42020-04-24 14:02:51 +000025import json
tierno1d213f42020-04-24 14:02:51 +000026import logging
27import logging.handlers
sousaedu049cbb12022-01-05 11:39:35 +000028from os import environ, path
tierno1d213f42020-04-24 14:02:51 +000029import sys
sousaedu049cbb12022-01-05 11:39:35 +000030import time
tierno1d213f42020-04-24 14:02:51 +000031
sousaedu049cbb12022-01-05 11:39:35 +000032import cherrypy
tierno1d213f42020-04-24 14:02:51 +000033from osm_common.dbbase import DbException
34from osm_common.fsbase import FsException
35from osm_common.msgbase import MsgException
tierno1d213f42020-04-24 14:02:51 +000036from osm_ng_ro import version as ro_version, version_date as ro_version_date
sousaedu049cbb12022-01-05 11:39:35 +000037import osm_ng_ro.html_out as html
38from osm_ng_ro.ns import Ns, NsException
39from osm_ng_ro.validation import ValidationError
40from osm_ng_ro.vim_admin import VimAdminThread
41import yaml
42
tierno1d213f42020-04-24 14:02:51 +000043
44__author__ = "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>"
sousaedu80135b92021-02-17 15:05:18 +010045__version__ = "0.1." # file version, not NBI version
tierno1d213f42020-04-24 14:02:51 +000046version_date = "May 2020"
47
sousaedu80135b92021-02-17 15:05:18 +010048database_version = "1.2"
49auth_database_version = "1.0"
50ro_server = None # instance of Server class
51vim_admin_thread = None # instance of VimAdminThread class
tierno70eeb182020-10-19 16:38:00 +000052
tierno1d213f42020-04-24 14:02:51 +000053# vim_threads = None # instance of VimThread class
54
55"""
56RO North Bound Interface
57URL: /ro GET POST PUT DELETE PATCH
58 /ns/v1/deploy O
59 /<nsrs_id> O O O
60 /<action_id> O
61 /cancel O
62
63"""
64
65valid_query_string = ("ADMIN", "SET_PROJECT", "FORCE", "PUBLIC")
66# ^ Contains possible administrative query string words:
67# ADMIN=True(by default)|Project|Project-list: See all elements, or elements of a project
68# (not owned by my session project).
69# PUBLIC=True(by default)|False: See/hide public elements. Set/Unset a topic to be public
70# FORCE=True(by default)|False: Force edition/deletion operations
71# SET_PROJECT=Project|Project-list: Add/Delete the topic to the projects portfolio
72
73valid_url_methods = {
74 # contains allowed URL and methods, and the role_permission name
75 "admin": {
76 "v1": {
77 "tokens": {
78 "METHODS": ("POST",),
79 "ROLE_PERMISSION": "tokens:",
sousaedu80135b92021-02-17 15:05:18 +010080 "<ID>": {"METHODS": ("DELETE",), "ROLE_PERMISSION": "tokens:id:"},
tierno1d213f42020-04-24 14:02:51 +000081 },
82 }
83 },
84 "ns": {
85 "v1": {
k4.rahul78f474e2022-05-02 15:47:57 +000086 "rebuild": {
87 "METHODS": ("POST",),
88 "ROLE_PERMISSION": "rebuild:",
89 "<ID>": {
90 "METHODS": ("POST",),
91 "ROLE_PERMISSION": "rebuild:id:",
92 },
93 },
94 "start": {
95 "METHODS": ("POST",),
96 "ROLE_PERMISSION": "start:",
97 "<ID>": {
98 "METHODS": ("POST",),
99 "ROLE_PERMISSION": "start:id:",
100 },
101 },
102 "stop": {
103 "METHODS": ("POST",),
104 "ROLE_PERMISSION": "stop:",
105 "<ID>": {
106 "METHODS": ("POST",),
107 "ROLE_PERMISSION": "stop:id:",
108 },
109 },
tierno1d213f42020-04-24 14:02:51 +0000110 "deploy": {
111 "METHODS": ("GET",),
112 "ROLE_PERMISSION": "deploy:",
113 "<ID>": {
114 "METHODS": ("GET", "POST", "DELETE"),
115 "ROLE_PERMISSION": "deploy:id:",
116 "<ID>": {
117 "METHODS": ("GET",),
118 "ROLE_PERMISSION": "deploy:id:id:",
119 "cancel": {
120 "METHODS": ("POST",),
121 "ROLE_PERMISSION": "deploy:id:id:cancel",
sousaedu80135b92021-02-17 15:05:18 +0100122 },
123 },
124 },
tierno1d213f42020-04-24 14:02:51 +0000125 },
palaciosj8f2060b2022-02-24 12:05:59 +0000126 "recreate": {
127 "<ID>": {
128 "METHODS": ("POST"),
129 "ROLE_PERMISSION": "recreate:id:",
130 "<ID>": {
131 "METHODS": ("GET",),
132 "ROLE_PERMISSION": "recreate:id:id:",
133 },
134 },
135 },
elumalai8658c2c2022-04-28 19:09:31 +0530136 "migrate": {
137 "<ID>": {
138 "METHODS": ("POST"),
139 "ROLE_PERMISSION": "migrate:id:",
140 "<ID>": {
141 "METHODS": ("GET",),
142 "ROLE_PERMISSION": "migrate:id:id:",
143 },
144 },
145 },
sritharan29a4c1a2022-05-05 12:15:04 +0000146 "verticalscale": {
147 "<ID>": {
148 "METHODS": ("POST"),
149 "ROLE_PERMISSION": "verticalscale:id:",
150 "<ID>": {
151 "METHODS": ("GET",),
152 "ROLE_PERMISSION": "verticalscale:id:id:",
153 },
154 },
155 },
tierno1d213f42020-04-24 14:02:51 +0000156 }
157 },
158}
159
160
161class RoException(Exception):
tierno1d213f42020-04-24 14:02:51 +0000162 def __init__(self, message, http_code=HTTPStatus.METHOD_NOT_ALLOWED):
163 Exception.__init__(self, message)
164 self.http_code = http_code
165
166
167class AuthException(RoException):
168 pass
169
170
171class Authenticator:
tierno1d213f42020-04-24 14:02:51 +0000172 def __init__(self, valid_url_methods, valid_query_string):
173 self.valid_url_methods = valid_url_methods
174 self.valid_query_string = valid_query_string
175
176 def authorize(self, *args, **kwargs):
177 return {"token": "ok", "id": "ok"}
sousaedu80135b92021-02-17 15:05:18 +0100178
tierno1d213f42020-04-24 14:02:51 +0000179 def new_token(self, token_info, indata, remote):
sousaedu80135b92021-02-17 15:05:18 +0100180 return {"token": "ok", "id": "ok", "remote": remote}
tierno1d213f42020-04-24 14:02:51 +0000181
182 def del_token(self, token_id):
183 pass
184
185 def start(self, engine_config):
186 pass
187
188
189class Server(object):
190 instance = 0
191 # to decode bytes to str
192 reader = getreader("utf-8")
193
194 def __init__(self):
195 self.instance += 1
196 self.authenticator = Authenticator(valid_url_methods, valid_query_string)
197 self.ns = Ns()
198 self.map_operation = {
199 "token:post": self.new_token,
200 "token:id:delete": self.del_token,
201 "deploy:get": self.ns.get_deploy,
202 "deploy:id:get": self.ns.get_actions,
203 "deploy:id:post": self.ns.deploy,
204 "deploy:id:delete": self.ns.delete,
205 "deploy:id:id:get": self.ns.status,
206 "deploy:id:id:cancel:post": self.ns.cancel,
k4.rahul78f474e2022-05-02 15:47:57 +0000207 "rebuild:id:post": self.ns.rebuild_start_stop,
208 "start:id:post": self.ns.rebuild_start_stop,
209 "stop:id:post": self.ns.rebuild_start_stop,
palaciosj8f2060b2022-02-24 12:05:59 +0000210 "recreate:id:post": self.ns.recreate,
211 "recreate:id:id:get": self.ns.recreate_status,
elumalai8658c2c2022-04-28 19:09:31 +0530212 "migrate:id:post": self.ns.migrate,
sritharan29a4c1a2022-05-05 12:15:04 +0000213 "verticalscale:id:post": self.ns.verticalscale,
tierno1d213f42020-04-24 14:02:51 +0000214 }
215
216 def _format_in(self, kwargs):
217 try:
218 indata = None
sousaedu80135b92021-02-17 15:05:18 +0100219
tierno1d213f42020-04-24 14:02:51 +0000220 if cherrypy.request.body.length:
221 error_text = "Invalid input format "
222
223 if "Content-Type" in cherrypy.request.headers:
224 if "application/json" in cherrypy.request.headers["Content-Type"]:
225 error_text = "Invalid json format "
226 indata = json.load(self.reader(cherrypy.request.body))
227 cherrypy.request.headers.pop("Content-File-MD5", None)
228 elif "application/yaml" in cherrypy.request.headers["Content-Type"]:
229 error_text = "Invalid yaml format "
aticig78ac0cf2022-07-15 00:43:09 +0300230 indata = yaml.safe_load(cherrypy.request.body)
tierno1d213f42020-04-24 14:02:51 +0000231 cherrypy.request.headers.pop("Content-File-MD5", None)
sousaedu80135b92021-02-17 15:05:18 +0100232 elif (
233 "application/binary" in cherrypy.request.headers["Content-Type"]
234 or "application/gzip"
235 in cherrypy.request.headers["Content-Type"]
236 or "application/zip" in cherrypy.request.headers["Content-Type"]
237 or "text/plain" in cherrypy.request.headers["Content-Type"]
238 ):
tierno1d213f42020-04-24 14:02:51 +0000239 indata = cherrypy.request.body # .read()
sousaedu80135b92021-02-17 15:05:18 +0100240 elif (
241 "multipart/form-data"
242 in cherrypy.request.headers["Content-Type"]
243 ):
tierno1d213f42020-04-24 14:02:51 +0000244 if "descriptor_file" in kwargs:
245 filecontent = kwargs.pop("descriptor_file")
sousaedu80135b92021-02-17 15:05:18 +0100246
tierno1d213f42020-04-24 14:02:51 +0000247 if not filecontent.file:
sousaedu80135b92021-02-17 15:05:18 +0100248 raise RoException(
249 "empty file or content", HTTPStatus.BAD_REQUEST
250 )
251
tierno1d213f42020-04-24 14:02:51 +0000252 indata = filecontent.file # .read()
sousaedu80135b92021-02-17 15:05:18 +0100253
tierno1d213f42020-04-24 14:02:51 +0000254 if filecontent.content_type.value:
sousaedu80135b92021-02-17 15:05:18 +0100255 cherrypy.request.headers[
256 "Content-Type"
257 ] = filecontent.content_type.value
tierno1d213f42020-04-24 14:02:51 +0000258 else:
259 # raise cherrypy.HTTPError(HTTPStatus.Not_Acceptable,
260 # "Only 'Content-Type' of type 'application/json' or
261 # 'application/yaml' for input format are available")
262 error_text = "Invalid yaml format "
aticig78ac0cf2022-07-15 00:43:09 +0300263 indata = yaml.safe_load(cherrypy.request.body)
tierno1d213f42020-04-24 14:02:51 +0000264 cherrypy.request.headers.pop("Content-File-MD5", None)
265 else:
266 error_text = "Invalid yaml format "
aticig78ac0cf2022-07-15 00:43:09 +0300267 indata = yaml.safe_load(cherrypy.request.body)
tierno1d213f42020-04-24 14:02:51 +0000268 cherrypy.request.headers.pop("Content-File-MD5", None)
sousaedu80135b92021-02-17 15:05:18 +0100269
tierno1d213f42020-04-24 14:02:51 +0000270 if not indata:
271 indata = {}
272
273 format_yaml = False
274 if cherrypy.request.headers.get("Query-String-Format") == "yaml":
275 format_yaml = True
276
277 for k, v in kwargs.items():
278 if isinstance(v, str):
279 if v == "":
280 kwargs[k] = None
281 elif format_yaml:
282 try:
aticig78ac0cf2022-07-15 00:43:09 +0300283 kwargs[k] = yaml.safe_load(v)
284 except Exception as yaml_error:
285 logging.exception(
286 f"{yaml_error} occured while parsing the yaml"
287 )
sousaedu80135b92021-02-17 15:05:18 +0100288 elif (
289 k.endswith(".gt")
290 or k.endswith(".lt")
291 or k.endswith(".gte")
292 or k.endswith(".lte")
293 ):
tierno1d213f42020-04-24 14:02:51 +0000294 try:
295 kwargs[k] = int(v)
296 except Exception:
297 try:
298 kwargs[k] = float(v)
aticig78ac0cf2022-07-15 00:43:09 +0300299 except Exception as keyword_error:
300 logging.exception(
301 f"{keyword_error} occured while getting the keyword arguments"
302 )
tierno1d213f42020-04-24 14:02:51 +0000303 elif v.find(",") > 0:
304 kwargs[k] = v.split(",")
305 elif isinstance(v, (list, tuple)):
306 for index in range(0, len(v)):
307 if v[index] == "":
308 v[index] = None
309 elif format_yaml:
310 try:
aticig78ac0cf2022-07-15 00:43:09 +0300311 v[index] = yaml.safe_load(v[index])
312 except Exception as error:
313 logging.exception(
314 f"{error} occured while parsing the yaml"
315 )
tierno1d213f42020-04-24 14:02:51 +0000316
317 return indata
318 except (ValueError, yaml.YAMLError) as exc:
319 raise RoException(error_text + str(exc), HTTPStatus.BAD_REQUEST)
320 except KeyError as exc:
321 raise RoException("Query string error: " + str(exc), HTTPStatus.BAD_REQUEST)
322 except Exception as exc:
323 raise RoException(error_text + str(exc), HTTPStatus.BAD_REQUEST)
324
325 @staticmethod
326 def _format_out(data, token_info=None, _format=None):
327 """
328 return string of dictionary data according to requested json, yaml, xml. By default json
329 :param data: response to be sent. Can be a dict, text or file
330 :param token_info: Contains among other username and project
331 :param _format: The format to be set as Content-Type if data is a file
332 :return: None
333 """
334 accept = cherrypy.request.headers.get("Accept")
sousaedu80135b92021-02-17 15:05:18 +0100335
tierno1d213f42020-04-24 14:02:51 +0000336 if data is None:
337 if accept and "text/html" in accept:
sousaedu80135b92021-02-17 15:05:18 +0100338 return html.format(
339 data, cherrypy.request, cherrypy.response, token_info
340 )
341
tierno1d213f42020-04-24 14:02:51 +0000342 # cherrypy.response.status = HTTPStatus.NO_CONTENT.value
343 return
344 elif hasattr(data, "read"): # file object
345 if _format:
346 cherrypy.response.headers["Content-Type"] = _format
347 elif "b" in data.mode: # binariy asssumig zip
sousaedu80135b92021-02-17 15:05:18 +0100348 cherrypy.response.headers["Content-Type"] = "application/zip"
tierno1d213f42020-04-24 14:02:51 +0000349 else:
sousaedu80135b92021-02-17 15:05:18 +0100350 cherrypy.response.headers["Content-Type"] = "text/plain"
351
tierno1d213f42020-04-24 14:02:51 +0000352 # TODO check that cherrypy close file. If not implement pending things to close per thread next
353 return data
sousaedu80135b92021-02-17 15:05:18 +0100354
tierno1d213f42020-04-24 14:02:51 +0000355 if accept:
356 if "application/json" in accept:
sousaedu80135b92021-02-17 15:05:18 +0100357 cherrypy.response.headers[
358 "Content-Type"
359 ] = "application/json; charset=utf-8"
tierno1d213f42020-04-24 14:02:51 +0000360 a = json.dumps(data, indent=4) + "\n"
sousaedu80135b92021-02-17 15:05:18 +0100361
tierno1d213f42020-04-24 14:02:51 +0000362 return a.encode("utf8")
363 elif "text/html" in accept:
sousaedu80135b92021-02-17 15:05:18 +0100364 return html.format(
365 data, cherrypy.request, cherrypy.response, token_info
366 )
367 elif (
368 "application/yaml" in accept
369 or "*/*" in accept
370 or "text/plain" in accept
371 ):
tierno1d213f42020-04-24 14:02:51 +0000372 pass
373 # if there is not any valid accept, raise an error. But if response is already an error, format in yaml
374 elif cherrypy.response.status >= 400:
sousaedu80135b92021-02-17 15:05:18 +0100375 raise cherrypy.HTTPError(
376 HTTPStatus.NOT_ACCEPTABLE.value,
377 "Only 'Accept' of type 'application/json' or 'application/yaml' "
378 "for output format are available",
379 )
380
381 cherrypy.response.headers["Content-Type"] = "application/yaml"
382
383 return yaml.safe_dump(
384 data,
385 explicit_start=True,
386 indent=4,
387 default_flow_style=False,
388 tags=False,
389 encoding="utf-8",
390 allow_unicode=True,
391 ) # , canonical=True, default_style='"'
tierno1d213f42020-04-24 14:02:51 +0000392
393 @cherrypy.expose
394 def index(self, *args, **kwargs):
395 token_info = None
sousaedu80135b92021-02-17 15:05:18 +0100396
tierno1d213f42020-04-24 14:02:51 +0000397 try:
398 if cherrypy.request.method == "GET":
399 token_info = self.authenticator.authorize()
sousaedu80135b92021-02-17 15:05:18 +0100400 outdata = token_info # Home page
tierno1d213f42020-04-24 14:02:51 +0000401 else:
sousaedu80135b92021-02-17 15:05:18 +0100402 raise cherrypy.HTTPError(
403 HTTPStatus.METHOD_NOT_ALLOWED.value,
404 "Method {} not allowed for tokens".format(cherrypy.request.method),
405 )
tierno1d213f42020-04-24 14:02:51 +0000406
407 return self._format_out(outdata, token_info)
tierno1d213f42020-04-24 14:02:51 +0000408 except (NsException, AuthException) as e:
409 # cherrypy.log("index Exception {}".format(e))
410 cherrypy.response.status = e.http_code.value
sousaedu80135b92021-02-17 15:05:18 +0100411
tierno1d213f42020-04-24 14:02:51 +0000412 return self._format_out("Welcome to OSM!", token_info)
413
414 @cherrypy.expose
415 def version(self, *args, **kwargs):
416 # TODO consider to remove and provide version using the static version file
417 try:
418 if cherrypy.request.method != "GET":
sousaedu80135b92021-02-17 15:05:18 +0100419 raise RoException(
420 "Only method GET is allowed",
421 HTTPStatus.METHOD_NOT_ALLOWED,
422 )
tierno1d213f42020-04-24 14:02:51 +0000423 elif args or kwargs:
sousaedu80135b92021-02-17 15:05:18 +0100424 raise RoException(
425 "Invalid URL or query string for version",
426 HTTPStatus.METHOD_NOT_ALLOWED,
427 )
428
tierno1d213f42020-04-24 14:02:51 +0000429 # TODO include version of other modules, pick up from some kafka admin message
430 osm_ng_ro_version = {"version": ro_version, "date": ro_version_date}
sousaedu80135b92021-02-17 15:05:18 +0100431
tierno1d213f42020-04-24 14:02:51 +0000432 return self._format_out(osm_ng_ro_version)
433 except RoException as e:
434 cherrypy.response.status = e.http_code.value
435 problem_details = {
436 "code": e.http_code.name,
437 "status": e.http_code.value,
438 "detail": str(e),
439 }
sousaedu80135b92021-02-17 15:05:18 +0100440
tierno1d213f42020-04-24 14:02:51 +0000441 return self._format_out(problem_details, None)
442
443 def new_token(self, engine_session, indata, *args, **kwargs):
444 token_info = None
445
446 try:
447 token_info = self.authenticator.authorize()
448 except Exception:
449 token_info = None
sousaedu80135b92021-02-17 15:05:18 +0100450
tierno1d213f42020-04-24 14:02:51 +0000451 if kwargs:
452 indata.update(kwargs)
sousaedu80135b92021-02-17 15:05:18 +0100453
tierno1d213f42020-04-24 14:02:51 +0000454 # This is needed to log the user when authentication fails
455 cherrypy.request.login = "{}".format(indata.get("username", "-"))
sousaedu80135b92021-02-17 15:05:18 +0100456 token_info = self.authenticator.new_token(
457 token_info, indata, cherrypy.request.remote
458 )
459 cherrypy.session["Authorization"] = token_info["id"]
tierno1d213f42020-04-24 14:02:51 +0000460 self._set_location_header("admin", "v1", "tokens", token_info["id"])
461 # for logging
462
463 # cherrypy.response.cookie["Authorization"] = outdata["id"]
464 # cherrypy.response.cookie["Authorization"]['expires'] = 3600
sousaedu80135b92021-02-17 15:05:18 +0100465
tierno1d213f42020-04-24 14:02:51 +0000466 return token_info, token_info["id"], True
467
468 def del_token(self, engine_session, indata, version, _id, *args, **kwargs):
469 token_id = _id
sousaedu80135b92021-02-17 15:05:18 +0100470
tierno1d213f42020-04-24 14:02:51 +0000471 if not token_id and "id" in kwargs:
472 token_id = kwargs["id"]
473 elif not token_id:
474 token_info = self.authenticator.authorize()
475 # for logging
476 token_id = token_info["id"]
sousaedu80135b92021-02-17 15:05:18 +0100477
tierno1d213f42020-04-24 14:02:51 +0000478 self.authenticator.del_token(token_id)
479 token_info = None
sousaedu80135b92021-02-17 15:05:18 +0100480 cherrypy.session["Authorization"] = "logout"
tierno1d213f42020-04-24 14:02:51 +0000481 # cherrypy.response.cookie["Authorization"] = token_id
482 # cherrypy.response.cookie["Authorization"]['expires'] = 0
sousaedu80135b92021-02-17 15:05:18 +0100483
tierno1d213f42020-04-24 14:02:51 +0000484 return None, None, True
sousaedu80135b92021-02-17 15:05:18 +0100485
tierno1d213f42020-04-24 14:02:51 +0000486 @cherrypy.expose
487 def test(self, *args, **kwargs):
sousaedu80135b92021-02-17 15:05:18 +0100488 if not cherrypy.config.get("server.enable_test") or (
489 isinstance(cherrypy.config["server.enable_test"], str)
490 and cherrypy.config["server.enable_test"].lower() == "false"
491 ):
tierno1d213f42020-04-24 14:02:51 +0000492 cherrypy.response.status = HTTPStatus.METHOD_NOT_ALLOWED.value
tierno1d213f42020-04-24 14:02:51 +0000493
sousaedu80135b92021-02-17 15:05:18 +0100494 return "test URL is disabled"
495
496 thread_info = None
497
498 if args and args[0] == "help":
499 return (
500 "<html><pre>\ninit\nfile/<name> download file\ndb-clear/table\nfs-clear[/folder]\nlogin\nlogin2\n"
501 "sleep/<time>\nmessage/topic\n</pre></html>"
502 )
tierno1d213f42020-04-24 14:02:51 +0000503 elif args and args[0] == "init":
504 try:
505 # self.ns.load_dbase(cherrypy.request.app.config)
506 self.ns.create_admin()
sousaedu80135b92021-02-17 15:05:18 +0100507
tierno1d213f42020-04-24 14:02:51 +0000508 return "Done. User 'admin', password 'admin' created"
509 except Exception:
510 cherrypy.response.status = HTTPStatus.FORBIDDEN.value
sousaedu80135b92021-02-17 15:05:18 +0100511
tierno1d213f42020-04-24 14:02:51 +0000512 return self._format_out("Database already initialized")
513 elif args and args[0] == "file":
sousaedu80135b92021-02-17 15:05:18 +0100514 return cherrypy.lib.static.serve_file(
515 cherrypy.tree.apps["/ro"].config["storage"]["path"] + "/" + args[1],
516 "text/plain",
517 "attachment",
518 )
tierno1d213f42020-04-24 14:02:51 +0000519 elif args and args[0] == "file2":
sousaedu80135b92021-02-17 15:05:18 +0100520 f_path = cherrypy.tree.apps["/ro"].config["storage"]["path"] + "/" + args[1]
tierno1d213f42020-04-24 14:02:51 +0000521 f = open(f_path, "r")
522 cherrypy.response.headers["Content-type"] = "text/plain"
523 return f
524
525 elif len(args) == 2 and args[0] == "db-clear":
526 deleted_info = self.ns.db.del_list(args[1], kwargs)
527 return "{} {} deleted\n".format(deleted_info["deleted"], args[1])
528 elif len(args) and args[0] == "fs-clear":
529 if len(args) >= 2:
530 folders = (args[1],)
531 else:
532 folders = self.ns.fs.dir_ls(".")
sousaedu80135b92021-02-17 15:05:18 +0100533
tierno1d213f42020-04-24 14:02:51 +0000534 for folder in folders:
535 self.ns.fs.file_delete(folder)
sousaedu80135b92021-02-17 15:05:18 +0100536
tierno1d213f42020-04-24 14:02:51 +0000537 return ",".join(folders) + " folders deleted\n"
538 elif args and args[0] == "login":
539 if not cherrypy.request.headers.get("Authorization"):
sousaedu80135b92021-02-17 15:05:18 +0100540 cherrypy.response.headers[
541 "WWW-Authenticate"
542 ] = 'Basic realm="Access to OSM site", charset="UTF-8"'
tierno1d213f42020-04-24 14:02:51 +0000543 cherrypy.response.status = HTTPStatus.UNAUTHORIZED.value
544 elif args and args[0] == "login2":
545 if not cherrypy.request.headers.get("Authorization"):
sousaedu80135b92021-02-17 15:05:18 +0100546 cherrypy.response.headers[
547 "WWW-Authenticate"
548 ] = 'Bearer realm="Access to OSM site"'
tierno1d213f42020-04-24 14:02:51 +0000549 cherrypy.response.status = HTTPStatus.UNAUTHORIZED.value
550 elif args and args[0] == "sleep":
551 sleep_time = 5
sousaedu80135b92021-02-17 15:05:18 +0100552
tierno1d213f42020-04-24 14:02:51 +0000553 try:
554 sleep_time = int(args[1])
555 except Exception:
556 cherrypy.response.status = HTTPStatus.FORBIDDEN.value
557 return self._format_out("Database already initialized")
sousaedu80135b92021-02-17 15:05:18 +0100558
tierno1d213f42020-04-24 14:02:51 +0000559 thread_info = cherrypy.thread_data
560 print(thread_info)
561 time.sleep(sleep_time)
562 # thread_info
563 elif len(args) >= 2 and args[0] == "message":
564 main_topic = args[1]
565 return_text = "<html><pre>{} ->\n".format(main_topic)
sousaedu80135b92021-02-17 15:05:18 +0100566
tierno1d213f42020-04-24 14:02:51 +0000567 try:
sousaedu80135b92021-02-17 15:05:18 +0100568 if cherrypy.request.method == "POST":
aticig78ac0cf2022-07-15 00:43:09 +0300569 to_send = yaml.safe_load(cherrypy.request.body)
tierno1d213f42020-04-24 14:02:51 +0000570 for k, v in to_send.items():
571 self.ns.msg.write(main_topic, k, v)
572 return_text += " {}: {}\n".format(k, v)
sousaedu80135b92021-02-17 15:05:18 +0100573 elif cherrypy.request.method == "GET":
tierno1d213f42020-04-24 14:02:51 +0000574 for k, v in kwargs.items():
aticig78ac0cf2022-07-15 00:43:09 +0300575 self.ns.msg.write(main_topic, k, yaml.safe_load(v))
576 return_text += " {}: {}\n".format(k, yaml.safe_load(v))
tierno1d213f42020-04-24 14:02:51 +0000577 except Exception as e:
578 return_text += "Error: " + str(e)
sousaedu80135b92021-02-17 15:05:18 +0100579
tierno1d213f42020-04-24 14:02:51 +0000580 return_text += "</pre></html>\n"
sousaedu80135b92021-02-17 15:05:18 +0100581
tierno1d213f42020-04-24 14:02:51 +0000582 return return_text
583
584 return_text = (
sousaedu80135b92021-02-17 15:05:18 +0100585 "<html><pre>\nheaders:\n args: {}\n".format(args)
586 + " kwargs: {}\n".format(kwargs)
587 + " headers: {}\n".format(cherrypy.request.headers)
588 + " path_info: {}\n".format(cherrypy.request.path_info)
589 + " query_string: {}\n".format(cherrypy.request.query_string)
590 + " session: {}\n".format(cherrypy.session)
591 + " cookie: {}\n".format(cherrypy.request.cookie)
592 + " method: {}\n".format(cherrypy.request.method)
593 + " session: {}\n".format(cherrypy.session.get("fieldname"))
594 + " body:\n"
595 )
tierno1d213f42020-04-24 14:02:51 +0000596 return_text += " length: {}\n".format(cherrypy.request.body.length)
sousaedu80135b92021-02-17 15:05:18 +0100597
tierno1d213f42020-04-24 14:02:51 +0000598 if cherrypy.request.body.length:
599 return_text += " content: {}\n".format(
sousaedu80135b92021-02-17 15:05:18 +0100600 str(
601 cherrypy.request.body.read(
602 int(cherrypy.request.headers.get("Content-Length", 0))
603 )
604 )
605 )
606
tierno1d213f42020-04-24 14:02:51 +0000607 if thread_info:
608 return_text += "thread: {}\n".format(thread_info)
sousaedu80135b92021-02-17 15:05:18 +0100609
tierno1d213f42020-04-24 14:02:51 +0000610 return_text += "</pre></html>"
sousaedu80135b92021-02-17 15:05:18 +0100611
tierno1d213f42020-04-24 14:02:51 +0000612 return return_text
613
614 @staticmethod
615 def _check_valid_url_method(method, *args):
616 if len(args) < 3:
sousaedu80135b92021-02-17 15:05:18 +0100617 raise RoException(
618 "URL must contain at least 'main_topic/version/topic'",
619 HTTPStatus.METHOD_NOT_ALLOWED,
620 )
tierno1d213f42020-04-24 14:02:51 +0000621
622 reference = valid_url_methods
623 for arg in args:
624 if arg is None:
625 break
sousaedu80135b92021-02-17 15:05:18 +0100626
tierno1d213f42020-04-24 14:02:51 +0000627 if not isinstance(reference, dict):
sousaedu80135b92021-02-17 15:05:18 +0100628 raise RoException(
629 "URL contains unexpected extra items '{}'".format(arg),
630 HTTPStatus.METHOD_NOT_ALLOWED,
631 )
tierno1d213f42020-04-24 14:02:51 +0000632
633 if arg in reference:
634 reference = reference[arg]
635 elif "<ID>" in reference:
636 reference = reference["<ID>"]
637 elif "*" in reference:
638 # reference = reference["*"]
639 break
640 else:
sousaedu80135b92021-02-17 15:05:18 +0100641 raise RoException(
642 "Unexpected URL item {}".format(arg),
643 HTTPStatus.METHOD_NOT_ALLOWED,
644 )
645
tierno1d213f42020-04-24 14:02:51 +0000646 if "TODO" in reference and method in reference["TODO"]:
sousaedu80135b92021-02-17 15:05:18 +0100647 raise RoException(
648 "Method {} not supported yet for this URL".format(method),
649 HTTPStatus.NOT_IMPLEMENTED,
650 )
tierno1d213f42020-04-24 14:02:51 +0000651 elif "METHODS" not in reference or method not in reference["METHODS"]:
sousaedu80135b92021-02-17 15:05:18 +0100652 raise RoException(
653 "Method {} not supported for this URL".format(method),
654 HTTPStatus.METHOD_NOT_ALLOWED,
655 )
656
tierno1d213f42020-04-24 14:02:51 +0000657 return reference["ROLE_PERMISSION"] + method.lower()
658
659 @staticmethod
660 def _set_location_header(main_topic, version, topic, id):
661 """
662 Insert response header Location with the URL of created item base on URL params
663 :param main_topic:
664 :param version:
665 :param topic:
666 :param id:
667 :return: None
668 """
669 # Use cherrypy.request.base for absoluted path and make use of request.header HOST just in case behind aNAT
sousaedu80135b92021-02-17 15:05:18 +0100670 cherrypy.response.headers["Location"] = "/ro/{}/{}/{}/{}".format(
671 main_topic, version, topic, id
672 )
673
tierno1d213f42020-04-24 14:02:51 +0000674 return
675
676 @cherrypy.expose
sousaedu80135b92021-02-17 15:05:18 +0100677 def default(
678 self,
679 main_topic=None,
680 version=None,
681 topic=None,
682 _id=None,
683 _id2=None,
684 *args,
685 **kwargs,
686 ):
tierno1d213f42020-04-24 14:02:51 +0000687 token_info = None
688 outdata = None
689 _format = None
690 method = "DONE"
691 rollback = []
692 engine_session = None
sousaedu80135b92021-02-17 15:05:18 +0100693
tierno1d213f42020-04-24 14:02:51 +0000694 try:
695 if not main_topic or not version or not topic:
sousaedu80135b92021-02-17 15:05:18 +0100696 raise RoException(
697 "URL must contain at least 'main_topic/version/topic'",
698 HTTPStatus.METHOD_NOT_ALLOWED,
699 )
tierno1d213f42020-04-24 14:02:51 +0000700
sousaedu80135b92021-02-17 15:05:18 +0100701 if main_topic not in (
702 "admin",
703 "ns",
704 ):
705 raise RoException(
706 "URL main_topic '{}' not supported".format(main_topic),
707 HTTPStatus.METHOD_NOT_ALLOWED,
708 )
709
710 if version != "v1":
711 raise RoException(
712 "URL version '{}' not supported".format(version),
713 HTTPStatus.METHOD_NOT_ALLOWED,
714 )
715
716 if (
717 kwargs
718 and "METHOD" in kwargs
719 and kwargs["METHOD"] in ("PUT", "POST", "DELETE", "GET", "PATCH")
720 ):
tierno1d213f42020-04-24 14:02:51 +0000721 method = kwargs.pop("METHOD")
722 else:
723 method = cherrypy.request.method
724
sousaedu80135b92021-02-17 15:05:18 +0100725 role_permission = self._check_valid_url_method(
726 method, main_topic, version, topic, _id, _id2, *args, **kwargs
727 )
tierno1d213f42020-04-24 14:02:51 +0000728 # skip token validation if requesting a token
729 indata = self._format_in(kwargs)
sousaedu80135b92021-02-17 15:05:18 +0100730
tierno1d213f42020-04-24 14:02:51 +0000731 if main_topic != "admin" or topic != "tokens":
732 token_info = self.authenticator.authorize(role_permission, _id)
sousaedu80135b92021-02-17 15:05:18 +0100733
tierno1d213f42020-04-24 14:02:51 +0000734 outdata, created_id, done = self.map_operation[role_permission](
sousaedu80135b92021-02-17 15:05:18 +0100735 engine_session, indata, version, _id, _id2, *args, *kwargs
736 )
737
tierno1d213f42020-04-24 14:02:51 +0000738 if created_id:
739 self._set_location_header(main_topic, version, topic, _id)
sousaedu80135b92021-02-17 15:05:18 +0100740
741 cherrypy.response.status = (
742 HTTPStatus.ACCEPTED.value
743 if not done
744 else HTTPStatus.OK.value
745 if outdata is not None
746 else HTTPStatus.NO_CONTENT.value
747 )
748
tierno1d213f42020-04-24 14:02:51 +0000749 return self._format_out(outdata, token_info, _format)
750 except Exception as e:
sousaedu80135b92021-02-17 15:05:18 +0100751 if isinstance(
752 e,
753 (
754 RoException,
755 NsException,
756 DbException,
757 FsException,
758 MsgException,
759 AuthException,
760 ValidationError,
761 ),
762 ):
tierno1d213f42020-04-24 14:02:51 +0000763 http_code_value = cherrypy.response.status = e.http_code.value
764 http_code_name = e.http_code.name
765 cherrypy.log("Exception {}".format(e))
766 else:
sousaedu80135b92021-02-17 15:05:18 +0100767 http_code_value = (
768 cherrypy.response.status
769 ) = HTTPStatus.BAD_REQUEST.value # INTERNAL_SERVER_ERROR
tierno1d213f42020-04-24 14:02:51 +0000770 cherrypy.log("CRITICAL: Exception {}".format(e), traceback=True)
771 http_code_name = HTTPStatus.BAD_REQUEST.name
sousaedu80135b92021-02-17 15:05:18 +0100772
tierno1d213f42020-04-24 14:02:51 +0000773 if hasattr(outdata, "close"): # is an open file
774 outdata.close()
sousaedu80135b92021-02-17 15:05:18 +0100775
tierno1d213f42020-04-24 14:02:51 +0000776 error_text = str(e)
777 rollback.reverse()
sousaedu80135b92021-02-17 15:05:18 +0100778
tierno1d213f42020-04-24 14:02:51 +0000779 for rollback_item in rollback:
780 try:
781 if rollback_item.get("operation") == "set":
sousaedu80135b92021-02-17 15:05:18 +0100782 self.ns.db.set_one(
783 rollback_item["topic"],
784 {"_id": rollback_item["_id"]},
785 rollback_item["content"],
786 fail_on_empty=False,
787 )
tierno1d213f42020-04-24 14:02:51 +0000788 else:
sousaedu80135b92021-02-17 15:05:18 +0100789 self.ns.db.del_one(
790 rollback_item["topic"],
791 {"_id": rollback_item["_id"]},
792 fail_on_empty=False,
793 )
tierno1d213f42020-04-24 14:02:51 +0000794 except Exception as e2:
sousaedu80135b92021-02-17 15:05:18 +0100795 rollback_error_text = "Rollback Exception {}: {}".format(
796 rollback_item, e2
797 )
tierno1d213f42020-04-24 14:02:51 +0000798 cherrypy.log(rollback_error_text)
799 error_text += ". " + rollback_error_text
sousaedu80135b92021-02-17 15:05:18 +0100800
tierno1d213f42020-04-24 14:02:51 +0000801 # if isinstance(e, MsgException):
802 # error_text = "{} has been '{}' but other modules cannot be informed because an error on bus".format(
803 # engine_topic[:-1], method, error_text)
804 problem_details = {
805 "code": http_code_name,
806 "status": http_code_value,
807 "detail": error_text,
808 }
sousaedu80135b92021-02-17 15:05:18 +0100809
tierno1d213f42020-04-24 14:02:51 +0000810 return self._format_out(problem_details, token_info)
811 # raise cherrypy.HTTPError(e.http_code.value, str(e))
812 finally:
813 if token_info:
814 if method in ("PUT", "PATCH", "POST") and isinstance(outdata, dict):
815 for logging_id in ("id", "op_id", "nsilcmop_id", "nslcmop_id"):
816 if outdata.get(logging_id):
sousaedu80135b92021-02-17 15:05:18 +0100817 cherrypy.request.login += ";{}={}".format(
818 logging_id, outdata[logging_id][:36]
819 )
tierno1d213f42020-04-24 14:02:51 +0000820
821
822def _start_service():
823 """
824 Callback function called when cherrypy.engine starts
825 Override configuration with env variables
826 Set database, storage, message configuration
827 Init database with admin/admin user password
828 """
tierno70eeb182020-10-19 16:38:00 +0000829 global ro_server, vim_admin_thread
tierno1d213f42020-04-24 14:02:51 +0000830 # global vim_threads
831 cherrypy.log.error("Starting osm_ng_ro")
832 # update general cherrypy configuration
833 update_dict = {}
sousaedu80135b92021-02-17 15:05:18 +0100834 engine_config = cherrypy.tree.apps["/ro"].config
tierno1d213f42020-04-24 14:02:51 +0000835
tierno1d213f42020-04-24 14:02:51 +0000836 for k, v in environ.items():
837 if not k.startswith("OSMRO_"):
838 continue
sousaedu80135b92021-02-17 15:05:18 +0100839
tierno1d213f42020-04-24 14:02:51 +0000840 k1, _, k2 = k[6:].lower().partition("_")
sousaedu80135b92021-02-17 15:05:18 +0100841
tierno1d213f42020-04-24 14:02:51 +0000842 if not k2:
843 continue
sousaedu80135b92021-02-17 15:05:18 +0100844
tierno1d213f42020-04-24 14:02:51 +0000845 try:
846 if k1 in ("server", "test", "auth", "log"):
847 # update [global] configuration
sousaedu80135b92021-02-17 15:05:18 +0100848 update_dict[k1 + "." + k2] = yaml.safe_load(v)
tierno1d213f42020-04-24 14:02:51 +0000849 elif k1 == "static":
850 # update [/static] configuration
851 engine_config["/static"]["tools.staticdir." + k2] = yaml.safe_load(v)
852 elif k1 == "tools":
853 # update [/] configuration
sousaedu80135b92021-02-17 15:05:18 +0100854 engine_config["/"]["tools." + k2.replace("_", ".")] = yaml.safe_load(v)
aticig973bed22022-06-30 19:29:04 +0300855 elif k1 in ("message", "database", "storage", "authentication", "period"):
tierno70eeb182020-10-19 16:38:00 +0000856 engine_config[k1][k2] = yaml.safe_load(v)
tierno1d213f42020-04-24 14:02:51 +0000857
858 except Exception as e:
859 raise RoException("Cannot load env '{}': {}".format(k, e))
860
861 if update_dict:
862 cherrypy.config.update(update_dict)
863 engine_config["global"].update(update_dict)
864
865 # logging cherrypy
sousaedu80135b92021-02-17 15:05:18 +0100866 log_format_simple = (
867 "%(asctime)s %(levelname)s %(name)s %(filename)s:%(lineno)s %(message)s"
868 )
869 log_formatter_simple = logging.Formatter(
870 log_format_simple, datefmt="%Y-%m-%dT%H:%M:%S"
871 )
tierno1d213f42020-04-24 14:02:51 +0000872 logger_server = logging.getLogger("cherrypy.error")
873 logger_access = logging.getLogger("cherrypy.access")
874 logger_cherry = logging.getLogger("cherrypy")
sousaedue493e9b2021-02-09 15:30:01 +0100875 logger = logging.getLogger("ro")
tierno1d213f42020-04-24 14:02:51 +0000876
877 if "log.file" in engine_config["global"]:
sousaedu80135b92021-02-17 15:05:18 +0100878 file_handler = logging.handlers.RotatingFileHandler(
879 engine_config["global"]["log.file"], maxBytes=100e6, backupCount=9, delay=0
880 )
tierno1d213f42020-04-24 14:02:51 +0000881 file_handler.setFormatter(log_formatter_simple)
882 logger_cherry.addHandler(file_handler)
sousaedue493e9b2021-02-09 15:30:01 +0100883 logger.addHandler(file_handler)
sousaedu80135b92021-02-17 15:05:18 +0100884
tierno1d213f42020-04-24 14:02:51 +0000885 # log always to standard output
sousaedu80135b92021-02-17 15:05:18 +0100886 for format_, logger in {
887 "ro.server %(filename)s:%(lineno)s": logger_server,
888 "ro.access %(filename)s:%(lineno)s": logger_access,
889 "%(name)s %(filename)s:%(lineno)s": logger,
890 }.items():
tierno1d213f42020-04-24 14:02:51 +0000891 log_format_cherry = "%(asctime)s %(levelname)s {} %(message)s".format(format_)
sousaedu80135b92021-02-17 15:05:18 +0100892 log_formatter_cherry = logging.Formatter(
893 log_format_cherry, datefmt="%Y-%m-%dT%H:%M:%S"
894 )
tierno1d213f42020-04-24 14:02:51 +0000895 str_handler = logging.StreamHandler()
896 str_handler.setFormatter(log_formatter_cherry)
897 logger.addHandler(str_handler)
898
899 if engine_config["global"].get("log.level"):
900 logger_cherry.setLevel(engine_config["global"]["log.level"])
sousaedue493e9b2021-02-09 15:30:01 +0100901 logger.setLevel(engine_config["global"]["log.level"])
sousaedu80135b92021-02-17 15:05:18 +0100902
tierno1d213f42020-04-24 14:02:51 +0000903 # logging other modules
sousaedu80135b92021-02-17 15:05:18 +0100904 for k1, logname in {
905 "message": "ro.msg",
906 "database": "ro.db",
907 "storage": "ro.fs",
908 }.items():
tierno1d213f42020-04-24 14:02:51 +0000909 engine_config[k1]["logger_name"] = logname
910 logger_module = logging.getLogger(logname)
sousaedu80135b92021-02-17 15:05:18 +0100911
tierno1d213f42020-04-24 14:02:51 +0000912 if "logfile" in engine_config[k1]:
sousaedu80135b92021-02-17 15:05:18 +0100913 file_handler = logging.handlers.RotatingFileHandler(
914 engine_config[k1]["logfile"], maxBytes=100e6, backupCount=9, delay=0
915 )
tierno1d213f42020-04-24 14:02:51 +0000916 file_handler.setFormatter(log_formatter_simple)
917 logger_module.addHandler(file_handler)
sousaedu80135b92021-02-17 15:05:18 +0100918
tierno1d213f42020-04-24 14:02:51 +0000919 if "loglevel" in engine_config[k1]:
920 logger_module.setLevel(engine_config[k1]["loglevel"])
921 # TODO add more entries, e.g.: storage
922
923 engine_config["assignment"] = {}
924 # ^ each VIM, SDNc will be assigned one worker id. Ns class will add items and VimThread will auto-assign
sousaedu80135b92021-02-17 15:05:18 +0100925 cherrypy.tree.apps["/ro"].root.ns.start(engine_config)
926 cherrypy.tree.apps["/ro"].root.authenticator.start(engine_config)
927 cherrypy.tree.apps["/ro"].root.ns.init_db(target_version=database_version)
tierno1d213f42020-04-24 14:02:51 +0000928
929 # # start subscriptions thread:
tierno70eeb182020-10-19 16:38:00 +0000930 vim_admin_thread = VimAdminThread(config=engine_config, engine=ro_server.ns)
931 vim_admin_thread.start()
tierno1d213f42020-04-24 14:02:51 +0000932 # # Do not capture except SubscriptionException
933
tierno70eeb182020-10-19 16:38:00 +0000934 # backend = engine_config["authentication"]["backend"]
935 # cherrypy.log.error("Starting OSM NBI Version '{} {}' with '{}' authentication backend"
936 # .format(ro_version, ro_version_date, backend))
tierno1d213f42020-04-24 14:02:51 +0000937
938
939def _stop_service():
940 """
941 Callback function called when cherrypy.engine stops
942 TODO: Ending database connections.
943 """
tierno70eeb182020-10-19 16:38:00 +0000944 global vim_admin_thread
sousaedu80135b92021-02-17 15:05:18 +0100945
tierno70eeb182020-10-19 16:38:00 +0000946 # terminate vim_admin_thread
947 if vim_admin_thread:
948 vim_admin_thread.terminate()
sousaedu80135b92021-02-17 15:05:18 +0100949
tierno70eeb182020-10-19 16:38:00 +0000950 vim_admin_thread = None
sousaedu80135b92021-02-17 15:05:18 +0100951 cherrypy.tree.apps["/ro"].root.ns.stop()
tierno1d213f42020-04-24 14:02:51 +0000952 cherrypy.log.error("Stopping osm_ng_ro")
953
954
955def ro_main(config_file):
956 global ro_server
sousaedu80135b92021-02-17 15:05:18 +0100957
tierno1d213f42020-04-24 14:02:51 +0000958 ro_server = Server()
sousaedu80135b92021-02-17 15:05:18 +0100959 cherrypy.engine.subscribe("start", _start_service)
960 cherrypy.engine.subscribe("stop", _stop_service)
961 cherrypy.quickstart(ro_server, "/ro", config_file)
tierno1d213f42020-04-24 14:02:51 +0000962
963
964def usage():
sousaedu80135b92021-02-17 15:05:18 +0100965 print(
966 """Usage: {} [options]
tierno1d213f42020-04-24 14:02:51 +0000967 -c|--config [configuration_file]: loads the configuration file (default: ./ro.cfg)
968 -h|--help: shows this help
sousaedu80135b92021-02-17 15:05:18 +0100969 """.format(
970 sys.argv[0]
971 )
972 )
tierno1d213f42020-04-24 14:02:51 +0000973 # --log-socket-host HOST: send logs to this host")
974 # --log-socket-port PORT: send logs using this port (default: 9022)")
975
976
sousaedu80135b92021-02-17 15:05:18 +0100977if __name__ == "__main__":
tierno1d213f42020-04-24 14:02:51 +0000978 try:
979 # load parameters and configuration
980 opts, args = getopt.getopt(sys.argv[1:], "hvc:", ["config=", "help"])
981 # TODO add "log-socket-host=", "log-socket-port=", "log-file="
982 config_file = None
sousaedu80135b92021-02-17 15:05:18 +0100983
tierno1d213f42020-04-24 14:02:51 +0000984 for o, a in opts:
985 if o in ("-h", "--help"):
986 usage()
987 sys.exit()
988 elif o in ("-c", "--config"):
989 config_file = a
990 else:
aticig78ac0cf2022-07-15 00:43:09 +0300991 raise ValueError("Unhandled option")
sousaedu80135b92021-02-17 15:05:18 +0100992
tierno1d213f42020-04-24 14:02:51 +0000993 if config_file:
994 if not path.isfile(config_file):
sousaedu80135b92021-02-17 15:05:18 +0100995 print(
996 "configuration file '{}' that not exist".format(config_file),
997 file=sys.stderr,
998 )
tierno1d213f42020-04-24 14:02:51 +0000999 exit(1)
1000 else:
sousaedu80135b92021-02-17 15:05:18 +01001001 for config_file in (
1002 path.dirname(__file__) + "/ro.cfg",
1003 "./ro.cfg",
1004 "/etc/osm/ro.cfg",
1005 ):
tierno1d213f42020-04-24 14:02:51 +00001006 if path.isfile(config_file):
1007 break
1008 else:
sousaedu80135b92021-02-17 15:05:18 +01001009 print(
1010 "No configuration file 'ro.cfg' found neither at local folder nor at /etc/osm/",
1011 file=sys.stderr,
1012 )
tierno1d213f42020-04-24 14:02:51 +00001013 exit(1)
sousaedu80135b92021-02-17 15:05:18 +01001014
tierno1d213f42020-04-24 14:02:51 +00001015 ro_main(config_file)
tierno70eeb182020-10-19 16:38:00 +00001016 except KeyboardInterrupt:
1017 print("KeyboardInterrupt. Finishing", file=sys.stderr)
tierno1d213f42020-04-24 14:02:51 +00001018 except getopt.GetoptError as e:
1019 print(str(e), file=sys.stderr)
1020 # usage()
1021 exit(1)