X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=installers%2Fcharm%2Fro-k8s%2Freactive%2Fro.py;h=062cc078f8c6e1c5cfb6b33485a612b88fb21f16;hb=refs%2Ftags%2Fv9.1.5;hp=8ea80705ec19bef8c4eccdbc81d1e0aab2f34061;hpb=a578d7154146a7b80c4e07fc9c07d107c89cbadb;p=osm%2Fdevops.git diff --git a/installers/charm/ro-k8s/reactive/ro.py b/installers/charm/ro-k8s/reactive/ro.py index 8ea80705..062cc078 100644 --- a/installers/charm/ro-k8s/reactive/ro.py +++ b/installers/charm/ro-k8s/reactive/ro.py @@ -11,78 +11,105 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. +import yaml + +from charmhelpers.core.hookenv import log, metadata, config +from charms import layer from charms.layer.caas_base import pod_spec_set +from charms.osm.k8s import get_service_ip from charms.reactive import endpoint_from_flag from charms.reactive import when, when_not, hook from charms.reactive.flags import set_flag, clear_flag -from charmhelpers.core.hookenv import log, metadata, config -from charms import layer -from charms.osm.k8s import get_service_ip @hook("upgrade-charm") @when("leadership.is_leader") def upgrade(): clear_flag("ro-k8s.configured") + clear_flag("ro-k8s.ready") @when("config.changed") @when("leadership.is_leader") def restart(): clear_flag("ro-k8s.configured") + clear_flag("ro-k8s.ready") + + +@when_not("mongo.ready") +@when_not("mysql.available") +@when_not("ro-k8s.configured") +@when("leadership.is_leader") +def waiting_for_mysql(): + layer.status.waiting("Waiting for db to be ready") + clear_flag("ro-k8s.ready") + @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") + clear_flag("ro-k8s.ready") -@when_not("mongo.ready") -@when_not("ro-k8s.configured") -@when("leadership.is_leader") -def waiting_for_mongo(): - layer.status.waiting("Waiting for mongo to be ready") +@when_not("ro-k8s.ready") +@when("mysql.available") +def ro_k8s_mysql_ready(): + set_flag("ro-k8s.ready") -@when("kafka.ready", "mongo.ready") +@when_not("ro-k8s.ready") +@when("kafka.ready") +@when("mongo.ready") +def ro_k8s_kafka_mongo_ready(): + set_flag("ro-k8s.ready") + + +@when("ro-k8s.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") kafka = endpoint_from_flag("kafka.ready") mongo = endpoint_from_flag("mongo.ready") - - if kafka and mongo: + spec = None + if mysql: + if mysql.host() is not None: + spec = make_pod_spec( + mysql.host(), + mysql.port(), + mysql.user(), + mysql.password(), + mysql.root_password(), + ) + elif 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"] + # and kafka_unit["port"] ): - spec = make_pod_spec( + spec = make_pod_spec_new_ro( kafka_unit["host"], - kafka_unit["port"], + # 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") + if spec: + 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("ro-k8s.ready") @when_not("leadership.is_leader") def non_leaders_active(): layer.status.active("ready") @@ -102,20 +129,25 @@ def send_config(): service_ip = get_service_ip("ro") if service_ip: ro.send_connection( - service_ip, get_ro_port(), + service_ip, + get_ro_port(), ) clear_flag("ro.joined") except Exception as e: log("Fail sending RO configuration: {}".format(e)) -def make_pod_spec(kafka_host, kafka_port, mongo_uri): +def make_pod_spec( + mysql_host, mysql_port, mysql_user, mysql_password, mysql_root_password +): """Make pod specification for Kubernetes Args: - kafka_host (str): Kafka hostname or IP - kafka_port (int): Kafka port - mongo_uri (str): Mongo URI + 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 Returns: pod_spec: Pod specification for Kubernetes """ @@ -123,18 +155,62 @@ def make_pod_spec(kafka_host, kafka_port, mongo_uri): with open("reactive/spec_template.yaml") as spec_file: pod_spec_template = spec_file.read() - cfg = config() md = metadata() + cfg = config() data = { "name": md.get("name"), - "kafka_host": kafka_host, - "kafka_port": kafka_port, - "mongo_uri": mongo_uri, } data.update(cfg) + spec = yaml.safe_load(pod_spec_template % data) + spec["containers"][0]["config"].update( + { + "RO_DB_HOST": mysql_host, + "RO_DB_PORT": mysql_port, + "RO_DB_NAME": cfg.get("ro_database"), + "RO_DB_USER": mysql_user, + "RO_DB_ROOT_PASSWORD": mysql_root_password, + "RO_DB_PASSWORD": mysql_password, + "RO_DB_OVIM_PASSWORD": mysql_password, + "RO_DB_OVIM_HOST": mysql_host, + "RO_DB_OVIM_PORT": mysql_port, + "RO_DB_OVIM_USER": mysql_user, + "RO_DB_OVIM_ROOT_PASSWORD": mysql_root_password, + "RO_DB_OVIM_NAME": cfg.get("vim_database"), + } + ) + return spec + + +def make_pod_spec_new_ro(kafka_host, mongodb_uri): + """Make pod specification for Kubernetes + + Args: + kafka_host (str): Kafka host + mongodb_uri (str): Mongodb URI + Returns: + pod_spec: Pod specification for Kubernetes + """ - return pod_spec_template % data + with open("reactive/spec_template.yaml") as spec_file: + pod_spec_template = spec_file.read() + + md = metadata() + cfg = config() + + data = { + "name": md.get("name"), + } + data.update(cfg) + spec = yaml.safe_load(pod_spec_template % data) + spec["containers"][0]["config"].update( + { + "OSMRO_DATABASE_URI": mongodb_uri, + "OSMRO_MESSAGE_HOST": kafka_host, + "OSMRO_DATABASE_COMMONKEY": cfg.get("database_commonkey"), + } + ) + return spec def get_ro_port():