Compare commits

...

3 Commits
main ... config

Author SHA1 Message Date
2efd59c518
Support multiline environment variables 2025-02-02 22:47:49 +01:00
d2bfbb076a
add function for getting early config without logging
This supports reading early config parameters from
environment variables or files.
2025-02-01 21:25:43 +01:00
4c3fabf014
add function to read config from environment var 2025-02-01 21:24:38 +01:00
2 changed files with 36 additions and 3 deletions

View File

@ -59,19 +59,19 @@ sub load_logging {
return if $LOGGING_LOADED; # already done return if $LOGGING_LOADED; # already done
return if $hooks->{'logging'}; # avoid triggering log activity return if $hooks->{'logging'}; # avoid triggering log activity
my @plugin_dirs = $self->conf->from_file('plugin_dirs'); my @plugin_dirs = $self->conf->early_config('plugin_dirs');
if (!@plugin_dirs) { if (!@plugin_dirs) {
my ($name) = ($0 =~ m!(.*?)/([^/]+)$!); my ($name) = ($0 =~ m!(.*?)/([^/]+)$!);
@plugin_dirs = ("$name/plugins"); @plugin_dirs = ("$name/plugins");
} }
my @loggers = $self->conf->from_file('logging'); my @loggers = $self->conf->early_config('logging');
for my $logger (@loggers) { for my $logger (@loggers) {
$self->_load_plugin($logger, @plugin_dirs); $self->_load_plugin($logger, @plugin_dirs);
$self->log(LOGINFO, "Loaded $logger"); $self->log(LOGINFO, "Loaded $logger");
} }
$TraceLevel = $self->conf->from_file('loglevel'); $TraceLevel = $self->conf->early_config('loglevel');
unless (defined($TraceLevel) && $TraceLevel =~ /^\d+$/) { unless (defined($TraceLevel) && $TraceLevel =~ /^\d+$/) {
$TraceLevel = LOGWARN; # Default if no loglevel file found. $TraceLevel = LOGWARN; # Default if no loglevel file found.

View File

@ -27,6 +27,17 @@ sub log {
warn join(' ', $$, @log) . "\n"; warn join(' ', $$, @log) . "\n";
} }
sub early_config {
my ($self, $config, $file, $visited) = @_;
my @config;
@config = $self->from_environment($config);
return wantarray ? @config : $config[0] if @config;
return $self->from_file($config, $file, $visited);
}
sub config { sub config {
my ($self, $qp, $c, $type) = @_; my ($self, $qp, $c, $type) = @_;
@ -34,6 +45,11 @@ sub config {
# first run the user_config hooks # first run the user_config hooks
my ($rc, @config); my ($rc, @config);
@config = $self->from_environment($c);
return wantarray ? @config : $config[0] if @config;
if (ref $type && UNIVERSAL::can($type, 'address')) { if (ref $type && UNIVERSAL::can($type, 'address')) {
($rc, @config) = $qp->run_hooks_no_respond('user_config', $type, $c); ($rc, @config) = $qp->run_hooks_no_respond('user_config', $type, $c);
if (defined $rc && $rc == OK) { if (defined $rc && $rc == OK) {
@ -127,6 +143,23 @@ sub get_qmail_map {
return \%h; return \%h;
} }
sub from_environment {
my ($self, $config) = @_;
return unless $config;
my $env_name = "QPSMTPD_" . $config;
return unless $ENV{$env_name};
my @config = split "\n", $ENV{$env_name};
chomp @config;
for (@config) { s/^\s+//; s/\s+$//; } # trim leading/trailing whitespace
return wantarray ? @config : $config[0];
}
sub from_file { sub from_file {
my ($self, $config, $file, $visited) = @_; my ($self, $config, $file, $visited) = @_;
$file ||= $self->config_dir($config) . "/$config"; $file ||= $self->config_dir($config) . "/$config";