Skip to content

Commit 0969d68

Browse files
authoredFeb 22, 2019
Debian: fix overwriting of config settings on upgrade (#4696)
Make sure that users' changes to the config files are preserved. Fixes #4440.
1 parent e07384c commit 0969d68

5 files changed

+165
-8
lines changed
 

‎debian/changelog

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
matrix-synapse-py3 (0.99.2) UNRELEASED; urgency=medium
2+
3+
* Fix overwriting of config settings on upgrade.
4+
5+
-- Synapse Packaging team <packages@matrix.org> Wed, 20 Feb 2019 17:11:25 +0000
6+
17
matrix-synapse-py3 (0.99.1.1) stable; urgency=medium
28

39
* New synapse release 0.99.1.1

‎debian/install

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
debian/log.yaml etc/matrix-synapse
2+
debian/manage_debconf.pl /opt/venvs/matrix-synapse/lib/

‎debian/manage_debconf.pl

+130
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
#!/usr/bin/perl
2+
#
3+
# Interface between our config files and the debconf database.
4+
#
5+
# Usage:
6+
#
7+
# manage_debconf.pl <action>
8+
#
9+
# where <action> can be:
10+
#
11+
# read: read the configuration from the yaml into debconf
12+
# update: update the yaml config according to the debconf database
13+
use strict;
14+
use warnings;
15+
16+
use Debconf::Client::ConfModule (qw/get set/);
17+
18+
# map from the name of a setting in our .yaml file to the relevant debconf
19+
# setting.
20+
my %MAPPINGS=(
21+
server_name => 'matrix-synapse/server-name',
22+
report_stats => 'matrix-synapse/report-stats',
23+
);
24+
25+
# enable debug if dpkg --debug
26+
my $DEBUG = $ENV{DPKG_MAINTSCRIPT_DEBUG};
27+
28+
sub read_config {
29+
my @files = @_;
30+
31+
foreach my $file (@files) {
32+
print STDERR "reading $file\n" if $DEBUG;
33+
34+
open my $FH, "<", $file or next;
35+
36+
# rudimentary parsing which (a) avoids having to depend on a yaml library,
37+
# and (b) is tolerant of yaml errors
38+
while($_ = <$FH>) {
39+
while (my ($setting, $debconf) = each %MAPPINGS) {
40+
$setting = quotemeta $setting;
41+
if(/^${setting}\s*:(.*)$/) {
42+
my $val = $1;
43+
44+
# remove leading/trailing whitespace
45+
$val =~ s/^\s*//;
46+
$val =~ s/\s*$//;
47+
48+
# remove surrounding quotes
49+
if ($val =~ /^"(.*)"$/ || $val =~ /^'(.*)'$/) {
50+
$val = $1;
51+
}
52+
53+
print STDERR ">> $debconf = $val\n" if $DEBUG;
54+
set($debconf, $val);
55+
}
56+
}
57+
}
58+
close $FH;
59+
}
60+
}
61+
62+
sub update_config {
63+
my @files = @_;
64+
65+
my %substs = ();
66+
while (my ($setting, $debconf) = each %MAPPINGS) {
67+
my @res = get($debconf);
68+
$substs{$setting} = $res[1] if $res[0] == 0;
69+
}
70+
71+
foreach my $file (@files) {
72+
print STDERR "checking $file\n" if $DEBUG;
73+
74+
open my $FH, "<", $file or next;
75+
76+
my $updated = 0;
77+
78+
# read the whole file into memory
79+
my @lines = <$FH>;
80+
81+
while (my ($setting, $val) = each %substs) {
82+
$setting = quotemeta $setting;
83+
84+
map {
85+
if (/^${setting}\s*:\s*(.*)\s*$/) {
86+
my $current = $1;
87+
if ($val ne $current) {
88+
$_ = "${setting}: $val\n";
89+
$updated = 1;
90+
}
91+
}
92+
} @lines;
93+
}
94+
close $FH;
95+
96+
next unless $updated;
97+
98+
print STDERR "updating $file\n" if $DEBUG;
99+
open $FH, ">", $file or die "unable to update $file";
100+
print $FH @lines;
101+
close $FH;
102+
}
103+
}
104+
105+
106+
my $cmd = $ARGV[0];
107+
108+
my $read = 0;
109+
my $update = 0;
110+
111+
if (not $cmd) {
112+
die "must specify a command to perform\n";
113+
} elsif ($cmd eq 'read') {
114+
$read = 1;
115+
} elsif ($cmd eq 'update') {
116+
$update = 1;
117+
} else {
118+
die "unknown command '$cmd'\n";
119+
}
120+
121+
my @files = (
122+
"/etc/matrix-synapse/homeserver.yaml",
123+
glob("/etc/matrix-synapse/conf.d/*.yaml"),
124+
);
125+
126+
if ($read) {
127+
read_config(@files);
128+
} elsif ($update) {
129+
update_config(@files);
130+
}

‎debian/config ‎debian/matrix-synapse-py3.config

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ set -e
44

55
. /usr/share/debconf/confmodule
66

7+
# try to update the debconf db according to whatever is in the config files
8+
/opt/venvs/matrix-synapse/lib/manage_debconf.pl read || true
9+
710
db_input high matrix-synapse/server-name || true
811
db_input high matrix-synapse/report-stats || true
912
db_go

‎debian/matrix-synapse-py3.postinst

+25-8
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,36 @@ USER="matrix-synapse"
88

99
case "$1" in
1010
configure|reconfigure)
11-
# Set server name in config file
12-
mkdir -p "/etc/matrix-synapse/conf.d/"
13-
db_get matrix-synapse/server-name
1411

15-
if [ "$RET" ]; then
16-
echo "server_name: $RET" > $CONFIGFILE_SERVERNAME
12+
# generate template config files if they don't exist
13+
mkdir -p "/etc/matrix-synapse/conf.d/"
14+
if [ ! -e "$CONFIGFILE_SERVERNAME" ]; then
15+
cat > "$CONFIGFILE_SERVERNAME" <<EOF
16+
# This file is autogenerated, and will be recreated on upgrade if it is deleted.
17+
# Any changes you make will be preserved.
18+
19+
# The domain name of the server, with optional explicit port.
20+
# This is used by remote servers to connect to this server,
21+
# e.g. matrix.org, localhost:8080, etc.
22+
# This is also the last part of your UserID.
23+
#
24+
server_name: ''
25+
EOF
1726
fi
1827

19-
db_get matrix-synapse/report-stats
20-
if [ "$RET" ]; then
21-
echo "report_stats: $RET" > $CONFIGFILE_REPORTSTATS
28+
if [ ! -e "$CONFIGFILE_REPORTSTATS" ]; then
29+
cat > "$CONFIGFILE_REPORTSTATS" <<EOF
30+
# This file is autogenerated, and will be recreated on upgrade if it is deleted.
31+
# Any changes you make will be preserved.
32+
33+
# Whether to report anonymized homeserver usage statistics.
34+
report_stats: false
35+
EOF
2236
fi
2337

38+
# update the config files according to whatever is in the debconf database
39+
/opt/venvs/matrix-synapse/lib/manage_debconf.pl update
40+
2441
if ! getent passwd $USER >/dev/null; then
2542
adduser --quiet --system --no-create-home --home /var/lib/matrix-synapse $USER
2643
fi

0 commit comments

Comments
 (0)
Please sign in to comment.