Skip to content

Instantly share code, notes, and snippets.

@baby-gnu
Last active April 1, 2021 13:43
Show Gist options
  • Save baby-gnu/f7b14e8eed4b6f7fa10443c0d9bc36f6 to your computer and use it in GitHub Desktop.
Save baby-gnu/f7b14e8eed4b6f7fa10443c0d9bc36f6 to your computer and use it in GitHub Desktop.
Tool to extract the mapdata dumps from kitchen containers and store them under `test/integration/default/files/_mapdata`
#!/usr/bin/perl
# This script is used to split the content of mapdata dumps extracted
# from the kitchen containers:
#
# 0. make sure to have <FORMULA>._mapdata in kitchen.yml suites
# 1. build all platforms: ./bin/kitchen converge
# 2. extract the YAML dumps: ./bin/kitchen exec -c 'base64 /tmp/salt_mapdata_dump.yaml' | tee /tmp/kitchen_mapdata_dumps.txt
# 3. split the mapdata dumps per platform: split-platforms-mapdata-dumps /tmp/kitchen_mapdata_dumps.txt
use 5.20.0;
use utf8;
use warnings qw< FATAL utf8 >;
no warnings qw< experimental >;
use open qw< :std :utf8 >;
use File::Path qw< make_path >;
use MIME::Base64 qw< decode_base64 >;
END { close STDOUT; close STDERR; }
exit main ( @ARGV );
sub main {
my $profile_name = $_[1] || 'default';
my $profile_dir = "test/integration/$profile_name";
my $_mapdata_dir = join('/', $profile_dir, 'files', '_mapdata');
die "Unable to access $profile_dir" unless -d ${profile_dir};
die "Missing output file name" unless $_[0];
make_path($_mapdata_dir) unless -d $_mapdata_dir;
open(my $content, '<', "$_[0]")
or die "$0: can't open file '$_[0]' for reading: $!";
my @parts;
my $output;
my $b64_encoded;
while ( my $line = <$content> ) {
if ($line =~ /^-+> Execute command on (.*)/ ) {
dump_yaml($output, $b64_encoded);
# Close any previously opened file handle;
close $output if $output;
$b64_encoded = '';
@parts = split(/-/, $1);
my $family = $parts[1];
my $release;
for ($family) {
$release = substr($parts[2], 0, 2) when /^ubuntu$/;
$release = join('-', @parts[2,3]) when /^arch$/;
$release = build_opensuse_release(@parts) when /^opensuse$/;
$release = build_gentoo_release(@parts) when /^gentoo$/;
default { $release = $parts[2] };
}
my $output_filename = join('/', $_mapdata_dir, "$family-$release.yaml");
if (-f $output_filename) {
say "Skip map.jinja reference file '$output_filename': already exists";
next;
}
say "Create map.jinja reference file '$output_filename'";
open($output, '>', $output_filename)
or die "$0: can't open file '$output_filename' for writing: $!";
} elsif ( $output and fileno($output) ) {
# Gather lines only when the output file is open
# Strip leading spaces
$b64_encoded .= substr($line, 7);
}
}
dump_yaml($output, $b64_encoded);
}
sub build_opensuse_release {
my @parts = @_;
my $release;
for ( $parts[2] ) {
$release = 'tumbleweed' when /^tmbl$/;
default { $release = substr($parts[3], 0, 2) };
}
return $release;
}
sub build_gentoo_release {
my @parts = @_;
my $release;
for ( $parts[3] ) {
# Use static 2 prefix since we can't deduce it from kitchen output
$release = '2-sysv' when /^latest$/;
$release = '2-sysd' when /^systemd$/;
}
return $release;
}
sub dump_yaml {
my ($output, $b64_encoded) = @_;
if ( $output and fileno($output) ) {
say $output decode_base64($b64_encoded);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment