[PATCH 0/4] PostgreSQL service changes (add record type, and system test)

  • Done
  • quality assurance status badge
Details
2 participants
  • Clément Lassieur
  • Christopher Baines
Owner
unassigned
Submitted by
Christopher Baines
Severity
normal

Debbugs page

Christopher Baines wrote 7 years ago
(address . guix-patches@gnu.org)
87po4jpsgc.fsf@cbaines.net
I was mostly adding the system test, but also ended up reworking the
service so that Shepherd knows about the PID file.


Christopher Baines (4):
services: Rework the PostgreSQL config file to use a record type.
services: Use a external pid file for PostgreSQL.
tests: databases: Add a system test for PostgreSQL.
services: databases: Add postgresql-configuration record exports.

gnu/services/databases.scm | 125 ++++++++++++++++++++++++++++++++++++---------
gnu/tests/databases.scm | 59 +++++++++++++++++++++
2 files changed, 161 insertions(+), 23 deletions(-)
-----BEGIN PGP SIGNATURE-----

iQKTBAEBCgB9FiEEPonu50WOcg2XVOCyXiijOwuE9XcFAlqcRLNfFIAAAAAALgAo
aXNzdWVyLWZwckBub3RhdGlvbnMub3BlbnBncC5maWZ0aGhvcnNlbWFuLm5ldDNF
ODlFRUU3NDU4RTcyMEQ5NzU0RTBCMjVFMjhBMzNCMEI4NEY1NzcACgkQXiijOwuE
9Xdb4BAAp3eLXoLOwFu1/Re5AnXXe7d37jP4W2b8H5Qqz9mOfjvjBySw7C87LxMi
eFHEGGnI/H0nb2sPe5ij2CGSMEfhJdFqLtdvX7CwSOHR7UB2r6uEciz4B0ov/6+w
j8zBzAYmGhz+UtnqUz1IX4HUdp8Y+9yFdx/C9D9pvPDIFSRIGfsab4AdwU+VcZ+H
CT1KiSYZcgO9CWjLnX7BlLbuuCPxD7UpamW65YL0gIYdyculhwiZfDkQRvkyh1U+
cVLSJqH5Q9i17pSMiHcaRi1++T27M/uPbqmbHxEbez/a2hbmix3h9378a2XfVUtQ
pKjMy0DuOzcx0EE2Iodo7VgxnBuQ+PNldPUEbV4eLiUxXIZQMEjg/JwYvyLPfpB+
ZAwQi5oQ6aoiqXzyUaSoNYvnttfA/OmzXHpfCFJ2xACHphUNTeiYwsp+0lJEedvP
cwUXKrWK4u2anJK+dkgpC2N+YmG9mSjTl1rdmWe9/ZPqfUrFSY39VOPgS4wGVxkL
soGIPqByd7AAigZlOAGusFlBA14zVcNDhpcHlrf11t/meupO/FtarAB+t2QUHBnG
v+lM8JKZ9881Q541HmmEgEX+aQBQOljhYz0+3mo2cwIPZuCmk3h3KFdIiS16v3/u
cW8n9ie0+rFE0KU52TwTnOLoD+ZOXy1y3rd2/yiu/GnciSC72M0=
=R9oO
-----END PGP SIGNATURE-----

Christopher Baines wrote 7 years ago
[PATCH 3/4] tests: databases: Add a system test for PostgreSQL.
(address . 30701@debbugs.gnu.org)
20180304191633.20262-3-mail@cbaines.net
* gnu/tests/databases.scm (%postgresql-os, %test-postgresql): New variables.
(run-postgresql-test): New procedure.
---
gnu/tests/databases.scm | 59 +++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 59 insertions(+)

