Peter Pentchev | 469dfea | 2022-06-27 12:48:18 +0300 | [diff] [blame] | 1 | #! /usr/bin/env python3 |
| 2 | |
Biser Milanov | 81bdc46 | 2022-09-14 10:28:45 +0300 | [diff] [blame] | 3 | # Copyright 2022 StorPool |
Peter Pentchev | 469dfea | 2022-06-27 12:48:18 +0300 | [diff] [blame] | 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 | 80c3991 | 2022-09-07 13:03:34 +0300 | [diff] [blame] | 17 | """A StorPool backend charm for Cinder""" |
| 18 | |
Biser Milanov | 20d5060 | 2022-08-09 17:16:36 +0300 | [diff] [blame] | 19 | import dataclasses |
Biser Milanov | 2afeb43 | 2022-08-05 10:42:40 +0300 | [diff] [blame] | 20 | import logging |
Biser Milanov | 20d5060 | 2022-08-09 17:16:36 +0300 | [diff] [blame] | 21 | import pathlib |
Peter Pentchev | 469dfea | 2022-06-27 12:48:18 +0300 | [diff] [blame] | 22 | |
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 |
Biser Milanov | 80c3991 | 2022-09-07 13:03:34 +0300 | [diff] [blame] | 25 | from ops_openstack.core import charm_class, get_charm_class |
| 26 | from ops_openstack.plugins.classes import CinderStoragePluginCharm |
Biser Milanov | 2afeb43 | 2022-08-05 10:42:40 +0300 | [diff] [blame] | 27 | |
| 28 | logger = logging.getLogger(__name__) |
Peter Pentchev | 469dfea | 2022-06-27 12:48:18 +0300 | [diff] [blame] | 29 | |
| 30 | |
Biser Milanov | 20d5060 | 2022-08-09 17:16:36 +0300 | [diff] [blame] | 31 | @dataclasses.dataclass(frozen=True) |
| 32 | class StorPoolConfItems: |
Biser Milanov | 5f47c47 | 2022-09-02 17:17:56 +0300 | [diff] [blame] | 33 | """ |
| 34 | 'Serialize' StorPool configuration items depending on the target file format |
| 35 | """ |
| 36 | |
Biser Milanov | 20d5060 | 2022-08-09 17:16:36 +0300 | [diff] [blame] | 37 | sp_api_http_host: str |
| 38 | sp_api_http_port: str |
| 39 | sp_auth_token: str |
| 40 | |
Peter Pentchev | 28fea78 | 2022-08-23 01:07:37 +0300 | [diff] [blame] | 41 | @classmethod |
| 42 | def from_config(cls, data) -> "StorPoolConfItems": |
Biser Milanov | 80c3991 | 2022-09-07 13:03:34 +0300 | [diff] [blame] | 43 | """Create an object from an options dictionary""" |
Peter Pentchev | 28fea78 | 2022-08-23 01:07:37 +0300 | [diff] [blame] | 44 | args = { |
| 45 | field.name: str(data[field.name.replace("_", "-")]) for field in dataclasses.fields(cls) |
| 46 | } |
| 47 | return cls(**args) |
| 48 | |
Biser Milanov | 20d5060 | 2022-08-09 17:16:36 +0300 | [diff] [blame] | 49 | def to_ini_key_value_pairs(self) -> str: |
Biser Milanov | 80c3991 | 2022-09-07 13:03:34 +0300 | [diff] [blame] | 50 | """Serialize to ini-style key-value pairs""" |
Biser Milanov | 20d5060 | 2022-08-09 17:16:36 +0300 | [diff] [blame] | 51 | return "".join( |
| 52 | f"{name.upper()}={value}\n" for name, value in dataclasses.asdict(self).items() |
| 53 | ) |
| 54 | |
| 55 | |
Peter Pentchev | 469dfea | 2022-06-27 12:48:18 +0300 | [diff] [blame] | 56 | class CinderCharmBase(CinderStoragePluginCharm): |
Biser Milanov | 80c3991 | 2022-09-07 13:03:34 +0300 | [diff] [blame] | 57 | """ |
| 58 | Base class for the StorPool charm |
| 59 | """ |
Peter Pentchev | 469dfea | 2022-06-27 12:48:18 +0300 | [diff] [blame] | 60 | |
Biser Milanov | 8bc000b | 2022-08-08 14:39:29 +0300 | [diff] [blame] | 61 | PACKAGES = ["charm-cinder-storpool-deps", "cinder-common"] |
Biser Milanov | 4ea5921 | 2022-08-05 11:03:05 +0300 | [diff] [blame] | 62 | MANDATORY_CONFIG = [ |
| 63 | "protocol", |
| 64 | "storpool-template", |
| 65 | "sp-api-http-host", |
| 66 | "sp-api-http-port", |
| 67 | "sp-auth-token", |
Peter Pentchev | 38a6aa5 | 2022-08-23 01:17:17 +0300 | [diff] [blame] | 68 | "iscsi-portal-group", |
Biser Milanov | 4ea5921 | 2022-08-05 11:03:05 +0300 | [diff] [blame] | 69 | ] |
Peter Pentchev | 469dfea | 2022-06-27 12:48:18 +0300 | [diff] [blame] | 70 | # Overriden from the parent. May be set depending on the charm's properties |
| 71 | stateless = True |
| 72 | active_active = True |
| 73 | |
Biser Milanov | 2afeb43 | 2022-08-05 10:42:40 +0300 | [diff] [blame] | 74 | def on_config(self, event): |
| 75 | config = dict(self.framework.model.config) |
| 76 | conf_error = self._check_for_config_errors(config) |
| 77 | if conf_error is not None: |
| 78 | logger.error(conf_error) |
| 79 | self.unit.status = BlockedStatus(conf_error) |
Biser Milanov | 57725c2 | 2022-08-16 15:59:41 +0300 | [diff] [blame] | 80 | self._stored.is_started = False |
| 81 | |
Biser Milanov | 2afeb43 | 2022-08-05 10:42:40 +0300 | [diff] [blame] | 82 | return |
| 83 | |
Biser Milanov | fd3f55d | 2022-09-02 17:09:59 +0300 | [diff] [blame] | 84 | create_storpool_conf(StorPoolConfItems.from_config(config)) |
Biser Milanov | 20d5060 | 2022-08-09 17:16:36 +0300 | [diff] [blame] | 85 | |
Biser Milanov | 2afeb43 | 2022-08-05 10:42:40 +0300 | [diff] [blame] | 86 | super().on_config(event) |
| 87 | |
Biser Milanov | 57725c2 | 2022-08-16 15:59:41 +0300 | [diff] [blame] | 88 | self._stored.is_started = True |
| 89 | |
Biser Milanov | 80c3991 | 2022-09-07 13:03:34 +0300 | [diff] [blame] | 90 | def custom_status_check(self): |
| 91 | """Overriding abstract, which is not used anywhere""" |
| 92 | return BlockedStatus("Should not be here") |
| 93 | |
| 94 | def cinder_configuration(self, charm_config): |
| 95 | conf_error = self._check_for_config_errors(charm_config) |
Biser Milanov | 2afeb43 | 2022-08-05 10:42:40 +0300 | [diff] [blame] | 96 | if conf_error is not None: |
| 97 | logger.error(conf_error) |
Biser Milanov | 57725c2 | 2022-08-16 15:59:41 +0300 | [diff] [blame] | 98 | self._stored.is_started = False |
| 99 | |
Biser Milanov | 2afeb43 | 2022-08-05 10:42:40 +0300 | [diff] [blame] | 100 | return [] |
| 101 | |
Peter Pentchev | 469dfea | 2022-06-27 12:48:18 +0300 | [diff] [blame] | 102 | # Return the configuration to be set by the principal. |
Biser Milanov | 80c3991 | 2022-09-07 13:03:34 +0300 | [diff] [blame] | 103 | backend_name = charm_config.get("volume-backend-name", self.framework.model.app.name) |
Biser Milanov | 53644f6 | 2022-08-03 16:08:59 +0300 | [diff] [blame] | 104 | volume_driver = "cinder.volume.drivers.storpool.StorPoolDriver" |
Biser Milanov | 2afeb43 | 2022-08-05 10:42:40 +0300 | [diff] [blame] | 105 | |
Peter Pentchev | 469dfea | 2022-06-27 12:48:18 +0300 | [diff] [blame] | 106 | options = [ |
Biser Milanov | 53644f6 | 2022-08-03 16:08:59 +0300 | [diff] [blame] | 107 | ("volume_driver", volume_driver), |
| 108 | ("volume_backend_name", backend_name), |
Biser Milanov | 80c3991 | 2022-09-07 13:03:34 +0300 | [diff] [blame] | 109 | ("storpool_template", charm_config["storpool-template"]), |
| 110 | ("sp_api_http_host", charm_config["sp-api-http-host"]), |
| 111 | ("sp_api_http_port", charm_config["sp-api-http-port"]), |
| 112 | ("sp_auth_token", charm_config["sp-auth-token"]), |
Peter Pentchev | 38a6aa5 | 2022-08-23 01:17:17 +0300 | [diff] [blame] | 113 | ("iscsi_export_to", "*"), |
Biser Milanov | 80c3991 | 2022-09-07 13:03:34 +0300 | [diff] [blame] | 114 | ("iscsi_portal_group", charm_config["iscsi-portal-group"]), |
Peter Pentchev | 469dfea | 2022-06-27 12:48:18 +0300 | [diff] [blame] | 115 | ] |
| 116 | |
Biser Milanov | 80c3991 | 2022-09-07 13:03:34 +0300 | [diff] [blame] | 117 | if charm_config.get("use-multipath"): |
Biser Milanov | 53644f6 | 2022-08-03 16:08:59 +0300 | [diff] [blame] | 118 | options.extend( |
| 119 | [ |
| 120 | ("use_multipath_for_image_xfer", True), |
| 121 | ("enforce_multipath_for_image_xfer", True), |
| 122 | ] |
| 123 | ) |
Peter Pentchev | 469dfea | 2022-06-27 12:48:18 +0300 | [diff] [blame] | 124 | |
Biser Milanov | 80c3991 | 2022-09-07 13:03:34 +0300 | [diff] [blame] | 125 | create_storpool_conf(StorPoolConfItems.from_config(charm_config)) |
Biser Milanov | 20d5060 | 2022-08-09 17:16:36 +0300 | [diff] [blame] | 126 | |
Biser Milanov | 57725c2 | 2022-08-16 15:59:41 +0300 | [diff] [blame] | 127 | self._stored.is_started = True |
| 128 | |
Peter Pentchev | 469dfea | 2022-06-27 12:48:18 +0300 | [diff] [blame] | 129 | return options |
| 130 | |
Biser Milanov | fd3f55d | 2022-09-02 17:09:59 +0300 | [diff] [blame] | 131 | def _check_for_config_errors(self, config): |
| 132 | missing = [] |
| 133 | for mandatory in self.MANDATORY_CONFIG: |
| 134 | if mandatory not in config: |
| 135 | missing.append(mandatory) |
| 136 | |
| 137 | if missing: |
| 138 | return f"Mandatory options are missing: {', '.join(missing)}" |
| 139 | |
| 140 | if config["protocol"] not in ["block", "iscsi"]: |
| 141 | return ( |
| 142 | f"""Invalid 'protocol' option provided: '{config["protocol"]}';""" |
| 143 | "valid are 'block' and 'iscsi'" |
| 144 | ) |
| 145 | |
| 146 | if config["protocol"] == "block": |
| 147 | return "'protocol' value 'block' not yet supported" |
| 148 | |
Biser Milanov | 80c3991 | 2022-09-07 13:03:34 +0300 | [diff] [blame] | 149 | if not 0 < config["sp-api-http-port"] < 65536: |
Biser Milanov | fd3f55d | 2022-09-02 17:09:59 +0300 | [diff] [blame] | 150 | return ( |
| 151 | f"""'sp-api-http-port' ('{config["sp-api-http-port"]}')""" |
| 152 | "is not a valid port (0-65535)" |
| 153 | ) |
Biser Milanov | 20d5060 | 2022-08-09 17:16:36 +0300 | [diff] [blame] | 154 | |
Biser Milanov | 80c3991 | 2022-09-07 13:03:34 +0300 | [diff] [blame] | 155 | return None |
| 156 | |
Peter Pentchev | 469dfea | 2022-06-27 12:48:18 +0300 | [diff] [blame] | 157 | |
| 158 | @charm_class |
| 159 | class CinderStorPoolCharm(CinderCharmBase): |
Biser Milanov | 80c3991 | 2022-09-07 13:03:34 +0300 | [diff] [blame] | 160 | """ |
| 161 | Actual class for the StorPool charm |
| 162 | """ |
| 163 | |
Biser Milanov | 53644f6 | 2022-08-03 16:08:59 +0300 | [diff] [blame] | 164 | release = "yoga" |
Peter Pentchev | 469dfea | 2022-06-27 12:48:18 +0300 | [diff] [blame] | 165 | |
| 166 | |
Biser Milanov | fd3f55d | 2022-09-02 17:09:59 +0300 | [diff] [blame] | 167 | def create_storpool_conf(sp_conf_items: StorPoolConfItems): |
Biser Milanov | 80c3991 | 2022-09-07 13:03:34 +0300 | [diff] [blame] | 168 | """Generate a storpool.conf with the provided options""" |
Biser Milanov | fd3f55d | 2022-09-02 17:09:59 +0300 | [diff] [blame] | 169 | pathlib.Path("/etc/storpool.conf").write_text( |
| 170 | "# Do not edit; this file is generated by the cinder-storpool charm.\n" |
| 171 | + sp_conf_items.to_ini_key_value_pairs(), |
| 172 | encoding="UTF-8", |
| 173 | ) |
| 174 | |
| 175 | |
Biser Milanov | 53644f6 | 2022-08-03 16:08:59 +0300 | [diff] [blame] | 176 | if __name__ == "__main__": |
Peter Pentchev | 3a17d7d | 2022-07-27 13:11:57 +0000 | [diff] [blame] | 177 | # main(get_charm_class_for_release()) |
Peter Pentchev | a826690 | 2022-07-29 05:55:35 +0000 | [diff] [blame] | 178 | # main(CinderStorPoolCharm) |
| 179 | main(get_charm_class(release="yoga")) |