blob: 2e935f81f094545868222b2be2534aef67949ef7 [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
35 def to_ini_key_value_pairs(self) -> str:
36 return "".join(
37 f"{name.upper()}={value}\n" for name, value in dataclasses.asdict(self).items()
38 )
39
40
Peter Pentchev469dfea2022-06-27 12:48:18 +030041class CinderCharmBase(CinderStoragePluginCharm):
42
Biser Milanov8bc000b2022-08-08 14:39:29 +030043 PACKAGES = ["charm-cinder-storpool-deps", "cinder-common"]
Biser Milanov4ea59212022-08-05 11:03:05 +030044 MANDATORY_CONFIG = [
45 "protocol",
46 "storpool-template",
47 "sp-api-http-host",
48 "sp-api-http-port",
49 "sp-auth-token",
50 ]
Peter Pentchev469dfea2022-06-27 12:48:18 +030051 # Overriden from the parent. May be set depending on the charm's properties
52 stateless = True
53 active_active = True
54
55 def __init__(self, *args, **kwargs):
56 super().__init__(*args, **kwargs)
57
Biser Milanov2afeb432022-08-05 10:42:40 +030058 def _check_for_config_errors(self, config):
59 missing = []
60 for mandatory in self.MANDATORY_CONFIG:
61 if mandatory not in config:
62 missing.append(mandatory)
63
64 if missing:
65 return f"Mandatory options are missing: {', '.join(missing)}"
66
67 if config["protocol"] not in ["block", "iscsi"]:
68 return (
69 f"""Invalid 'protocol' option provided: '{config["protocol"]}';"""
70 "valid are 'block' and 'iscsi'"
71 )
72
73 if config["protocol"] == "block":
74 return "'protocol' value 'block' not yet supported"
75
Biser Milanov4ea59212022-08-05 11:03:05 +030076 if not (0 < config["sp-api-http-port"] < 65536):
77 return (
78 f"""'sp-api-http-port' ('{config["sp-api-http-port"]}')"""
79 "is not a valid port (0-65535)"
80 )
81
Biser Milanov2afeb432022-08-05 10:42:40 +030082 def on_config(self, event):
83 config = dict(self.framework.model.config)
84 conf_error = self._check_for_config_errors(config)
85 if conf_error is not None:
86 logger.error(conf_error)
87 self.unit.status = BlockedStatus(conf_error)
Biser Milanov57725c22022-08-16 15:59:41 +030088 self._stored.is_started = False
89
Biser Milanov2afeb432022-08-05 10:42:40 +030090 return
91
Biser Milanov20d50602022-08-09 17:16:36 +030092 self.create_storpool_conf(
93 StorPoolConfItems(
94 config["sp-api-http-host"], str(config["sp-api-http-port"]), config["sp-auth-token"]
95 )
96 )
97
Biser Milanov2afeb432022-08-05 10:42:40 +030098 super().on_config(event)
99
Biser Milanov57725c22022-08-16 15:59:41 +0300100 self._stored.is_started = True
101
Peter Pentchev469dfea2022-06-27 12:48:18 +0300102 def cinder_configuration(self, config):
Biser Milanov2afeb432022-08-05 10:42:40 +0300103 conf_error = self._check_for_config_errors(config)
104 if conf_error is not None:
105 logger.error(conf_error)
Biser Milanov57725c22022-08-16 15:59:41 +0300106 self._stored.is_started = False
107
Biser Milanov2afeb432022-08-05 10:42:40 +0300108 return []
109
Peter Pentchev469dfea2022-06-27 12:48:18 +0300110 # Return the configuration to be set by the principal.
Biser Milanov53644f62022-08-03 16:08:59 +0300111 backend_name = config.get("volume-backend-name", self.framework.model.app.name)
112 volume_driver = "cinder.volume.drivers.storpool.StorPoolDriver"
Biser Milanov2afeb432022-08-05 10:42:40 +0300113
Peter Pentchev469dfea2022-06-27 12:48:18 +0300114 options = [
Biser Milanov53644f62022-08-03 16:08:59 +0300115 ("volume_driver", volume_driver),
116 ("volume_backend_name", backend_name),
Biser Milanov7712cce2022-08-05 10:57:29 +0300117 ("storpool_template", config["storpool-template"]),
Biser Milanov4ea59212022-08-05 11:03:05 +0300118 ("sp_api_http_host", config["sp-api-http-host"]),
119 ("sp_api_http_port", config["sp-api-http-port"]),
120 ("sp_auth_token", config["sp-auth-token"]),
Peter Pentchev469dfea2022-06-27 12:48:18 +0300121 ]
122
Biser Milanov53644f62022-08-03 16:08:59 +0300123 if config.get("use-multipath"):
124 options.extend(
125 [
126 ("use_multipath_for_image_xfer", True),
127 ("enforce_multipath_for_image_xfer", True),
128 ]
129 )
Peter Pentchev469dfea2022-06-27 12:48:18 +0300130
Biser Milanov20d50602022-08-09 17:16:36 +0300131 self.create_storpool_conf(
132 StorPoolConfItems(
133 config["sp-api-http-host"], str(config["sp-api-http-port"]), config["sp-auth-token"]
134 )
135 )
136
Biser Milanov57725c22022-08-16 15:59:41 +0300137 self._stored.is_started = True
138
Peter Pentchev469dfea2022-06-27 12:48:18 +0300139 return options
140
Biser Milanov20d50602022-08-09 17:16:36 +0300141 @staticmethod
142 def create_storpool_conf(sp_conf_items: StorPoolConfItems):
143 pathlib.Path("/etc/storpool.conf").write_text(
144 "# Do not edit; this file is generated by the cinder-storpool charm.\n"
145 + sp_conf_items.to_ini_key_value_pairs(),
146 encoding="UTF-8",
147 )
148
Peter Pentchev469dfea2022-06-27 12:48:18 +0300149
150@charm_class
151class CinderStorPoolCharm(CinderCharmBase):
Biser Milanov53644f62022-08-03 16:08:59 +0300152 release = "yoga"
Peter Pentchev469dfea2022-06-27 12:48:18 +0300153
154
Biser Milanov53644f62022-08-03 16:08:59 +0300155if __name__ == "__main__":
Peter Pentchev3a17d7d2022-07-27 13:11:57 +0000156 # main(get_charm_class_for_release())
Peter Pentcheva8266902022-07-29 05:55:35 +0000157 # main(CinderStorPoolCharm)
158 main(get_charm_class(release="yoga"))