Skip to content

Instantly share code, notes, and snippets.

@smoser
Created August 9, 2012 02:20
Show Gist options
  • Save smoser/3300362 to your computer and use it in GitHub Desktop.
Save smoser/3300362 to your computer and use it in GitHub Desktop.
debconf-get-selections
#!/usr/bin/perl
#
# The goal of this is to read a bunch of variables from debconf
# To implement a "late_command_runparts" like function.
# Ie, you would populate the preseed with content like:
# late-command-d late-command/90-fix-hosts string content-for-fix-hosts
# late-command-d late-command/10-install-my-packages string payload
#
# Then, in the actual late_command, we would suck out all those
# values, and run them by doing something like this:
# mkdir /root/late-command.d
# ./late-command-d late-command/ /root/late-command.d
# run-parts /root/late-command.d
#
# TODO:
# * remove the variables from debconf after used
# * expect values to be base64 encoded and base64 decode them
# or you have to debconf-escape them.
use strict;
use warnings;
use Debconf::Db;
use MIME::Base64;
#sub debconf_unescape {
# my($unescaped, $escaped)
# $unescaped = @_[0]
# for (split(/(\\.)/, $input) {
# s/\\(.)/$1 eq "n" ? "\n" : $1/eg;
# print;
# }
#}
Debconf::Db->load(readonly => "true");
my $prefix = $ARGV[0];
my $prefix_len = length($prefix);
my $out_d = $ARGV[1];
if (! -d $out_d) {
mkdir($out_d) or die "Cannot make directory: $out_d";
}
my $qi = Debconf::Question->iterator;
while (my $q = $qi->iterate) {
my ($name, $type, $value) = ($q->name, $q->type, $q->value);
next if (substr($name, 0, $prefix_len) ne $prefix);
my $fname = substr($name, $prefix_len);
my $fpath = $out_d . "/" . $fname;
my $decoded = "";
print $fname . "\n";
if ($type eq "base64") {
$decoded = decode_base64($value);
} else {
$decoded = decode_unescape($value);
}
open(FNAME, ">$fpath") or die $!;
print FNAME "$value";
close(FNAME);
}
@smoser
Copy link
Author

smoser commented Sep 10, 2012

Note to self: type cannot be "base64" (debconf will complain), so I considered pre/postfixing name with "b64-" to indicate payload was base64.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment