]>
git.proxmox.com Git - ceph.git/blob - ceph/src/python-common/ceph/deployment/hostspec.py
3 from typing
import Optional
, List
, Any
5 pass # just for type checking
8 class SpecValidationError(Exception):
10 Defining an exception here is a bit problematic, cause you cannot properly catch it,
11 if it was raised in a different mgr module.
15 errno
: int = -errno
.EINVAL
):
16 super(SpecValidationError
, self
).__init
__(msg
)
20 class HostSpec(object):
22 Information about hosts. Like e.g. ``kubectl get nodes``
26 addr
=None, # type: Optional[str]
27 labels
=None, # type: Optional[List[str]]
28 status
=None, # type: Optional[str]
30 self
.service_type
= 'host'
32 #: the bare hostname on the host. Not the FQDN.
33 self
.hostname
= hostname
# type: str
35 #: DNS name or IP address to reach it
36 self
.addr
= addr
or hostname
# type: str
39 self
.labels
= labels
or [] # type: List[str]
41 #: human readable status
42 self
.status
= status
or '' # type: str
44 def to_json(self
) -> dict:
46 'hostname': self
.hostname
,
48 'labels': list(set((self
.labels
))),
49 'status': self
.status
,
53 def from_json(cls
, host_spec
: dict) -> 'HostSpec':
54 host_spec
= cls
.normalize_json(host_spec
)
55 _cls
= cls(host_spec
['hostname'],
56 host_spec
['addr'] if 'addr' in host_spec
else None,
57 list(set(host_spec
['labels'])) if 'labels' in host_spec
else None,
58 host_spec
['status'] if 'status' in host_spec
else None)
62 def normalize_json(host_spec
: dict) -> dict:
63 labels
= host_spec
.get('labels')
66 if isinstance(labels
, list):
68 if not isinstance(labels
, str):
69 raise SpecValidationError(f
'Labels ({labels}) must be a string or list of strings')
70 host_spec
['labels'] = [labels
]
73 def __repr__(self
) -> str:
74 args
= [self
.hostname
] # type: List[Any]
75 if self
.addr
is not None:
76 args
.append(self
.addr
)
78 args
.append(self
.labels
)
80 args
.append(self
.status
)
82 return "HostSpec({})".format(', '.join(map(repr, args
)))
84 def __str__(self
) -> str:
85 if self
.hostname
!= self
.addr
:
86 return f
'{self.hostname} ({self.addr})'
89 def __eq__(self
, other
: Any
) -> bool:
90 # Let's omit `status` for the moment, as it is still the very same host.
91 return self
.hostname
== other
.hostname
and \
92 self
.addr
== other
.addr
and \
93 self
.labels
== other
.labels