|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +import os |
| 4 | +from os import path |
| 5 | +from typing import Dict |
| 6 | +from ..exception import ModuleException |
| 7 | +from ..module import ModuleHandler |
| 8 | + |
| 9 | + |
| 10 | +class OSISMConfigurationHandler(ModuleHandler): |
| 11 | + CONFIGURATION_FILE_NAME = "osism_configuration.yaml" |
| 12 | + IMAGES_FILE_NAME = "osism_images.yaml" |
| 13 | + |
| 14 | + REQUIRES = [ |
| 15 | + "migrate_mons", |
| 16 | + "migrate_osds", |
| 17 | + "migrate_osd_pools", |
| 18 | + "migrate_mds", |
| 19 | + "migrate_mds_pools", |
| 20 | + "migrate_mgrs", |
| 21 | + "migrate_rgws", |
| 22 | + "migrate_rgw_pools", |
| 23 | + ] |
| 24 | + |
| 25 | + OSISM_CONFIGURATION_FILE_LOCATION = ( |
| 26 | + "/opt/configuration/environments/rook/configuration.yml" |
| 27 | + ) |
| 28 | + |
| 29 | + OSISM_IMAGES_FILE_LOCATION = "/opt/configuration/environments/rook/images.yml" |
| 30 | + OSISM_SECRETS_FILE_LOCATION = "/opt/configuration/environments/rook/secrets.yml" |
| 31 | + SECRETS_FILE_NAME = "osism_secrets.yaml" |
| 32 | + |
| 33 | + def preflight(self) -> None: |
| 34 | + if not os.access("./", os.W_OK): |
| 35 | + raise ModuleException( |
| 36 | + "OSISM configuration can not be written to the current working directory: {0}".format( |
| 37 | + path.abspath("./") |
| 38 | + ) |
| 39 | + ) |
| 40 | + |
| 41 | + def execute(self) -> None: |
| 42 | + self._write_configuration_yaml() |
| 43 | + self._write_images_yaml() |
| 44 | + self._write_secrets_yaml() |
| 45 | + |
| 46 | + def get_readable_key_value_state(self) -> Dict[str, str]: |
| 47 | + return {"OSISM configuration path": path.abspath("./")} |
| 48 | + |
| 49 | + def _write_configuration_yaml(self) -> None: |
| 50 | + analyze_ceph_data = self.machine.get_preflight_state("AnalyzeCephHandler").data |
| 51 | + rook_config = self._config["rook"] |
| 52 | + |
| 53 | + (rook_ceph_repository, rook_ceph_version) = rook_config["ceph"]["image"].split( |
| 54 | + ":", 1 |
| 55 | + ) |
| 56 | + osd_hosts_list = [ |
| 57 | + {"name": osd_host} |
| 58 | + for osd_host in analyze_ceph_data["node"]["ls"]["osd"].keys() |
| 59 | + ] |
| 60 | + |
| 61 | + create_rook_cluster_data = self.machine.get_preflight_state( |
| 62 | + "CreateRookClusterHandler" |
| 63 | + ) |
| 64 | + |
| 65 | + configuration_values = { |
| 66 | + "ceph_repository": rook_ceph_repository, |
| 67 | + "ceph_version": rook_ceph_version, |
| 68 | + "cluster_name": rook_config["cluster"]["name"], |
| 69 | + "cluster_namespace": rook_config["cluster"]["namespace"], |
| 70 | + "osd_hosts_list": osd_hosts_list, |
| 71 | + "mon_count": create_rook_cluster_data.mon_count, |
| 72 | + "mgr_count": create_rook_cluster_data.mgr_count, |
| 73 | + "mds_count": len(analyze_ceph_data["node"]["ls"]["mds"]), |
| 74 | + } |
| 75 | + |
| 76 | + if len(rook_config["ceph"].get("public_network", "")) > 0: |
| 77 | + configuration_values["public_network"] = rook_config["ceph"][ |
| 78 | + "public_network" |
| 79 | + ] |
| 80 | + else: |
| 81 | + self.logger.warn( |
| 82 | + "Rook Ceph cluster will be configured without a public network and determine it automatically during runtime" |
| 83 | + ) |
| 84 | + |
| 85 | + if len(rook_config["ceph"].get("cluster_network", "")) > 0: |
| 86 | + configuration_values["cluster_network"] = rook_config["ceph"][ |
| 87 | + "cluster_network" |
| 88 | + ] |
| 89 | + else: |
| 90 | + self.logger.info( |
| 91 | + "Rook Ceph cluster will be configured without a cluster network" |
| 92 | + ) |
| 93 | + |
| 94 | + # Render cluster config from template |
| 95 | + configuration = self.load_template( |
| 96 | + "configuration.yaml.j2", **configuration_values |
| 97 | + ) |
| 98 | + |
| 99 | + with open(OSISMConfigurationHandler.CONFIGURATION_FILE_NAME, "w") as fp: |
| 100 | + fp.write(configuration.raw) |
| 101 | + |
| 102 | + self.logger.info( |
| 103 | + "Generated '{0}' to be copied to '{1}'".format( |
| 104 | + OSISMConfigurationHandler.CONFIGURATION_FILE_NAME, |
| 105 | + OSISMConfigurationHandler.OSISM_CONFIGURATION_FILE_LOCATION, |
| 106 | + ) |
| 107 | + ) |
| 108 | + |
| 109 | + def _write_images_yaml(self) -> None: |
| 110 | + # Render cluster config from template |
| 111 | + images = self.load_template("images.yaml.j2") |
| 112 | + |
| 113 | + with open(OSISMConfigurationHandler.IMAGES_FILE_NAME, "w") as fp: |
| 114 | + fp.write(images.raw) |
| 115 | + |
| 116 | + self.logger.info( |
| 117 | + "Generated '{0}' to be copied to '{1}'".format( |
| 118 | + OSISMConfigurationHandler.IMAGES_FILE_NAME, |
| 119 | + OSISMConfigurationHandler.OSISM_IMAGES_FILE_LOCATION, |
| 120 | + ) |
| 121 | + ) |
| 122 | + |
| 123 | + def _write_secrets_yaml(self) -> None: |
| 124 | + # Render cluster config from template |
| 125 | + secrets = self.load_template("secrets.yaml.j2") |
| 126 | + |
| 127 | + with open(OSISMConfigurationHandler.SECRETS_FILE_NAME, "w") as fp: |
| 128 | + fp.write(secrets.raw) |
| 129 | + |
| 130 | + self.logger.info( |
| 131 | + "Generated '{0}' to be copied to '{1}'".format( |
| 132 | + OSISMConfigurationHandler.SECRETS_FILE_NAME, |
| 133 | + OSISMConfigurationHandler.OSISM_SECRETS_FILE_LOCATION, |
| 134 | + ) |
| 135 | + ) |
0 commit comments