aboutsummaryrefslogtreecommitdiff
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2019 David Thompson <davet@gnu.org>
;;; Copyright © 2019 Jakob L. Kreuze <zerodaysfordays@sdf.org>
;;;
;;; This file is part of GNU Guix.
;;;
;;; GNU Guix 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 of the License, or (at
;;; your option) any later version.
;;;
;;; GNU Guix 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 GNU Guix.  If not, see <http://www.gnu.org/licenses/>.

(define-module (gnu machine)
  #:use-module (gnu system)
  #:use-module (guix derivations)
  #:use-module (guix monads)
  #:use-module (guix records)
  #:use-module (guix store)
  #:use-module ((guix diagnostics) #:select (source-properties->location))
  #:use-module (srfi srfi-35)
  #:export (environment-type
            environment-type?
            environment-type-name
            environment-type-description
            environment-type-location

            machine
            machine?

            machine-operating-system
            machine-environment
            machine-configuration
            machine-display-name

            deploy-machine
            roll-back-machine
            machine-remote-eval

            &deploy-error
            deploy-error?
            deploy-error-should-roll-back
            deploy-error-captured-args))

;;; Commentary:
;;;
;;; This module provides the types used to declare individual machines in a
;;; heterogeneous Guix deployment. The interface allows users to specify system
;;; configurations and the means by which resources should be provisioned on a
;;; per-host basis.
;;;
;;; Code:


;;;
;;; Declarations for resources that can be provisioned.
;;;

