aboutsummaryrefslogtreecommitdiff
path: root/src/combinator.lisp
blob: d2d02ffc2e598dcf6a2e919e2fedcc9e828c2b0d (plain)
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
;;; Consfigurator -- Lisp declarative configuration management system

;;; Copyright (C) 2021  Sean Whitton <spwhitton@spwhitton.name>

;;; This file is free software; you can redistribute it and/or modify
;;; it under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3, or (at your option)
;;; any later version.

;;; This file is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.

;;; You should have received a copy of the GNU General Public License
;;; along with this program.  If not, see <http://www.gnu.org/licenses/>.

(in-package :consfigurator)
(named-readtables:in-readtable :consfigurator)

;;;; Property combinators

(defmacro define-function-property-combinator (name args &body body)
  (multiple-value-bind (forms declarations docstring)
      (parse-body body :documentation t)
    `(defun ,name ,args
       ,@(and docstring `(,docstring))
       ,@declarations
       (flet ((:retprop (&rest all &key args &allow-other-keys)
                (let ((psym (gensym ,(symbol-name name)))
                      (setprop-args (remove-from-plist all :args)))
                  (unless (getf setprop-args :desc)
                    (setf (get ',name 'inline-combinator) t))
                  (setf (get psym 'combinator) ',name)
                  (apply #'setprop psym setprop-args)
                  (return-from ,name (list* psym args)))))
         ,@forms))))

