Peter Pentchev | 469dfea | 2022-06-27 12:48:18 +0300 | [diff] [blame] | 1 | #! /usr/bin/env python3 |
| 2 | |
| 3 | # Copyright 2021 Canonical Ltd |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain 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, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
Biser Milanov | 20d5060 | 2022-08-09 17:16:36 +0300 | [diff] [blame] | 17 | import dataclasses |
Biser Milanov | 2afeb43 | 2022-08-05 10:42:40 +0300 | [diff] [blame] | 18 | import logging |
Biser Milanov | 20d5060 | 2022-08-09 17:16:36 +0300 | [diff] [blame] | 19 | import pathlib |
Peter Pentchev | 469dfea | 2022-06-27 12:48:18 +0300 | [diff] [blame] | 20 | |
| 21 | from ops_openstack.plugins.classes import CinderStoragePluginCharm |
Peter Pentchev | a826690 | 2022-07-29 05:55:35 +0000 | [diff] [blame] | 22 | from ops_openstack.core import charm_class, get_charm_class |
Peter Pentchev | 469dfea | 2022-06-27 12:48:18 +0300 | [diff] [blame] | 23 | from ops.main import main |
Biser Milanov | 2afeb43 | 2022-08-05 10:42:40 +0300 | [diff] [blame] | 24 | from ops.model import BlockedStatus |
| 25 | |
| 26 | logger = logging.getLogger(__name__) |
Peter Pentchev | 469dfea | 2022-06-27 12:48:18 +0300 | [diff] [blame] | 27 | |
| 28 | |
Biser Milanov | 20d5060 | 2022-08-09 17:16:36 +0300 | [diff] [blame] | 29 | @dataclasses.dataclass(frozen=True) |
| 30 | class StorPoolConfItems: |
| 31 | sp_api_http_host: str |
| 32 | sp_api_http_port: str |
| 33 | sp_auth_token: str |
| 34 | |
Peter Pentchev | 28fea78 | 2022-08-23 01:07:37 +0300 | [diff] [blame^] | 35 | @classmethod |
| 36 | def from_config(cls, data) -> "StorPoolConfItems": |
| 37 | args = { |
| 38 | field.name: str(data[field.name.replace("_", "-")]) for field in dataclasses.fields(cls) |
| 39 | } |
| 40 | return cls(**args) |
| 41 | |
Biser Milanov | 20d5060 | 2022-08-09 17:16:36 +0300 | [diff] [blame] | 42 | def to_ini_key_value_pairs(self) -> str: |
| 43 | return "".join( |
| 44 | f"{name.upper()}={value}\n" for name, value in dataclasses.asdict(self).items() |
| 45 | ) |
| 46 | |
| 47 | |
Peter Pentchev | 469dfea | 2022-06-27 12:48:18 +0300 | [diff] [blame] | 48 | class CinderCharmBase(CinderStoragePluginCharm): |
| 49 | |
Biser Milanov | 8bc000b | 2022-08-08 14:39:29 +0300 | [diff] [blame] | 50 | PACKAGES = ["charm-cinder-storpool-deps", "cinder-common"] |
Biser Milanov | 4ea5921 | 2022-08-05 11:03:05 +0300 | [diff] [blame] | 51 | MANDATORY_CONFIG = [ |
| 52 | "protocol", |
| 53 | "storpool-template", |
| 54 | "sp-api-http-host", |
| 55 | "sp-api-http-port", |
| 56 | "sp-auth-token", |
| 57 | ] |
Peter Pentchev | 469dfea | 2022-06-27 12:48:18 +0300 | [diff] [blame] | 58 | # Overriden from the parent. May be set depending on the charm's properties |
| 59 | stateless = True |
| 60 | active_active = True |
| 61 | |
| 62 | def __init__(self, *args, **kwargs): |
| 63 | super().__init__(*args, **kwargs) |
| 64 | |
Biser Milanov | 2afeb43 | 2022-08-05 10:42:40 +0300 | [diff] [blame] | 65 | def _check_for_config_errors(self, config): |
| 66 | missing = [] |
| 67 | for mandatory in self.MANDATORY_CONFIG: |
| 68 | if mandatory not in config: |
| 69 | missing.append(mandatory) |
| 70 | |
| 71 | if missing: |
| 72 | return f"Mandatory options are missing: {', '.join(missing)}" |
| 73 | |
| 74 | if config["protocol"] not in ["block", "iscsi"]: |
| 75 | return ( |
| 76 | f"""Invalid 'protocol' option provided: '{config["protocol"]}';""" |
| 77 | "valid are 'block' and 'iscsi'" |
| 78 | ) |
| 79 | |
| 80 | if config["protocol"] == "block": |
| 81 | return "'protocol' value 'block' not yet supported" |
| 82 | |
Biser Milanov | 4ea5921 | 2022-08-05 11:03:05 +0300 | [diff] [blame] | 83 | if not (0 < config["sp-api-http-port"] < 65536): |
| 84 | return ( |
| 85 | f"""'sp-api-http-port' ('{config["sp-api-http-port"]}')""" |
| 86 | "is not a valid port (0-65535)" |
| 87 | ) |
| 88 | |
Biser Milanov | 2afeb43 | 2022-08-05 10:42:40 +0300 | [diff] [blame] | 89 | def on_config(self, event): |
| 90 | config = dict(self.framework.model.config) |
| 91 | conf_error = self._check_for_config_errors(config) |
| 92 | if conf_error is not None: |
| 93 | logger.error(conf_error) |
| 94 | self.unit.status = BlockedStatus(conf_error) |
Biser Milanov | 57725c2 | 2022-08-16 15:59:41 +0300 | [diff] [blame] | 95 | self._stored.is_started = False |
| 96 | |
Biser Milanov | 2afeb43 | 2022-08-05 10:42:40 +0300 | [diff] [blame] | 97 | return |
| 98 | |
Peter Pentchev | 28fea78 | 2022-08-23 01:07:37 +0300 | [diff] [blame^] | 99 | self.create_storpool_conf(StorPoolConfItems.from_config(config)) |
Biser Milanov | 20d5060 | 2022-08-09 17:16:36 +0300 | [diff] [blame] | 100 | |
Biser Milanov | 2afeb43 | 2022-08-05 10:42:40 +0300 | [diff] [blame] | 101 | super().on_config(event) |
| 102 | |
Biser Milanov | 57725c2 | 2022-08-16 15:59:41 +0300 | [diff] [blame] | 103 | self._stored.is_started = True |
| 104 | |
Peter Pentchev | 469dfea | 2022-06-27 12:48:18 +0300 | [diff] [blame] | 105 | def cinder_configuration(self, config): |
Biser Milanov | 2afeb43 | 2022-08-05 10:42:40 +0300 | [diff] [blame] | 106 | conf_error = self._check_for_config_errors(config) |
| 107 | if conf_error is not None: |
| 108 | logger.error(conf_error) |
Biser Milanov | 57725c2 | 2022-08-16 15:59:41 +0300 | [diff] [blame] | 109 | self._stored.is_started = False |
| 110 | |
Biser Milanov | 2afeb43 | 2022-08-05 10:42:40 +0300 | [diff] [blame] | 111 | return [] |
| 112 | |
Peter Pentchev | 469dfea | 2022-06-27 12:48:18 +0300 | [diff] [blame] | 113 | # Return the configuration to be set by the principal. |
Biser Milanov | 53644f6 | 2022-08-03 16:08:59 +0300 | [diff] [blame] | 114 | backend_name = config.get("volume-backend-name", self.framework.model.app.name) |
| 115 | volume_driver = "cinder.volume.drivers.storpool.StorPoolDriver" |
Biser Milanov | 2afeb43 | 2022-08-05 10:42:40 +0300 | [diff] [blame] | 116 | |
Peter Pentchev | 469dfea | 2022-06-27 12:48:18 +0300 | [diff] [blame] | 117 | options = [ |
Biser Milanov | 53644f6 | 2022-08-03 16:08:59 +0300 | [diff] [blame] | 118 | ("volume_driver", volume_driver), |
| 119 | ("volume_backend_name", backend_name), |
Biser Milanov | 7712cce | 2022-08-05 10:57:29 +0300 | [diff] [blame] | 120 | ("storpool_template", config["storpool-template"]), |
Biser Milanov | 4ea5921 | 2022-08-05 11:03:05 +0300 | [diff] [blame] | 121 | ("sp_api_http_host", config["sp-api-http-host"]), |
| 122 | ("sp_api_http_port", config["sp-api-http-port"]), |
| 123 | ("sp_auth_token", config["sp-auth-token"]), |
Peter Pentchev | 469dfea | 2022-06-27 12:48:18 +0300 | [diff] [blame] | 124 | ] |
| 125 | |
Biser Milanov | 53644f6 | 2022-08-03 16:08:59 +0300 | [diff] [blame] | 126 | if config.get("use-multipath"): |
| 127 | options.extend( |
| 128 | [ |
| 129 | ("use_multipath_for_image_xfer", True), |
| 130 | ("enforce_multipath_for_image_xfer", True), |
| 131 | ] |
| 132 | ) |
Peter Pentchev | 469dfea | 2022-06-27 12:48:18 +0300 | [diff] [blame] | 133 | |
Peter Pentchev | 28fea78 | 2022-08-23 01:07:37 +0300 | [diff] [blame^] | 134 | self.create_storpool_conf(StorPoolConfItems.from_config(config)) |
Biser Milanov | 20d5060 | 2022-08-09 17:16:36 +0300 | [diff] [blame] | 135 | |
Biser Milanov | 57725c2 | 2022-08-16 15:59:41 +0300 | [diff] [blame] | 136 | self._stored.is_started = True |
| 137 | |
Peter Pentchev | 469dfea | 2022-06-27 12:48:18 +0300 | [diff] [blame] | 138 | return options |
| 139 | |
Biser Milanov | 20d5060 | 2022-08-09 17:16:36 +0300 | [diff] [blame] | 140 | @staticmethod |
| 141 | def create_storpool_conf(sp_conf_items: StorPoolConfItems): |
| 142 | pathlib.Path("/etc/storpool.conf").write_text( |
| 143 | "# Do not edit; this file is generated by the cinder-storpool charm.\n" |
| 144 | + sp_conf_items.to_ini_key_value_pairs(), |
| 145 | encoding="UTF-8", |
| 146 | ) |
| 147 | |
Peter Pentchev | 469dfea | 2022-06-27 12:48:18 +0300 | [diff] [blame] | 148 | |
| 149 | @charm_class |
| 150 | class CinderStorPoolCharm(CinderCharmBase): |
Biser Milanov | 53644f6 | 2022-08-03 16:08:59 +0300 | [diff] [blame] | 151 | release = "yoga" |
Peter Pentchev | 469dfea | 2022-06-27 12:48:18 +0300 | [diff] [blame] | 152 | |
| 153 | |
Biser Milanov | 53644f6 | 2022-08-03 16:08:59 +0300 | [diff] [blame] | 154 | if __name__ == "__main__": |
Peter Pentchev | 3a17d7d | 2022-07-27 13:11:57 +0000 | [diff] [blame] | 155 | # main(get_charm_class_for_release()) |
Peter Pentchev | a826690 | 2022-07-29 05:55:35 +0000 | [diff] [blame] | 156 | # main(CinderStorPoolCharm) |
| 157 | main(get_charm_class(release="yoga")) |