blob: 6e4fb2e103083d96a0ec364e87f8b9e4c40cc7d7 [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",
Peter Pentchev38a6aa52022-08-23 01:17:17 +030057 "iscsi-portal-group",
Biser Milanov4ea59212022-08-05 11:03:05 +030058 ]
Peter Pentchev469dfea2022-06-27 12:48:18 +030059 # Overriden from the parent. May be set depending on the charm's properties
60 stateless = True
61 active_active = True
62
63 def __init__(self, *args, **kwargs):
64 super().__init__(*args, **kwargs)
65
Biser Milanov2afeb432022-08-05 10:42:40 +030066 def on_config(self, event):
67 config = dict(self.framework.model.config)
68 conf_error = self._check_for_config_errors(config)
69 if conf_error is not None:
70 logger.error(conf_error)
71 self.unit.status = BlockedStatus(conf_error)
Biser Milanov57725c22022-08-16 15:59:41 +030072 self._stored.is_started = False
73
Biser Milanov2afeb432022-08-05 10:42:40 +030074 return
75
Biser Milanovfd3f55d2022-09-02 17:09:59 +030076 create_storpool_conf(StorPoolConfItems.from_config(config))
Biser Milanov20d50602022-08-09 17:16:36 +030077
Biser Milanov2afeb432022-08-05 10:42:40 +030078 super().on_config(event)
79
Biser Milanov57725c22022-08-16 15:59:41 +030080 self._stored.is_started = True
81
Peter Pentchev469dfea2022-06-27 12:48:18 +030082 def cinder_configuration(self, config):
Biser Milanov2afeb432022-08-05 10:42:40 +030083 conf_error = self._check_for_config_errors(config)
84 if conf_error is not None:
85 logger.error(conf_error)
Biser Milanov57725c22022-08-16 15:59:41 +030086 self._stored.is_started = False
87
Biser Milanov2afeb432022-08-05 10:42:40 +030088 return []
89
Peter Pentchev469dfea2022-06-27 12:48:18 +030090 # Return the configuration to be set by the principal.
Biser Milanov53644f62022-08-03 16:08:59 +030091 backend_name = config.get("volume-backend-name", self.framework.model.app.name)
92 volume_driver = "cinder.volume.drivers.storpool.StorPoolDriver"
Biser Milanov2afeb432022-08-05 10:42:40 +030093
Peter Pentchev469dfea2022-06-27 12:48:18 +030094 options = [
Biser Milanov53644f62022-08-03 16:08:59 +030095 ("volume_driver", volume_driver),
96 ("volume_backend_name", backend_name),
Biser Milanov7712cce2022-08-05 10:57:29 +030097 ("storpool_template", config["storpool-template"]),
Biser Milanov4ea59212022-08-05 11:03:05 +030098 ("sp_api_http_host", config["sp-api-http-host"]),
99 ("sp_api_http_port", config["sp-api-http-port"]),
100 ("sp_auth_token", config["sp-auth-token"]),
Peter Pentchev38a6aa52022-08-23 01:17:17 +0300101 ("iscsi_export_to", "*"),
102 ("iscsi_portal_group", config["iscsi-portal-group"]),
Peter Pentchev469dfea2022-06-27 12:48:18 +0300103 ]
104
Biser Milanov53644f62022-08-03 16:08:59 +0300105 if config.get("use-multipath"):
106 options.extend(
107 [
108 ("use_multipath_for_image_xfer", True),
109 ("enforce_multipath_for_image_xfer", True),
110 ]
111 )
Peter Pentchev469dfea2022-06-27 12:48:18 +0300112
Biser Milanovfd3f55d2022-09-02 17:09:59 +0300113 create_storpool_conf(StorPoolConfItems.from_config(config))
Biser Milanov20d50602022-08-09 17:16:36 +0300114
Biser Milanov57725c22022-08-16 15:59:41 +0300115 self._stored.is_started = True
116
Peter Pentchev469dfea2022-06-27 12:48:18 +0300117 return options
118
Biser Milanovfd3f55d2022-09-02 17:09:59 +0300119 def _check_for_config_errors(self, config):
120 missing = []
121 for mandatory in self.MANDATORY_CONFIG:
122 if mandatory not in config:
123 missing.append(mandatory)
124
125 if missing:
126 return f"Mandatory options are missing: {', '.join(missing)}"
127
128 if config["protocol"] not in ["block", "iscsi"]:
129 return (
130 f"""Invalid 'protocol' option provided: '{config["protocol"]}';"""
131 "valid are 'block' and 'iscsi'"
132 )
133
134 if config["protocol"] == "block":
135 return "'protocol' value 'block' not yet supported"
136
137 if not (0 < config["sp-api-http-port"] < 65536):
138 return (
139 f"""'sp-api-http-port' ('{config["sp-api-http-port"]}')"""
140 "is not a valid port (0-65535)"
141 )
Biser Milanov20d50602022-08-09 17:16:36 +0300142
Peter Pentchev469dfea2022-06-27 12:48:18 +0300143
144@charm_class
145class CinderStorPoolCharm(CinderCharmBase):
Biser Milanov53644f62022-08-03 16:08:59 +0300146 release = "yoga"
Peter Pentchev469dfea2022-06-27 12:48:18 +0300147
148
Biser Milanovfd3f55d2022-09-02 17:09:59 +0300149def create_storpool_conf(sp_conf_items: StorPoolConfItems):
150 pathlib.Path("/etc/storpool.conf").write_text(
151 "# Do not edit; this file is generated by the cinder-storpool charm.\n"
152 + sp_conf_items.to_ini_key_value_pairs(),
153 encoding="UTF-8",
154 )
155
156
Biser Milanov53644f62022-08-03 16:08:59 +0300157if __name__ == "__main__":
Peter Pentchev3a17d7d2022-07-27 13:11:57 +0000158 # main(get_charm_class_for_release())
Peter Pentcheva8266902022-07-29 05:55:35 +0000159 # main(CinderStorPoolCharm)
160 main(get_charm_class(release="yoga"))