(defmacro define-choosing-property-combinator
    (name lambda-list &key type choose)
  `(progn
     (define-function-property-combinator ,name ,lambda-list
       (flet ((choose-propapp () ,choose))
         (:retprop :type ,type
                   :desc (lambda (&rest args)
                           (declare (ignore args))
                           (propappdesc (choose-propapp)))
                   :hostattrs (lambda (&rest args)
                                (declare (ignore args))
                                (propappattrs (choose-propapp)))
                   :apply (lambda (&rest args)
                            (declare (ignore args))
                            (propappapply (choose-propapp)))
                   :unapply (lambda (&rest args)
                              (declare (ignore args))
                              (propappunapply (choose-propapp))))))
     (setf (get ',name 'inline-combinator) t)))

(defmacro with-skip-failed-changes (&body forms)
  `(handler-bind ((failed-change
                    (lambda (c)
                      (with-indented-inform
                        (informat t
                                  (simple-condition-format-control c)
                                  (simple-condition-format-arguments c)))
                      (invoke-restart 'skip-property))))
     ,@forms))

(define-function-property-combinator eseqprops (&rest propapps)
  (:retprop :type (collapse-types (mapcar #'propapptype propapps))
            :hostattrs (lambda () (mapc #'propappattrs propapps))
            :apply (lambda () (apply-and-print propapps))
            :unapply (lambda () (apply-and-print propapps t))))

(define-function-property-combinator seqprops (&rest propapps)
  (:retprop :type (collapse-types (mapcar #'propapptype propapps))
            :hostattrs (lambda () (mapc #'propappattrs propapps))
            :apply (lambda ()
                     (with-skip-failed-changes
                       (apply-and-print propapps)))
            :unapply (lambda ()
                       (with-skip-failed-changes
                         (apply-and-print propapps t)))))

(defmacro with-requirements (propapp &body requirements)
  "Apply PROPAPP only after applying each dependency in REQUIREMENTS.
Each item in REQUIREMENTS implicitly depends on the one preceding it, i.e., we
apply the elements of REQUIREMENTS in reverse order."
  `(eseqprops ,@(reverse requirements) ,propapp))

(define-function-property-combinator silent-seqprops (&rest propapps)
  (:retprop :type (collapse-types (mapcar #'propapptype propapps))
            :hostattrs (lambda () (mapc #'propappattrs propapps))
            :apply (lambda ()
                     (with-skip-failed-changes
                       (mapc #'propappapply propapps)))
            :unapply (lambda ()
                       (with-skip-failed-changes
                         (mapc #'propappunapply (reverse propapps))))))

;; note that the :FAILED-CHANGE value is only used within this function and
;; should not be returned by property subroutines, per the spec
(defun apply-and-print (propapps &optional unapply)
  (labels ((propapp-apply (propapp)
             (if unapply (propappunapply propapp) (propappapply propapp)))
           (announce-propapp-apply (propapp)
             (let ((buffer (make-array '(0)
				       :element-type 'base-char
				       :fill-pointer 0 :adjustable t)))
               (values (with-output-to-string (*standard-output* buffer)
                         (with-indented-inform
                           (propapp-apply propapp)))
                       buffer))))
    (let ((ret :no-change)
          ;; Remove any null propapps because we don't want to print anything
          ;; for those, and applying them will do nothing.
          (propapps
            (remove-if #'null (if unapply (reverse propapps) propapps))))
      (dolist (propapp propapps ret)
        (let ((announce (or (> 1 *consfigurator-debug-level*)
                            (not (get (get (car propapp) 'combinator)
                                   'inline-combinator)))))
          (multiple-value-bind (result output)
              ;; TODO Nested combinators can mean that we establish this
              ;; restart more than once, and they all appear in the debugger
              ;; without any way to distinguish them.  Perhaps we can use the
              ;; :TEST argument to RESTART-CASE such that only the
              ;; innermost(?) skip option appears.
              (restart-case (if announce
                                (announce-propapp-apply propapp)
                                (propapp-apply propapp))
                (skip-property () :failed-change))
            (when (and output (or (> 1 *consfigurator-debug-level*)
                                  (not (eql result :no-change))))
              (fresh-line)
              (princ output))
            (when announce
              (informat t "~&~@[~A :: ~]~@[~A ... ~]~A~%"
                        (get-hostname) (propappdesc propapp)
                        (case result
                          (:no-change     "ok")
                          (:failed-change "failed")
                          (t              "done"))))
            (unless (or (not ret) (eql result :no-change))
              (setq ret nil))))))))

(define-function-property-combinator unapply (propapp)
  (destructuring-bind (psym . args) propapp
    (:retprop :type (proptype psym)
              :lambda (proplambda psym)
              :desc (lambda (&rest args)
                      (strcat "Unapply: " (apply #'propdesc psym args)))
              :check (when-let ((check (get psym 'check)))
                       (complement check))
              :hostattrs (lambda (&rest args)
                           ;; run the :HOSTATTRS subroutine but throw away any
                           ;; new hostattrs; when unapplying, the :HOSTATTRS
                           ;; subroutine is only to check compatibility
                           (with-preserve-hostattrs
                             (apply #'propattrs psym args)))
              :apply (get psym 'unapply)
              :unapply (get psym 'papply)
              :args args)))

(define-function-property-combinator desc (desc propapp)
  (:retprop :type (propapptype propapp)
            :desc (lambda () desc)
            :hostattrs (lambda (&rest args)
                         (declare (ignore args))
                         (propappattrs propapp))
            :apply (lambda (&rest args)
                     (declare (ignore args))
                     (propappapply propapp))
            :unapply (lambda (&rest args)
                       (declare (ignore args))
                       (propappunapply propapp))))

(defmacro on-change (propapp &body on-change)
  "If applying PROPAPP makes a change, also apply each of of the propapps
ON-CHANGE in order."
  `(on-change* ,propapp ,@on-change))

(define-function-property-combinator on-change* (propapp &rest propapps)
  (:retprop :type (collapse-types (propapptype propapp)
                                  (mapcar #'propapptype propapps))
            :desc (get (car propapp) 'desc)
            :hostattrs (lambda (&rest args)
                         (apply #'propattrs (car propapp) args)
                         (mapc #'propappattrs propapps))
            :apply (lambda (&rest args)
                     (if (eql :no-change
                              (propappapply (cons (car propapp) args)))
                         :no-change
                         (dolist (propapp propapps)
                           (propappapply propapp))))
            :unapply (lambda (&rest args)
                       (if (eql :no-change
                                (propappunapply (cons (car propapp) args)))
                           :no-change
                           (dolist (propapp (reverse propapps))
                             (propappunapply propapp))))
            :args (cdr propapp)))

(defmacro as (user &body properties)
  "Apply PROPERTIES as USER by reconnecting with the :AS connection type.
Note that the :AS connection type requires root, so as a special case, this
macro just expands to ESEQPROPS if USER is the literal string \"root\"
(without evaluation).  This makes it possible to use this macro to annotate
applications of properties which are normally applied by non-root, to make it
explicit that in this case they're being applied as root, e.g. that they will
affect /root and not /home."
  (if (and (stringp user) (string= user "root"))
      `(eseqprops ,@properties)
      `(reconnects. `((:as :to ,,user)) ,@properties)))