90ba31
#!/usr/bin/perl -w
90ba31
#
90ba31
# Copyright Lars Vogdt <lars@linux-schulserver.de>
90ba31
#
90ba31
# This program is free software; you can redistribute it and/or
90ba31
# modify it under the terms of the GNU General Public License
90ba31
# as published by the Free Software Foundation; either version 2
90ba31
# of the License, or (at your option) any later version.
90ba31
#
90ba31
# This program is distributed in the hope that it will be useful,
90ba31
# but WITHOUT ANY WARRANTY; without even the implied warranty of
90ba31
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
90ba31
# GNU General Public License for more details.
90ba31
#
90ba31
# You should have received a copy of the GNU General Public License
90ba31
# along with this program; if not, write to the
90ba31
# Free Software Foundation, Inc.,
90ba31
# 51 Franklin Street,
90ba31
# Fifth Floor,
90ba31
# Boston, MA  02110-1301,
90ba31
# USA.
90ba31
#
90ba31
use strict;
90ba31
use warnings;
90ba31
use Data::Dumper;
90ba31
use File::Basename;
90ba31
use Getopt::Long;
90ba31
use Scalar::Util qw(reftype);
90ba31
use YAML::Tiny;
90ba31
90ba31
my $pillar_id_path='pillar/id';
90ba31
my @wanted=qw(aliases responsible virt_cluster weburls hostusage partners description documentation);
90ba31
my $print_help='';
90ba31
my $full_html='';
90ba31
my $dumpforredmine='';
90ba31
my $header='';
90ba31
my $footer='';
90ba31
my $table_style="width='1820' border='1' style='table-layout:fixed;'";
90ba31
my $td_style="style='word-wrap:break-word; padding-left:.5em'";
90ba31
my $td_color_even='#f6f6f6';
90ba31
my $td_color_odd='fcf8e3';
90ba31
my $freeipa_user_url='https://freeipa.infra.opensuse.org/ipa/ui/#/e/user/details';
90ba31
90ba31
sub getFiles($$) {
90ba31
    my $path=shift;
90ba31
    my $ending=shift;
90ba31
    opendir(DIR,"$path") || die ("Could not open $path: $! $?!\n");
90ba31
    my @Files= grep {/.*\Q$ending\E/} readdir(DIR);
90ba31
    closedir(DIR);
90ba31
    my $number = @Files;
90ba31
    if ( $number gt 0 ){
90ba31
      my @return;
90ba31
      foreach (@Files){
90ba31
        next if ( $_ eq "." );
90ba31
        next if ( $_ eq ".." );
90ba31
        push @return,$_;
90ba31
      }
90ba31
     return \@return;
90ba31
    }
90ba31
 return 0;
90ba31
}
90ba31
90ba31
sub usage($){
90ba31
    my $exitcode=shift || 1;
90ba31
    print <
90ba31
90ba31
$0 Usage Information:
90ba31
90ba31
 $0 [OPTION]
90ba31
90ba31
 -d <path> | --pillardir <path>  : Path to the files with the Pillar information.
90ba31
 -t        | --tableonly         : Print only the HTML table (for inclusion in Redmine).
90ba31
Christian Boltz 1f004e
 -r        | --dumpforredmine    : Print including a short manual for direct copy and paste into the Redmine wiki.
90ba31
 -h        | --help              : This help
90ba31
90ba31
Use this script to generate a HTML page (or just a HTML table), based on the 
90ba31
static pillar information provided in YAML files.
90ba31
Currently the list of tags that end up in the HTML is hardcoded in the script. If you 
90ba31
want to change it, please edit the line starting with 
90ba31
  my \@wanted=...
90ba31
90ba31
90ba31
EOF
90ba31
;
90ba31
90ba31
   exit $exitcode;
90ba31
}
90ba31
90ba31
my $redmine_header="
90ba31
Machines
90ba31
========
90ba31
90ba31
This is a placeholder for some wiki pages containing more details about machines and their setups. The list below is generated based on the [pillar/id](https://gitlab.infra.opensuse.org/infra/salt/tree/production/pillar/id) information in [our Salt Repo in Gitlab](https://gitlab.infra.opensuse.org/).
90ba31
90ba31
You need a GIT checkout of the repository and the following perl modules installed on your system:
90ba31
90ba31
```bash
90ba31
  sudo zypper in 'perl(File::Basename)' 'perl(Getopt::Long)' 'perl(Scalar::Util)' 'perl(YAML::Tiny)'
90ba31
```
90ba31
90ba31
After that you should be able to call the script from the root of the checked out Salt repository:
90ba31
90ba31
```bash
90ba31
  bin/generate_redmine_page.pl -t
90ba31
```
90ba31
90ba31
* If you are administrator of a machine, please keep the information of the machine in GIT up to date, so others might be able to step in if there is a problem with the machine and you are not available.
90ba31
* If you are 'just interested' or needed to fix something on the machine, please get in contact with the administrator mentioned
90ba31
90ba31
List of machines
90ba31
================
90ba31
";
90ba31
90ba31
Getopt::Long::Configure('bundling');
90ba31
90ba31
GetOptions(    
90ba31
    'h|help'            => \$print_help,
90ba31
    'd|pillardir=s'     => \$pillar_id_path,
90ba31
    'r|dumpforredmine'  => \$dumpforredmine,
90ba31
    't|tableonly'       => \$full_html,
90ba31
);
90ba31
90ba31
usage(0) if ($print_help);
90ba31
90ba31
90ba31
90ba31
my $sls_files=getFiles($pillar_id_path,'.sls');
90ba31
90ba31
if (! $full_html){
90ba31
$header="
90ba31
<html>
90ba31
  <head>
90ba31
    <meta charset='utf-8'>
90ba31
    <title>Machine list</title>
90ba31
    <style type='text/css' media='print'>
90ba31
      \@page
90ba31
      {
90ba31
          size: auto; /* auto is the initial value */
90ba31
          margin: 2mm 4mm 2mm 4mm; /* this affects the margin in the printer settings */
90ba31
      }
90ba31
	  thead {display: table-header-group;}
90ba31
    </style>
90ba31
	<style type='text/css' media='screen'>
90ba31
      /* thead { display: block; } */
90ba31
      ul { list-style-type: disc; 
90ba31
           padding-left:1.5em; }
90ba31
    </style>
90ba31
  </head>
90ba31
  <body>\n";
90ba31
$footer="  </body>
90ba31
</html>";
90ba31
}
90ba31
90ba31
if (! $dumpforredmine){
90ba31
	print "$header";
90ba31
}
90ba31
90ba31
my $javascript="
90ba31
<script>
90ba31
function sortTable(n) {
90ba31
  var table, rows, switching, i, x, y, shouldSwitch, dir, switchcount = 0;
90ba31
  table = document.getElementById('machines');
90ba31
  switching = true;
90ba31
  // Set the sorting direction to ascending:
90ba31
  dir = 'asc';
90ba31
  /* Make a loop that will continue until
90ba31
  no switching has been done: */
90ba31
  while (switching) {
90ba31
    // Start by saying: no switching is done:
90ba31
    switching = false;
90ba31
    rows = table.rows;
90ba31
    /* Loop through all table rows (except the
90ba31
    first, which contains table headers): */
90ba31
    for (i = 1; i < (rows.length - 1); i++) {
90ba31
      // Start by saying there should be no switching:
90ba31
      shouldSwitch = false;
90ba31
      /* Get the two elements you want to compare,
90ba31
      one from current row and one from the next: */
90ba31
      x = rows[i].getElementsByTagName('TD')[n];
90ba31
      y = rows[i + 1].getElementsByTagName('TD')[n];
90ba31
      /* Check if the two rows should switch place,
90ba31
      based on the direction, asc or desc: */
90ba31
      if (dir == 'asc') {
90ba31
        if (x.innerHTML.toLowerCase() > y.innerHTML.toLowerCase()) {
90ba31
          // If so, mark as a switch and break the loop:
90ba31
          shouldSwitch = true;
90ba31
          break;
90ba31
        }
90ba31
      } else if (dir == 'desc') {
90ba31
        if (x.innerHTML.toLowerCase() < y.innerHTML.toLowerCase()) {
90ba31
          // If so, mark as a switch and break the loop:
90ba31
          shouldSwitch = true;
90ba31
          break;
90ba31
        }
90ba31
      }
90ba31
    }
90ba31
    if (shouldSwitch) {
90ba31
      /* If a switch has been marked, make the switch
90ba31
      and mark that a switch has been done: */
90ba31
      rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
90ba31
      switching = true;
90ba31
      // Each time a switch is done, increase this count by 1:
90ba31
      switchcount ++;
90ba31
    } else {
90ba31
      /* If no switching has been done AND the direction is asc,
90ba31
      set the direction to desc and run the while loop again. */
90ba31
      if (switchcount == 0 && dir == 'asc') {
90ba31
        dir = 'desc';
90ba31
        switching = true;
90ba31
      }
90ba31
    }
90ba31
  }
90ba31
}
90ba31
</script>";
90ba31
90ba31
if ( "$sls_files" ne "0" ){
90ba31
    my $i=1;
90ba31
	if ( $dumpforredmine ){
90ba31
		print "$redmine_header\n";	
90ba31
	}
Stasiek Michalski a6c237
  # print "$javascript\n";
ccad43
    print "<theader>\n";
ccad43
#	print "|# ";
ccad43
#	print "|Hostname ";
ccad43
	print "#Hostname";
90ba31
	my $td=1;
90ba31
    foreach my $entry (sort(@wanted)){
ccad43
	    print "$entry";
90ba31
    }
ccad43
        print "\n";
ccad43
        print "</theader>\n";
ccad43
#    foreach my $entry (sort(@wanted)){
ccad43
#            print "|--- ";
ccad43
#    }
ccad43
#        print "|\n";
90ba31
90ba31
    for my $file (sort (@$sls_files)){
90ba31
	my $hostname=basename("$file",'.sls');
90ba31
	$hostname=~ s/_/./g;
ccad43
   	print "$i$hostname";
90ba31
	my $yaml = YAML::Tiny->read("$pillar_id_path/$file");
90ba31
	my $grains=$yaml->[0];
90ba31
	# print Data::Dumper->Dump([$grains])."\n";
90ba31
	foreach my $entry (sort(@wanted)){
ccad43
		print "";
90ba31
		if (defined($grains->{'grains'}->{$entry})){
90ba31
			my $type=reftype $grains->{'grains'}->{$entry};
90ba31
			if (defined($type) && "$type" eq 'ARRAY'){
ccad43
				print "
    \n";
90ba31
				if (@{$grains->{'grains'}->{$entry}} > 0){
90ba31
					foreach my $string (sort(@{$grains->{'grains'}->{$entry}})){
90ba31
						if ("$string" =~ m/^http.*/){
ccad43
							print "
  • [$string]($string)
  • \n";
    90ba31
    						} 
    90ba31
    						else {
    90ba31
    							if ("$entry" eq "partners"){
    ccad43
    								print "
  • [$string](#$string)
  • \n";
    90ba31
    							}
    90ba31
    							elsif ("$entry" eq "responsible"){
    ccad43
    								print "
  • [$string]($freeipa_user_url/$string)
  • \n";
    90ba31
    							}
    90ba31
    							else {
    ccad43
    								print "
  • $string
  • \n";
    90ba31
    							}
    90ba31
    						}
    90ba31
    					}
    90ba31
    				}
    ccad43
    				print "\”";
    90ba31
    			}
    90ba31
    			else {
    90ba31
    				print "$grains->{'grains'}->{$entry}" if ($grains->{'grains'}->{$entry} ne "");
    90ba31
    			}
    90ba31
    		}
    90ba31
    		else {
    90ba31
    			print " ";
    90ba31
    		}
    90ba31
    	}
    ccad43
    	print "\n";
    90ba31
    	$i++;
    90ba31
        }
    ccad43
    	print "\n";
    90ba31
    }
    ccad43
    print "$footer\n";
    90ba31