summaryrefslogtreecommitdiff
path: root/bin/pkglist2preseed
blob: 1082111bb632d101abf3b8b6d1774b4f3e30b64d (plain)
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use autodie qw(:all);;
  5. use Capture::Tiny qw(capture_stdout);
  6. use Path::Tiny;
  7. use YAML::XS;
  8. use Try::Tiny;
  9. my $node = $ARGV[0];
  10. my $outdir = path($ARGV[1]);
  11. my $basedir = path('.');
  12. my $skeldir = path( $basedir, 'skel' );
  13. my $infile = path( $skeldir, 'preseed.cfg.in' );
  14. my $altinfile = path( $skeldir, 'script.sh.in' );
  15. my $data = Load( scalar( capture_stdout {
  16. system(qw( reclass -b profiles/wheezy --inventory ));
  17. }));
  18. my $outfile = path( $outdir, 'preseed.cfg' );
  19. my $altoutfile = path( $outdir, 'script.sh' );
  20. my %desc;
  21. my %params = %{ $data->{'nodes'}{$node}{'parameters'} };
  22. #FIXME: sort by explicit list
  23. foreach my $key ( sort keys $params{doc} ) {
  24. my $headline = $params{doc}{$key}{headline}[0] || $key;
  25. if ( $params{pkg} and $params{doc}{$key}{pkg} ) {
  26. push @{ $desc{pkg} }, "# $headline";
  27. foreach ( @{ $params{doc}{$key}{pkg} } ) {
  28. push @{ $desc{pkg} }, "# * $_";
  29. };
  30. };
  31. if ( $params{tweak} and $params{doc}{$key}{tweak} ) {
  32. push @{ $desc{tweak} }, "# $headline";
  33. foreach ( @{ $params{doc}{$key}{tweak} } ) {
  34. push @{ $desc{tweak} }, "# * $_";
  35. };
  36. };
  37. };
  38. my $pkgdesc = defined( $desc{pkg} )
  39. ? join( "\n", @{ $desc{pkg} } )
  40. : '';
  41. my $tweakdesc = defined( $desc{tweak} )
  42. ? join( "\n", @{ $desc{tweak} } )
  43. : '';
  44. my @pkg = try { @{ $params{pkg} } }
  45. catch { die "No packages resolved" };
  46. my @pkgauto = try { @{ $params{'pkg-auto'} } }
  47. catch { die "No package auto-markings resolved" };
  48. my @pkgavoid = try { @{ $params{'pkg-avoid'} } }
  49. catch { die "No package avoidance resolved" };
  50. my @tweak = try { @{ $params{tweak} } }
  51. catch { die "No tweaks resolved" };
  52. my $pkglist = join( ' ', sort @pkg );
  53. $pkglist .= " \\\n ";
  54. $pkglist .= join( ' ', sort map { $_.'-' } @pkgavoid );
  55. my $pkgautolist = join( ' ', sort @pkgauto );
  56. grep {chomp} @tweak;
  57. my $tweaklist = join( ";\\\n ", 'set -e', @tweak );
  58. $outdir->mkpath;
  59. $_ = $altinfile->slurp;
  60. s,__PKGDESC__,$pkgdesc,;
  61. s,__PKGLIST__,$pkglist,;
  62. s,__TWEAKDESC__,$tweakdesc,;
  63. s,__TWEAKLIST__,$tweaklist,;
  64. s,__PKGAUTOLIST__,$pkgautolist,;
  65. s,chroot\s+/target\s+,,g;
  66. s,/target/,/,g;
  67. $altoutfile->spew($_);
  68. $_ = $infile->slurp;
  69. s,__PKGDESC__,$pkgdesc,;
  70. s,__PKGLIST__,$pkglist,;
  71. s,__TWEAKDESC__,$tweakdesc,;
  72. s,__TWEAKLIST__,$tweaklist,;
  73. s,__PKGAUTOLIST__,$pkgautolist,;
  74. $outfile->spew($_);
  75. 1;