mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-12-23 02:21:57 +00:00
Implement subscribing predicates found in yaml
This commit is contained in:
parent
ea76a19595
commit
d71a25e30a
@ -1,5 +1,6 @@
|
||||
from subprocess import Popen
|
||||
from subprocess import Popen, run
|
||||
from functools import partial
|
||||
from contextlib import suppress
|
||||
|
||||
import yaml
|
||||
from transitions import State
|
||||
@ -13,6 +14,7 @@ class YamlFSM(FSMBase):
|
||||
self.for_config_states_and_transitions_do(self.patch_config_callbacks)
|
||||
self.setup_states()
|
||||
super().__init__() # FSMBase.__init__() requires states
|
||||
self.for_config_states_and_transitions_do(self.subscribe_and_remove_predicates)
|
||||
self.setup_transitions()
|
||||
|
||||
@staticmethod
|
||||
@ -38,6 +40,24 @@ class YamlFSM(FSMBase):
|
||||
if key in topatch:
|
||||
json_obj[key] = partial(self.run, json_obj[key])
|
||||
|
||||
def subscribe_and_remove_predicates(self, json_obj):
|
||||
for key in json_obj:
|
||||
if key == 'predicates':
|
||||
for predicate in json_obj[key]:
|
||||
self.subscribe_predicate(
|
||||
json_obj['trigger'],
|
||||
partial(
|
||||
self.statuscode_is_zero,
|
||||
predicate
|
||||
)
|
||||
)
|
||||
with suppress(KeyError):
|
||||
json_obj.pop('predicates')
|
||||
|
||||
@staticmethod
|
||||
def run(command, event):
|
||||
Popen(command, shell=True)
|
||||
|
||||
@staticmethod
|
||||
def statuscode_is_zero(command):
|
||||
return run(command, shell=True).returncode == 0
|
||||
|
Loading…
Reference in New Issue
Block a user