X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=installers%2Fcharm%2Fro-k8s%2Freactive%2Fro.py;h=8ea80705ec19bef8c4eccdbc81d1e0aab2f34061;hb=a578d7154146a7b80c4e07fc9c07d107c89cbadb;hp=afd7f459186ea4950526329a6efcbb02172efce2;hpb=82c5ffa15e1abb4c7ff58dc489f22cbe276c78c3;p=osm%2Fdevops.git diff --git a/installers/charm/ro-k8s/reactive/ro.py b/installers/charm/ro-k8s/reactive/ro.py index afd7f459..8ea80705 100644 --- a/installers/charm/ro-k8s/reactive/ro.py +++ b/installers/charm/ro-k8s/reactive/ro.py @@ -31,37 +31,63 @@ def upgrade(): def restart(): clear_flag("ro-k8s.configured") +@when_not("kafka.ready") +@when_not("ro-k8s.configured") +@when("leadership.is_leader") +def waiting_for_kafka(): + layer.status.waiting("Waiting for kafka to be ready") + -@when_not("mysql.available") +@when_not("mongo.ready") @when_not("ro-k8s.configured") -def waiting_for_mysql(): - layer.status.waiting("Waiting for mysql to be ready") +@when("leadership.is_leader") +def waiting_for_mongo(): + layer.status.waiting("Waiting for mongo to be ready") -@when("mysql.available") +@when("kafka.ready", "mongo.ready") @when_not("ro-k8s.configured") @when("leadership.is_leader") def configure(): layer.status.maintenance("Configuring ro container") try: - mysql = endpoint_from_flag("mysql.available") - - spec = make_pod_spec( - mysql.host(), - mysql.port(), - mysql.user(), - mysql.password(), - mysql.root_password(), - ) - - log("set pod spec:\n{}".format(spec)) - pod_spec_set(spec) - layer.status.active("creating container") - set_flag("ro-k8s.configured") + kafka = endpoint_from_flag("kafka.ready") + mongo = endpoint_from_flag("mongo.ready") + + if kafka and mongo: + kafka_units = kafka.kafkas() + kafka_unit = kafka_units[0] + + mongo_uri = mongo.connection_string() + log("Mongo URI: {}".format(mongo_uri)) + + if ( + mongo_uri + and kafka_unit["host"] + and kafka_unit["port"] + and ro_unit["host"] + and ro_unit["port"] + ): + spec = make_pod_spec( + kafka_unit["host"], + kafka_unit["port"], + mongo_uri, + ) + + log("set pod spec:\n{}".format(spec)) + pod_spec_set(spec) + layer.status.active("creating container") + set_flag("ro-k8s.configured") except Exception as e: layer.status.blocked("k8s spec failed to deploy: {}".format(e)) +@when("kafka.ready", "mongo.ready") +@when_not("leadership.is_leader") +def non_leaders_active(): + layer.status.active("ready") + + @when("ro-k8s.configured") def set_ro_active(): layer.status.active("ready") @@ -83,17 +109,13 @@ def send_config(): log("Fail sending RO configuration: {}".format(e)) -def make_pod_spec( - mysql_host, mysql_port, mysql_user, mysql_password, mysql_root_password -): +def make_pod_spec(kafka_host, kafka_port, mongo_uri): """Make pod specification for Kubernetes Args: - mysql_name (str): RO DB name - mysql_host (str): RO DB host - mysql_port (int): RO DB port - mysql_user (str): RO DB user - mysql_password (str): RO DB password + kafka_host (str): Kafka hostname or IP + kafka_port (int): Kafka port + mongo_uri (str): Mongo URI Returns: pod_spec: Pod specification for Kubernetes """ @@ -101,17 +123,14 @@ def make_pod_spec( with open("reactive/spec_template.yaml") as spec_file: pod_spec_template = spec_file.read() - md = metadata() cfg = config() + md = metadata() data = { "name": md.get("name"), - "docker_image": cfg.get("image"), - "mysql_host": mysql_host, - "mysql_port": mysql_port, - "mysql_user": mysql_user, - "mysql_password": mysql_password, - "mysql_root_password": mysql_root_password, + "kafka_host": kafka_host, + "kafka_port": kafka_port, + "mongo_uri": mongo_uri, } data.update(cfg)