aboutsummaryrefslogtreecommitdiff
path: root/src/property/os.lisp
blob: edb64c7d21b170c92a5f45183d6a66e1c07437da (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
;;; Consfigurator -- Lisp declarative configuration management system

;;; Copyright (C) 2021-2022  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.property.os)
(named-readtables:in-readtable :consfigurator)

;;;; Basic OS types

(defclass unixlike () ())

(defclass linux (unixlike) ())

(defprop linux :posix ()
  (:desc "Host kernel is Linux")
  (:hostattrs (push-hostattr :os (make-instance 'linux))))

(define-simple-print-object linux)

(defclass debianlike (linux) ())

(defclass debian (debianlike)
  ((architecture
    :initarg :arch :reader debian-architecture
    :documentation
    "Keyword whose name is Debian's name for this architecture, e.g. :AMD64")
   (suite :initarg :suite
          :reader debian-suite
          :initform (error "Must provide suite"))))

(define-simple-print-object debian)

(defclass debian-stable (debian) ())

(defprop debian-stable :posix (suite architecture)
  (:desc
   (declare (ignore architecture))
   #?{Host is Debian "${suite}"})
  (:hostattrs
   (push-hostattr :os
                  (make-instance 'debian-stable
                                 :arch architecture :suite suite))))

(defclass debian-testing (debian)
  ((suite :initform "testing")))

(defprop debian-testing :posix (architecture)
  (:desc
   (declare (ignore architecture))
   "Host is Debian testing")
  (:hostattrs
   (push-hostattr :os
                  (make-instance 'debian-testing
                                 :arch architecture))))

(defclass debian-unstable (debian)
  ((suite :initform "unstable")))

(defprop debian-unstable :posix (architecture)
  (:desc
   (declare (ignore architecture))
   "Host is Debian unstable")
  (:hostattrs
   (push-hostattr :os
                  (make-instance 'debian-unstable
                                 :arch architecture))))

(defclass debian-experimental (debian)
  ((suite :initform "experimental")))

(defmethod debian-architecture-string ((os debian))
  "Return a string representing the architecture of OS as used by Debian."
  (string-downcase (symbol-name (debian-architecture os))))


;;;; Property combinators

(defun typecase-type (cases)
  (combine-propapp-types (loop for pa in (cdr cases) by #'cddr collect pa)))

(defun typecase-host (host)
  (class-of (if host
                (get-hostattrs-car :os host)
                (get-hostattrs-car :os))))

(defun typecase-choose (host cases)
  (loop with os = (typecase-host host)
        for (type propapp) on cases by #'cddr
        when (subtypep os type) return propapp))

(define-choosing-property-combinator os-typecase* (host &rest cases)
  :type (typecase-type cases)
  :choose (typecase-choose host cases))

(define-choosing-property-combinator os-etypecase* (host &rest cases)
  :type (typecase-type cases)
  :choose
  (or (typecase-choose host cases)
      (inapplicable-property
       "Host's OS ~S fell through OS:ETYPECASE." (typecase-host host))))

(defmacro host-typecase* (macro host &body cases)
  `(,macro
    ,host
    ,@(loop for case in cases
            collect `',(intern (symbol-name (car case))
                               (find-package :consfigurator.property.os))
            collect (let ((forms (cdr case)))
                      (if (cdr forms) `(eseqprops ,@forms) (car forms))))))

(defmacro typecase (&body cases)
  `(host-typecase* os-typecase* nil ,@cases))

(defmacro etypecase (&body cases)
  `(host-typecase* os-etypecase* nil ,@cases))

(defmacro host-typecase (host &body cases)
  `(host-typecase* os-typecase* ,host ,@cases))

(defmacro host-etypecase (host &body cases)
  `(host-typecase* os-etypecase* ,host ,@cases))


;;;; Utilities

(defun required (type)
  "Error out if the OS of the host being deployed is not of type TYPE.

Used in property :HOSTATTRS subroutines."
  (let ((os (class-of (get-hostattrs-car :os))))
    (unless (and os (subtypep os type))
      (inapplicable-property #?"Property requires OS of type ${type}"))))

(defgeneric supports-arch-p (target-os binary-os)
  (:documentation "Can binaries for BINARY-OS run on TARGET-OS?"))

(defmethod supports-arch-p ((target-os debian) (binary-os debian))
  (let ((target (debian-architecture target-os))
        (binary (debian-architecture binary-os)))
    (or (eq target binary)
        (member binary (assoc target '((:amd64 :i386)
                                       (:i386  :amd64)))))))