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 | 2afeb43 | 2022-08-05 10:42:40 +0300 | [diff] [blame] | 17 | import logging |
Peter Pentchev | 469dfea | 2022-06-27 12:48:18 +0300 | [diff] [blame] | 18 | |
| 19 | from ops_openstack.plugins.classes import CinderStoragePluginCharm |
Peter Pentchev | a826690 | 2022-07-29 05:55:35 +0000 | [diff] [blame] | 20 | from ops_openstack.core import charm_class, get_charm_class |
Peter Pentchev | 469dfea | 2022-06-27 12:48:18 +0300 | [diff] [blame] | 21 | from ops.main import main |
Biser Milanov | 2afeb43 | 2022-08-05 10:42:40 +0300 | [diff] [blame] | 22 | from ops.model import BlockedStatus |
| 23 | |
| 24 | logger = logging.getLogger(__name__) |
Peter Pentchev | 469dfea | 2022-06-27 12:48:18 +0300 | [diff] [blame] | 25 | |
| 26 | |
| 27 | class CinderCharmBase(CinderStoragePluginCharm): |
| 28 | |
Biser Milanov | 8bc000b | 2022-08-08 14:39:29 +0300 | [diff] [blame^] | 29 | PACKAGES = ["charm-cinder-storpool-deps", "cinder-common"] |
Biser Milanov | 4ea5921 | 2022-08-05 11:03:05 +0300 | [diff] [blame] | 30 | MANDATORY_CONFIG = [ |
| 31 | "protocol", |
| 32 | "storpool-template", |
| 33 | "sp-api-http-host", |
| 34 | "sp-api-http-port", |
| 35 | "sp-auth-token", |
| 36 | ] |
Peter Pentchev | 469dfea | 2022-06-27 12:48:18 +0300 | [diff] [blame] | 37 | # Overriden from the parent. May be set depending on the charm's properties |
| 38 | stateless = True |
| 39 | active_active = True |
| 40 | |
| 41 | def __init__(self, *args, **kwargs): |
| 42 | super().__init__(*args, **kwargs) |
| 43 | |
Biser Milanov | 2afeb43 | 2022-08-05 10:42:40 +0300 | [diff] [blame] | 44 | def _check_for_config_errors(self, config): |
| 45 | missing = [] |
| 46 | for mandatory in self.MANDATORY_CONFIG: |
| 47 | if mandatory not in config: |
| 48 | missing.append(mandatory) |
| 49 | |
| 50 | if missing: |
| 51 | return f"Mandatory options are missing: {', '.join(missing)}" |
| 52 | |
| 53 | if config["protocol"] not in ["block", "iscsi"]: |
| 54 | return ( |
| 55 | f"""Invalid 'protocol' option provided: '{config["protocol"]}';""" |
| 56 | "valid are 'block' and 'iscsi'" |
| 57 | ) |
| 58 | |
| 59 | if config["protocol"] == "block": |
| 60 | return "'protocol' value 'block' not yet supported" |
| 61 | |
Biser Milanov | 4ea5921 | 2022-08-05 11:03:05 +0300 | [diff] [blame] | 62 | if not (0 < config["sp-api-http-port"] < 65536): |
| 63 | return ( |
| 64 | f"""'sp-api-http-port' ('{config["sp-api-http-port"]}')""" |
| 65 | "is not a valid port (0-65535)" |
| 66 | ) |
| 67 | |
Biser Milanov | 2afeb43 | 2022-08-05 10:42:40 +0300 | [diff] [blame] | 68 | def on_config(self, event): |
| 69 | config = dict(self.framework.model.config) |
| 70 | conf_error = self._check_for_config_errors(config) |
| 71 | if conf_error is not None: |
| 72 | logger.error(conf_error) |
| 73 | self.unit.status = BlockedStatus(conf_error) |
| 74 | return |
| 75 | |
| 76 | super().on_config(event) |
| 77 | |
Peter Pentchev | 469dfea | 2022-06-27 12:48:18 +0300 | [diff] [blame] | 78 | def cinder_configuration(self, config): |
Biser Milanov | 2afeb43 | 2022-08-05 10:42:40 +0300 | [diff] [blame] | 79 | conf_error = self._check_for_config_errors(config) |
| 80 | if conf_error is not None: |
| 81 | logger.error(conf_error) |
| 82 | return [] |
| 83 | |
Peter Pentchev | 469dfea | 2022-06-27 12:48:18 +0300 | [diff] [blame] | 84 | # Return the configuration to be set by the principal. |
Biser Milanov | 53644f6 | 2022-08-03 16:08:59 +0300 | [diff] [blame] | 85 | backend_name = config.get("volume-backend-name", self.framework.model.app.name) |
| 86 | volume_driver = "cinder.volume.drivers.storpool.StorPoolDriver" |
Biser Milanov | 2afeb43 | 2022-08-05 10:42:40 +0300 | [diff] [blame] | 87 | |
Peter Pentchev | 469dfea | 2022-06-27 12:48:18 +0300 | [diff] [blame] | 88 | options = [ |
Biser Milanov | 53644f6 | 2022-08-03 16:08:59 +0300 | [diff] [blame] | 89 | ("volume_driver", volume_driver), |
| 90 | ("volume_backend_name", backend_name), |
Biser Milanov | 7712cce | 2022-08-05 10:57:29 +0300 | [diff] [blame] | 91 | ("storpool_template", config["storpool-template"]), |
Biser Milanov | 4ea5921 | 2022-08-05 11:03:05 +0300 | [diff] [blame] | 92 | ("sp_api_http_host", config["sp-api-http-host"]), |
| 93 | ("sp_api_http_port", config["sp-api-http-port"]), |
| 94 | ("sp_auth_token", config["sp-auth-token"]), |
Peter Pentchev | 469dfea | 2022-06-27 12:48:18 +0300 | [diff] [blame] | 95 | ] |
| 96 | |
Biser Milanov | 53644f6 | 2022-08-03 16:08:59 +0300 | [diff] [blame] | 97 | if config.get("use-multipath"): |
| 98 | options.extend( |
| 99 | [ |
| 100 | ("use_multipath_for_image_xfer", True), |
| 101 | ("enforce_multipath_for_image_xfer", True), |
| 102 | ] |
| 103 | ) |
Peter Pentchev | 469dfea | 2022-06-27 12:48:18 +0300 | [diff] [blame] | 104 | |
| 105 | return options |
| 106 | |
| 107 | |
| 108 | @charm_class |
| 109 | class CinderStorPoolCharm(CinderCharmBase): |
Biser Milanov | 53644f6 | 2022-08-03 16:08:59 +0300 | [diff] [blame] | 110 | release = "yoga" |
Peter Pentchev | 469dfea | 2022-06-27 12:48:18 +0300 | [diff] [blame] | 111 | |
| 112 | |
Biser Milanov | 53644f6 | 2022-08-03 16:08:59 +0300 | [diff] [blame] | 113 | if __name__ == "__main__": |
Peter Pentchev | 3a17d7d | 2022-07-27 13:11:57 +0000 | [diff] [blame] | 114 | # main(get_charm_class_for_release()) |
Peter Pentchev | a826690 | 2022-07-29 05:55:35 +0000 | [diff] [blame] | 115 | # main(CinderStorPoolCharm) |
| 116 | main(get_charm_class(release="yoga")) |