X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=installers%2Fcharm%2Fng-ui%2Fsrc%2Fcharm.py;h=6f5ca5b50d8f137c0d8070887f1817d9bb17f7d8;hb=0fc2ee1ada11d0608d5890ef6012e5cd27672ab1;hp=7d461307c199c17d3a7a06962aef7ee4325f4e3d;hpb=889e8bb27900b540ef7ff08e892e3e80731069fb;p=osm%2Fdevops.git diff --git a/installers/charm/ng-ui/src/charm.py b/installers/charm/ng-ui/src/charm.py index 7d461307..6f5ca5b5 100755 --- a/installers/charm/ng-ui/src/charm.py +++ b/installers/charm/ng-ui/src/charm.py @@ -13,10 +13,12 @@ # See the License for the specific language governing permissions and # limitations under the License. -import sys +import base64 +from glob import glob import logging - -sys.path.append("lib") +from pathlib import Path +from string import Template +import sys from ops.charm import CharmBase from ops.framework import StoredState, Object @@ -26,11 +28,9 @@ from ops.model import ( MaintenanceStatus, BlockedStatus, ModelError, + WaitingStatus, ) -from glob import glob -from pathlib import Path -from string import Template logger = logging.getLogger(__name__) @@ -54,59 +54,34 @@ class NGUICharm(CharmBase): # SSL Certificate path self.ssl_folder = "/certs" - self.ssl_crt = "{}/ssl_certificate.crt".format(self.ssl_folder) - self.ssl_key = "{}/ssl_certificate.key".format(self.ssl_folder) + self.ssl_crt_name = "ssl_certificate.crt" + self.ssl_key_name = "ssl_certificate.key" def _apply_spec(self): # Only apply the spec if this unit is a leader. unit = self.model.unit if not unit.is_leader(): - unit.status = ActiveStatus("Ready") + unit.status = ActiveStatus("ready") return if not self.state.nbi_host or not self.state.nbi_port: - unit.status = MaintenanceStatus("Waiting for NBI") + unit.status = WaitingStatus("Waiting for NBI") return unit.status = MaintenanceStatus("Applying new pod spec") new_spec = self.make_pod_spec() if new_spec == self.state.spec: - unit.status = ActiveStatus("Ready") + unit.status = ActiveStatus("ready") return self.framework.model.pod.set_spec(new_spec) self.state.spec = new_spec - unit.status = ActiveStatus("Ready") + unit.status = ActiveStatus("ready") def make_pod_spec(self): config = self.framework.model.config - ports = [ - {"name": "port", "containerPort": config["port"], "protocol": "TCP",}, - ] - - kubernetes = { - "readinessProbe": { - "tcpSocket": {"port": config["port"]}, - "timeoutSeconds": 5, - "periodSeconds": 5, - "initialDelaySeconds": 10, - }, - "livenessProbe": { - "tcpSocket": {"port": config["port"]}, - "timeoutSeconds": 5, - "initialDelaySeconds": 45, - }, - } - - ssl_certificate = None - ssl_certificate_key = None - try: - ssl_certificate = self.model.resources.fetch("ssl_certificate") - ssl_certificate_key = self.model.resources.fetch("ssl_certificate_key") - except ModelError as e: - logger.info(e) - config_spec = { - "port": config["port"], + "http_port": config["port"], + "https_port": config["https_port"], "server_name": config["server_name"], "client_max_body_size": config["client_max_body_size"], "nbi_host": self.state.nbi_host or config["nbi_host"], @@ -115,10 +90,28 @@ class NGUICharm(CharmBase): "ssl_crt_key": "", } - if ssl_certificate and ssl_certificate_key: - config_spec["ssl_crt"] = "ssl_certificate {};".format(self.ssl_crt) - config_spec["ssl_crt_key"] = "ssl_certificate_key {};".format(self.ssl_key) - config_spec["port"] = "{} ssl".format(config_spec["port"]) + ssl_certificate = None + ssl_certificate_key = None + ssl_enabled = False + + if "ssl_certificate" in config and "ssl_certificate_key" in config: + # Get bytes of cert and key + cert_b = base64.b64decode(config["ssl_certificate"]) + key_b = base64.b64decode(config["ssl_certificate_key"]) + # Decode key and cert + ssl_certificate = cert_b.decode("utf-8") + ssl_certificate_key = key_b.decode("utf-8") + # Get paths + cert_path = "{}/{}".format(self.ssl_folder, self.ssl_crt_name) + key_path = "{}/{}".format(self.ssl_folder, self.ssl_key_name) + + config_spec["port"] = "{} ssl".format(config["https_port"]) + config_spec["ssl_crt"] = "ssl_certificate {};".format(cert_path) + config_spec["ssl_crt_key"] = "ssl_certificate_key {};".format(key_path) + ssl_enabled = True + else: + config_spec["ssl_crt"] = "" + config_spec["ssl_crt_key"] = "" files = [ { @@ -132,6 +125,28 @@ class NGUICharm(CharmBase): }, } ] + port = config["https_port"] if ssl_enabled else config["port"] + ports = [ + { + "name": "port", + "containerPort": port, + "protocol": "TCP", + }, + ] + + kubernetes = { + "readinessProbe": { + "tcpSocket": {"port": port}, + "timeoutSeconds": 5, + "periodSeconds": 5, + "initialDelaySeconds": 10, + }, + "livenessProbe": { + "tcpSocket": {"port": port}, + "timeoutSeconds": 5, + "initialDelaySeconds": 45, + }, + } if ssl_certificate and ssl_certificate_key: files.append( @@ -139,20 +154,24 @@ class NGUICharm(CharmBase): "name": "ssl", "mountPath": self.ssl_folder, "files": { - Path(filename) - .name: Template(Path(filename).read_text()) - .substitute(config_spec) - for filename in [ssl_certificate, ssl_certificate_key] + self.ssl_crt_name: ssl_certificate, + self.ssl_key_name: ssl_certificate_key, }, } ) + logger.debug(files) + spec = { "version": 2, "containers": [ { "name": self.framework.model.app.name, - "image": "{}".format(config["image"]), + "imageDetails": { + "imagePath": config["image"], + "username": config["image_username"], + "password": config["image_password"], + }, "ports": ports, "kubernetes": kubernetes, "files": files, @@ -177,20 +196,14 @@ class NGUICharm(CharmBase): self.on_start(event) def on_nbi_relation_changed(self, event): - unit = self.model.unit - if not unit.is_leader(): - return - self.state.nbi_host = event.relation.data[event.unit].get("host") - self.state.nbi_port = event.relation.data[event.unit].get("port") + nbi_host = event.relation.data[event.unit].get("host") + nbi_port = event.relation.data[event.unit].get("port") + if nbi_host and self.state.nbi_host != nbi_host: + self.state.nbi_host = nbi_host + if nbi_port and self.state.nbi_port != nbi_port: + self.state.nbi_port = nbi_port self._apply_spec() - def resource_get(self, resource_name: str) -> Path: - from pathlib import Path - from subprocess import run - - result = run(["resource-get", resource_name], output=True, text=True) - return Path(result.stdout.strip()) - if __name__ == "__main__": main(NGUICharm)