1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import enum
import typing as ty
from keystoneauth1 import adapter as ksa_adapter
from openstack import exceptions
from openstack import resource
from openstack import utils
class Level(enum.Enum):
ERROR = 'ERROR'
WARNING = 'WARNING'
INFO = 'INFO'
DEBUG = 'DEBUG'
class Binary(enum.Enum):
ANY = '*'
API = 'cinder-api'
VOLUME = 'cinder-volume'
SCHEDULER = 'cinder-scheduler'
BACKUP = 'cinder-backup'
class LogLevel(resource.Resource):
# Properties
#: The binary name of the service.
binary = resource.Body('binary')
# TODO(stephenfin): Do we need these? They are request-only.
# #: The name of the host.
# server = resource.Body('server')
# #: he prefix for the log path we are querying, for example ``cinder.`` or
# #: ``sqlalchemy.engine.`` When not present or the empty string is passed
# #: all log levels will be retrieved.
# prefix = resource.Body('prefix')
#: The name of the host.
host = resource.Body('host')
#: The current log level that queried.
levels = resource.Body('levels', type=dict)
class Service(resource.Resource):
resources_key = 'services'
base_path = '/os-services'
# capabilities
allow_list = True
_query_mapping = resource.QueryParameters(
'binary',
'host',
)
# Properties
#: The ID of active storage backend (cinder-volume services only)
active_backend_id = resource.Body('active_backend_id')
#: The availability zone of service
availability_zone = resource.Body('zone')
#: The state of storage backend (cinder-volume services only) (since 3.49)
backend_state = resource.Body('backend_state')
#: Binary name of service
binary = resource.Body('binary')
#: The cluster name (since 3.7)
cluster = resource.Body('cluster')
#: Disabled reason of service
disabled_reason = resource.Body('disabled_reason')
#: The name of the host where service runs
host = resource.Body('host')
# Whether the host is frozen or not (cinder-volume services only)
is_frozen = resource.Body('frozen')
#: Service name
name = resource.Body('name', alias='binary')
#: The volume service replication status (cinder-volume services only)
replication_status = resource.Body('replication_status')
#: State of service
state = resource.Body('state')
#: Status of service
status = resource.Body('status')
#: The date and time when the resource was updated
updated_at = resource.Body('updated_at')
# 3.32 introduced the 'set-log' action
_max_microversion = '3.32'
@classmethod
def find(cls, session, name_or_id, ignore_missing=True, **params):
# No direct request possible, thus go directly to list
data = cls.list(session, **params)
result = None
for maybe_result in data:
# Since ID might be both int and str force cast
id_value = str(cls._get_id(maybe_result))
name_value = maybe_result.name
if str(name_or_id) in (id_value, name_value):
if 'host' in params and maybe_result['host'] != params['host']:
continue
# Only allow one resource to be found. If we already
# found a match, raise an exception to show it.
if result is None:
result = maybe_result
else:
msg = "More than one %s exists with the name '%s'."
msg = msg % (cls.__name__, name_or_id)
raise exceptions.DuplicateResource(msg)
if result is not None:
return result
if ignore_missing:
return None
raise exceptions.NotFoundException(
f"No {cls.__name__} found for {name_or_id}"
)
def commit(self, session, prepend_key=False, *args, **kwargs):
# we need to set prepend_key to false
return super().commit(
session,
prepend_key,
*args,
**kwargs,
)
def _action(self, session, action, body, microversion=None):
if not microversion:
microversion = session.default_microversion
url = utils.urljoin(Service.base_path, action)
response = session.put(url, json=body, microversion=microversion)
self._translate_response(response)
return self
def enable(self, session):
"""Enable service."""
body = {'binary': self.binary, 'host': self.host}
return self._action(session, 'enable', body)
def disable(self, session, *, reason=None):
"""Disable service."""
body = {'binary': self.binary, 'host': self.host}
if not reason:
action = 'disable'
else:
action = 'disable-log-reason'
body['disabled_reason'] = reason
return self._action(session, action, body)
def thaw(self, session):
body = {'host': self.host}
return self._action(session, 'thaw', body)
def freeze(self, session):
body = {'host': self.host}
return self._action(session, 'freeze', body)
@classmethod
def set_log_levels(
cls,
session: ksa_adapter.Adapter,
*,
level: Level,
binary: Binary | None = None,
server: str | None = None,
prefix: str | None = None,
) -> None:
"""Set log level for services.
:param session: The session to use for making this request.
:param level: The log level to set, case insensitive, accepted values
are ``INFO``, ``WARNING``, ``ERROR`` and ``DEBUG``.
:param binary: The binary name of the service.
:param server: The name of the host.
:param prefix: The prefix for the log path we are querying, for example
``cinder.`` or ``sqlalchemy.engine.`` When not present or the empty
string is passed all log levels will be retrieved.
:returns: None.
"""
microversion = cls._assert_microversion_for(
session, '3.32', error_message="Cannot use set-log action"
)
body = {
'level': level,
'binary': binary or '', # cinder insists on strings
'server': server,
'prefix': prefix,
}
url = utils.urljoin(cls.base_path, 'set-log')
response = session.put(url, json=body, microversion=microversion)
exceptions.raise_from_response(response)
@classmethod
def get_log_levels(
cls,
session: ksa_adapter.Adapter,
*,
binary: Binary | None = None,
server: str | None = None,
prefix: str | None = None,
) -> ty.Generator[LogLevel, None, None]:
"""Get log level for services.
:param session: The session to use for making this request.
:param binary: The binary name of the service.
:param server: The name of the host.
:param prefix: The prefix for the log path we are querying, for example
``cinder.`` or ``sqlalchemy.engine.`` When not present or the empty
string is passed all log levels will be retrieved.
:returns: A generator of
:class:`~openstack.block_storage.v3.service.LogLevel` objects.
"""
microversion = cls._assert_microversion_for(
session, '3.32', error_message="Cannot use get-log action"
)
body = {
'binary': binary or '', # cinder insists on strings
'server': server,
'prefix': prefix,
}
url = utils.urljoin(cls.base_path, 'get-log')
response = session.put(url, json=body, microversion=microversion)
exceptions.raise_from_response(response)
for entry in response.json()['log_levels']:
yield LogLevel(
binary=entry['binary'],
host=entry['host'],
levels=entry['levels'],
)
def failover(
self,
session,
*,
cluster=None,
backend_id=None,
):
"""Failover a service
Only applies to replicating cinder-volume services.
"""
body = {'host': self.host}
if cluster:
body['cluster'] = cluster
if backend_id:
body['backend_id'] = backend_id
action = 'failover_host'
if utils.supports_microversion(session, '3.26'):
action = 'failover'
return self._action(session, action, body)
|