From c69a9e200730819805463f7ad77b726dac0a2408 Mon Sep 17 00:00:00 2001 From: Ray Miller Date: Sat, 21 Jun 2025 15:55:28 +0100 Subject: [PATCH] Factor out task and handler into their own modules --- ordo/handler.scm | 22 ++++++++++++++++++++++ ordo/play.scm | 45 ++++----------------------------------------- ordo/task.scm | 26 ++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 41 deletions(-) create mode 100644 ordo/handler.scm create mode 100644 ordo/task.scm diff --git a/ordo/handler.scm b/ordo/handler.scm new file mode 100644 index 0000000..a6202db --- /dev/null +++ b/ordo/handler.scm @@ -0,0 +1,22 @@ +(define-module (ordo handler) + #:use-module (srfi srfi-9) + #:use-module (ordo logger) + #:export (make-handler + handler? + handler-name + handler-action + handler + run-handler)) + +(define-record-type + (make-handler name action) + handler? + (name handler-name) + (action handler-action)) + +(define* (handler #:key name action) + (make-handler name action)) + +(define (run-handler h conn) + (log-msg 'NOTICE "Running handler: " (handler-name h)) + ((handler-action h) conn)) diff --git a/ordo/play.scm b/ordo/play.scm index 8ef756a..95532f6 100644 --- a/ordo/play.scm +++ b/ordo/play.scm @@ -18,8 +18,10 @@ this program. If not, see . (define-module (ordo play) #:use-module (ordo connection) #:use-module (ordo context) + #:use-module (ordo handler) #:use-module (ordo inventory) #:use-module (ordo logger) + #:use-module (ordo task) #:use-module (ordo util flatten) #:use-module (srfi srfi-9) #:use-module (srfi srfi-26) @@ -33,17 +35,7 @@ this program. If not, see . play-vars play-tasks play-handlers - run-play - task - task? - task-name - task-pre-condition - task-action - run-task - handler - handler? - handler-name - handler-action)) + run-play)) (define-record-type (make-play name host sudo? sudo-user sudo-password vars tasks handlers) @@ -60,36 +52,6 @@ this program. If not, see . (define* (play #:key name host (sudo? #f) (sudo-user #f) (sudo-password #f) (vars '()) (tasks '()) (handlers '())) (make-play name host sudo? sudo-user sudo-password (alist->hash-table vars) tasks handlers)) -(define-record-type - (make-task name action pre-condition) - task? - (name task-name) - (pre-condition task-pre-condition) - (action task-action)) - -(define* (task #:key name action (pre-condition (const #t))) - (make-task name action pre-condition)) - -(define (run-task t conn) - (if ((task-pre-condition t) conn) - (begin - (log-msg 'NOTICE "Running task " (task-name t)) - ((task-action t) conn)) - (log-msg 'NOTICE "Skipping task " (task-name t) ": pre-condition not met"))) - -(define-record-type - (make-handler name action) - handler? - (name handler-name) - (action handler-action)) - -(define* (handler #:key name action) - (make-handler name action)) - -(define (run-handler h conn) - (log-msg 'NOTICE "Running handler: " (handler-name h)) - ((handler-action h) conn)) - (define (run-play p) (log-msg 'NOTICE "Running play: " (play-name p)) (parameterize ((*play-handlers* (map handler-name (play-handlers p))) @@ -102,6 +64,7 @@ this program. If not, see . (define (run-host-play p h) (log-msg 'NOTICE "Running play on host: " (host-name h)) (parameterize ((*host-vars* (host-vars h)) + (*play-handlers* (play-handlers p)) (*play-triggers* (make-bitvector (length (play-handlers p)) #f))) (call-with-connection (host-connection h) diff --git a/ordo/task.scm b/ordo/task.scm new file mode 100644 index 0000000..8ccce13 --- /dev/null +++ b/ordo/task.scm @@ -0,0 +1,26 @@ +(define-module (ordo task) + #:use-module (ordo logger) + #:use-module (srfi srfi-9) + #:export (task + task? + task-name + task-pre-condition + task-action + run-task)) + +(define-record-type + (make-task name action pre-condition) + task? + (name task-name) + (pre-condition task-pre-condition) + (action task-action)) + +(define* (task #:key name action (pre-condition (const #t))) + (make-task name action pre-condition)) + +(define (run-task t conn) + (if ((task-pre-condition t) conn) + (begin + (log-msg 'NOTICE "Running task " (task-name t)) + ((task-action t) conn)) + (log-msg 'NOTICE "Skipping task " (task-name t) ": pre-condition not met")))