Toggle diff (79 lines)
diff --git a/gnu/tests/databases.scm b/gnu/tests/databases.scm
index e7097690a..0597105da 100644
--- a/gnu/tests/databases.scm
+++ b/gnu/tests/databases.scm
@@ -30,6 +30,7 @@
#:use-module (guix store)
#:export (%test-memcached
%test-mongodb
+ %test-postgresql
%test-mysql))
(define %memcached-os
@@ -208,6 +209,64 @@
(value (run-mongodb-test))))
+;;;
+;;; The PostgreSQL service.
+;;;
+
+(define %postgresql-os
+ (simple-operating-system
+ (service postgresql-service-type)))
+
+(define* (run-postgresql-test)
+ "Run tests in %POSTGRESQL-OS."
+ (define os
+ (marionette-operating-system
+ %postgresql-os
+ #:imported-modules '((gnu services herd)
+ (guix combinators))))
+
+ (define vm
+ (virtual-machine
+ (operating-system os)
+ (memory-size 512)))
+
+ (define test
+ (with-imported-modules '((gnu build marionette))
+ #~(begin
+ (use-modules (srfi srfi-11) (srfi srfi-64)
+ (gnu build marionette))
+
+ (define marionette
+ (make-marionette (list #$vm)))
+
+ (mkdir #$output)
+ (chdir #$output)
+
+ (test-begin "postgresql")
+
+ (test-assert "service running"
+ (marionette-eval
+ '(begin
+ (use-modules (gnu services herd))
+ (match (start-service 'postgres)
+ (#f #f)
+ (('service response-parts ...)
+ (match (assq-ref response-parts 'running)
+ ((pid) (number? pid))))))
+ marionette))
+
+ (test-end)
+ (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
+
+ (gexp->derivation "postgresql-test" test))
+
+(define %test-postgresql
+ (system-test
+ (name "postgresql")
+ (description "Start the PostgreSQL service.")
+ (value (run-postgresql-test))))
+
+
;;;
;;; The MySQL service.
;;;
--
2.16.0
Christopher Baines wrote 7 years ago
[PATCH 4/4] services: databases: Add postgresql-configuration record exports.
(address . 30701@debbugs.gnu.org)
20180304191633.20262-4-mail@cbaines.net
* gnu/services/databases.scm: Export the record type, and all the field
accessors.
---
gnu/services/databases.scm | 9 +++++++++
1 file changed, 9 insertions(+)

Toggle diff (23 lines)
diff --git a/gnu/services/databases.scm b/gnu/services/databases.scm
index 4090277a7..1e410cd39 100644
--- a/gnu/services/databases.scm
+++ b/gnu/services/databases.scm
@@ -43,7 +43,16 @@
postgresql-config-file-external-pid-file
postgresql-config-file-extra-config
+ <postgresql-configuration>
+ postgresql-configuration
postgresql-configuration?
+ postgresql-configuration-postgresql
+ postgresql-configuration-port
+ postgresql-configuration-locale
+ postgresql-configuration-pid-file
+ postgresql-configuration-file
+ postgresql-configuration-data-directory
+
postgresql-service
postgresql-service-type
--
2.16.0
Christopher Baines wrote 7 years ago
[PATCH 1/4] services: Rework the PostgreSQL config file to use a record type.
(address . 30701@debbugs.gnu.org)
20180304191633.20262-1-mail@cbaines.net
For the default config file representation. This makes it possible to more
easily change the configuration file, and have dynamic content. In particular,
I'm looking at adding a pid file location to the config file.

* gnu/services/databases.scm (<postgresql-config-file>): New record type.
(%default-postgres-config): Remove this, it's been replaced by the
configuration file.
(<postgresql-configuration>): Alter the default for the config file field.
(postgresql-service): Alter the default value for the config-file parameter.
---
gnu/services/databases.scm | 86 +++++++++++++++++++++++++++++++++++-----------
1 file changed, 66 insertions(+), 20 deletions(-)

Toggle diff (127 lines)
diff --git a/gnu/services/databases.scm b/gnu/services/databases.scm
index 3ca8f471f..9ffb6a5e9 100644
--- a/gnu/services/databases.scm
+++ b/gnu/services/databases.scm
@@ -26,11 +26,20 @@
#:use-module (gnu system shadow)
#:use-module (gnu packages admin)
#:use-module (gnu packages databases)
+ #:use-module (guix store)
#:use-module (guix modules)
#:use-module (guix records)
#:use-module (guix gexp)
+ #:use-module (srfi srfi-1)
#:use-module (ice-9 match)
- #:export (postgresql-configuration
+ #:export (<postgresql-config-file>
+ postgresql-config-file
+ postgresql-config-file?
+ postgresql-config-file-log-destination
+ postgresql-config-file-hba-file
+ postgresql-config-file-ident-file
+ postgresql-config-file-extra-config
+
postgresql-configuration?
postgresql-service
postgresql-service-type
@@ -68,6 +77,60 @@
;;;
;;; Code:
+(define %default-postgres-hba
+ (plain-file "pg_hba.conf"
+ "
+local all all trust
+host all all 127.0.0.1/32 trust
+host all all ::1/128 trust"))
+
+(define %default-postgres-ident
+ (plain-file "pg_ident.conf"
+ "# MAPNAME SYSTEM-USERNAME PG-USERNAME"))
+
+(define-record-type* <postgresql-config-file>
+ postgresql-config-file make-postgresql-config-file
+ postgresql-config-file?
+ (log-destination postgresql-config-file-log-destination
+ (default "syslog"))
+ (hba-file postgresql-config-file-hba-file
+ (default %default-postgres-hba))
+ (ident-file postgresql-config-file-ident-file
+ (default %default-postgres-ident))
+ (extra-config postgresql-config-file-extra-config
+ (default '())))
+
+(define-gexp-compiler (postgresql-config-file-compiler
+ (file <postgresql-config-file>) system target)
+ (match file
+ (($ <postgresql-config-file> log-destination hba-file
+ ident-file extra-config)
+ (define (quote string)
+ (if string
+ (list "'" string "'")
+ (list)))
+
+ (define contents
+ (append-map
+ (match-lambda
+ ((key) (list))
+ ((key . #f) (list))
+ ((key values ...) `(,key " = " ,@values "\n")))
+
+ `(("log_destination" ,@(quote log-destination))
+ ("hba_file" ,@(quote hba-file))
+ ("ident_file" ,@(quote ident-file))
+ ,@extra-config)))
+
+ (gexp->derivation
+ "postgresql.conf"
+ #~(call-with-output-file (ungexp output "out")
+ (lambda (port)
+ (display
+ (string-append #$@contents)
+ port)))
+ #:local-build? #t))))
+
(define-record-type* <postgresql-configuration>
postgresql-configuration make-postgresql-configuration
postgresql-configuration?
@@ -78,27 +141,10 @@
(locale postgresql-configuration-locale
(default "en_US.utf8"))
(config-file postgresql-configuration-file
- (default %default-postgres-config))
+ (default (postgresql-config-file)))
(data-directory postgresql-configuration-data-directory
(default "/var/lib/postgresql/data")))
-(define %default-postgres-hba
- (plain-file "pg_hba.conf"
- "
-local all all trust
-host all all 127.0.0.1/32 trust
-host all all ::1/128 trust"))
-
-(define %default-postgres-ident
- (plain-file "pg_ident.conf"
- "# MAPNAME SYSTEM-USERNAME PG-USERNAME"))
-
-(define %default-postgres-config
- (mixed-text-file "postgresql.conf"
- "log_destination = 'syslog'\n"
- "hba_file = '" %default-postgres-hba "'\n"
- "ident_file = '" %default-postgres-ident "'\n"))
-
(define %postgresql-accounts
(list (user-group (name "postgres") (system? #t))
(user-account
@@ -192,7 +238,7 @@ host all all ::1/128 trust"))
(define* (postgresql-service #:key (postgresql postgresql)
(port 5432)
(locale "en_US.utf8")
- (config-file %default-postgres-config)
+ (config-file (postgresql-config-file))
(data-directory "/var/lib/postgresql/data"))
"Return a service that runs @var{postgresql}, the PostgreSQL database server.
--
2.16.0
Christopher Baines wrote 7 years ago
[PATCH 2/4] services: Use a external pid file for PostgreSQL.
(address . 30701@debbugs.gnu.org)
20180304191633.20262-2-mail@cbaines.net
* gnu/services/databases.scm (postgresql-pid-file-for-version): New procedure.
(<postgresql-config-file>): Add a new external-pid-file field.
(postgresql-config-file-compiler): Add support for the external-pid-file.
(postgresql-activation): Create the directory for the pid file.
(postgresql-shepherd-service): Use the pid-file when starting the service.
---
gnu/services/databases.scm | 48 ++++++++++++++++++++++++++++++++++------------
1 file changed, 36 insertions(+), 12 deletions(-)

Toggle diff (129 lines)
diff --git a/gnu/services/databases.scm b/gnu/services/databases.scm
index 9ffb6a5e9..4090277a7 100644
--- a/gnu/services/databases.scm
+++ b/gnu/services/databases.scm
@@ -26,10 +26,12 @@
#:use-module (gnu system shadow)
#:use-module (gnu packages admin)
#:use-module (gnu packages databases)
+ #:use-module (guix packages)
#:use-module (guix store)
#:use-module (guix modules)
#:use-module (guix records)
#:use-module (guix gexp)
+ #:use-module (guix utils)
#:use-module (srfi srfi-1)
#:use-module (ice-9 match)
#:export (<postgresql-config-file>
@@ -38,6 +40,7 @@
postgresql-config-file-log-destination
postgresql-config-file-hba-file
postgresql-config-file-ident-file
+ postgresql-config-file-external-pid-file
postgresql-config-file-extra-config
postgresql-configuration?
@@ -88,23 +91,32 @@ host all all ::1/128 trust"))
(plain-file "pg_ident.conf"
"# MAPNAME SYSTEM-USERNAME PG-USERNAME"))
+(define (postgresql-pid-file-for-version version)
+ (string-append "/var/run/postgresql/"
+ (version-major+minor version)
+ "-main.pid"))
+
(define-record-type* <postgresql-config-file>
postgresql-config-file make-postgresql-config-file
postgresql-config-file?
- (log-destination postgresql-config-file-log-destination
- (default "syslog"))
- (hba-file postgresql-config-file-hba-file
- (default %default-postgres-hba))
- (ident-file postgresql-config-file-ident-file
- (default %default-postgres-ident))
- (extra-config postgresql-config-file-extra-config
- (default '())))
+ (log-destination postgresql-config-file-log-destination
+ (default "syslog"))
+ (hba-file postgresql-config-file-hba-file
+ (default %default-postgres-hba))
+ (ident-file postgresql-config-file-ident-file
+ (default %default-postgres-ident))
+ (external-pid-file postgresql-config-file-external-pid-file
+ (default (postgresql-pid-file-for-version
+ (package-version postgresql))))
+ (extra-config postgresql-config-file-extra-config
+ (default '())))
(define-gexp-compiler (postgresql-config-file-compiler
(file <postgresql-config-file>) system target)
(match file
(($ <postgresql-config-file> log-destination hba-file
- ident-file extra-config)
+ ident-file external-pid-file
+ extra-config)
(define (quote string)
(if string
(list "'" string "'")
@@ -120,6 +132,7 @@ host all all ::1/128 trust"))
`(("log_destination" ,@(quote log-destination))
("hba_file" ,@(quote hba-file))
("ident_file" ,@(quote ident-file))
+ ("external_pid_file" ,@(quote external-pid-file))
,@extra-config)))
(gexp->derivation
@@ -140,6 +153,9 @@ host all all ::1/128 trust"))
(default 5432))
(locale postgresql-configuration-locale
(default "en_US.utf8"))
+ (pid-file postgresql-configuration-pid-file
+ (default (postgresql-pid-file-for-version
+ (package-version postgresql))))
(config-file postgresql-configuration-file
(default (postgresql-config-file)))
(data-directory postgresql-configuration-data-directory
@@ -157,7 +173,8 @@ host all all ::1/128 trust"))
(define postgresql-activation
(match-lambda
- (($ <postgresql-configuration> postgresql port locale config-file data-directory)
+ (($ <postgresql-configuration> postgresql port locale pid-file
+ config-file data-directory)
#~(begin
(use-modules (guix build utils)
(ice-9 match))
@@ -173,6 +190,10 @@ host all all ::1/128 trust"))
(mkdir-p #$data-directory)
(chown #$data-directory (passwd:uid user) (passwd:gid user))
+ ;; Create a directory for the pid file
+ (mkdir-p #$(dirname pid-file))
+ (chown #$(dirname pid-file) (passwd:uid user) (passwd:gid user))
+
;; Drop privileges and init state directory in a new
;; process. Wait for it to finish before proceeding.
(match (primitive-fork)
@@ -195,7 +216,8 @@ host all all ::1/128 trust"))
(define postgresql-shepherd-service
(match-lambda
- (($ <postgresql-configuration> postgresql port locale config-file data-directory)
+ (($ <postgresql-configuration> postgresql port locale pid-file
+ config-file data-directory)
(let* ((pg_ctl-wrapper
;; Wrapper script that switches to the 'postgres' user before
;; launching daemon.
@@ -221,7 +243,9 @@ host all all ::1/128 trust"))
(provision '(postgres))
(documentation "Run the PostgreSQL daemon.")
(requirement '(user-processes loopback syslogd))
- (start (action "start"))
+ (start #~(make-forkexec-constructor
+ '(#$pg_ctl-wrapper "start")
+ #:pid-file #$pid-file))
(stop (action "stop"))))))))
(define postgresql-service-type
--
2.16.0
Clément Lassieur wrote 7 years ago
Re: [bug#30701] [PATCH 1/4] services: Rework the PostgreSQL config file to use a record type.
(name . Christopher Baines)(address . mail@cbaines.net)(address . 30701@debbugs.gnu.org)
87lgf7z7jp.fsf@lassieur.org
Hi Christopher,

Christopher Baines <mail@cbaines.net> writes:

Toggle quote (13 lines)
> For the default config file representation. This makes it possible to more
> easily change the configuration file, and have dynamic content. In particular,
> I'm looking at adding a pid file location to the config file.
>
> * gnu/services/databases.scm (<postgresql-config-file>): New record type.
> (%default-postgres-config): Remove this, it's been replaced by the
> configuration file.
> (<postgresql-configuration>): Alter the default for the config file field.
> (postgresql-service): Alter the default value for the config-file parameter.
> ---
> gnu/services/databases.scm | 86 +++++++++++++++++++++++++++++++++++-----------
> 1 file changed, 66 insertions(+), 20 deletions(-)

Thank you for this work!

Toggle quote (10 lines)
> +(define-gexp-compiler (postgresql-config-file-compiler
> + (file <postgresql-config-file>) system target)
> + (match file
> + (($ <postgresql-config-file> log-destination hba-file
> + ident-file extra-config)
> + (define (quote string)
> + (if string
> + (list "'" string "'")
> + (list)))

I don't think it's a good thing to hide one of the most important lisp
functions :-).
Clément Lassieur wrote 7 years ago
Re: [bug#30701] [PATCH 2/4] services: Use a external pid file for PostgreSQL.
(name . Christopher Baines)(address . mail@cbaines.net)(address . 30701@debbugs.gnu.org)
87k1urz7jh.fsf@lassieur.org
Christopher Baines <mail@cbaines.net> writes:

Toggle quote (9 lines)
> * gnu/services/databases.scm (postgresql-pid-file-for-version): New procedure.
> (<postgresql-config-file>): Add a new external-pid-file field.
> (postgresql-config-file-compiler): Add support for the external-pid-file.
> (postgresql-activation): Create the directory for the pid file.
> (postgresql-shepherd-service): Use the pid-file when starting the service.
> ---
> gnu/services/databases.scm | 48 ++++++++++++++++++++++++++++++++++------------
> 1 file changed, 36 insertions(+), 12 deletions(-)

...

Toggle quote (26 lines)
> +(define (postgresql-pid-file-for-version version)
> + (string-append "/var/run/postgresql/"
> + (version-major+minor version)
> + "-main.pid"))
> +
> (define-record-type* <postgresql-config-file>
> postgresql-config-file make-postgresql-config-file
> postgresql-config-file?
> - (log-destination postgresql-config-file-log-destination
> - (default "syslog"))
> - (hba-file postgresql-config-file-hba-file
> - (default %default-postgres-hba))
> - (ident-file postgresql-config-file-ident-file
> - (default %default-postgres-ident))
> - (extra-config postgresql-config-file-extra-config
> - (default '())))
> + (log-destination postgresql-config-file-log-destination
> + (default "syslog"))
> + (hba-file postgresql-config-file-hba-file
> + (default %default-postgres-hba))
> + (ident-file postgresql-config-file-ident-file
> + (default %default-postgres-ident))
> + (external-pid-file postgresql-config-file-external-pid-file
> + (default (postgresql-pid-file-for-version
> + (package-version postgresql))))

Why does external-pid-file have a default value. It's optional, and the
user already has access to $DATA/postmaster.pid anyway.

Toggle quote (8 lines)
> @@ -140,6 +153,9 @@ host all all ::1/128 trust"))
> (default 5432))
> (locale postgresql-configuration-locale
> (default "en_US.utf8"))
> + (pid-file postgresql-configuration-pid-file
> + (default (postgresql-pid-file-for-version
> + (package-version postgresql))))

The main PID file is chosen by Postgres, and put at
$DATA/postmaster.pid. I don't think it's customizable. This setting
(pid-file) probably doesn't affect the daemon the way you think it does.

Toggle quote (9 lines)
> (config-file postgresql-configuration-file
> (default (postgresql-config-file)))
> (data-directory postgresql-configuration-data-directory
> @@ -157,7 +173,8 @@ host all all ::1/128 trust"))
>
> + ;; Create a directory for the pid file
> + (mkdir-p #$(dirname pid-file))
> + (chown #$(dirname pid-file) (passwd:uid user) (passwd:gid user))

I think it would make more sense to create the directory for the
external-pid-file.

Toggle quote (18 lines)
> (define postgresql-shepherd-service
> (match-lambda
> - (($ <postgresql-configuration> postgresql port locale config-file data-directory)
> + (($ <postgresql-configuration> postgresql port locale pid-file
> + config-file data-directory)
> (let* ((pg_ctl-wrapper
> ;; Wrapper script that switches to the 'postgres' user before
> ;; launching daemon.
> @@ -221,7 +243,9 @@ host all all ::1/128 trust"))
> (provision '(postgres))
> (documentation "Run the PostgreSQL daemon.")
> (requirement '(user-processes loopback syslogd))
> - (start (action "start"))
> + (start #~(make-forkexec-constructor
> + '(#$pg_ctl-wrapper "start")
> + #:pid-file #$pid-file))
> (stop (action "stop"))))))))

If pid-file != external-pid-file, Sherpherd will wait for a file that
doesn't exist won't it? Because Postgresql will never be aware of that
pid-file.

Plus, there is no reason to use make-forkexec-constructor on pg_ctl
because pg_ctl returns after it has checked that the daemon is running.
For the same reason, Shepherd doesn't need to know about Postgres' PID.
All the hard work is done by pg_ctl.
Clément Lassieur wrote 7 years ago
Re: [bug#30701] [PATCH 3/4] tests: databases: Add a system test for PostgreSQL.
(name . Christopher Baines)(address . mail@cbaines.net)(address . 30701@debbugs.gnu.org)
87inabz7j3.fsf@lassieur.org
Christopher Baines <mail@cbaines.net> writes:

Toggle quote (5 lines)
> * gnu/tests/databases.scm (%postgresql-os, %test-postgresql): New variables.
> (run-postgresql-test): New procedure.
> ---
> gnu/tests/databases.scm | 59 +++++++++++++++++++++++++++++++++++++++++++++++++
> 1 file changed, 59 insertions(+)
...

Toggle quote (11 lines)
> + (test-assert "service running"
> + (marionette-eval
> + '(begin
> + (use-modules (gnu services herd))
> + (match (start-service 'postgres)
> + (#f #f)
> + (('service response-parts ...)
> + (match (assq-ref response-parts 'running)
> + ((pid) (number? pid))))))
> + marionette))

I don't understand the point of the PID check here. pg_ctl will ensure
that the daemon has started (by checking its PID), so I don't think
there is any need to redo its work. I guess the PID you'll get here is
the one of pg_ctl, which is probably not what you want.

I believe that (start-service 'postgres) returning true means pg_ctl
succeeded in its check that the daemon is running. So this is probably
enough:

(test-assert "service running"
(marionette-eval
'(begin
(use-modules (gnu services herd))
(start-service 'postgres))
marionette))

Clément
Christopher Baines wrote 7 years ago
Re: [bug#30701] [PATCH 1/4] services: Rework the PostgreSQL config file to use a record type.
(name . Clément Lassieur)(address . clement@lassieur.org)(address . 30701@debbugs.gnu.org)
87lgf7ot6u.fsf@cbaines.net
Clément Lassieur <clement@lassieur.org> writes:

Toggle quote (19 lines)
> Hi Christopher,
>
> Christopher Baines <mail@cbaines.net> writes:
>
>> For the default config file representation. This makes it possible to more
>> easily change the configuration file, and have dynamic content. In particular,
>> I'm looking at adding a pid file location to the config file.
>>
>> * gnu/services/databases.scm (<postgresql-config-file>): New record type.
>> (%default-postgres-config): Remove this, it's been replaced by the
>> configuration file.
>> (<postgresql-configuration>): Alter the default for the config file field.
>> (postgresql-service): Alter the default value for the config-file parameter.
>> ---
>> gnu/services/databases.scm | 86 +++++++++++++++++++++++++++++++++++-----------
>> 1 file changed, 66 insertions(+), 20 deletions(-)
>
> Thank you for this work!

No problem, I've finally got around to going through some patches I've
had sitting around for a while.

Toggle quote (13 lines)
>> +(define-gexp-compiler (postgresql-config-file-compiler
>> + (file <postgresql-config-file>) system target)
>> + (match file
>> + (($ <postgresql-config-file> log-destination hba-file
>> + ident-file extra-config)
>> + (define (quote string)
>> + (if string
>> + (list "'" string "'")
>> + (list)))
>
> I don't think it's a good thing to hide one of the most important lisp
> functions :-).

I don't quite follow. I was trying to use '() rather than (list) if that
is what you mean, but I kept getting odd errors from Guile, so I gave
up, and ended up going with this.
-----BEGIN PGP SIGNATURE-----

iQKTBAEBCgB9FiEEPonu50WOcg2XVOCyXiijOwuE9XcFAlqc9zlfFIAAAAAALgAo
aXNzdWVyLWZwckBub3RhdGlvbnMub3BlbnBncC5maWZ0aGhvcnNlbWFuLm5ldDNF
ODlFRUU3NDU4RTcyMEQ5NzU0RTBCMjVFMjhBMzNCMEI4NEY1NzcACgkQXiijOwuE
9XdCFg/+IVM1Nsib8MPKz7604WL6PJWBQ4MKcmYjDtcPz9gCSwTQLbRqTSxH3VDK
zBbs1P5fBR65Flspnh4q5A4FF30QNxsbqajUkw228TJzkchY3HB7jeTirzlmpuu+
I47emiTD9g/MKgs4QtzvTX/3X59aOcOrEbqmmGimQvANBfdgyhchlcMTQPRXyMz+
V6r3HhAxybTDoyN7A9JS9k17/A4MI3+6fh25NPJ7e6cTN1IewmzTWz46fdiOf/cl
ondkwe2ZCKj2wrsHXgywiQck9skqgnThft+WxFJzbHzloDKHfhaiqqeXWO4PTJnL
frImSvkvApZFYV5J67xpe4fHKVE5gRVYl6Ph1QePxMRbhPxJyQJEGQ7Uw4pV/99o
fe9yZGEB8QRa7pXKh2sVMfTzlQr6bcVkpn/jT2+FxvS+9w1/496D2RzcWpifyldn
WruoNpFvQzxPc+iXZ2idFGpD3mQL/YEqbymFXHnmDMXVW7ZoSAeUFf6MjCaCdvZt
ow4PJvqUsgwmdgGVGVs12iA6qASQ7YAcuyEr+892D9AfstWXuNWUzji1I1hbxCcK
i3GrNK3JCkT0gA1DhHJLVp+9fSbT0QcN5a8Frt3t0bz90Hb6WSNwhq3KhB2ajL48
w3SqnvU5dnoD+2rdHWqqdUNRDf60ID9attjSlJBhT24uTFCmjK8=
=MzZY
-----END PGP SIGNATURE-----

Christopher Baines wrote 7 years ago
Re: [bug#30701] [PATCH 2/4] services: Use a external pid file for PostgreSQL.
(name . Clément Lassieur)(address . clement@lassieur.org)(address . 30701@debbugs.gnu.org)
87k1uroru8.fsf@cbaines.net
Clément Lassieur <clement@lassieur.org> writes:

Toggle quote (42 lines)
> Christopher Baines <mail@cbaines.net> writes:
>
>> * gnu/services/databases.scm (postgresql-pid-file-for-version): New procedure.
>> (<postgresql-config-file>): Add a new external-pid-file field.
>> (postgresql-config-file-compiler): Add support for the external-pid-file.
>> (postgresql-activation): Create the directory for the pid file.
>> (postgresql-shepherd-service): Use the pid-file when starting the service.
>> ---
>> gnu/services/databases.scm | 48 ++++++++++++++++++++++++++++++++++------------
>> 1 file changed, 36 insertions(+), 12 deletions(-)
>
> ...
>
>> +(define (postgresql-pid-file-for-version version)
>> + (string-append "/var/run/postgresql/"
>> + (version-major+minor version)
>> + "-main.pid"))
>> +
>> (define-record-type* <postgresql-config-file>
>> postgresql-config-file make-postgresql-config-file
>> postgresql-config-file?
>> - (log-destination postgresql-config-file-log-destination
>> - (default "syslog"))
>> - (hba-file postgresql-config-file-hba-file
>> - (default %default-postgres-hba))
>> - (ident-file postgresql-config-file-ident-file
>> - (default %default-postgres-ident))
>> - (extra-config postgresql-config-file-extra-config
>> - (default '())))
>> + (log-destination postgresql-config-file-log-destination
>> + (default "syslog"))
>> + (hba-file postgresql-config-file-hba-file
>> + (default %default-postgres-hba))
>> + (ident-file postgresql-config-file-ident-file
>> + (default %default-postgres-ident))
>> + (external-pid-file postgresql-config-file-external-pid-file
>> + (default (postgresql-pid-file-for-version
>> + (package-version postgresql))))
>
> Why does external-pid-file have a default value. It's optional, and the
> user already has access to $DATA/postmaster.pid anyway.

I ended up adding this as I was writing the system test. I was coping
previous tests that I have written, in which I've been checking that the
shepherd gets the PID back when it starts the process.

Before I set out in writing this, I didn't realise that PostgreSQL
stores the PID in the data directory by default, or that pg_ctl waits
for actions to complete by default either.

I'm not particularly attached to this patch, I guess it mostly offers
consistency with other services.

Toggle quote (12 lines)
>> @@ -140,6 +153,9 @@ host all all ::1/128 trust"))
>> (default 5432))
>> (locale postgresql-configuration-locale
>> (default "en_US.utf8"))
>> + (pid-file postgresql-configuration-pid-file
>> + (default (postgresql-pid-file-for-version
>> + (package-version postgresql))))
>
> The main PID file is chosen by Postgres, and put at
> $DATA/postmaster.pid. I don't think it's customizable. This setting
> (pid-file) probably doesn't affect the daemon the way you think it does.

This part of the configuration is just meant to be where the Guix part
of the service expects to find the pid-file (if one is used, and it's
not #f).

Toggle quote (12 lines)
>> (config-file postgresql-configuration-file
>> (default (postgresql-config-file)))
>> (data-directory postgresql-configuration-data-directory
>> @@ -157,7 +173,8 @@ host all all ::1/128 trust"))
>>
>> + ;; Create a directory for the pid file
>> + (mkdir-p #$(dirname pid-file))
>> + (chown #$(dirname pid-file) (passwd:uid user) (passwd:gid user))
>
> I think it would make more sense to create the directory for the
> external-pid-file.

As far as I understand it, this is what this does.

Toggle quote (22 lines)
>> (define postgresql-shepherd-service
>> (match-lambda
>> - (($ <postgresql-configuration> postgresql port locale config-file data-directory)
>> + (($ <postgresql-configuration> postgresql port locale pid-file
>> + config-file data-directory)
>> (let* ((pg_ctl-wrapper
>> ;; Wrapper script that switches to the 'postgres' user before
>> ;; launching daemon.
>> @@ -221,7 +243,9 @@ host all all ::1/128 trust"))
>> (provision '(postgres))
>> (documentation "Run the PostgreSQL daemon.")
>> (requirement '(user-processes loopback syslogd))
>> - (start (action "start"))
>> + (start #~(make-forkexec-constructor
>> + '(#$pg_ctl-wrapper "start")
>> + #:pid-file #$pid-file))
>> (stop (action "stop"))))))))
>
> If pid-file != external-pid-file, Sherpherd will wait for a file that
> doesn't exist won't it? Because Postgresql will never be aware of that
> pid-file.

Yep.

Toggle quote (5 lines)
> Plus, there is no reason to use make-forkexec-constructor on pg_ctl
> because pg_ctl returns after it has checked that the daemon is running.
> For the same reason, Shepherd doesn't need to know about Postgres' PID.
> All the hard work is done by pg_ctl.

As the comment I made at the top, I did this when I was writing the
system test. If you remove this patch, when you call (start-service
'postgres), it will return #f if the service starts successfully. If you
tweak the service to make it fail to start (e.g. by changing the "start"
action to something else), you get the same observable behaviour,
start-service returns #f.

The way this works for other services, normally through
make-forkexec-constructor is that calling start-service will return the
PID.

While the system test does still add some value even without checking if
the service has started, doing so would be really good. Even if it's not
using the PID file approach, maybe the exit code of pg_ctl could be
used? I'm not really sure why it isn't working like that already, as
invoke usually returns either #t or #f...
-----BEGIN PGP SIGNATURE-----

iQKTBAEBCgB9FiEEPonu50WOcg2XVOCyXiijOwuE9XcFAlqc/g9fFIAAAAAALgAo
aXNzdWVyLWZwckBub3RhdGlvbnMub3BlbnBncC5maWZ0aGhvcnNlbWFuLm5ldDNF
ODlFRUU3NDU4RTcyMEQ5NzU0RTBCMjVFMjhBMzNCMEI4NEY1NzcACgkQXiijOwuE
9Xer6RAAlKPJ/qS8FoAqVWPl3DwNw7jcF0N9Jgj0FgWWvMNJJCqoeWamnAzSCNel
zYtKQm7eAmYTnRR+fk9RPA7nCgKL55OkQus5bTu4VzKY6pEnRyDWHU/q3cUX9o4X
lHbiQBkzCIfuK+cHWd78+izdNhCJXf5/ik7L9hy9QiBJGQ6NnyRZi+rgvYQdYxGw
v3tXzgY1vVKMH9LJX4durscykvuQSM9PucZk77ckYipZ/kPaBLyplfTkuRfvK6ld
3kbMbYoQCUyz5JCzLqmB/shNVT8Gt5EEYjSWaLkNt4k/VpPrO6sXiHgtXD8b+M70
F/8P6/M72nOpbRKjUfVFZ9RJ5ghXzkxhOlBVAveLdqQy+mTVR29B+uzb9NKxnGoF
AUPlxi59P+KiHA9mBcNW4rpgwX7Y6lVuzw2wc8qTeUcxV+Fsf8nYtKJeo9tqw7o/
iPYZsVStBwDBqnKh7YnXTWmzeGx+KGp2vJ1PrmYzPPqg8HKmSrfkgK+luCm9twRN
i3VIU8UamAPHFWlOVFnkmecOYamGR7D4ms74EahWMUT2cg+uK0BgjlHm5H4Pvu2C
wHLyxXdVexj2Q4FrSX5I3gaa81GYnsdHyaPWUb4YrxpoGGbQOYjxUNhGaJoNOpbn
j9JQg9vY2pTaMEIRm8aJWsDfvKDGITe/S79fCJCqPn30QzdS328=
=3sSe
-----END PGP SIGNATURE-----

Christopher Baines wrote 7 years ago
(name . Clément Lassieur)(address . clement@lassieur.org)(address . 30701@debbugs.gnu.org)
87inaborjo.fsf@cbaines.net
Christopher Baines <mail@cbaines.net> writes:

Toggle quote (24 lines)
> Clément Lassieur <clement@lassieur.org> writes:
>
>> Plus, there is no reason to use make-forkexec-constructor on pg_ctl
>> because pg_ctl returns after it has checked that the daemon is running.
>> For the same reason, Shepherd doesn't need to know about Postgres' PID.
>> All the hard work is done by pg_ctl.
>
> As the comment I made at the top, I did this when I was writing the
> system test. If you remove this patch, when you call (start-service
> 'postgres), it will return #f if the service starts successfully. If you
> tweak the service to make it fail to start (e.g. by changing the "start"
> action to something else), you get the same observable behaviour,
> start-service returns #f.
>
> The way this works for other services, normally through
> make-forkexec-constructor is that calling start-service will return the
> PID.
>
> While the system test does still add some value even without checking if
> the service has started, doing so would be really good. Even if it's not
> using the PID file approach, maybe the exit code of pg_ctl could be
> used? I'm not really sure why it isn't working like that already, as
> invoke usually returns either #t or #f...

Ah, I've just realised why this is the case, I was misreading the system
test results, it does actually return #t/#f, but as the system test was
expecting a number, it just returns #f to indicate failure.
-----BEGIN PGP SIGNATURE-----

iQKTBAEBCgB9FiEEPonu50WOcg2XVOCyXiijOwuE9XcFAlqc/4tfFIAAAAAALgAo
aXNzdWVyLWZwckBub3RhdGlvbnMub3BlbnBncC5maWZ0aGhvcnNlbWFuLm5ldDNF
ODlFRUU3NDU4RTcyMEQ5NzU0RTBCMjVFMjhBMzNCMEI4NEY1NzcACgkQXiijOwuE
9Xf4hQ//aiNn3T2pacYGLKmT0rHiiIC3WJvaChs3H2p9D1eSg4FowDV26IsJfmT/
stjmgGcqIQ3cv6tyycfXcYQSxOhJsw5NXN+rHDXOXfpHwb/RqxBjgXk79OWwZ3Tr
BbJ+0vzzx8cY+bePbtNenhHNiLDs9b+oPoqEiCvOq/We8zz3/9EmJqx3CvztGhc6
7ps9iFZ9lsWLrf5tk/2q2ff+ogXKVfTVF72wZdnFLGPshA60WduT4kDZEZp7qVPN
Mzo4ersRaF8rlOH3nUMdqzK1kFVMt65T5NQx23pbv3mSFxz5zmk5NP/OjkNsmnM8
DRKSeT3BkpTYFHt9fZCQjnZ4n/R2Pdb25t3FVlF2Hls/9PdHCK6ar62oRANAeHwd
tuG5c2ZQO7QGV7o8gaF5nuW2ZtlP+ijljCmZ6wEQxymAP7ojeRYlsFojCqv6uYMw
P7nc2OedYRaUHfP6/BBkRn0FktOrh8DuVjHvAzbzGXx6Y+V6nN3m2qzhk7+gDFqg
SuMfMSoXVlOMpSTFLYt30vTN+2VmnQq/ByKThqPYYMMWcAB1p0Y/QIcDgwHOmtab
OKPrdlHhI3d7iE31JXyJk5hWfwxvXN5jD4Vri8W75EkTXaujNoK7JMoW5hqKYj5W
Xv/kafDaMY6En/e7jqFULYtWxKa/S/jcRCUOvoEiF49NxogWqH0=
=50nr
-----END PGP SIGNATURE-----

Christopher Baines wrote 7 years ago
Re: [bug#30701] [PATCH 3/4] tests: databases: Add a system test for PostgreSQL.
(name . Clément Lassieur)(address . clement@lassieur.org)(address . 30701@debbugs.gnu.org)
87h8puq5t2.fsf@cbaines.net
Clément Lassieur <clement@lassieur.org> writes:

Toggle quote (25 lines)
> Christopher Baines <mail@cbaines.net> writes:
>
>> * gnu/tests/databases.scm (%postgresql-os, %test-postgresql): New variables.
>> (run-postgresql-test): New procedure.
>> ---
>> gnu/tests/databases.scm | 59 +++++++++++++++++++++++++++++++++++++++++++++++++
>> 1 file changed, 59 insertions(+)
> ...
>
>> + (test-assert "service running"
>> + (marionette-eval
>> + '(begin
>> + (use-modules (gnu services herd))
>> + (match (start-service 'postgres)
>> + (#f #f)
>> + (('service response-parts ...)
>> + (match (assq-ref response-parts 'running)
>> + ((pid) (number? pid))))))
>> + marionette))
>
> I don't understand the point of the PID check here. pg_ctl will ensure
> that the daemon has started (by checking its PID), so I don't think
> there is any need to redo its work. I guess the PID you'll get here is
> the one of pg_ctl, which is probably not what you want.

Because of make-forkexec-constructor, it is the main PID as set in the
external pid file created by PostgreSQL.

Toggle quote (11 lines)
> I believe that (start-service 'postgres) returning true means pg_ctl
> succeeded in its check that the daemon is running. So this is probably
> enough:
>
> (test-assert "service running"
> (marionette-eval
> '(begin
> (use-modules (gnu services herd))
> (start-service 'postgres))
> marionette))

Sure, I'm happy with this. I'll send some new patches soonish.
-----BEGIN PGP SIGNATURE-----

iQKTBAEBCgB9FiEEPonu50WOcg2XVOCyXiijOwuE9XcFAlqdARlfFIAAAAAALgAo
aXNzdWVyLWZwckBub3RhdGlvbnMub3BlbnBncC5maWZ0aGhvcnNlbWFuLm5ldDNF
ODlFRUU3NDU4RTcyMEQ5NzU0RTBCMjVFMjhBMzNCMEI4NEY1NzcACgkQXiijOwuE
9XdJsBAAoefkU/Hl1UwsPHIDFqQWfs/pHbhbu4X1BMrYcYx78GWwR3bCMaUd92dp
ctQ3NT/RruWFmj3mfGxhAwTaIuBoe9Z5vnlafMnB3AJ0M+lgCmRtt/UYPQv9MGCR
6qwfkquMqEYxf9R4mhcTkOpt4rFGkQAzSeOiui9KEInbVe3tS9LcFUqL75FsI1rT
H7KsVGF8mi8SD1oyZLBBsY+Z6fNOYwAFk+8NrRVZWxofyZKe/Y8qQueIq6oLJb5e
nJGmCrHTQeQMPYLb06BI7QcBZr7skkwoBMqvT0VEsdZc37PP4xg0B/i4GzhGNcrc
NsjOHpIUo83u63l+s5lIZbCWKZDo0RuPDCeMa1V4wvwA+Ti24nAAWBRtLvS5WVUm
J0rYHRb9YmdWBZsBAdU3Hd1AQ13Qe4mPI81K1n5K+KSXvQVcB63/ExtHutoZgIaw
H8ayyfrJp5ngwbIbAmleC0woXq9XybGpZ7XOz5+0PmthhWgVkqYksQCZNgh4QAJQ
Uqbh056mY0TN7/pfGBOWPyDuju8HA5OAJ2KzCu+QcC27XhwgkdXObQrr6S9DhHCE
ShtJqxgdP0o+TAi91IYSn2peqXfTzYo1iIOMejd9ujvU+iPGekJocQFBomzgtWjr
rtU5Qu7CjWKTdIecybNuSDFTtO4FEVtp9HKzZ62Eep4nOwvUicY=
=x3LN
-----END PGP SIGNATURE-----

Christopher Baines wrote 7 years ago
[PATCH 2/3] tests: databases: Add a system test for PostgreSQL.
(address . 30701@debbugs.gnu.org)
20180305083917.24122-2-mail@cbaines.net
* gnu/tests/databases.scm (%postgresql-os, %test-postgresql): New variables.
(run-postgresql-test): New procedure.
---
gnu/tests/databases.scm | 55 +++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 55 insertions(+)

Toggle diff (75 lines)
diff --git a/gnu/tests/databases.scm b/gnu/tests/databases.scm
index e7097690a..583f484d7 100644
--- a/gnu/tests/databases.scm
+++ b/gnu/tests/databases.scm
@@ -30,6 +30,7 @@
#:use-module (guix store)
#:export (%test-memcached
%test-mongodb
+ %test-postgresql
%test-mysql))
(define %memcached-os
@@ -208,6 +209,60 @@
(value (run-mongodb-test))))
+;;;
+;;; The PostgreSQL service.
+;;;
+
+(define %postgresql-os
+ (simple-operating-system
+ (service postgresql-service-type)))
+
+(define* (run-postgresql-test)
+ "Run tests in %POSTGRESQL-OS."
+ (define os
+ (marionette-operating-system
+ %postgresql-os
+ #:imported-modules '((gnu services herd)
+ (guix combinators))))
+
+ (define vm
+ (virtual-machine
+ (operating-system os)
+ (memory-size 512)))
+
+ (define test
+ (with-imported-modules '((gnu build marionette))
+ #~(begin
+ (use-modules (srfi srfi-11) (srfi srfi-64)
+ (gnu build marionette))
+
+ (define marionette
+ (make-marionette (list #$vm)))
+
+ (mkdir #$output)
+ (chdir #$output)
+
+ (test-begin "postgresql")
+
+ (test-assert "service running"
+ (marionette-eval
+ '(begin
+ (use-modules (gnu services herd))
+ (start-service 'postgres))
+ marionette))
+
+ (test-end)
+ (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
+
+ (gexp->derivation "postgresql-test" test))
+
+(define %test-postgresql
+ (system-test
+ (name "postgresql")
+ (description "Start the PostgreSQL service.")
+ (value (run-postgresql-test))))
+
+
;;;
;;; The MySQL service.
;;;
--
2.16.0
Christopher Baines wrote 7 years ago
[PATCH 3/3] services: databases: Add postgresql-configuration record exports.
(address . 30701@debbugs.gnu.org)
20180305083917.24122-3-mail@cbaines.net
* gnu/services/databases.scm: Export the record type, and all the field
accessors.
---
gnu/services/databases.scm | 8 ++++++++
1 file changed, 8 insertions(+)

Toggle diff (22 lines)
diff --git a/gnu/services/databases.scm b/gnu/services/databases.scm
index 9ffb6a5e9..b05c0442f 100644
--- a/gnu/services/databases.scm
+++ b/gnu/services/databases.scm
@@ -40,7 +40,15 @@
postgresql-config-file-ident-file
postgresql-config-file-extra-config
+ <postgresql-configuration>
+ postgresql-configuration
postgresql-configuration?
+ postgresql-configuration-postgresql
+ postgresql-configuration-port
+ postgresql-configuration-locale
+ postgresql-configuration-file
+ postgresql-configuration-data-directory
+
postgresql-service
postgresql-service-type
--
2.16.0
Christopher Baines wrote 7 years ago
[PATCH 1/3] services: Rework the PostgreSQL config file to use a record type.
(address . 30701@debbugs.gnu.org)
20180305083917.24122-1-mail@cbaines.net
For the default config file representation. This makes it possible to more
easily change the configuration file, and have dynamic content. In particular,
I'm looking at adding a pid file location to the config file.

* gnu/services/databases.scm (<postgresql-config-file>): New record type.
(%default-postgres-config): Remove this, it's been replaced by the
configuration file.
(<postgresql-configuration>): Alter the default for the config file field.
(postgresql-service): Alter the default value for the config-file parameter.
---
gnu/services/databases.scm | 86 +++++++++++++++++++++++++++++++++++-----------
1 file changed, 66 insertions(+), 20 deletions(-)

Toggle diff (127 lines)
diff --git a/gnu/services/databases.scm b/gnu/services/databases.scm
index 3ca8f471f..9ffb6a5e9 100644
--- a/gnu/services/databases.scm
+++ b/gnu/services/databases.scm
@@ -26,11 +26,20 @@
#:use-module (gnu system shadow)
#:use-module (gnu packages admin)
#:use-module (gnu packages databases)
+ #:use-module (guix store)
#:use-module (guix modules)
#:use-module (guix records)
#:use-module (guix gexp)
+ #:use-module (srfi srfi-1)
#:use-module (ice-9 match)
- #:export (postgresql-configuration
+ #:export (<postgresql-config-file>
+ postgresql-config-file
+ postgresql-config-file?
+ postgresql-config-file-log-destination
+ postgresql-config-file-hba-file
+ postgresql-config-file-ident-file
+ postgresql-config-file-extra-config
+
postgresql-configuration?
postgresql-service
postgresql-service-type
@@ -68,6 +77,60 @@
;;;
;;; Code:
+(define %default-postgres-hba
+ (plain-file "pg_hba.conf"
+ "
+local all all trust
+host all all 127.0.0.1/32 trust
+host all all ::1/128 trust"))
+
+(define %default-postgres-ident
+ (plain-file "pg_ident.conf"
+ "# MAPNAME SYSTEM-USERNAME PG-USERNAME"))
+
+(define-record-type* <postgresql-config-file>
+ postgresql-config-file make-postgresql-config-file
+ postgresql-config-file?
+ (log-destination postgresql-config-file-log-destination
+ (default "syslog"))
+ (hba-file postgresql-config-file-hba-file
+ (default %default-postgres-hba))
+ (ident-file postgresql-config-file-ident-file
+ (default %default-postgres-ident))
+ (extra-config postgresql-config-file-extra-config
+ (default '())))
+
+(define-gexp-compiler (postgresql-config-file-compiler
+ (file <postgresql-config-file>) system target)
+ (match file
+ (($ <postgresql-config-file> log-destination hba-file
+ ident-file extra-config)
+ (define (quote string)
+ (if string
+ (list "'" string "'")
+ (list)))
+
+ (define contents
+ (append-map
+ (match-lambda
+ ((key) (list))
+ ((key . #f) (list))
+ ((key values ...) `(,key " = " ,@values "\n")))
+
+ `(("log_destination" ,@(quote log-destination))
+ ("hba_file" ,@(quote hba-file))
+ ("ident_file" ,@(quote ident-file))
+ ,@extra-config)))
+
+ (gexp->derivation
+ "postgresql.conf"
+ #~(call-with-output-file (ungexp output "out")
+ (lambda (port)
+ (display
+ (string-append #$@contents)
+ port)))
+ #:local-build? #t))))
+
(define-record-type* <postgresql-configuration>
postgresql-configuration make-postgresql-configuration
postgresql-configuration?
@@ -78,27 +141,10 @@
(locale postgresql-configuration-locale
(default "en_US.utf8"))
(config-file postgresql-configuration-file
- (default %default-postgres-config))
+ (default (postgresql-config-file)))
(data-directory postgresql-configuration-data-directory
(default "/var/lib/postgresql/data")))
-(define %default-postgres-hba
- (plain-file "pg_hba.conf"
- "
-local all all trust
-host all all 127.0.0.1/32 trust
-host all all ::1/128 trust"))
-
-(define %default-postgres-ident
- (plain-file "pg_ident.conf"
- "# MAPNAME SYSTEM-USERNAME PG-USERNAME"))
-
-(define %default-postgres-config
- (mixed-text-file "postgresql.conf"
- "log_destination = 'syslog'\n"
- "hba_file = '" %default-postgres-hba "'\n"
- "ident_file = '" %default-postgres-ident "'\n"))
-
(define %postgresql-accounts
(list (user-group (name "postgres") (system? #t))
(user-account
@@ -192,7 +238,7 @@ host all all ::1/128 trust"))
(define* (postgresql-service #:key (postgresql postgresql)
(port 5432)
(locale "en_US.utf8")
- (config-file %default-postgres-config)
+ (config-file (postgresql-config-file))
(data-directory "/var/lib/postgresql/data"))
"Return a service that runs @var{postgresql}, the PostgreSQL database server.
--
2.16.0
Clément Lassieur wrote 7 years ago
Re: [bug#30701] [PATCH 1/4] services: Rework the PostgreSQL config file to use a record type.
(name . Christopher Baines)(address . mail@cbaines.net)(address . 30701@debbugs.gnu.org)
87h8puzyys.fsf@lassieur.org
Christopher Baines <mail@cbaines.net> writes:

Toggle quote (41 lines)
> Clément Lassieur <clement@lassieur.org> writes:
>
>> Hi Christopher,
>>
>> Christopher Baines <mail@cbaines.net> writes:
>>
>>> For the default config file representation. This makes it possible to more
>>> easily change the configuration file, and have dynamic content. In particular,
>>> I'm looking at adding a pid file location to the config file.
>>>
>>> * gnu/services/databases.scm (<postgresql-config-file>): New record type.
>>> (%default-postgres-config): Remove this, it's been replaced by the
>>> configuration file.
>>> (<postgresql-configuration>): Alter the default for the config file field.
>>> (postgresql-service): Alter the default value for the config-file parameter.
>>> ---
>>> gnu/services/databases.scm | 86 +++++++++++++++++++++++++++++++++++-----------
>>> 1 file changed, 66 insertions(+), 20 deletions(-)
>>
>> Thank you for this work!
>
> No problem, I've finally got around to going through some patches I've
> had sitting around for a while.
>
>>> +(define-gexp-compiler (postgresql-config-file-compiler
>>> + (file <postgresql-config-file>) system target)
>>> + (match file
>>> + (($ <postgresql-config-file> log-destination hba-file
>>> + ident-file extra-config)
>>> + (define (quote string)
>>> + (if string
>>> + (list "'" string "'")
>>> + (list)))
>>
>> I don't think it's a good thing to hide one of the most important lisp
>> functions :-).
>
> I don't quite follow. I was trying to use '() rather than (list) if that
> is what you mean, but I kept getting odd errors from Guile, so I gave
> up, and ended up going with this.

Sorry for not being clear. I meant that if you define 'quote', you
won't be able to use the Guile 'quote' anymore. See

You could just rename it to 'quote-val' or something else.
Clément Lassieur wrote 7 years ago
(name . Christopher Baines)(address . mail@cbaines.net)(address . 30701@debbugs.gnu.org)
87efkyzwy2.fsf@lassieur.org
Christopher Baines <mail@cbaines.net> writes:

Toggle quote (41 lines)
> Clément Lassieur <clement@lassieur.org> writes:
>
>> Hi Christopher,
>>
>> Christopher Baines <mail@cbaines.net> writes:
>>
>>> For the default config file representation. This makes it possible to more
>>> easily change the configuration file, and have dynamic content. In particular,
>>> I'm looking at adding a pid file location to the config file.
>>>
>>> * gnu/services/databases.scm (<postgresql-config-file>): New record type.
>>> (%default-postgres-config): Remove this, it's been replaced by the
>>> configuration file.
>>> (<postgresql-configuration>): Alter the default for the config file field.
>>> (postgresql-service): Alter the default value for the config-file parameter.
>>> ---
>>> gnu/services/databases.scm | 86 +++++++++++++++++++++++++++++++++++-----------
>>> 1 file changed, 66 insertions(+), 20 deletions(-)
>>
>> Thank you for this work!
>
> No problem, I've finally got around to going through some patches I've
> had sitting around for a while.
>
>>> +(define-gexp-compiler (postgresql-config-file-compiler
>>> + (file <postgresql-config-file>) system target)
>>> + (match file
>>> + (($ <postgresql-config-file> log-destination hba-file
>>> + ident-file extra-config)
>>> + (define (quote string)
>>> + (if string
>>> + (list "'" string "'")
>>> + (list)))
>>
>> I don't think it's a good thing to hide one of the most important lisp
>> functions :-).
>
> I don't quite follow. I was trying to use '() rather than (list) if that
> is what you mean, but I kept getting odd errors from Guile, so I gave
> up, and ended up going with this.

You couldn't use '() because '() is the same thing as (quote ()) and you
redefined 'quote'.
Clément Lassieur wrote 7 years ago
Re: [bug#30701] [PATCH 2/3] tests: databases: Add a system test for PostgreSQL.
(name . Christopher Baines)(address . mail@cbaines.net)(address . 30701@debbugs.gnu.org)
87d10izqia.fsf@lassieur.org
Christopher Baines <mail@cbaines.net> writes:

Toggle quote (32 lines)
> * gnu/tests/databases.scm (%postgresql-os, %test-postgresql): New variables.
> (run-postgresql-test): New procedure.
> ---
> gnu/tests/databases.scm | 55 +++++++++++++++++++++++++++++++++++++++++++++++++
> 1 file changed, 55 insertions(+)
>
> diff --git a/gnu/tests/databases.scm b/gnu/tests/databases.scm
> index e7097690a..583f484d7 100644
> --- a/gnu/tests/databases.scm
> +++ b/gnu/tests/databases.scm
> @@ -30,6 +30,7 @@
> #:use-module (guix store)
> #:export (%test-memcached
> %test-mongodb
> + %test-postgresql
> %test-mysql))
>
> (define %memcached-os
> @@ -208,6 +209,60 @@
> (value (run-mongodb-test))))
>
>
> +;;;
> +;;; The PostgreSQL service.
> +;;;
> +
> +(define %postgresql-os
> + (simple-operating-system
> + (service postgresql-service-type)))
> +
> +(define* (run-postgresql-test)

define, instead of define*

Toggle quote (17 lines)
> + "Run tests in %POSTGRESQL-OS."
> + (define os
> + (marionette-operating-system
> + %postgresql-os
> + #:imported-modules '((gnu services herd)
> + (guix combinators))))
> +
> + (define vm
> + (virtual-machine
> + (operating-system os)
> + (memory-size 512)))
> +
> + (define test
> + (with-imported-modules '((gnu build marionette))
> + #~(begin
> + (use-modules (srfi srfi-11) (srfi srfi-64)

I think srfi-11 is useless.

Toggle quote (32 lines)
> + (gnu build marionette))
> +
> + (define marionette
> + (make-marionette (list #$vm)))
> +
> + (mkdir #$output)
> + (chdir #$output)
> +
> + (test-begin "postgresql")
> +
> + (test-assert "service running"
> + (marionette-eval
> + '(begin
> + (use-modules (gnu services herd))
> + (start-service 'postgres))
> + marionette))
> +
> + (test-end)
> + (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
> +
> + (gexp->derivation "postgresql-test" test))
> +
> +(define %test-postgresql
> + (system-test
> + (name "postgresql")
> + (description "Start the PostgreSQL service.")
> + (value (run-postgresql-test))))
> +
> +
> ;;;
> ;;; The MySQL service.
> ;;;
Clément Lassieur wrote 7 years ago
Re: [bug#30701] [PATCH 2/4] services: Use a external pid file for PostgreSQL.
(name . Christopher Baines)(address . mail@cbaines.net)(address . 30701@debbugs.gnu.org)
87bmg2zpjr.fsf@lassieur.org
Christopher Baines <mail@cbaines.net> writes:

Toggle quote (131 lines)
> Clément Lassieur <clement@lassieur.org> writes:
>
>> Christopher Baines <mail@cbaines.net> writes:
>>
>>> * gnu/services/databases.scm (postgresql-pid-file-for-version): New procedure.
>>> (<postgresql-config-file>): Add a new external-pid-file field.
>>> (postgresql-config-file-compiler): Add support for the external-pid-file.
>>> (postgresql-activation): Create the directory for the pid file.
>>> (postgresql-shepherd-service): Use the pid-file when starting the service.
>>> ---
>>> gnu/services/databases.scm | 48 ++++++++++++++++++++++++++++++++++------------
>>> 1 file changed, 36 insertions(+), 12 deletions(-)
>>
>> ...
>>
>>> +(define (postgresql-pid-file-for-version version)
>>> + (string-append "/var/run/postgresql/"
>>> + (version-major+minor version)
>>> + "-main.pid"))
>>> +
>>> (define-record-type* <postgresql-config-file>
>>> postgresql-config-file make-postgresql-config-file
>>> postgresql-config-file?
>>> - (log-destination postgresql-config-file-log-destination
>>> - (default "syslog"))
>>> - (hba-file postgresql-config-file-hba-file
>>> - (default %default-postgres-hba))
>>> - (ident-file postgresql-config-file-ident-file
>>> - (default %default-postgres-ident))
>>> - (extra-config postgresql-config-file-extra-config
>>> - (default '())))
>>> + (log-destination postgresql-config-file-log-destination
>>> + (default "syslog"))
>>> + (hba-file postgresql-config-file-hba-file
>>> + (default %default-postgres-hba))
>>> + (ident-file postgresql-config-file-ident-file
>>> + (default %default-postgres-ident))
>>> + (external-pid-file postgresql-config-file-external-pid-file
>>> + (default (postgresql-pid-file-for-version
>>> + (package-version postgresql))))
>>
>> Why does external-pid-file have a default value. It's optional, and the
>> user already has access to $DATA/postmaster.pid anyway.
>
> I ended up adding this as I was writing the system test. I was coping
> previous tests that I have written, in which I've been checking that the
> shepherd gets the PID back when it starts the process.
>
> Before I set out in writing this, I didn't realise that PostgreSQL
> stores the PID in the data directory by default, or that pg_ctl waits
> for actions to complete by default either.
>
> I'm not particularly attached to this patch, I guess it mostly offers
> consistency with other services.
>
>>> @@ -140,6 +153,9 @@ host all all ::1/128 trust"))
>>> (default 5432))
>>> (locale postgresql-configuration-locale
>>> (default "en_US.utf8"))
>>> + (pid-file postgresql-configuration-pid-file
>>> + (default (postgresql-pid-file-for-version
>>> + (package-version postgresql))))
>>
>> The main PID file is chosen by Postgres, and put at
>> $DATA/postmaster.pid. I don't think it's customizable. This setting
>> (pid-file) probably doesn't affect the daemon the way you think it does.
>
> This part of the configuration is just meant to be where the Guix part
> of the service expects to find the pid-file (if one is used, and it's
> not #f).
>
>>> (config-file postgresql-configuration-file
>>> (default (postgresql-config-file)))
>>> (data-directory postgresql-configuration-data-directory
>>> @@ -157,7 +173,8 @@ host all all ::1/128 trust"))
>>>
>>> + ;; Create a directory for the pid file
>>> + (mkdir-p #$(dirname pid-file))
>>> + (chown #$(dirname pid-file) (passwd:uid user) (passwd:gid user))
>>
>> I think it would make more sense to create the directory for the
>> external-pid-file.
>
> As far as I understand it, this is what this does.
>
>>> (define postgresql-shepherd-service
>>> (match-lambda
>>> - (($ <postgresql-configuration> postgresql port locale config-file data-directory)
>>> + (($ <postgresql-configuration> postgresql port locale pid-file
>>> + config-file data-directory)
>>> (let* ((pg_ctl-wrapper
>>> ;; Wrapper script that switches to the 'postgres' user before
>>> ;; launching daemon.
>>> @@ -221,7 +243,9 @@ host all all ::1/128 trust"))
>>> (provision '(postgres))
>>> (documentation "Run the PostgreSQL daemon.")
>>> (requirement '(user-processes loopback syslogd))
>>> - (start (action "start"))
>>> + (start #~(make-forkexec-constructor
>>> + '(#$pg_ctl-wrapper "start")
>>> + #:pid-file #$pid-file))
>>> (stop (action "stop"))))))))
>>
>> If pid-file != external-pid-file, Sherpherd will wait for a file that
>> doesn't exist won't it? Because Postgresql will never be aware of that
>> pid-file.
>
> Yep.
>
>> Plus, there is no reason to use make-forkexec-constructor on pg_ctl
>> because pg_ctl returns after it has checked that the daemon is running.
>> For the same reason, Shepherd doesn't need to know about Postgres' PID.
>> All the hard work is done by pg_ctl.
>
> As the comment I made at the top, I did this when I was writing the
> system test. If you remove this patch, when you call (start-service
> 'postgres), it will return #f if the service starts successfully. If you
> tweak the service to make it fail to start (e.g. by changing the "start"
> action to something else), you get the same observable behaviour,
> start-service returns #f.
>
> The way this works for other services, normally through
> make-forkexec-constructor is that calling start-service will return the
> PID.
>
> While the system test does still add some value even without checking if
> the service has started, doing so would be really good. Even if it's not
> using the PID file approach, maybe the exit code of pg_ctl could be
> used? I'm not really sure why it isn't working like that already, as
> invoke usually returns either #t or #f...

The PID approch only makes sense when the executable run by Shepherd
doesn't return; i.e. it blocks. Then Shepherd needs to fork a child
process (with make-forkexec-constructor), so that it doesn't block. And
the only way to know if the daemon has started is to find a "proof",
which is the PID file. That's the point of the #:pid-file parameter of
make-forkexec-constructor.

But the PostgreSQL package is more elaborate: it provides an 'pg_ctl'
executable that does all the hard work:
- starting the daemon,
- checking its PID to make sure the daemon is running,
- exiting with 0 if the daemon is running.
So Shepherd doesn't need to fork, it just checks the return value of
pg_ctl.

Conclusion: (start-service 'postgres) returning #t means that pg_ctl
exit code is 0, which means that the daemon is running. I hope it makes
more sense :-)
Clément Lassieur wrote 7 years ago
Re: [bug#30701] [PATCH 4/4] services: databases: Add postgresql-configuration record exports.
(name . Christopher Baines)(address . mail@cbaines.net)(address . 30701@debbugs.gnu.org)
87a7vmzpht.fsf@lassieur.org
Christopher Baines <mail@cbaines.net> writes:

Toggle quote (2 lines)
> * gnu/services/databases.scm: Export the record type, and all the field
> accessors.
^
Small nit-pick: there is no indentation here :-). You can check "git
log" to see how other people do.

Toggle quote (24 lines)
> ---
> gnu/services/databases.scm | 9 +++++++++
> 1 file changed, 9 insertions(+)
>
> diff --git a/gnu/services/databases.scm b/gnu/services/databases.scm
> index 4090277a7..1e410cd39 100644
> --- a/gnu/services/databases.scm
> +++ b/gnu/services/databases.scm
> @@ -43,7 +43,16 @@
> postgresql-config-file-external-pid-file
> postgresql-config-file-extra-config
>
> + <postgresql-configuration>
> + postgresql-configuration
> postgresql-configuration?
> + postgresql-configuration-postgresql
> + postgresql-configuration-port
> + postgresql-configuration-locale
> + postgresql-configuration-pid-file
> + postgresql-configuration-file
> + postgresql-configuration-data-directory
> +
> postgresql-service
> postgresql-service-type
Christopher Baines wrote 7 years ago
Re: [bug#30701] [PATCH 2/3] tests: databases: Add a system test for PostgreSQL.
(name . Clément Lassieur)(address . clement@lassieur.org)(address . 30701@debbugs.gnu.org)
87efkypbnx.fsf@cbaines.net
Clément Lassieur <clement@lassieur.org> writes:

Toggle quote (55 lines)
> Christopher Baines <mail@cbaines.net> writes:
>
>> * gnu/tests/databases.scm (%postgresql-os, %test-postgresql): New variables.
>> (run-postgresql-test): New procedure.
>> ---
>> gnu/tests/databases.scm | 55 +++++++++++++++++++++++++++++++++++++++++++++++++
>> 1 file changed, 55 insertions(+)
>>
>> diff --git a/gnu/tests/databases.scm b/gnu/tests/databases.scm
>> index e7097690a..583f484d7 100644
>> --- a/gnu/tests/databases.scm
>> +++ b/gnu/tests/databases.scm
>> @@ -30,6 +30,7 @@
>> #:use-module (guix store)
>> #:export (%test-memcached
>> %test-mongodb
>> + %test-postgresql
>> %test-mysql))
>>
>> (define %memcached-os
>> @@ -208,6 +209,60 @@
>> (value (run-mongodb-test))))
>>
>>
>> +;;;
>> +;;; The PostgreSQL service.
>> +;;;
>> +
>> +(define %postgresql-os
>> + (simple-operating-system
>> + (service postgresql-service-type)))
>> +
>> +(define* (run-postgresql-test)
>
> define, instead of define*
>
>> + "Run tests in %POSTGRESQL-OS."
>> + (define os
>> + (marionette-operating-system
>> + %postgresql-os
>> + #:imported-modules '((gnu services herd)
>> + (guix combinators))))
>> +
>> + (define vm
>> + (virtual-machine
>> + (operating-system os)
>> + (memory-size 512)))
>> +
>> + (define test
>> + (with-imported-modules '((gnu build marionette))
>> + #~(begin
>> + (use-modules (srfi srfi-11) (srfi srfi-64)
>
> I think srfi-11 is useless.

Good spot, I'll make these changes and send some new patches.
-----BEGIN PGP SIGNATURE-----

iQKTBAEBCgB9FiEEPonu50WOcg2XVOCyXiijOwuE9XcFAlqdmbJfFIAAAAAALgAo
aXNzdWVyLWZwckBub3RhdGlvbnMub3BlbnBncC5maWZ0aGhvcnNlbWFuLm5ldDNF
ODlFRUU3NDU4RTcyMEQ5NzU0RTBCMjVFMjhBMzNCMEI4NEY1NzcACgkQXiijOwuE
9Xe3JQ//T2ZBUcypPItOD2GF28cp0mGmlobU9IqJwNQ8QY/ouaLk77ySzWXZh+6R
MRD9BxyILanHXW4yNNWIsnWL6uOAB1r4iFALkGy6nKJywzndGEFdbN8Io5cvEwXx
Mgc+v1EjA4EcWeTcxY1xOdyBUNqRAhLCy++1wGQGgVvrB8EJJF4oa4Rsi/1xtqwA
34wFuAljtxA9aOaUFiQH0w7MzG3Cqf0iPGalfaAmWtms34+U9SU0xADi5Wmb686e
xkcbJqgehcF1+rpgvwD+hhqmr6k2eRs4vyUXD2sTt+r/c3x3juno8T77OS4xSFdK
LsVzcUdg+0ECNsBLLPkmLRfg8yMeKuLe42ystndUardB80DIrs8BZnXZLK4uy7gi
xgAah8dNV1D5A/2uOmST4+cZYo9HyJuF3Z1jkEz77v1ePm1L2119jL2awj4szlcS
dsLPAMGGCQ6TjVVwv6PzWCduKx5CHGeSSAYRUtt5hJkYfknebhqWuaFl7APv4d7O
CzjbB8kl8DVWmp135B6P7naPnP9LDejqFf+70bigfFipMOsCZ1sSnAdH8eiUFVYa
tvRVb0uV4Cq6M6z6gAzmO/QP3qk/i/Poi1wbYoX27S4KUqPnnhRuYIa+ectt8z9d
w7I20zDDcIngKiyIr2Iagk3ARphqLLHs6FEXP78eQRTStyGUp9I=
=ou27
-----END PGP SIGNATURE-----

Christopher Baines wrote 7 years ago
[PATCH 2/3] tests: databases: Add a system test for PostgreSQL.
(address . 30701@debbugs.gnu.org)
20180305193719.28652-2-mail@cbaines.net
* gnu/tests/databases.scm (%postgresql-os, %test-postgresql): New variables.
(run-postgresql-test): New procedure.
---
gnu/tests/databases.scm | 55 +++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 55 insertions(+)

Toggle diff (75 lines)
diff --git a/gnu/tests/databases.scm b/gnu/tests/databases.scm
index e7097690a..5c8ca85c1 100644
--- a/gnu/tests/databases.scm
+++ b/gnu/tests/databases.scm
@@ -30,6 +30,7 @@
#:use-module (guix store)
#:export (%test-memcached
%test-mongodb
+ %test-postgresql
%test-mysql))
(define %memcached-os
@@ -208,6 +209,60 @@
(value (run-mongodb-test))))
+;;;
+;;; The PostgreSQL service.
+;;;
+
+(define %postgresql-os
+ (simple-operating-system
+ (service postgresql-service-type)))
+
+(define (run-postgresql-test)
+ "Run tests in %POSTGRESQL-OS."
+ (define os
+ (marionette-operating-system
+ %postgresql-os
+ #:imported-modules '((gnu services herd)
+ (guix combinators))))
+
+ (define vm
+ (virtual-machine
+ (operating-system os)
+ (memory-size 512)))
+
+ (define test
+ (with-imported-modules '((gnu build marionette))
+ #~(begin
+ (use-modules (srfi srfi-64)
+ (gnu build marionette))
+
+ (define marionette
+ (make-marionette (list #$vm)))
+
+ (mkdir #$output)
+ (chdir #$output)
+
+ (test-begin "postgresql")
+
+ (test-assert "service running"
+ (marionette-eval
+ '(begin
+ (use-modules (gnu services herd))
+ (start-service 'postgres))
+ marionette))
+
+ (test-end)
+ (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
+
+ (gexp->derivation "postgresql-test" test))
+
+(define %test-postgresql
+ (system-test
+ (name "postgresql")
+ (description "Start the PostgreSQL service.")
+ (value (run-postgresql-test))))
+
+
;;;
;;; The MySQL service.
;;;
--
2.16.0
Christopher Baines wrote 7 years ago
[PATCH 3/3] services: databases: Add postgresql-configuration record exports.
(address . 30701@debbugs.gnu.org)
20180305193719.28652-3-mail@cbaines.net
* gnu/services/databases.scm: Export the record type, and all the field
accessors.
---
gnu/services/databases.scm | 8 ++++++++
1 file changed, 8 insertions(+)

Toggle diff (22 lines)
diff --git a/gnu/services/databases.scm b/gnu/services/databases.scm
index f7d5fffd0..4c9a50a5a 100644
--- a/gnu/services/databases.scm
+++ b/gnu/services/databases.scm
@@ -40,7 +40,15 @@
postgresql-config-file-ident-file
postgresql-config-file-extra-config
+ <postgresql-configuration>
+ postgresql-configuration
postgresql-configuration?
+ postgresql-configuration-postgresql
+ postgresql-configuration-port
+ postgresql-configuration-locale
+ postgresql-configuration-file
+ postgresql-configuration-data-directory
+
postgresql-service
postgresql-service-type
--
2.16.0
Christopher Baines wrote 7 years ago
[PATCH 1/3] services: Rework the PostgreSQL config file to use a record type.
(address . 30701@debbugs.gnu.org)
20180305193719.28652-1-mail@cbaines.net
For the default config file representation. This makes it possible to more
easily change the configuration file, and have dynamic content. In particular,
I'm looking at adding a pid file location to the config file.

* gnu/services/databases.scm (<postgresql-config-file>): New record type.
(%default-postgres-config): Remove this, it's been replaced by the
configuration file.
(<postgresql-configuration>): Alter the default for the config file field.
(postgresql-service): Alter the default value for the config-file parameter.
---
gnu/services/databases.scm | 86 +++++++++++++++++++++++++++++++++++-----------
1 file changed, 66 insertions(+), 20 deletions(-)

Toggle diff (127 lines)
diff --git a/gnu/services/databases.scm b/gnu/services/databases.scm
index 3ca8f471f..f7d5fffd0 100644
--- a/gnu/services/databases.scm
+++ b/gnu/services/databases.scm
@@ -26,11 +26,20 @@
#:use-module (gnu system shadow)
#:use-module (gnu packages admin)
#:use-module (gnu packages databases)
+ #:use-module (guix store)
#:use-module (guix modules)
#:use-module (guix records)
#:use-module (guix gexp)
+ #:use-module (srfi srfi-1)
#:use-module (ice-9 match)
- #:export (postgresql-configuration
+ #:export (<postgresql-config-file>
+ postgresql-config-file
+ postgresql-config-file?
+ postgresql-config-file-log-destination
+ postgresql-config-file-hba-file
+ postgresql-config-file-ident-file
+ postgresql-config-file-extra-config
+
postgresql-configuration?
postgresql-service
postgresql-service-type
@@ -68,6 +77,60 @@
;;;
;;; Code:
+(define %default-postgres-hba
+ (plain-file "pg_hba.conf"
+ "
+local all all trust
+host all all 127.0.0.1/32 trust
+host all all ::1/128 trust"))
+
+(define %default-postgres-ident
+ (plain-file "pg_ident.conf"
+ "# MAPNAME SYSTEM-USERNAME PG-USERNAME"))
+
+(define-record-type* <postgresql-config-file>
+ postgresql-config-file make-postgresql-config-file
+ postgresql-config-file?
+ (log-destination postgresql-config-file-log-destination
+ (default "syslog"))
+ (hba-file postgresql-config-file-hba-file
+ (default %default-postgres-hba))
+ (ident-file postgresql-config-file-ident-file
+ (default %default-postgres-ident))
+ (extra-config postgresql-config-file-extra-config
+ (default '())))
+
+(define-gexp-compiler (postgresql-config-file-compiler
+ (file <postgresql-config-file>) system target)
+ (match file
+ (($ <postgresql-config-file> log-destination hba-file
+ ident-file extra-config)
+ (define (with-single-quotes string)
+ (if string
+ (list "'" string "'")
+ '()))
+
+ (define contents
+ (append-map
+ (match-lambda
+ ((key) '())
+ ((key . #f) '())
+ ((key values ...) `(,key " = " ,@values "\n")))
+
+ `(("log_destination" ,@(with-single-quotes log-destination))
+ ("hba_file" ,@(with-single-quotes hba-file))
+ ("ident_file" ,@(with-single-quotes ident-file))
+ ,@extra-config)))
+
+ (gexp->derivation
+ "postgresql.conf"
+ #~(call-with-output-file (ungexp output "out")
+ (lambda (port)
+ (display
+ (string-append #$@contents)
+ port)))
+ #:local-build? #t))))
+
(define-record-type* <postgresql-configuration>
postgresql-configuration make-postgresql-configuration
postgresql-configuration?
@@ -78,27 +141,10 @@
(locale postgresql-configuration-locale
(default "en_US.utf8"))
(config-file postgresql-configuration-file
- (default %default-postgres-config))
+ (default (postgresql-config-file)))
(data-directory postgresql-configuration-data-directory
(default "/var/lib/postgresql/data")))
-(define %default-postgres-hba
- (plain-file "pg_hba.conf"
- "
-local all all trust
-host all all 127.0.0.1/32 trust
-host all all ::1/128 trust"))
-
-(define %default-postgres-ident
- (plain-file "pg_ident.conf"
- "# MAPNAME SYSTEM-USERNAME PG-USERNAME"))
-
-(define %default-postgres-config
- (mixed-text-file "postgresql.conf"
- "log_destination = 'syslog'\n"
- "hba_file = '" %default-postgres-hba "'\n"
- "ident_file = '" %default-postgres-ident "'\n"))
-
(define %postgresql-accounts
(list (user-group (name "postgres") (system? #t))
(user-account
@@ -192,7 +238,7 @@ host all all ::1/128 trust"))
(define* (postgresql-service #:key (postgresql postgresql)
(port 5432)
(locale "en_US.utf8")
- (config-file %default-postgres-config)
+ (config-file (postgresql-config-file))
(data-directory "/var/lib/postgresql/data"))
"Return a service that runs @var{postgresql}, the PostgreSQL database server.
--
2.16.0
Christopher Baines wrote 7 years ago
Re: [bug#30701] [PATCH 4/4] services: databases: Add postgresql-configuration record exports.
(name . Clément Lassieur)(address . clement@lassieur.org)(address . 30701@debbugs.gnu.org)
87d10ipb3n.fsf@cbaines.net
Clément Lassieur <clement@lassieur.org> writes:

Toggle quote (8 lines)
> Christopher Baines <mail@cbaines.net> writes:
>
>> * gnu/services/databases.scm: Export the record type, and all the field
>> accessors.
> ^
> Small nit-pick: there is no indentation here :-). You can check "git
> log" to see how other people do.

I've updated the git commit messages, and sent some updated
patches. Thanks for taking a look :)
-----BEGIN PGP SIGNATURE-----

iQKTBAEBCgB9FiEEPonu50WOcg2XVOCyXiijOwuE9XcFAlqdnIxfFIAAAAAALgAo
aXNzdWVyLWZwckBub3RhdGlvbnMub3BlbnBncC5maWZ0aGhvcnNlbWFuLm5ldDNF
ODlFRUU3NDU4RTcyMEQ5NzU0RTBCMjVFMjhBMzNCMEI4NEY1NzcACgkQXiijOwuE
9XcBKw//S2WJquM4R5Ig4MiI3rnBbc6qlhGVh4ciHZG+c8pmRkGCTom18LZDrLVt
+nQeLwkB8IDUCFNn1WpLXl+/9Br9v+TRgoU0r18vKO3KmpxFOogWlYd0TS4uFZvY
rxFQNfxpSeXvykgDbJAKh78oEuiXczqDVmtU4Hu1Tjp6C9mHiMiPPFe9Z1C3rNa7
YrD4xtnmKAz3r6WAA9Qoo95GaLcMpixvAt96ViFdNsvCmYsi0QdqKwumvL2NfZE+
hOjRjzhOYbDy5bdt65buyvFXwtIR5WPxChkR1IJl/DJoxNgG9YuT/hZtzQdP+f1D
EvDOaD0q4nTS4XPMXuv92cKf4wLpWqHdmPFiXHsSZf+wzBKp0xhRS5DWE9QPBibI
nr0+lZbsGWa0ofEGsbBdEKJOI/6YXp488i9h8pUeM3hlARvJmBZYDazGVjJ0Xfmz
kOstomJxA9rPCdWjhvJ0IIYsMDy2+uAhTpljPl3DwkM839hLT6fRlUSJn0BlLiyq
55jHn+G6Rzg7kUKah1JY4g2i0Pwl6PodPKUx81/Lcy/u3BRDBJBrK0TxAkIVfptr
0ivpC9JvOeeSQgguMYUhPmCbQSpfxsiJbzZXsmqTo4kXS0mWDs9KElX7BSAZyWPt
IY/ZdcMmyQ6N2DdgmqBAUWya6AOWcgkozuAfefEqBOvW43GOoLE=
=YZK/
-----END PGP SIGNATURE-----

Clément Lassieur wrote 7 years ago
Re: [bug#30701] [PATCH 1/3] services: Rework the PostgreSQL config file to use a record type.
(name . Christopher Baines)(address . mail@cbaines.net)(address . 30701@debbugs.gnu.org)
87fu5eyzpe.fsf@lassieur.org
Christopher Baines <mail@cbaines.net> writes:

Toggle quote (4 lines)
> For the default config file representation. This makes it possible to more
> easily change the configuration file, and have dynamic content. In particular,
> I'm looking at adding a pid file location to the config file.

^
Could you please remove this last sentence (with the pid file)?

Toggle quote (19 lines)
> * gnu/services/databases.scm (<postgresql-config-file>): New record type.
> (%default-postgres-config): Remove this, it's been replaced by the
> configuration file.
> (<postgresql-configuration>): Alter the default for the config file field.
> (postgresql-service): Alter the default value for the config-file parameter.
> ---
> gnu/services/databases.scm | 86 +++++++++++++++++++++++++++++++++++-----------
> 1 file changed, 66 insertions(+), 20 deletions(-)
>
> diff --git a/gnu/services/databases.scm b/gnu/services/databases.scm
> index 3ca8f471f..f7d5fffd0 100644
> --- a/gnu/services/databases.scm
> +++ b/gnu/services/databases.scm
> @@ -26,11 +26,20 @@
> #:use-module (gnu system shadow)
> #:use-module (gnu packages admin)
> #:use-module (gnu packages databases)
> + #:use-module (guix store)

I don't think (guix store) is used. Is it?

Toggle quote (4 lines)
> #:use-module (guix modules)
> #:use-module (guix records)
> #:use-module (guix gexp)

...

Toggle quote (3 lines)
> + `(("log_destination" ,@(with-single-quotes log-destination))
> + ("hba_file" ,@(with-single-quotes hba-file))
> + ("ident_file" ,@(with-single-quotes ident-file))
^
Could you please use a shorter name? Like "enclose", so that we won't
go over 80 columns too easily :-).

Apart from those small things, the three patches LGTM. Thank you
again!

Clément
Christopher Baines wrote 7 years ago
(name . Clément Lassieur)(address . clement@lassieur.org)(address . 30701-done@debbugs.gnu.org)
87605z27xh.fsf@cbaines.net
Sorry, I made some changes, and merged these patches on the weekend, but
I forgot to reply.

Clément Lassieur <clement@lassieur.org> writes:

Toggle quote (9 lines)
> Christopher Baines <mail@cbaines.net> writes:
>
>> For the default config file representation. This makes it possible to more
>> easily change the configuration file, and have dynamic content. In particular,
>> I'm looking at adding a pid file location to the config file.
>
> ^
> Could you please remove this last sentence (with the pid file)?

Done.

Toggle quote (21 lines)
>> * gnu/services/databases.scm (<postgresql-config-file>): New record type.
>> (%default-postgres-config): Remove this, it's been replaced by the
>> configuration file.
>> (<postgresql-configuration>): Alter the default for the config file field.
>> (postgresql-service): Alter the default value for the config-file parameter.
>> ---
>> gnu/services/databases.scm | 86 +++++++++++++++++++++++++++++++++++-----------
>> 1 file changed, 66 insertions(+), 20 deletions(-)
>>
>> diff --git a/gnu/services/databases.scm b/gnu/services/databases.scm
>> index 3ca8f471f..f7d5fffd0 100644
>> --- a/gnu/services/databases.scm
>> +++ b/gnu/services/databases.scm
>> @@ -26,11 +26,20 @@
>> #:use-module (gnu system shadow)
>> #:use-module (gnu packages admin)
>> #:use-module (gnu packages databases)
>> + #:use-module (guix store)
>
> I don't think (guix store) is used. Is it?

It wasn't, I've removed it.

Toggle quote (13 lines)
>> #:use-module (guix modules)
>> #:use-module (guix records)
>> #:use-module (guix gexp)
>
> ...
>
>> + `(("log_destination" ,@(with-single-quotes log-destination))
>> + ("hba_file" ,@(with-single-quotes hba-file))
>> + ("ident_file" ,@(with-single-quotes ident-file))
> ^
> Could you please use a shorter name? Like "enclose", so that we won't
> go over 80 columns too easily :-).

I went with quote' as I think that works well.

Toggle quote (3 lines)
> Apart from those small things, the three patches LGTM. Thank you
> again!

Thanks for taking a look and for your comments :)

Chris
-----BEGIN PGP SIGNATURE-----

iQKTBAEBCgB9FiEEPonu50WOcg2XVOCyXiijOwuE9XcFAlqoDFpfFIAAAAAALgAo
aXNzdWVyLWZwckBub3RhdGlvbnMub3BlbnBncC5maWZ0aGhvcnNlbWFuLm5ldDNF
ODlFRUU3NDU4RTcyMEQ5NzU0RTBCMjVFMjhBMzNCMEI4NEY1NzcACgkQXiijOwuE
9XcKBBAAmojnwgpIzO3XdgVSVsHzBvYtTKGOeK7+Ez20ILVG4b6h9FGyKQOt91fY
rC1Dflp7ZnyA1PUtDU7NW/YZBI8mC0mPjEiRQJJUSpHQ3hwXIHahQfuwEVeMXhqn
t/f/ZelfKyYHAzP7fla44UyV0wxGxtYbaevoAIsRzf1R0W3UmLN/TEiRxrUpVPIp
g+zvUiyi3H4XYJvZ4L+NZx7C0KRgX6YO4fyNnABJ1qmvQy6y1XqPlZB6En3wIGEN
PgDB7vUDCGg91UIM2FgrlbwdI24g+5gOfYALw+QujXgCOQCW+x5F2S5SVpNPZegR
jcxnMOw8Faqb3ObVRyRb56cjBsbykZWclWS2ozx8LeepvHJAXqmQB8ra9Tv0LTm6
731N3A8VsUc/LCfpXh1ixEZ2h86bYQ8e8G5ayY4vc9t+ydxgH9fPKywk2XUBRbd3
WsgdWk/U8Z/ElSxc7iXay2M/IADr/BeeymcQKSQeXr836caW/LsZaKsUsBCkgC1N
lu6QnNVnQpamId6/VLbVdpw4RHshBSOm8sWEiiM0mbPzzGFlxzsspcTdO+liCYcD
mg85GejF93o44epBbplVFSSO9QU+zyNgGgQTBdGUSghEZSaosBSmzMzUZdXfqaYG
FrvWll/bayreQ8gvFEr4k/IchJ1jI/SpESAmbcNN3rzIHvMIs+c=
=kdPb
-----END PGP SIGNATURE-----

Closed
Clément Lassieur wrote 7 years ago
Re: bug#30701: [PATCH 1/3] services: Rework the PostgreSQL config file to use a record type.
(name . Christopher Baines)(address . mail@cbaines.net)(address . 30701-done@debbugs.gnu.org)
87o9jqtv6j.fsf@lassieur.org
Christopher Baines <mail@cbaines.net> writes:

Toggle quote (3 lines)
> Sorry, I made some changes, and merged these patches on the weekend, but
> I forgot to reply.

[...]

Toggle quote (9 lines)
>>> + `(("log_destination" ,@(with-single-quotes log-destination))
>>> + ("hba_file" ,@(with-single-quotes hba-file))
>>> + ("ident_file" ,@(with-single-quotes ident-file))
>> ^
>> Could you please use a shorter name? Like "enclose", so that we won't
>> go over 80 columns too easily :-).
>
> I went with quote' as I think that works well.

I don't like it because:

• The extra \' doesn't help describing what the function does. One
could believe it's a variant of 'quote', but it's actually very
different.

• It doesn't follow our coding style. See
written with English words separated by hyphens." See also the part
about "Funny Characters".

Toggle quote (2 lines)
> Thanks for taking a look and for your comments :)

You're welcome :-)

Clément
Closed
Christopher Baines wrote 7 years ago
(name . Clément Lassieur)(address . clement@lassieur.org)(address . 30701-done@debbugs.gnu.org)
87605ujv9k.fsf@cbaines.net
Clément Lassieur <clement@lassieur.org> writes:

Toggle quote (27 lines)
> Christopher Baines <mail@cbaines.net> writes:
>
>> Sorry, I made some changes, and merged these patches on the weekend, but
>> I forgot to reply.
>
> [...]
>
>>>> + `(("log_destination" ,@(with-single-quotes log-destination))
>>>> + ("hba_file" ,@(with-single-quotes hba-file))
>>>> + ("ident_file" ,@(with-single-quotes ident-file))
>>> ^
>>> Could you please use a shorter name? Like "enclose", so that we won't
>>> go over 80 columns too easily :-).
>>
>> I went with quote' as I think that works well.
>
> I don't like it because:
>
> • The extra \' doesn't help describing what the function does. One
> could believe it's a variant of 'quote', but it's actually very
> different.
>
> • It doesn't follow our coding style. See
> https://mumble.net/~campbell/scheme/style.txt. "Symbolic names are
> written with English words separated by hyphens." See also the part
> about "Funny Characters".

Fair enough, I've changed it to single-quote in
533808383f7fca6563aee1452f5202e0cd1b66b8.
-----BEGIN PGP SIGNATURE-----

iQKTBAEBCgB9FiEEPonu50WOcg2XVOCyXiijOwuE9XcFAlqte/dfFIAAAAAALgAo
aXNzdWVyLWZwckBub3RhdGlvbnMub3BlbnBncC5maWZ0aGhvcnNlbWFuLm5ldDNF
ODlFRUU3NDU4RTcyMEQ5NzU0RTBCMjVFMjhBMzNCMEI4NEY1NzcACgkQXiijOwuE
9XcoBQ/9GO/M1sPFvNRFo2IebyOsOP4p02Hymzm/s43Li9ElbMXM44eO+7OE4wJD
Ht36t1S9o33rymGOludv4mHn6JQipzg0ofWLTgsVfBqWVJ3YZGsNXq3kqMBxzN6w
RvuRK/J5HSjNOZcshclllN85uDeKx6t4FiQLHkwLTRV1JC7bX6tHMwYgk4L6LPrs
MIsjzZcq65+IzdlWWZyy/ZM9WrQhD8a3WE2jNwB5WZ3TmFEKtFBP2g7nui1TUk/l
5GOh4EYBkN9hMe7xFfJGKgaxI03bylLdHWCNpG3woowF5/tFdc1q4xvVUX+dVnZe
kdGT9QDhm+/jhV3FMiC8ODB8VIQgzt2lJ8zKjVspoBD11WQqf/FjT+1M5sERRLnd
RpvDjKmhMcbAGWy9vU7wu8/TG9ZjI8AGj5R1poZJOQ15dBcBY5SAMMs5b/dbvgRp
7O8aDO7V79dMGrQqx9IpUdmFJT2rmnWy/l/pUkitw6Oh9+yjsVEhs5q9pk1msMfk
++lZ9b0UjFcik00CF2XJ/R8yijCNsi4RDv9FpyF0rPlEWyltBwUb5MGHEeEHOf/S
HAtbozuig1NMhzDE1V7K8MU+pF/fGGjlNdfQIaFZp2W8r2fJuCztfulG6TqPT9Qc
4JUgiRWE2swPIpTakXfLhCjTyHsRhDt803QgHH5P/JqjbEFENdE=
=nMnK
-----END PGP SIGNATURE-----

Closed
Clément Lassieur wrote 7 years ago
Re: [bug#30701] [PATCH 1/3] services: Rework the PostgreSQL config file to use a record type.
(name . Christopher Baines)(address . mail@cbaines.net)(address . 30701-done@debbugs.gnu.org)
87fu4yfcgn.fsf@lassieur.org
Christopher Baines <mail@cbaines.net> writes:

Toggle quote (32 lines)
> Clément Lassieur <clement@lassieur.org> writes:
>
>> Christopher Baines <mail@cbaines.net> writes:
>>
>>> Sorry, I made some changes, and merged these patches on the weekend, but
>>> I forgot to reply.
>>
>> [...]
>>
>>>>> + `(("log_destination" ,@(with-single-quotes log-destination))
>>>>> + ("hba_file" ,@(with-single-quotes hba-file))
>>>>> + ("ident_file" ,@(with-single-quotes ident-file))
>>>> ^
>>>> Could you please use a shorter name? Like "enclose", so that we won't
>>>> go over 80 columns too easily :-).
>>>
>>> I went with quote' as I think that works well.
>>
>> I don't like it because:
>>
>> • The extra \' doesn't help describing what the function does. One
>> could believe it's a variant of 'quote', but it's actually very
>> different.
>>
>> • It doesn't follow our coding style. See
>> https://mumble.net/~campbell/scheme/style.txt. "Symbolic names are
>> written with English words separated by hyphens." See also the part
>> about "Funny Characters".
>
> Fair enough, I've changed it to single-quote in
> 533808383f7fca6563aee1452f5202e0cd1b66b8.

Thank you Christopher!
Closed
?
Your comment

This issue is archived.

To comment on this conversation send an email to 30701@debbugs.gnu.org

To respond to this issue using the mumi CLI, first switch to it
mumi current 30701
Then, you may apply the latest patchset in this issue (with sign off)
mumi am -- -s
Or, compose a reply to this issue
mumi compose
Or, send patches to this issue
mumi send-email *.patch
You may also tag this issue. See list of standard tags. For example, to set the confirmed and easy tags
mumi command -t +confirmed -t +easy
Or, remove the moreinfo tag and set the help tag
mumi command -t -moreinfo -t +help