Skip to content

Commit

Permalink
CI: write $CI_DESIRED_DATABASE to containers.conf
Browse files Browse the repository at this point in the history
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
edsantiago committed Mar 21, 2023
1 parent 58d4b15 commit 8fd2bd5
Show file tree
Hide file tree
Showing 3 changed files with 210 additions and 2 deletions.
6 changes: 5 additions & 1 deletion contrib/cirrus/setup_environment.sh
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ case "$CG_FS_TYPE" in
if ((CONTAINER==0)); then
warn "Forcing testing with runc instead of crun"
echo "OCI_RUNTIME=runc" >> /etc/ci_environment
printf "[engine]\nruntime=\"runc\"\n" >>/etc/containers/containers.conf
hack/toml-edit "engine.runtime=runc"
fi
;;
cgroup2fs)
Expand All @@ -93,6 +93,10 @@ case "$CG_FS_TYPE" in
*) die_unknown CG_FS_TYPE
esac

# Force the requested database backend without having to use command-line args
# shellcheck disable=SC2154
hack/toml-edit "engine.database_backend=$CI_DESIRED_DATABASE"

if ((CONTAINER==0)); then # Not yet running inside a container
# Discovered reemergence of BFQ scheduler bug in kernel 5.8.12-200
# which causes a kernel panic when system is under heavy I/O load.
Expand Down
205 changes: 205 additions & 0 deletions hack/toml-edit
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;
1 change: 0 additions & 1 deletion test/system/005-info.bats
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ host.slirp4netns.executable | $expr_path
}

@test "podman info - confirm desired database" {
skip "FIXME: no way yet (2023-03-16) to override DB in system tests"
if [[ -z "$CI_DESIRED_DATABASE" ]]; then
# When running in Cirrus, CI_DESIRED_DATABASE *must* be defined
# in .cirrus.yml so we can double-check that all CI VMs are
Expand Down

0 comments on commit 8fd2bd5

Please sign in to comment.