- #!/usr/bin/perl
- use strict;
- use warnings;
- use autodie qw(:all);;
- use Capture::Tiny qw(capture_stdout);
- use Path::Tiny;
- use YAML::XS;
- use Try::Tiny;
- my $node = $ARGV[0];
- my $outdir = path($ARGV[1]);
- my $basedir = path('.');
- my $skeldir = path( $basedir, 'skel' );
- my $infile = path( $skeldir, 'preseed.cfg.in' );
- my $altinfile = path( $skeldir, 'script.sh.in' );
- my $data = Load( scalar( capture_stdout {
- system(qw( reclass -b profiles/wheezy --inventory ));
- }));
- my $outfile = path( $outdir, 'preseed.cfg' );
- my $altoutfile = path( $outdir, 'script.sh' );
- my %desc;
- my %params = %{ $data->{'nodes'}{$node}{'parameters'} };
- #FIXME: sort by explicit list
- foreach my $key ( sort keys $params{doc} ) {
- my $headline = $params{doc}{$key}{headline}[0] || $key;
- if ( $params{pkg} and $params{doc}{$key}{pkg} ) {
- push @{ $desc{pkg} }, "# $headline";
- foreach ( @{ $params{doc}{$key}{pkg} } ) {
- push @{ $desc{pkg} }, "# * $_";
- };
- };
- if ( $params{tweak} and $params{doc}{$key}{tweak} ) {
- push @{ $desc{tweak} }, "# $headline";
- foreach ( @{ $params{doc}{$key}{tweak} } ) {
- push @{ $desc{tweak} }, "# * $_";
- };
- };
- };
- my $pkgdesc = defined( $desc{pkg} )
- ? join( "\n", @{ $desc{pkg} } )
- : '';
- my $tweakdesc = defined( $desc{tweak} )
- ? join( "\n", @{ $desc{tweak} } )
- : '';
- my @pkg = try { @{ $params{pkg} } }
- catch { die "No packages resolved" };
- my @pkgauto = try { @{ $params{'pkg-auto'} } }
- catch { die "No package auto-markings resolved" };
- my @pkgavoid = try { @{ $params{'pkg-avoid'} } }
- catch { die "No package avoidance resolved" };
- my @tweak = try { @{ $params{tweak} } }
- catch { die "No tweaks resolved" };
- my $pkglist = join( ' ', sort @pkg );
- $pkglist .= " \\\n ";
- $pkglist .= join( ' ', sort map { $_.'-' } @pkgavoid );
- my $pkgautolist = join( ' ', sort @pkgauto );
- grep {chomp} @tweak;
- my $tweaklist = join( ";\\\n ", 'set -e', @tweak );
- $outdir->mkpath;
- $_ = $altinfile->slurp;
- s,__PKGDESC__,$pkgdesc,;
- s,__PKGLIST__,$pkglist,;
- s,__TWEAKDESC__,$tweakdesc,;
- s,__TWEAKLIST__,$tweaklist,;
- s,__PKGAUTOLIST__,$pkgautolist,;
- s,chroot\s+/target\s+,,g;
- s,/target/,/,g;
- $altoutfile->spew($_);
- $_ = $infile->slurp;
- s,__PKGDESC__,$pkgdesc,;
- s,__PKGLIST__,$pkglist,;
- s,__TWEAKDESC__,$tweakdesc,;
- s,__TWEAKLIST__,$tweaklist,;
- s,__PKGAUTOLIST__,$pkgautolist,;
- $outfile->spew($_);
- 1;
|