2 # -*- coding: utf-8 -*-
5 # Copyright 2018 Telefonica S.A.
7 # Licensed under the Apache License, Version 2.0 (the "License"); you may
8 # not use this file except in compliance with the License. You may obtain
9 # a copy of the License at
11 # http://www.apache.org/licenses/LICENSE-2.0
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16 # License for the specific language governing permissions and limitations
28 import logging
.handlers
32 from osm_lcm
import ns
, vim_sdn
, netslice
33 from osm_lcm
.ng_ro
import NgRoException
, NgRoClient
34 from osm_lcm
.ROclient
import ROClient
, ROClientException
37 from osm_lcm
.lcm_utils
import versiontuple
, LcmException
, TaskRegistry
, LcmExceptionExit
38 from osm_lcm
import version
as lcm_version
, version_date
as lcm_version_date
40 from osm_common
import msglocal
, msgkafka
41 from osm_common
import version
as common_version
42 from osm_common
.dbbase
import DbException
43 from osm_common
.fsbase
import FsException
44 from osm_common
.msgbase
import MsgException
45 from osm_lcm
.data_utils
.database
.database
import Database
46 from osm_lcm
.data_utils
.filesystem
.filesystem
import Filesystem
47 from osm_lcm
.data_utils
.lcm_config
import LcmCfg
48 from osm_lcm
.lcm_hc
import get_health_check_file
50 from random
import choice
as random_choice
51 from n2vc
import version
as n2vc_version
54 if os
.getenv("OSMLCM_PDB_DEBUG", None) is not None:
58 __author__
= "Alfonso Tierno"
59 min_RO_version
= "6.0.2"
60 min_n2vc_version
= "0.0.2"
62 min_common_version
= "0.1.19"
67 ping_interval_pace
= (
68 120 # how many time ping is send once is confirmed all is running
70 ping_interval_boot
= 5 # how many time ping is sent when booting
72 main_config
= LcmCfg()
74 def __init__(self
, config_file
, loop
=None):
76 Init, Connect to database, filesystem storage, and messaging
77 :param config: two level dictionary with configuration. Top level should contain 'database', 'storage',
84 self
.pings_not_received
= 1
85 self
.consecutive_errors
= 0
86 self
.first_start
= False
89 self
.logger
= logging
.getLogger("lcm")
91 self
.worker_id
= self
.get_process_id()
93 config
= self
.read_config_file(config_file
)
94 self
.main_config
.set_from_dict(config
)
95 self
.main_config
.transform()
96 self
.main_config
.load_from_env()
97 self
.logger
.critical("Loaded configuration:" + str(self
.main_config
.to_dict()))
98 # TODO: check if lcm_hc.py is necessary
99 self
.health_check_file
= get_health_check_file(self
.main_config
.to_dict())
100 self
.loop
= loop
or asyncio
.get_event_loop()
105 ) = self
.wim
= self
.sdn
= self
.k8scluster
= self
.vca
= self
.k8srepo
= None
108 log_format_simple
= (
109 "%(asctime)s %(levelname)s %(name)s %(filename)s:%(lineno)s %(message)s"
111 log_formatter_simple
= logging
.Formatter(
112 log_format_simple
, datefmt
="%Y-%m-%dT%H:%M:%S"
114 if self
.main_config
.globalConfig
.logfile
:
115 file_handler
= logging
.handlers
.RotatingFileHandler(
116 self
.main_config
.globalConfig
.logfile
,
121 file_handler
.setFormatter(log_formatter_simple
)
122 self
.logger
.addHandler(file_handler
)
123 if not self
.main_config
.globalConfig
.to_dict()["nologging"]:
124 str_handler
= logging
.StreamHandler()
125 str_handler
.setFormatter(log_formatter_simple
)
126 self
.logger
.addHandler(str_handler
)
128 if self
.main_config
.globalConfig
.to_dict()["loglevel"]:
129 self
.logger
.setLevel(self
.main_config
.globalConfig
.loglevel
)
131 # logging other modules
132 for logger
in ("message", "database", "storage", "tsdb"):
133 logger_config
= self
.main_config
.to_dict()[logger
]
134 logger_module
= logging
.getLogger(logger_config
["logger_name"])
135 if logger_config
["logfile"]:
136 file_handler
= logging
.handlers
.RotatingFileHandler(
137 logger_config
["logfile"], maxBytes
=100e6
, backupCount
=9, delay
=0
139 file_handler
.setFormatter(log_formatter_simple
)
140 logger_module
.addHandler(file_handler
)
141 if logger_config
["loglevel"]:
142 logger_module
.setLevel(logger_config
["loglevel"])
143 self
.logger
.critical(
144 "starting osm/lcm version {} {}".format(lcm_version
, lcm_version_date
)
147 # check version of N2VC
148 # TODO enhance with int conversion or from distutils.version import LooseVersion
149 # or with list(map(int, version.split(".")))
150 if versiontuple(n2vc_version
) < versiontuple(min_n2vc_version
):
152 "Not compatible osm/N2VC version '{}'. Needed '{}' or higher".format(
153 n2vc_version
, min_n2vc_version
156 # check version of common
157 if versiontuple(common_version
) < versiontuple(min_common_version
):
159 "Not compatible osm/common version '{}'. Needed '{}' or higher".format(
160 common_version
, min_common_version
165 self
.db
= Database(self
.main_config
.to_dict()).instance
.db
167 self
.fs
= Filesystem(self
.main_config
.to_dict()).instance
.fs
170 # copy message configuration in order to remove 'group_id' for msg_admin
171 config_message
= self
.main_config
.message
.to_dict()
172 config_message
["loop"] = self
.loop
173 if config_message
["driver"] == "local":
174 self
.msg
= msglocal
.MsgLocal()
175 self
.msg
.connect(config_message
)
176 self
.msg_admin
= msglocal
.MsgLocal()
177 config_message
.pop("group_id", None)
178 self
.msg_admin
.connect(config_message
)
179 elif config_message
["driver"] == "kafka":
180 self
.msg
= msgkafka
.MsgKafka()
181 self
.msg
.connect(config_message
)
182 self
.msg_admin
= msgkafka
.MsgKafka()
183 config_message
.pop("group_id", None)
184 self
.msg_admin
.connect(config_message
)
187 "Invalid configuration param '{}' at '[message]':'driver'".format(
188 self
.main_config
.message
.driver
191 except (DbException
, FsException
, MsgException
) as e
:
192 self
.logger
.critical(str(e
), exc_info
=True)
193 raise LcmException(str(e
))
195 # contains created tasks/futures to be able to cancel
196 self
.lcm_tasks
= TaskRegistry(self
.worker_id
, self
.logger
)
198 async def check_RO_version(self
):
202 ro_uri
= self
.main_config
.RO
.uri
206 # try new RO, if fail old RO
208 self
.main_config
.RO
.uri
= ro_uri
+ "ro"
209 ro_server
= NgRoClient(self
.loop
, **self
.main_config
.RO
.to_dict())
210 ro_version
= await ro_server
.get_version()
211 self
.main_config
.RO
.ng
= True
213 self
.main_config
.RO
.uri
= ro_uri
+ "openmano"
214 ro_server
= ROClient(self
.loop
, **self
.main_config
.RO
.to_dict())
215 ro_version
= await ro_server
.get_version()
216 self
.main_config
.RO
.ng
= False
217 if versiontuple(ro_version
) < versiontuple(min_RO_version
):
219 "Not compatible osm/RO version '{}'. Needed '{}' or higher".format(
220 ro_version
, min_RO_version
224 "Connected to RO version {} new-generation version {}".format(
225 ro_version
, self
.main_config
.RO
.ng
229 except (ROClientException
, NgRoException
) as e
:
230 self
.main_config
.RO
.uri
= ro_uri
232 traceback
.print_tb(e
.__traceback
__)
233 error_text
= "Error while connecting to RO on {}: {}".format(
234 self
.main_config
.RO
.uri
, e
237 self
.logger
.critical(error_text
)
238 raise LcmException(error_text
)
239 if last_error
!= error_text
:
240 last_error
= error_text
242 error_text
+ ". Waiting until {} seconds".format(5 * tries
)
244 await asyncio
.sleep(5)
246 async def test(self
, param
=None):
247 self
.logger
.debug("Starting/Ending test task: {}".format(param
))
249 async def kafka_ping(self
):
250 self
.logger
.debug("Task kafka_ping Enter")
251 consecutive_errors
= 0
253 kafka_has_received
= False
254 self
.pings_not_received
= 1
257 await self
.msg_admin
.aiowrite(
263 "worker_id": self
.worker_id
,
264 "version": lcm_version
,
268 # time between pings are low when it is not received and at starting
270 self
.ping_interval_boot
271 if not kafka_has_received
272 else self
.ping_interval_pace
274 if not self
.pings_not_received
:
275 kafka_has_received
= True
276 self
.pings_not_received
+= 1
277 await asyncio
.sleep(wait_time
, loop
=self
.loop
)
278 if self
.pings_not_received
> 10:
279 raise LcmException("It is not receiving pings from Kafka bus")
280 consecutive_errors
= 0
284 except Exception as e
:
285 # if not first_start is the first time after starting. So leave more time and wait
286 # to allow kafka starts
287 if consecutive_errors
== 8 if not first_start
else 30:
289 "Task kafka_read task exit error too many errors. Exception: {}".format(
294 consecutive_errors
+= 1
296 "Task kafka_read retrying after Exception {}".format(e
)
298 wait_time
= 2 if not first_start
else 5
299 await asyncio
.sleep(wait_time
, loop
=self
.loop
)
301 def kafka_read_callback(self
, topic
, command
, params
):
304 if topic
!= "admin" and command
!= "ping":
306 "Task kafka_read receives {} {}: {}".format(topic
, command
, params
)
308 self
.consecutive_errors
= 0
309 self
.first_start
= False
311 if command
== "exit":
312 raise LcmExceptionExit
313 elif command
.startswith("#"):
315 elif command
== "echo":
320 elif command
== "test":
321 asyncio
.Task(self
.test(params
), loop
=self
.loop
)
325 if command
== "ping" and params
["to"] == "lcm" and params
["from"] == "lcm":
326 if params
.get("worker_id") != self
.worker_id
:
328 self
.pings_not_received
= 0
330 with
open(self
.health_check_file
, "w") as f
:
332 except Exception as e
:
334 "Cannot write into '{}' for healthcheck: {}".format(
335 self
.health_check_file
, e
340 if command
== "placement":
341 self
.ns
.update_nsrs_with_pla_result(params
)
343 elif topic
== "k8scluster":
344 if command
== "create" or command
== "created":
345 k8scluster_id
= params
.get("_id")
346 task
= asyncio
.ensure_future(self
.k8scluster
.create(params
, order_id
))
347 self
.lcm_tasks
.register(
348 "k8scluster", k8scluster_id
, order_id
, "k8scluster_create", task
351 elif command
== "delete" or command
== "deleted":
352 k8scluster_id
= params
.get("_id")
353 task
= asyncio
.ensure_future(self
.k8scluster
.delete(params
, order_id
))
354 self
.lcm_tasks
.register(
355 "k8scluster", k8scluster_id
, order_id
, "k8scluster_delete", task
359 if command
== "create" or command
== "created":
360 vca_id
= params
.get("_id")
361 task
= asyncio
.ensure_future(self
.vca
.create(params
, order_id
))
362 self
.lcm_tasks
.register("vca", vca_id
, order_id
, "vca_create", task
)
364 elif command
== "delete" or command
== "deleted":
365 vca_id
= params
.get("_id")
366 task
= asyncio
.ensure_future(self
.vca
.delete(params
, order_id
))
367 self
.lcm_tasks
.register("vca", vca_id
, order_id
, "vca_delete", task
)
369 elif topic
== "k8srepo":
370 if command
== "create" or command
== "created":
371 k8srepo_id
= params
.get("_id")
372 self
.logger
.debug("k8srepo_id = {}".format(k8srepo_id
))
373 task
= asyncio
.ensure_future(self
.k8srepo
.create(params
, order_id
))
374 self
.lcm_tasks
.register(
375 "k8srepo", k8srepo_id
, order_id
, "k8srepo_create", task
378 elif command
== "delete" or command
== "deleted":
379 k8srepo_id
= params
.get("_id")
380 task
= asyncio
.ensure_future(self
.k8srepo
.delete(params
, order_id
))
381 self
.lcm_tasks
.register(
382 "k8srepo", k8srepo_id
, order_id
, "k8srepo_delete", task
386 if command
== "instantiate":
387 # self.logger.debug("Deploying NS {}".format(nsr_id))
389 nslcmop_id
= nslcmop
["_id"]
390 nsr_id
= nslcmop
["nsInstanceId"]
391 task
= asyncio
.ensure_future(self
.ns
.instantiate(nsr_id
, nslcmop_id
))
392 self
.lcm_tasks
.register(
393 "ns", nsr_id
, nslcmop_id
, "ns_instantiate", task
396 elif command
== "terminate":
397 # self.logger.debug("Deleting NS {}".format(nsr_id))
399 nslcmop_id
= nslcmop
["_id"]
400 nsr_id
= nslcmop
["nsInstanceId"]
401 self
.lcm_tasks
.cancel(topic
, nsr_id
)
402 task
= asyncio
.ensure_future(self
.ns
.terminate(nsr_id
, nslcmop_id
))
403 self
.lcm_tasks
.register("ns", nsr_id
, nslcmop_id
, "ns_terminate", task
)
405 elif command
== "vca_status_refresh":
407 nslcmop_id
= nslcmop
["_id"]
408 nsr_id
= nslcmop
["nsInstanceId"]
409 task
= asyncio
.ensure_future(
410 self
.ns
.vca_status_refresh(nsr_id
, nslcmop_id
)
412 self
.lcm_tasks
.register(
413 "ns", nsr_id
, nslcmop_id
, "ns_vca_status_refresh", task
416 elif command
== "action":
417 # self.logger.debug("Update NS {}".format(nsr_id))
419 nslcmop_id
= nslcmop
["_id"]
420 nsr_id
= nslcmop
["nsInstanceId"]
421 task
= asyncio
.ensure_future(self
.ns
.action(nsr_id
, nslcmop_id
))
422 self
.lcm_tasks
.register("ns", nsr_id
, nslcmop_id
, "ns_action", task
)
424 elif command
== "update":
425 # self.logger.debug("Update NS {}".format(nsr_id))
427 nslcmop_id
= nslcmop
["_id"]
428 nsr_id
= nslcmop
["nsInstanceId"]
429 task
= asyncio
.ensure_future(self
.ns
.update(nsr_id
, nslcmop_id
))
430 self
.lcm_tasks
.register("ns", nsr_id
, nslcmop_id
, "ns_update", task
)
432 elif command
== "scale":
433 # self.logger.debug("Update NS {}".format(nsr_id))
435 nslcmop_id
= nslcmop
["_id"]
436 nsr_id
= nslcmop
["nsInstanceId"]
437 task
= asyncio
.ensure_future(self
.ns
.scale(nsr_id
, nslcmop_id
))
438 self
.lcm_tasks
.register("ns", nsr_id
, nslcmop_id
, "ns_scale", task
)
440 elif command
== "heal":
441 # self.logger.debug("Healing NS {}".format(nsr_id))
443 nslcmop_id
= nslcmop
["_id"]
444 nsr_id
= nslcmop
["nsInstanceId"]
445 task
= asyncio
.ensure_future(self
.ns
.heal(nsr_id
, nslcmop_id
))
446 self
.lcm_tasks
.register("ns", nsr_id
, nslcmop_id
, "ns_heal", task
)
448 elif command
== "migrate":
450 nslcmop_id
= nslcmop
["_id"]
451 nsr_id
= nslcmop
["nsInstanceId"]
452 task
= asyncio
.ensure_future(self
.ns
.migrate(nsr_id
, nslcmop_id
))
453 self
.lcm_tasks
.register("ns", nsr_id
, nslcmop_id
, "ns_migrate", task
)
455 elif command
== "verticalscale":
457 nslcmop_id
= nslcmop
["_id"]
458 nsr_id
= nslcmop
["nsInstanceId"]
459 task
= asyncio
.ensure_future(self
.ns
.vertical_scale(nsr_id
, nslcmop_id
))
461 "nsr_id,nslcmop_id,task {},{},{}".format(nsr_id
, nslcmop_id
, task
)
463 self
.lcm_tasks
.register(
464 "ns", nsr_id
, nslcmop_id
, "ns_verticalscale", task
467 "LCM task registered {},{},{} ".format(nsr_id
, nslcmop_id
, task
)
470 elif command
== "show":
473 db_nsr
= self
.db
.get_one("nsrs", {"_id": nsr_id
})
475 "nsr:\n _id={}\n operational-status: {}\n config-status: {}"
476 "\n detailed-status: {}\n deploy: {}\n tasks: {}"
479 db_nsr
["operational-status"],
480 db_nsr
["config-status"],
481 db_nsr
["detailed-status"],
482 db_nsr
["_admin"]["deployed"],
483 self
.lcm_tasks
.task_registry
["ns"].get(nsr_id
, ""),
486 except Exception as e
:
487 print("nsr {} not found: {}".format(nsr_id
, e
))
490 elif command
== "deleted":
491 return # TODO cleaning of task just in case should be done
503 ): # "scaled-cooldown-time"
506 elif topic
== "nsi": # netslice LCM processes (instantiate, terminate, etc)
507 if command
== "instantiate":
508 # self.logger.debug("Instantiating Network Slice {}".format(nsilcmop["netsliceInstanceId"]))
510 nsilcmop_id
= nsilcmop
["_id"] # slice operation id
511 nsir_id
= nsilcmop
["netsliceInstanceId"] # slice record id
512 task
= asyncio
.ensure_future(
513 self
.netslice
.instantiate(nsir_id
, nsilcmop_id
)
515 self
.lcm_tasks
.register(
516 "nsi", nsir_id
, nsilcmop_id
, "nsi_instantiate", task
519 elif command
== "terminate":
520 # self.logger.debug("Terminating Network Slice NS {}".format(nsilcmop["netsliceInstanceId"]))
522 nsilcmop_id
= nsilcmop
["_id"] # slice operation id
523 nsir_id
= nsilcmop
["netsliceInstanceId"] # slice record id
524 self
.lcm_tasks
.cancel(topic
, nsir_id
)
525 task
= asyncio
.ensure_future(
526 self
.netslice
.terminate(nsir_id
, nsilcmop_id
)
528 self
.lcm_tasks
.register(
529 "nsi", nsir_id
, nsilcmop_id
, "nsi_terminate", task
532 elif command
== "show":
535 db_nsir
= self
.db
.get_one("nsirs", {"_id": nsir_id
})
537 "nsir:\n _id={}\n operational-status: {}\n config-status: {}"
538 "\n detailed-status: {}\n deploy: {}\n tasks: {}"
541 db_nsir
["operational-status"],
542 db_nsir
["config-status"],
543 db_nsir
["detailed-status"],
544 db_nsir
["_admin"]["deployed"],
545 self
.lcm_tasks
.task_registry
["nsi"].get(nsir_id
, ""),
548 except Exception as e
:
549 print("nsir {} not found: {}".format(nsir_id
, e
))
552 elif command
== "deleted":
553 return # TODO cleaning of task just in case should be done
560 ): # "scaled-cooldown-time"
562 elif topic
== "vim_account":
563 vim_id
= params
["_id"]
564 if command
in ("create", "created"):
565 if not self
.main_config
.RO
.ng
:
566 task
= asyncio
.ensure_future(self
.vim
.create(params
, order_id
))
567 self
.lcm_tasks
.register(
568 "vim_account", vim_id
, order_id
, "vim_create", task
571 elif command
== "delete" or command
== "deleted":
572 self
.lcm_tasks
.cancel(topic
, vim_id
)
573 task
= asyncio
.ensure_future(self
.vim
.delete(params
, order_id
))
574 self
.lcm_tasks
.register(
575 "vim_account", vim_id
, order_id
, "vim_delete", task
578 elif command
== "show":
579 print("not implemented show with vim_account")
582 elif command
in ("edit", "edited"):
583 if not self
.main_config
.RO
.ng
:
584 task
= asyncio
.ensure_future(self
.vim
.edit(params
, order_id
))
585 self
.lcm_tasks
.register(
586 "vim_account", vim_id
, order_id
, "vim_edit", task
589 elif command
== "deleted":
590 return # TODO cleaning of task just in case should be done
591 elif topic
== "wim_account":
592 wim_id
= params
["_id"]
593 if command
in ("create", "created"):
594 if not self
.main_config
.RO
.ng
:
595 task
= asyncio
.ensure_future(self
.wim
.create(params
, order_id
))
596 self
.lcm_tasks
.register(
597 "wim_account", wim_id
, order_id
, "wim_create", task
600 elif command
== "delete" or command
== "deleted":
601 self
.lcm_tasks
.cancel(topic
, wim_id
)
602 task
= asyncio
.ensure_future(self
.wim
.delete(params
, order_id
))
603 self
.lcm_tasks
.register(
604 "wim_account", wim_id
, order_id
, "wim_delete", task
607 elif command
== "show":
608 print("not implemented show with wim_account")
611 elif command
in ("edit", "edited"):
612 task
= asyncio
.ensure_future(self
.wim
.edit(params
, order_id
))
613 self
.lcm_tasks
.register(
614 "wim_account", wim_id
, order_id
, "wim_edit", task
617 elif command
== "deleted":
618 return # TODO cleaning of task just in case should be done
620 _sdn_id
= params
["_id"]
621 if command
in ("create", "created"):
622 if not self
.main_config
.RO
.ng
:
623 task
= asyncio
.ensure_future(self
.sdn
.create(params
, order_id
))
624 self
.lcm_tasks
.register(
625 "sdn", _sdn_id
, order_id
, "sdn_create", task
628 elif command
== "delete" or command
== "deleted":
629 self
.lcm_tasks
.cancel(topic
, _sdn_id
)
630 task
= asyncio
.ensure_future(self
.sdn
.delete(params
, order_id
))
631 self
.lcm_tasks
.register("sdn", _sdn_id
, order_id
, "sdn_delete", task
)
633 elif command
in ("edit", "edited"):
634 task
= asyncio
.ensure_future(self
.sdn
.edit(params
, order_id
))
635 self
.lcm_tasks
.register("sdn", _sdn_id
, order_id
, "sdn_edit", task
)
637 elif command
== "deleted":
638 return # TODO cleaning of task just in case should be done
639 self
.logger
.critical("unknown topic {} and command '{}'".format(topic
, command
))
641 async def kafka_read(self
):
643 "Task kafka_read Enter with worker_id={}".format(self
.worker_id
)
645 # future = asyncio.Future()
646 self
.consecutive_errors
= 0
647 self
.first_start
= True
648 while self
.consecutive_errors
< 10:
661 topics_admin
= ("admin",)
662 await asyncio
.gather(
664 topics
, self
.loop
, self
.kafka_read_callback
, from_beginning
=True
666 self
.msg_admin
.aioread(
669 self
.kafka_read_callback
,
674 except LcmExceptionExit
:
675 self
.logger
.debug("Bye!")
677 except Exception as e
:
678 # if not first_start is the first time after starting. So leave more time and wait
679 # to allow kafka starts
680 if self
.consecutive_errors
== 8 if not self
.first_start
else 30:
682 "Task kafka_read task exit error too many errors. Exception: {}".format(
687 self
.consecutive_errors
+= 1
689 "Task kafka_read retrying after Exception {}".format(e
)
691 wait_time
= 2 if not self
.first_start
else 5
692 await asyncio
.sleep(wait_time
, loop
=self
.loop
)
694 # self.logger.debug("Task kafka_read terminating")
695 self
.logger
.debug("Task kafka_read exit")
700 self
.loop
.run_until_complete(self
.check_RO_version())
702 self
.ns
= ns
.NsLcm(self
.msg
, self
.lcm_tasks
, self
.main_config
, self
.loop
)
703 # TODO: modify the rest of classes to use the LcmCfg object instead of dicts
704 self
.netslice
= netslice
.NetsliceLcm(
705 self
.msg
, self
.lcm_tasks
, self
.main_config
.to_dict(), self
.loop
, self
.ns
707 self
.vim
= vim_sdn
.VimLcm(
708 self
.msg
, self
.lcm_tasks
, self
.main_config
.to_dict(), self
.loop
710 self
.wim
= vim_sdn
.WimLcm(
711 self
.msg
, self
.lcm_tasks
, self
.main_config
.to_dict(), self
.loop
713 self
.sdn
= vim_sdn
.SdnLcm(
714 self
.msg
, self
.lcm_tasks
, self
.main_config
.to_dict(), self
.loop
716 self
.k8scluster
= vim_sdn
.K8sClusterLcm(
717 self
.msg
, self
.lcm_tasks
, self
.main_config
.to_dict(), self
.loop
719 self
.vca
= vim_sdn
.VcaLcm(
720 self
.msg
, self
.lcm_tasks
, self
.main_config
.to_dict(), self
.loop
722 self
.k8srepo
= vim_sdn
.K8sRepoLcm(
723 self
.msg
, self
.lcm_tasks
, self
.main_config
.to_dict(), self
.loop
726 self
.loop
.run_until_complete(
727 asyncio
.gather(self
.kafka_read(), self
.kafka_ping())
731 # self.logger.debug("Terminating cancelling creation tasks")
732 # self.lcm_tasks.cancel("ALL", "create")
734 # while self.is_pending_tasks():
735 # self.logger.debug("Task kafka_read terminating. Waiting for tasks termination")
736 # await asyncio.sleep(2, loop=self.loop)
739 # self.lcm_tasks.cancel("ALL", "ALL")
743 self
.db
.db_disconnect()
745 self
.msg
.disconnect()
747 self
.msg_admin
.disconnect()
749 self
.fs
.fs_disconnect()
751 def read_config_file(self
, config_file
):
753 with
open(config_file
) as f
:
754 return yaml
.safe_load(f
)
755 except Exception as e
:
756 self
.logger
.critical("At config file '{}': {}".format(config_file
, e
))
760 def get_process_id():
762 Obtain a unique ID for this process. If running from inside docker, it will get docker ID. If not it
763 will provide a random one
766 # Try getting docker id. If fails, get pid
768 with
open("/proc/self/cgroup", "r") as f
:
769 text_id_
= f
.readline()
770 _
, _
, text_id
= text_id_
.rpartition("/")
771 text_id
= text_id
.replace("\n", "")[:12]
777 return "".join(random_choice("0123456789abcdef") for _
in range(12))
782 """Usage: {} [options]
783 -c|--config [configuration_file]: loads the configuration file (default: ./lcm.cfg)
784 --health-check: do not run lcm, but inspect kafka bus to determine if lcm is healthy
785 -h|--help: shows this help
790 # --log-socket-host HOST: send logs to this host")
791 # --log-socket-port PORT: send logs using this port (default: 9022)")
794 if __name__
== "__main__":
797 # print("SYS.PATH='{}'".format(sys.path))
798 # load parameters and configuration
804 opts
, args
= getopt
.getopt(
805 sys
.argv
[1:], "hc:", ["config=", "help", "health-check"]
807 # TODO add "log-socket-host=", "log-socket-port=", "log-file="
810 if o
in ("-h", "--help"):
813 elif o
in ("-c", "--config"):
815 elif o
== "--health-check":
816 from osm_lcm
.lcm_hc
import health_check
818 health_check(config_file
, Lcm
.ping_interval_pace
)
819 # elif o == "--log-socket-port":
820 # log_socket_port = a
821 # elif o == "--log-socket-host":
822 # log_socket_host = a
823 # elif o == "--log-file":
826 assert False, "Unhandled option"
829 if not path
.isfile(config_file
):
831 "configuration file '{}' does not exist".format(config_file
),
837 __file__
[: __file__
.rfind(".")] + ".cfg",
841 if path
.isfile(config_file
):
845 "No configuration file 'lcm.cfg' found neither at local folder nor at /etc/osm/",
849 lcm
= Lcm(config_file
)
851 except (LcmException
, getopt
.GetoptError
) as e
:
852 print(str(e
), file=sys
.stderr
)