-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CI: write $CI_DESIRED_DATABASE to containers.conf
This should make system tests work with the requested DB Add new script, hack/toml-edit, to make it possible to update settings in containers.conf Signed-off-by: Ed Santiago <santiago@redhat.com>
- Loading branch information
1 parent
97f2107
commit d2c2cf4
Showing
3 changed files
with
210 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,205 @@ | ||
#!/usr/bin/perl | ||
# | ||
# toml-edit - create, update, edit toml files | ||
# | ||
package LibPod::TomlEdit; | ||
|
||
use v5.14; | ||
use utf8; | ||
|
||
use strict; | ||
use warnings; | ||
|
||
(our $ME = $0) =~ s|.*/||; | ||
|
||
############################################################################### | ||
# BEGIN user-customizable section | ||
|
||
# Unless overridden on command line, this is the file we create/update | ||
our $Default_Path = '/etc/containers/containers.conf'; | ||
|
||
# END user-customizable section | ||
############################################################################### | ||
# BEGIN boilerplate args checking, usage messages | ||
|
||
sub usage { | ||
print <<"END_USAGE"; | ||
Usage: $ME [OPTIONS] ARGS [...] | ||
$ME creates or updates TOML files: | ||
$ME /etc/containers/containers.conf engine.runtime=crun | ||
adds 'runtime="crun"' to the '[engine]' section | ||
$ME secrets.opts.root=/foo | ||
adds 'root="/foo"' to the '[secrets.opts]' section | ||
Note that: | ||
* non-digit values will always be quoted in double quotes | ||
* all dots except the last one become the [table.name]. (Yes, | ||
toml allows '[foo.bar] + key.subkey', but podman does not seem | ||
to use this. If podman ever does, we'll deal with that then. | ||
OPTIONS: | ||
-v, --verbose show verbose progress indicators | ||
-n, --dry-run make no actual changes | ||
--help display this message | ||
--version display program name and version | ||
END_USAGE | ||
|
||
exit; | ||
} | ||
|
||
# Command-line options. Note that this operates directly on @ARGV ! | ||
our $debug = 0; | ||
our $force = 0; | ||
our $verbose = 0; | ||
our $NOT = ''; # print "blahing the blah$NOT\n" if $debug | ||
sub handle_opts { | ||
use Getopt::Long; | ||
GetOptions( | ||
'debug!' => \$debug, | ||
'dry-run|n!' => sub { $NOT = ' [NOT]' }, | ||
'force' => \$force, | ||
'verbose|v' => \$verbose, | ||
|
||
help => \&usage, | ||
) or die "Try `$ME --help' for help\n"; | ||
} | ||
|
||
# END boilerplate args checking, usage messages | ||
############################################################################### | ||
|
||
############################## CODE BEGINS HERE ############################### | ||
|
||
# The term is "modulino". | ||
__PACKAGE__->main() unless caller(); | ||
|
||
# Main code. | ||
sub main { | ||
# Note that we operate directly on @ARGV, not on function parameters. | ||
# This is deliberate: it's because Getopt::Long only operates on @ARGV | ||
# and there's no clean way to make it use @_. | ||
handle_opts(); # will set package globals | ||
|
||
my $path = $Default_Path; | ||
my @updates; | ||
|
||
# If first arg is an absolute or relative path, that's the file we update | ||
if (@ARGV && $ARGV[0] =~ m!^[^=]*/!) { | ||
$path = shift(@ARGV); | ||
} | ||
|
||
# All other args must be table.field=value | ||
die "Usage: $ME something-to-do; try $ME --help\n" if !@ARGV; | ||
|
||
for my $arg (@ARGV) { | ||
$arg =~ /^(\S+)\.(\S+)=(.*)$/ | ||
or die "$ME: Cannot grok '$arg'; args must be 'foo.bar=value'\n"; | ||
push @updates, [ $1, $2, $3 ]; | ||
} | ||
|
||
do_updates($path, @updates); | ||
} | ||
|
||
################ | ||
# do_updates # Read existing file (if present), update keys, write it out | ||
################ | ||
sub do_updates { | ||
my ($path, @updates) = @_; | ||
|
||
my @existing; | ||
|
||
# Read | ||
if (-e $path) { | ||
open my $fh, '<', $path | ||
or die "$ME: Cannot read $path: $!\n"; | ||
while (my $line = <$fh>) { | ||
chomp $line; | ||
push @existing, $line; | ||
} | ||
close $fh; | ||
} | ||
|
||
# Merge | ||
my @merged = do_merge(\@existing, \@updates); | ||
|
||
# Write | ||
my $out_tmp = "$path.tmp.$$"; | ||
unlink $out_tmp; | ||
open my $fh, '>', $out_tmp | ||
or die "$ME: Cannot create $out_tmp: $!\n"; | ||
print { $fh } $_, "\n" for @merged; | ||
close $fh | ||
or die "$ME: Error writing $out_tmp: $!\n"; | ||
rename $out_tmp => $path | ||
or die "$ME: Could not rename $out_tmp: $!\n"; | ||
} | ||
|
||
############## | ||
# do_merge # update existing keys | ||
############## | ||
sub do_merge { | ||
my $existing = shift; | ||
my $updates = shift; | ||
my @merged; | ||
|
||
my %want_update; | ||
for my $tuple (@$updates) { | ||
my ($table, $key, $value) = @$tuple; | ||
|
||
# Quote non-numerics | ||
$value = "\"$value\"" | ||
if $value =~ /\D/ && $value !~ /^\".*\"$/; | ||
|
||
$want_update{$table}{$key} = sprintf("%s = %s", $key, $value); | ||
} | ||
|
||
my $in_table = ''; | ||
for my $line (@$existing) { | ||
push @merged, $line; | ||
|
||
next if $line =~ /^#/; # skip comments | ||
if ($line =~ /^\[(\S+)\]/) { | ||
my $new_table = $1; | ||
|
||
# Any un-updated keys for this table? | ||
# FIXME: push these *before* the new table! | ||
if ($want_update{$in_table}) { | ||
pop @merged; | ||
for my $k (sort keys %{$want_update{$in_table}}) { | ||
push @merged, delete($want_update{$in_table}{$k}); | ||
} | ||
delete $want_update{$in_table}; | ||
push @merged, $line; | ||
} | ||
|
||
$in_table = $new_table; | ||
} | ||
elsif ($line =~ /^\s*(\S+)\s*=\s*(.*)/) { | ||
my ($in_key, $in_value) = ($1, $2); | ||
$in_value =~ s/\s+$//; | ||
if ($want_update{$in_table}{$in_key}) { | ||
$merged[-1] = delete($want_update{$in_table}{$in_key}); | ||
} | ||
|
||
if (keys(%{$want_update{$in_table}}) == 0) { | ||
delete $want_update{$in_table}; | ||
} | ||
} | ||
} | ||
|
||
# Any tables we didn't see? | ||
for my $t (sort keys %want_update) { | ||
if (my @missing = sort values %{$want_update{$t}}) { | ||
push @merged, "", "[$t]" unless $t eq $in_table; | ||
push @merged, @missing; | ||
} | ||
} | ||
|
||
@merged; | ||
} | ||
|
||
1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters