1
0

manage_debconf.pl 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. use Debconf::Client::ConfModule (qw/get set/);
  16. # map from the name of a setting in our .yaml file to the relevant debconf
  17. # setting.
  18. my %MAPPINGS=(
  19. server_name => 'matrix-synapse/server-name',
  20. report_stats => 'matrix-synapse/report-stats',
  21. );
  22. # enable debug if dpkg --debug
  23. my $DEBUG = $ENV{DPKG_MAINTSCRIPT_DEBUG};
  24. sub read_config {
  25. my @files = @_;
  26. foreach my $file (@files) {
  27. print STDERR "reading $file\n" if $DEBUG;
  28. open my $FH, "<", $file or next;
  29. # rudimentary parsing which (a) avoids having to depend on a yaml library,
  30. # and (b) is tolerant of yaml errors
  31. while($_ = <$FH>) {
  32. while (my ($setting, $debconf) = each %MAPPINGS) {
  33. $setting = quotemeta $setting;
  34. if(/^${setting}\s*:(.*)$/) {
  35. my $val = $1;
  36. # remove leading/trailing whitespace
  37. $val =~ s/^\s*//;
  38. $val =~ s/\s*$//;
  39. # remove surrounding quotes
  40. if ($val =~ /^"(.*)"$/ || $val =~ /^'(.*)'$/) {
  41. $val = $1;
  42. }
  43. print STDERR ">> $debconf = $val\n" if $DEBUG;
  44. set($debconf, $val);
  45. }
  46. }
  47. }
  48. close $FH;
  49. }
  50. }
  51. sub update_config {
  52. my @files = @_;
  53. my %substs = ();
  54. while (my ($setting, $debconf) = each %MAPPINGS) {
  55. my @res = get($debconf);
  56. $substs{$setting} = $res[1] if $res[0] == 0;
  57. }
  58. foreach my $file (@files) {
  59. print STDERR "checking $file\n" if $DEBUG;
  60. open my $FH, "<", $file or next;
  61. my $updated = 0;
  62. # read the whole file into memory
  63. my @lines = <$FH>;
  64. while (my ($setting, $val) = each %substs) {
  65. $setting = quotemeta $setting;
  66. map {
  67. if (/^${setting}\s*:\s*(.*)\s*$/) {
  68. my $current = $1;
  69. if ($val ne $current) {
  70. $_ = "${setting}: $val\n";
  71. $updated = 1;
  72. }
  73. }
  74. } @lines;
  75. }
  76. close $FH;
  77. next unless $updated;
  78. print STDERR "updating $file\n" if $DEBUG;
  79. open $FH, ">", $file or die "unable to update $file";
  80. print $FH @lines;
  81. close $FH;
  82. }
  83. }
  84. my $cmd = $ARGV[0];
  85. my $read = 0;
  86. my $update = 0;
  87. if (not $cmd) {
  88. die "must specify a command to perform\n";
  89. } elsif ($cmd eq 'read') {
  90. $read = 1;
  91. } elsif ($cmd eq 'update') {
  92. $update = 1;
  93. } else {
  94. die "unknown command '$cmd'\n";
  95. }
  96. my @files = (
  97. "/etc/matrix-synapse/homeserver.yaml",
  98. glob("/etc/matrix-synapse/conf.d/*.yaml"),
  99. );
  100. if ($read) {
  101. read_config(@files);
  102. } elsif ($update) {
  103. update_config(@files);
  104. }