blob: 04ebb7ac434b2b09d82efd3a28ed56c207851844 [file] [log] [blame]
Peter Pentchev469dfea2022-06-27 12:48:18 +03001#! /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 Milanov20d50602022-08-09 17:16:36 +030017import dataclasses
Biser Milanov2afeb432022-08-05 10:42:40 +030018import logging
Biser Milanov20d50602022-08-09 17:16:36 +030019import pathlib
Peter Pentchev469dfea2022-06-27 12:48:18 +030020
21from ops_openstack.plugins.classes import CinderStoragePluginCharm
Peter Pentcheva8266902022-07-29 05:55:35 +000022from ops_openstack.core import charm_class, get_charm_class
Peter Pentchev469dfea2022-06-27 12:48:18 +030023from ops.main import main
Biser Milanov2afeb432022-08-05 10:42:40 +030024from ops.model import BlockedStatus
25
26logger = logging.getLogger(__name__)
Peter Pentchev469dfea2022-06-27 12:48:18 +030027
28
Biser Milanov20d50602022-08-09 17:16:36 +030029@dataclasses.dataclass(frozen=True)
30class StorPoolConfItems:
31 sp_api_http_host: str
32 sp_api_http_port: str
33 sp_auth_token: str
34
Peter Pentchev28fea782022-08-23 01:07:37 +030035 @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 Milanov20d50602022-08-09 17:16:36 +030042 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 Pentchev469dfea2022-06-27 12:48:18 +030048class CinderCharmBase(CinderStoragePluginCharm):
49
Biser Milanov8bc000b2022-08-08 14:39:29 +030050 PACKAGES = ["charm-cinder-storpool-deps", "cinder-common"]
Biser Milanov4ea59212022-08-05 11:03:05 +030051 MANDATORY_CONFIG = [
52 "protocol",
53 "storpool-template",
54 "sp-api-http-host",
55 "sp-api-http-port",
56 "sp-auth-token",
57 ]
Peter Pentchev469dfea2022-06-27 12:48:18 +030058 # 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 Milanov2afeb432022-08-05 10:42:40 +030065 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 Milanov4ea59212022-08-05 11:03:05 +030083 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 Milanov2afeb432022-08-05 10:42:40 +030089 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 Milanov57725c22022-08-16 15:59:41 +030095 self._stored.is_started = False
96
Biser Milanov2afeb432022-08-05 10:42:40 +030097 return
98
Peter Pentchev28fea782022-08-23 01:07:37 +030099 self.create_storpool_conf(StorPoolConfItems.from_config(config))
Biser Milanov20d50602022-08-09 17:16:36 +0300100
Biser Milanov2afeb432022-08-05 10:42:40 +0300101 super().on_config(event)
102
Biser Milanov57725c22022-08-16 15:59:41 +0300103 self._stored.is_started = True
104
Peter Pentchev469dfea2022-06-27 12:48:18 +0300105 def cinder_configuration(self, config):
Biser Milanov2afeb432022-08-05 10:42:40 +0300106 conf_error = self._check_for_config_errors(config)
107 if conf_error is not None:
108 logger.error(conf_error)
Biser Milanov57725c22022-08-16 15:59:41 +0300109 self._stored.is_started = False
110
Biser Milanov2afeb432022-08-05 10:42:40 +0300111 return []
112
Peter Pentchev469dfea2022-06-27 12:48:18 +0300113 # Return the configuration to be set by the principal.
Biser Milanov53644f62022-08-03 16:08:59 +0300114 backend_name = config.get("volume-backend-name", self.framework.model.app.name)
115 volume_driver = "cinder.volume.drivers.storpool.StorPoolDriver"
Biser Milanov2afeb432022-08-05 10:42:40 +0300116
Peter Pentchev469dfea2022-06-27 12:48:18 +0300117 options = [
Biser Milanov53644f62022-08-03 16:08:59 +0300118 ("volume_driver", volume_driver),
119 ("volume_backend_name", backend_name),
Biser Milanov7712cce2022-08-05 10:57:29 +0300120 ("storpool_template", config["storpool-template"]),
Biser Milanov4ea59212022-08-05 11:03:05 +0300121 ("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 Pentchev469dfea2022-06-27 12:48:18 +0300124 ]
125
Biser Milanov53644f62022-08-03 16:08:59 +0300126 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 Pentchev469dfea2022-06-27 12:48:18 +0300133
Peter Pentchev28fea782022-08-23 01:07:37 +0300134 self.create_storpool_conf(StorPoolConfItems.from_config(config))
Biser Milanov20d50602022-08-09 17:16:36 +0300135
Biser Milanov57725c22022-08-16 15:59:41 +0300136 self._stored.is_started = True
137
Peter Pentchev469dfea2022-06-27 12:48:18 +0300138 return options
139
Biser Milanov20d50602022-08-09 17:16:36 +0300140 @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 Pentchev469dfea2022-06-27 12:48:18 +0300148
149@charm_class
150class CinderStorPoolCharm(CinderCharmBase):
Biser Milanov53644f62022-08-03 16:08:59 +0300151 release = "yoga"
Peter Pentchev469dfea2022-06-27 12:48:18 +0300152
153
Biser Milanov53644f62022-08-03 16:08:59 +0300154if __name__ == "__main__":
Peter Pentchev3a17d7d2022-07-27 13:11:57 +0000155 # main(get_charm_class_for_release())
Peter Pentcheva8266902022-07-29 05:55:35 +0000156 # main(CinderStorPoolCharm)
157 main(get_charm_class(release="yoga"))