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