| palsus | b170aec | 2021-03-30 07:15:02 +0000 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | |
| 3 | # This file is part of OSM Monitoring module |
| 4 | |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 6 | # not use this file except in compliance with the License. You may obtain |
| 7 | # a copy of the License at |
| 8 | |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | # License for the specific language governing permissions and limitations |
| 15 | # under the License. |
| 16 | |
| 17 | import pymongo |
| 18 | import time |
| 19 | import socket |
| 20 | import logging |
| 21 | import kafka |
| palsus | 865c35c | 2021-04-16 07:36:15 +0000 | [diff] [blame] | 22 | |
| palsus | b170aec | 2021-03-30 07:15:02 +0000 | [diff] [blame] | 23 | |
| 24 | def wait_till_commondb_is_ready(config, process_name="osm-mon", commondb_wait_time=5): |
| 25 | |
| 26 | logging.debug("wait_till_commondb_is_ready") |
| 27 | |
| garciadeblas | 8e4179f | 2021-05-14 16:47:03 +0200 | [diff] [blame] | 28 | while True: |
| palsus | b170aec | 2021-03-30 07:15:02 +0000 | [diff] [blame] | 29 | commondb_url = config.conf["database"].get("uri") |
| 30 | try: |
| 31 | commondb = pymongo.MongoClient(commondb_url) |
| 32 | commondb.server_info() |
| 33 | break |
| 34 | except Exception: |
| garciadeblas | 8e4179f | 2021-05-14 16:47:03 +0200 | [diff] [blame] | 35 | logging.info( |
| 36 | "{} process is waiting for commondb to come up...".format(process_name) |
| 37 | ) |
| palsus | b170aec | 2021-03-30 07:15:02 +0000 | [diff] [blame] | 38 | time.sleep(commondb_wait_time) |
| 39 | |
| 40 | |
| 41 | def wait_till_kafka_is_ready(config, process_name="osm-mon", kafka_wait_time=5): |
| 42 | |
| 43 | logging.debug("wait_till_kafka_is_ready") |
| 44 | |
| garciadeblas | 8e4179f | 2021-05-14 16:47:03 +0200 | [diff] [blame] | 45 | while True: |
| palsus | b170aec | 2021-03-30 07:15:02 +0000 | [diff] [blame] | 46 | kafka_ready = False |
| 47 | try: |
| 48 | with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: |
| 49 | # Verify is kafka port is up |
| 50 | if ( |
| 51 | s.connect_ex( |
| 52 | ( |
| 53 | config.conf.get("message", {}).get("host", "kafka"), |
| 54 | int(config.conf["message"].get("port")), |
| 55 | ) |
| 56 | ) |
| 57 | == 0 |
| 58 | ): |
| 59 | # Get the list of topics. If kafka is not ready exception will be thrown. |
| garciadeblas | 8e4179f | 2021-05-14 16:47:03 +0200 | [diff] [blame] | 60 | consumer = kafka.KafkaConsumer( |
| 61 | group_id=config.conf["message"].get("group_id"), |
| 62 | bootstrap_servers=[ |
| 63 | config.conf.get("message", {}).get("host", "kafka") |
| 64 | + ":" |
| 65 | + config.conf["message"].get("port") |
| 66 | ], |
| 67 | ) |
| palsus | 865c35c | 2021-04-16 07:36:15 +0000 | [diff] [blame] | 68 | all_topics = consumer.topics() |
| 69 | logging.debug("Number of topics found: %s", len(all_topics)) |
| palsus | a949ff9 | 2021-04-06 11:47:21 +0000 | [diff] [blame] | 70 | |
| 71 | # Send dummy message in kafka topics. If kafka is not ready exception will be thrown. |
| garciadeblas | 8e4179f | 2021-05-14 16:47:03 +0200 | [diff] [blame] | 72 | producer = kafka.KafkaProducer( |
| 73 | bootstrap_servers=[ |
| 74 | config.conf.get("message", {}).get("host", "kafka") |
| 75 | + ":" |
| 76 | + config.conf["message"].get("port") |
| 77 | ] |
| 78 | ) |
| palsus | 865c35c | 2021-04-16 07:36:15 +0000 | [diff] [blame] | 79 | mon_topics = ["alarm_request", "users", "project"] |
| 80 | for mon_topic in mon_topics: |
| 81 | producer.send(mon_topic, key=b"echo", value=b"dummy message") |
| palsus | a949ff9 | 2021-04-06 11:47:21 +0000 | [diff] [blame] | 82 | |
| 83 | # Kafka is ready now |
| palsus | b170aec | 2021-03-30 07:15:02 +0000 | [diff] [blame] | 84 | kafka_ready = True |
| 85 | except Exception as e: |
| 86 | logging.info("Error when trying to get kafka status.") |
| 87 | logging.debug("Exception when trying to get kafka status: %s", str(e)) |
| 88 | finally: |
| 89 | if kafka_ready: |
| 90 | break |
| 91 | else: |
| garciadeblas | 8e4179f | 2021-05-14 16:47:03 +0200 | [diff] [blame] | 92 | logging.info( |
| 93 | "{} process is waiting for kafka to come up...".format(process_name) |
| 94 | ) |
| palsus | b170aec | 2021-03-30 07:15:02 +0000 | [diff] [blame] | 95 | time.sleep(kafka_wait_time) |
| 96 | |
| 97 | |
| garciadeblas | 8e4179f | 2021-05-14 16:47:03 +0200 | [diff] [blame] | 98 | def wait_till_core_services_are_ready( |
| 99 | config, process_name="osm-mon", commondb_wait_time=5, kafka_wait_time=5 |
| 100 | ): |
| palsus | b170aec | 2021-03-30 07:15:02 +0000 | [diff] [blame] | 101 | |
| palsus | 0cf6b55 | 2021-04-01 07:49:20 +0000 | [diff] [blame] | 102 | logging.debug("wait_till_core_services_are_ready") |
| palsus | b170aec | 2021-03-30 07:15:02 +0000 | [diff] [blame] | 103 | |
| 104 | if not config: |
| 105 | logging.info("Config information is not available") |
| 106 | return False |
| 107 | |
| 108 | # Check if common-db is ready |
| palsus | 0cf6b55 | 2021-04-01 07:49:20 +0000 | [diff] [blame] | 109 | wait_till_commondb_is_ready(config, process_name, commondb_wait_time) |
| palsus | b170aec | 2021-03-30 07:15:02 +0000 | [diff] [blame] | 110 | |
| 111 | # Check if kafka is ready |
| 112 | wait_till_kafka_is_ready(config, process_name, kafka_wait_time) |
| 113 | |
| 114 | return True |