aboutsummaryrefslogtreecommitdiff
path: root/src/property/package.lisp
blob: 7244d5181e230693a4316a180c040e37d01b9184 (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
;;; 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.property.package)
(named-readtables:in-readtable :consfigurator)

(define-constant +consfigurator-system-dependencies+
    '(:apt ("build-essential" "libacl1-dev" "libcap-dev"))
  :test #'equal)

(defgeneric %command (package-manager)
  (:documentation
   "Returns a command which, if found on PATH, indicates that the system package
manager identified by PACKAGE-MANAGER is available."))

(defmethod %command ((package-manager (eql :apt)))
  "apt-get")

(defgeneric %installed (package-manager packages)
  (:documentation
   "Install each of PACKAGES using the system package manager identified by
PACKAGE-MANAGER.

Implementations should not fail just because we are not root, or otherwise
privileged, if the package is already installed."))

(defmethod %installed ((package-manager (eql :apt)) packages)
  ;; Call APPLY-PROPAPP directly because we want the :CHECK subroutine run,
  ;; but it does not make sense to run the :HOSTATTRS subroutine because
  ;; *HOST* does not necessarily correspond to the host we're attempting to
  ;; install packages on.
  (apply-propapp `(apt:installed ,@packages)))

(define-simple-error package-manager-not-found (aborted-change))

(defprop installed :posix
    (package-manager &rest package-lists &aux package-list)
  "Attempt to use a system package manager to install system packages as
specified by PACKAGE-LISTS.  If PACKAGE-MANAGER, a keyword, use that
particular package manager; otherwise, see what we can find on PATH.

Each of PACKAGE-LISTS is a plist where the keys identify package managers, and
where the values are lists of package names to install using that package
manager.  See PACKAGE:+CONSFIGURATOR-SYSTEM-DEPENDENCIES+ for an example.

This property should not typically be applied to hosts.  It is preferable to
use an operating system-specific property, such as APT:INSTALLED.  This
property exists because in a few cases it is necessary to install packages
where there is no known-valid HOST value for the machine upon which we need to
install packages, and thus we cannot infer what package manager to use from
the host's OS, and must fall back to seeing what's on PATH.

In particular, when starting up a remote Lisp image when the REMAINING
argument to ESTABLISH-CONNECTION is non-nil, we might be starting up Lisp on a
machine other than the one to be deployed and we do not have HOST values for
intermediate hops.  Another case is INSTALLED:CLEANLY-INSTALLED-ONCE;
regardless of REMAINING, the initial OS might be the one we will replace, not
the declared OS for the host."
  (:apply
   (dolist (list package-lists)
     (doplist (k v list)
       (dolist (p (ensure-cons v))
         (push p (getf package-list k)))))
   (loop with reversed
         for (k v) on package-list by #'cddr
         do (push v reversed) (push k reversed)
         finally (setq package-list reversed))
   (if package-manager
       (return-from installed
         (%installed package-manager (getf package-list package-manager)))
       (doplist (package-manager packages package-list)
         (when (remote-executable-find (%command package-manager))
           (return-from installed (%installed package-manager packages)))))
   (package-manager-not-found
    "Could not find any package manager on PATH with which to install ~S."
    package-list)))