(define-record-type* <environment-type> environment-type
  make-environment-type
  environment-type?

  ;; Interface to the environment type's deployment code. Each procedure
  ;; should take the same arguments as the top-level procedure of this file
  ;; that shares the same name. For example, 'machine-remote-eval' should be
  ;; of the form '(machine-remote-eval machine exp)'.
  (machine-remote-eval environment-type-machine-remote-eval) ; procedure
  (deploy-machine      environment-type-deploy-machine)      ; procedure
  (roll-back-machine   environment-type-roll-back-machine)   ; procedure

  ;; Metadata.
  (name        environment-type-name)       ; symbol
  (description environment-type-description ; string
               (default #f))
  (location    environment-type-location    ; <location>
               (default (and=> (current-source-location)
                               source-properties->location))
               (innate)))


;;;
;;; Declarations for machines in a deployment.
;;;

(define-record-type* <machine> machine make-machine
  machine?
  (operating-system %machine-operating-system); <operating-system>
  (environment      machine-environment)      ; symbol
  (configuration    machine-configuration     ; configuration object
                    (default #f)))            ; specific to environment

(define (machine-operating-system machine)
  "Return the operating system of MACHINE."
  (operating-system-with-provenance
   (%machine-operating-system machine)))

(define (machine-display-name machine)
  "Return the host-name identifying MACHINE."
  (operating-system-host-name (machine-operating-system machine)))

(define (machine-remote-eval machine exp)
  "Evaluate EXP, a gexp, on MACHINE. Ensure that all the elements EXP refers to
are built and deployed to MACHINE beforehand."
  (let ((environment (machine-environment machine)))
    ((environment-type-machine-remote-eval environment) machine exp)))

(define (deploy-machine machine)
  "Monadic procedure transferring the new system's OS closure to the remote
MACHINE, activating it on MACHINE and switching MACHINE to the new generation."
  (let ((environment (machine-environment machine)))
    ((environment-type-deploy-machine environment) machine)))

(define (roll-back-machine machine)
  "Monadic procedure rolling back to the previous system generation on
MACHINE. Return the number of the generation that was current before switching
and the new generation number."
  (let ((environment (machine-environment machine)))
    ((environment-type-roll-back-machine environment) machine)))


;;;
;;; Error types.
;;;

(define-condition-type &deploy-error &error
  deploy-error?
  (should-roll-back deploy-error-should-roll-back)
  (captured-args deploy-error-captured-args))
ialize-getmail-options-configuration field-name val) (serialize-configuration val getmail-options-configuration-fields)) (define-configuration getmail-options-configuration (verbose (non-negative-integer 1) "If set to @samp{0}, getmail will only print warnings and errors. A value of @samp{1} means that messages will be printed about retrieving and deleting messages. If set to @samp{2}, getmail will print messages about each of it's actions.") (read-all (boolean #t) "If true, getmail will retrieve all available messages. Otherwise it will only retrieve messages it hasn't seen previously.") (delete (boolean #f) "If set to true, messages will be deleted from the server after retrieving and successfully delivering them. Otherwise, messages will be left on the server.") (delete-after (non-negative-integer 0) "Getmail will delete messages this number of days after seeing them, if they have been delivered. This means messages will be left on the server this number of days after delivering them. A value of @samp{0} disabled this feature.") (delete-bigger-than (non-negative-integer 0) "Delete messages larger than this of bytes after retrieving them, even if the delete and delete-after options are disabled. A value of @samp{0} disables this feature.") (max-bytes-per-session (non-negative-integer 0) "Retrieve messages totalling up to this number of bytes before closing the session with the server. A value of @samp{0} disables this feature.") (max-message-size (non-negative-integer 0) "Don't retrieve messages larger than this number of bytes. A value of @samp{0} disables this feature.") (delivered-to (boolean #t) "If true, getmail will add a Delivered-To header to messages.") (received (boolean #t) "If set, getmail adds a Received header to the messages.") (message-log (string "") "Getmail will record a log of its actions to the named file. A value of @samp{\"\"} disables this feature.") (message-log-syslog (boolean #f) "If true, getmail will record a log of its actions using the system logger.") (message-log-verbose (boolean #f) "If true, getmail will log information about messages not retrieved and the reason for not retrieving them, as well as starting and ending information lines.") (extra-parameters (parameter-alist '()) "Extra options to include.")) (define-configuration getmail-configuration-file (retriever (getmail-retriever-configuration (getmail-retriever-configuration)) "What mail account to retrieve mail from, and how to access that account.") (destination (getmail-destination-configuration (getmail-destination-configuration)) "What to do with retrieved messages.") (options (getmail-options-configuration (getmail-options-configuration)) "Configure getmail.")) (define (serialize-getmail-configuration-file field-name val) (match-record val <getmail-configuration-file> (retriever destination options) #~(string-append "[retriever]\n" #$(serialize-getmail-retriever-configuration #f retriever) "\n[destination]\n" #$(serialize-getmail-destination-configuration #f destination) "\n[options]\n" #$(serialize-getmail-options-configuration #f options)))) (define (serialize-symbol field-name val) "") (define (serialize-getmail-configuration field-name val) "") (define-configuration getmail-configuration (name (symbol "unset") "A symbol to identify the getmail service.") (package (file-like getmail) "The getmail package to use.") (user (string "getmail") "The user to run getmail as.") (group (string "getmail") "The group to run getmail as.") (directory (string "/var/lib/getmail/default") "The getmail directory to use.") (rcfile (getmail-configuration-file (getmail-configuration-file)) "The getmail configuration file to use.") (idle (list '()) "A list of mailboxes that getmail should wait on the server for new mail notifications. This depends on the server supporting the IDLE extension.") (environment-variables (list '()) "Environment variables to set for getmail.")) (define (generate-getmail-documentation) (generate-documentation `((getmail-configuration ,getmail-configuration-fields (rcfile getmail-configuration-file)) (getmail-configuration-file ,getmail-configuration-file-fields (retriever getmail-retriever-configuration) (destination getmail-destination-configuration) (options getmail-options-configuration)) (getmail-retriever-configuration ,getmail-retriever-configuration-fields) (getmail-destination-configuration ,getmail-destination-configuration-fields) (getmail-options-configuration ,getmail-options-configuration-fields)) 'getmail-configuration)) (define-gexp-compiler (getmail-configuration-file-compiler (rcfile <getmail-configuration-file>) system target) (gexp->derivation "getmailrc" #~(call-with-output-file #$output (lambda (port) (display #$(serialize-getmail-configuration-file #f rcfile) port))) #:system system #:target target)) (define (getmail-accounts configs) (let ((users (delete-duplicates (map getmail-configuration-user configs))) (groups (delete-duplicates (map getmail-configuration-group configs)))) (append (map (lambda (group) (user-group (name group) (system? #t))) groups) (map (lambda (user) (user-account (name user) (group (getmail-configuration-group (find (lambda (config) (and (string=? user (getmail-configuration-user config)) (getmail-configuration-group config))) configs))) (system? #t) (comment "Getmail user") (home-directory "/var/empty") (shell (file-append shadow "/sbin/nologin")))) users)))) (define (getmail-activation configs) "Return the activation GEXP for CONFIGS." (with-imported-modules '((guix build utils)) #~(begin (use-modules (guix build utils)) #$@(map (lambda (config) #~(let* ((pw (getpw #$(getmail-configuration-user config))) (uid (passwd:uid pw)) (gid (passwd:gid pw)) (getmaildir #$(getmail-configuration-directory config))) (mkdir-p getmaildir) (chown getmaildir uid gid))) configs)))) (define (getmail-shepherd-services configs) "Return a list of <shepherd-service> for CONFIGS." (map (lambda (config) (match-record config <getmail-configuration> (name package user group directory rcfile idle environment-variables) (shepherd-service (documentation "Run getmail.") (provision (list (symbol-append 'getmail- name))) (requirement '(networking)) (start #~(make-forkexec-constructor `(#$(file-append package "/bin/getmail") ,(string-append "--getmaildir=" #$directory) #$@(map (lambda (idle) (string-append "--idle=" idle)) idle) ,(string-append "--rcfile=" #$rcfile)) #:user #$user #:group #$group #:environment-variables (list #$@environment-variables) #:log-file #$(string-append "/var/log/getmail-" (symbol->string name)))) (stop #~(make-kill-destructor))))) configs)) (define getmail-service-type (service-type (name 'getmail) (extensions (list (service-extension shepherd-root-service-type getmail-shepherd-services) (service-extension activation-service-type getmail-activation) (service-extension account-service-type getmail-accounts))) (description "Run @command{getmail}, a mail retriever program.") (default-value '()) (compose concatenate) (extend append)))