Implement SYNC : apply operations to bring resources in line with specification.
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
diff --git a/core/resource.lisp b/core/resource.lisp
index cd52de3..9eec95d 100644
--- a/core/resource.lisp
+++ b/core/resource.lisp
@@ -141,3 +141,39 @@
#+nil
(describe-probed (resource 'mount "/rd") t)
+
+
+;; Sync
+
+(defmethod sync ((res resource))
+ (when-let ((diff (resource-diff res)))
+ (let* ((plist (resource-diff-to-plist diff))
+ (host (current-host))
+ (os (host-os host))
+ (ops (list-operations res plist os))
+ (sorted-ops (sort-operations ops)))
+ (iter (for op in sorted-ops)
+ (for op-keys = (operation-properties op))
+ (for op-plist = (get-properties op-keys plist))
+ (apply (operation-generic-function op)
+ res os op-plist)
+ (clear-probed res op-keys)
+ (for failed = (iter (for property in op-keys)
+ (for specified = (get-property property
+ op-plist))
+ (when (not (eq specified +undefined+))
+ (for probed = (get-probed res property))
+ (for desc = (describe-probed-property-value
+ res property probed))
+ (unless (match-specified-value
+ res property specified desc)
+ (collect property)
+ (collect specified)
+ (collect desc)))))
+ (when failed
+ (error 'resource-operation-failed
+ :diff failed
+ :operation op
+ :os os
+ :host host
+ :resource res))))))