Hash :
7d3f17c3
Author :
Thomas de Grivel
Date :
2024-07-11T17:52:33
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 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
;; cl-unix-cybernetics
;; Copyright 2013-2022 Thomas de Grivel <thodg@kmx.io>
;;
;; Permission is hereby granted to use this software granted
;; the above copyright notice and this permission paragraph
;; are included in all copies and substantial portions of this
;; software.
;;
;; THIS SOFTWARE IS PROVIDED "AS-IS" WITHOUT ANY GUARANTEE OF
;; PURPOSE AND PERFORMANCE. IN NO EVENT WHATSOEVER SHALL THE
;; AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
;; THIS SOFTWARE.
(in-package :cl-unix-cybernetics)
;; Misc
(unless (boundp '+undefined+)
(defconstant +undefined+ '+undefined+))
(defmethod subclasses (class))
;; Properties
(defgeneric compare-property-values (resource property value1 value2))
(defgeneric merge-property-values (resource property old new))
;; Probe
(defclass probe ()
((name :initarg :name
:initform (error "Probe without a name.")
:reader probe-name
:type symbol)
(properties :initarg :properties
:initform (error "Probe without properties.")
:reader probe-properties)))
(defgeneric probe-generic-function (probe))
;; Operations
(defclass operation ()
((name :initarg :name
:initform (error "Operation without a name.")
:reader operation-name
:type symbol)
(properties :initarg :properties
:initform (error "Operation without properties.")
:reader operation-properties)
(after :initarg :after
:initform nil
:reader operations-before)))
(defgeneric operation-generic-function (op))
;; Resource metaclass
(defvar *the-resource-class*)
(defclass resource-class (standard-class)
((direct-probes :initarg :direct-probes
:initform ()
:reader direct-probes
:type list)
(direct-operations :initarg :direct-operations
:initform ()
:reader direct-operations
:type list)
(probes :reader probes-of
:type list)
(operations :reader operations-of
:type list)
(op-properties :initarg :op-properties
:initform ()
:reader op-properties
:type list))
(:default-initargs :direct-superclasses (list *the-resource-class*)))
(defmethod closer-mop:validate-superclass ((c resource-class)
(super standard-class))
t)
(defmacro define-resource-class (name &optional
direct-superclasses
direct-slots
direct-probes
direct-ops
options)
`(defclass ,name ,(or direct-superclasses
'(resource))
,direct-slots
(:direct-operations ,@direct-ops)
(:direct-probes ,@direct-probes)
(:metaclass resource-class)
,@options))
(defgeneric probe-class (resource-class))
(defgeneric compute-probes (resource-class))
(defgeneric operation-class (resource-class))
(defgeneric compute-operations (resource-class))
;; Resource base class
(defclass resource (standard-object)
((id :type string
:initarg :id
:initform (error "Missing ID for resource.")
:reader resource-id)
(specified-properties :type list
:initarg :specified-properties
:initform nil
:accessor specified-properties)
(probed-properties :type list
:initarg :probed-properties
:initform nil
:accessor probed-properties))
(:metaclass resource-class))
(defgeneric resource-additional-specs (resource os))
(defgeneric resource-probes-properties (resource))
(defgeneric resource-operations-properties (resource))
(defgeneric sync (resource))
(setq *the-resource-class* (find-class 'resource))
(defvar *resource*)
;; Resource container
(defun make-resource-registry ()
(make-hash-table :test 'equalp))
(defclass resource-container (resource)
((resources :initarg :resources
:initform (make-resource-registry)
:type hash-table
:reader resource-registry))
(:metaclass resource-class))
(defvar *cl-unix-cybernetics*
(make-instance 'resource-container :id "cl-unix-cybernetics"))
(defvar *parent-resource*
*cl-unix-cybernetics*)
(defgeneric clear-resources% (resource-container))
(defmacro with-parent-resource (resource &body body)
`(let ((*parent-resource* ,(or resource '*cl-unix-cybernetics*)))
,@body))
(defgeneric resource-before-p (r1 r2))
;; Specifying resources
(defgeneric get-specified (resource property))
(defgeneric (setf get-specified) (value resource property))
(defvar *specification*)
(defgeneric parse-next-specification (resource spec))
(defgeneric parse-specification (resource spec))
;; OS
(defclass os ()
((machine :initarg :machine
:reader os-machine
:type string)
(name :initarg :name
:reader os-name
:type (or symbol string))
(release :initarg :release
:reader os-release
:type string)
(version :initarg :version
:reader os-version
:type string)))
;; Host
(define-resource-class host (resource-container)
((shell :initarg :shell
:type shell))
((probe-os-using-uname :properties (:os))
(probe-host-locale :properties (:locale))
(probe-host-packages :properties (:packages))
(probe-boot-time :properties (:boot-time))
(probe-host-homedir :properties (:homedir))
(probe-host-user :properties (:user))
(probe-hostname :properties (:hostname)))
((op-host-locale :properties (:locale))
(op-host-packages :properties (:packages))
(op-hostname :properties (:hostname))))
(defgeneric probe-os-using-uname (host os))
(defgeneric probe-hostname (host os))
(defgeneric probe-boot-time (host os))
(defgeneric host-disconnect (host))
(defgeneric host-shell (host))
(defgeneric (setf host-shell) (shell host))
(defgeneric host-os (host))
(defgeneric host-run (host &rest command))
(defvar *host*)
;; User
(define-resource-class user (resource-container)
()
((probe-user :properties (:ensure :login :uid :gid :realname
:home :shell))
(probe-user-groups :properties (:groups)))
((op-update-user :properties (:ensure :uid :gid :realname :home :shell
:login-class :groups))))
;; Probing resources
(defgeneric find-probe (resource property os))
(defgeneric probe (resource property))
(defgeneric get-probed (resource property))
(defgeneric clear-probed% (resource properties))
(defgeneric describe-probed% (resource output))
(defgeneric describe-probed-property-value (resource property value))
(define-condition resource-probe-error (error)
((resource :initarg :resource)
(property :initarg :property)
(host :initarg :host
:initform (current-host))
(os :initarg :os)))
(define-condition resource-probe-not-found (resource-probe-error)
())
(define-condition resource-probe-failed (resource-probe-error)
((probe :initarg :probe)))
;; Operating on resources
(defgeneric find-operation (resource property os))
(defgeneric list-operations (resource plist os))
(defgeneric operate (resource plist))
(define-condition resource-operation-error (error)
((resource :initarg :resource)
(host :initarg :host
:initform (current-host))
(os :initarg :os)))
(define-condition resource-operation-not-found (resource-operation-error)
((property :initarg :property)))
(define-condition resource-operation-failed (resource-operation-error)
((operation :initarg :operation :type operation)
(diff :initarg :diff)))
;; Operators on property lists
(defmacro remf* (place indicator)
`(loop
(unless (remf ,place ,indicator)
(return))))
;; Shebang
(set-dispatch-macro-character #\# #\! #'read-line)