Make it easier to override install and promote custome checks

This commit is contained in:
Liam Young 2020-05-01 07:20:02 +00:00
parent 2ef02f14d6
commit 18b8f1fcfe
1 changed files with 13 additions and 11 deletions

View File

@ -61,7 +61,7 @@ class OSBaseCharm(CharmBase):
self.framework.observe(self.on.pre_series_upgrade, self) self.framework.observe(self.on.pre_series_upgrade, self)
self.framework.observe(self.on.post_series_upgrade, self) self.framework.observe(self.on.post_series_upgrade, self)
def on_install(self, event): def install_pkgs(self):
logging.info("Installing packages") logging.info("Installing packages")
if self.model.config.get('source'): if self.model.config.get('source'):
add_source( add_source(
@ -71,11 +71,21 @@ class OSBaseCharm(CharmBase):
apt_install(self.PACKAGES, fatal=True) apt_install(self.PACKAGES, fatal=True)
self.update_status() self.update_status()
def on_install(self, event):
self.install_pkgs()
def custom_status_check(self): def custom_status_check(self):
raise NotImplementedError raise NotImplementedError
def update_status(self): def update_status(self):
logging.info("Updating status") logging.info("Updating status")
try:
# Custom checks return True if the checked passed else False.
# If the check failed the custom check will have set the status.
if not self.custom_status_check():
return
except NotImplementedError:
pass
if self.state.series_upgrade: if self.state.series_upgrade:
self.unit.status = BlockedStatus( self.unit.status = BlockedStatus(
'Ready for do-release-upgrade and reboot. ' 'Ready for do-release-upgrade and reboot. '
@ -87,20 +97,12 @@ class OSBaseCharm(CharmBase):
return return
missing_relations = [] missing_relations = []
for relation in self.REQUIRED_RELATIONS: for relation in self.REQUIRED_RELATIONS:
rel = self.model.get_relation(relation) if not self.model.get_relation(relation):
if rel is None:
missing_relations.append(relation) missing_relations.append(relation)
if missing_relations: if missing_relations:
self.unit.status = BlockedStatus( self.unit.status = BlockedStatus(
'Missing relations: {}'.format(', '.join(missing_relations))) 'Missing relations: {}'.format(', '.join(missing_relations)))
return return
try:
# Custom checks return True if the checked passed else False.
# If the check failed the custom check will have set the status.
if not self.custom_status_check():
return
except NotImplementedError:
pass
if self.state.is_started: if self.state.is_started:
self.unit.status = ActiveStatus('Unit is ready') self.unit.status = ActiveStatus('Unit is ready')
else: else:
@ -114,7 +116,7 @@ class OSBaseCharm(CharmBase):
_svcs = [] _svcs = []
for svc in self.RESTART_MAP.values(): for svc in self.RESTART_MAP.values():
_svcs.extend(svc) _svcs.extend(svc)
return sorted(list(set(_svcs))) return list(set(_svcs))
def on_pre_series_upgrade(self, event): def on_pre_series_upgrade(self, event):
_, messages = os_utils.manage_payload_services( _, messages = os_utils.manage_payload_services(