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 | 53644f6 | 2022-08-03 16:08:59 +0300 | [diff] [blame] | 29 | PACKAGES = ["cinder-common"] |
| 30 | MANDATORY_CONFIG = ["protocol"] |
Peter Pentchev | 469dfea | 2022-06-27 12:48:18 +0300 | [diff] [blame] | 31 | # Overriden from the parent. May be set depending on the charm's properties |
| 32 | stateless = True |
| 33 | active_active = True |
| 34 | |
| 35 | def __init__(self, *args, **kwargs): |
| 36 | super().__init__(*args, **kwargs) |
| 37 | |
Biser Milanov | 2afeb43 | 2022-08-05 10:42:40 +0300 | [diff] [blame^] | 38 | def _check_for_config_errors(self, config): |
| 39 | missing = [] |
| 40 | for mandatory in self.MANDATORY_CONFIG: |
| 41 | if mandatory not in config: |
| 42 | missing.append(mandatory) |
| 43 | |
| 44 | if missing: |
| 45 | return f"Mandatory options are missing: {', '.join(missing)}" |
| 46 | |
| 47 | if config["protocol"] not in ["block", "iscsi"]: |
| 48 | return ( |
| 49 | f"""Invalid 'protocol' option provided: '{config["protocol"]}';""" |
| 50 | "valid are 'block' and 'iscsi'" |
| 51 | ) |
| 52 | |
| 53 | if config["protocol"] == "block": |
| 54 | return "'protocol' value 'block' not yet supported" |
| 55 | |
| 56 | def on_config(self, event): |
| 57 | config = dict(self.framework.model.config) |
| 58 | conf_error = self._check_for_config_errors(config) |
| 59 | if conf_error is not None: |
| 60 | logger.error(conf_error) |
| 61 | self.unit.status = BlockedStatus(conf_error) |
| 62 | return |
| 63 | |
| 64 | super().on_config(event) |
| 65 | |
Peter Pentchev | 469dfea | 2022-06-27 12:48:18 +0300 | [diff] [blame] | 66 | def cinder_configuration(self, config): |
Biser Milanov | 2afeb43 | 2022-08-05 10:42:40 +0300 | [diff] [blame^] | 67 | conf_error = self._check_for_config_errors(config) |
| 68 | if conf_error is not None: |
| 69 | logger.error(conf_error) |
| 70 | return [] |
| 71 | |
Peter Pentchev | 469dfea | 2022-06-27 12:48:18 +0300 | [diff] [blame] | 72 | # Return the configuration to be set by the principal. |
Biser Milanov | 53644f6 | 2022-08-03 16:08:59 +0300 | [diff] [blame] | 73 | backend_name = config.get("volume-backend-name", self.framework.model.app.name) |
| 74 | volume_driver = "cinder.volume.drivers.storpool.StorPoolDriver" |
Biser Milanov | 2afeb43 | 2022-08-05 10:42:40 +0300 | [diff] [blame^] | 75 | |
Peter Pentchev | 469dfea | 2022-06-27 12:48:18 +0300 | [diff] [blame] | 76 | options = [ |
Biser Milanov | 53644f6 | 2022-08-03 16:08:59 +0300 | [diff] [blame] | 77 | ("volume_driver", volume_driver), |
| 78 | ("volume_backend_name", backend_name), |
Peter Pentchev | 469dfea | 2022-06-27 12:48:18 +0300 | [diff] [blame] | 79 | ] |
| 80 | |
Biser Milanov | 53644f6 | 2022-08-03 16:08:59 +0300 | [diff] [blame] | 81 | if config.get("use-multipath"): |
| 82 | options.extend( |
| 83 | [ |
| 84 | ("use_multipath_for_image_xfer", True), |
| 85 | ("enforce_multipath_for_image_xfer", True), |
| 86 | ] |
| 87 | ) |
Peter Pentchev | 469dfea | 2022-06-27 12:48:18 +0300 | [diff] [blame] | 88 | |
| 89 | return options |
| 90 | |
| 91 | |
| 92 | @charm_class |
| 93 | class CinderStorPoolCharm(CinderCharmBase): |
Biser Milanov | 53644f6 | 2022-08-03 16:08:59 +0300 | [diff] [blame] | 94 | release = "yoga" |
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 __name__ == "__main__": |
Peter Pentchev | 3a17d7d | 2022-07-27 13:11:57 +0000 | [diff] [blame] | 98 | # main(get_charm_class_for_release()) |
Peter Pentchev | a826690 | 2022-07-29 05:55:35 +0000 | [diff] [blame] | 99 | # main(CinderStorPoolCharm) |
| 100 | main(get_charm_class(release="yoga")) |