Hash :
30fcd8ad
Author :
Thomas de Grivel
Date :
2023-02-17T12:40:28
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
;; 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)
(in-re-readtable)
(defun get-sh-var (name file)
(let (value)
(with-sh-var (var val) (egrep (str "^" name "=") file)
(when (string= name var)
(setq value val)))
value))
(defsetf get-sh-var (name file) (value)
`(let ((name (re-quote ,name))
(value (re-quote ,value))
(file (sh-quote ,file)))
(run "perl -pi -e " (sh-quote (str "s/^" name "=.*$/" name "=" value "/"))
file)))
(defmethod probe-hostname ((host host) (os os-freebsd))
(let ((hostname (run-1 "hostname"))
(rc-conf (get-sh-var "hostname" "/etc/rc.conf")))
(list :hostname (if (equal hostname rc-conf)
hostname
(list hostname :rc-conf rc-conf)))))
(defmethod op-hostname ((host host) (os os-freebsd) &key hostname)
(let* ((probed (get-probed host :hostname))
(probed-rc probed))
(when (consp probed)
(setq probed (first probed)
probed-rc (getf (rest probed-rc) :rc-conf probed)))
(unless (equal hostname probed)
(call-next-method))
(unless (equal hostname probed-rc)
(setf (get-sh-var "hostname" "/etc/rc.conf") hostname))))
(defmethod op-update-group ((group group) (os os-freebsd) &key ensure gid)
(run-as-root
(join-str " "
(ecase ensure
((:absent) "pw groupdel")
((:present) "pw groupadd")
((nil) "pw groupmod"))
(when gid `("-g" ,(sh-quote gid)))
"-n" (sh-quote (resource-id group)))))
(defmethod op-update-user ((user user) (os os-freebsd) &key ensure uid gid
realname home shell
login-class
groups)
(sync-groups)
(run-as-root
(join-str " "
(ecase ensure
((:absent) "pw userdel")
((:present) "pw useradd")
((nil) "pw usermod"))
(when realname `("-c" ,(sh-quote realname)))
(when home `("-d" ,(sh-quote home)))
(when gid `("-g" ,(sh-quote gid)))
(when login-class `("-L" ,(sh-quote login-class)))
(when groups `("-G" ,(join-str "," (mapcar #'sh-quote groups))))
(when shell `("-s" ,(sh-quote shell)))
(when uid `("-u" ,(sh-quote uid)))
"-n" (sh-quote (resource-id user)))))
;; Pkg
(defun freebsd-pkg-version<8>-status (str)
(cond ((equal "=" str) :latest)
((equal "<" str) :update-available)
((equal ">" str) :newer)
((equal "?" str) :not-in-index)
((equal "!" str) :error)
(:otherwise nil)))
(define-syntax freebsd-pkg-version<8> (name version
(#'freebsd-pkg-version<8>-status status))
#~|^([-_0-9A-Za-z]+)-([_.,0-9A-Za-z]+)\s+([=<>?!])$|)
(defmethod probe-host-packages ((host host) (os os-freebsd))
(let ((packages))
(with-freebsd-pkg-version<8> (name version ensure)
(run "pkg version")
(when (and name version ensure)
(let ((pkg (resource 'freebsd-pkg name))
(versions (list version)))
(add-probed-properties pkg (properties* versions ensure))
(push pkg packages))))
(properties :packages (nreverse packages))))
(define-resource-class freebsd-pkg (pkg)
()
((probe-freebsd-pkg :properties (:ensure :versions)))
((op-freebsd-pkg :properties (:ensure :versions))))
(defmethod probe-freebsd-pkg ((pkg freebsd-pkg) (os os-freebsd))
(let ((id (resource-id pkg))
(ensure :absent)
versions)
(with-freebsd-pkg-version<8> (name ver status)
(run "pkg version | egrep ^" (sh-quote id) "-")
(when (equal id name)
(setq ensure status)
(push ver versions)))
(properties* ensure versions)))
(defmethod op-freebsd-pkg ((pkg freebsd-pkg) (os os-freebsd)
&key ensure versions)
(let ((id (resource-id pkg))
(cmd (ecase ensure
((:absent) "pkg delete -y ")
((:present :latest) "pkg install -y "))))
(if versions
(dolist (ver versions)
(run-as-root cmd (sh-quote id "-" ver)))
(run-as-root cmd (sh-quote id)))))