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';
b09a69
my @wanted=qw(aliases responsible weburls hostusage partners description documentation);
90ba31
my $print_help='';
90ba31
my $dumpforredmine='';
90ba31
my $header='';
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
);
90ba31
90ba31
usage(0) if ($print_help);
90ba31
90ba31
90ba31
90ba31
my $sls_files=getFiles($pillar_id_path,'.sls');
90ba31
90ba31
if ( "$sls_files" ne "0" ){
90ba31
    my $i=1;
90ba31
	if ( $dumpforredmine ){
90ba31
		print "$redmine_header\n";	
90ba31
	}
66fd4e
	print "|# ";
66fd4e
	print "|Hostname ";
90ba31
	my $td=1;
90ba31
    foreach my $entry (sort(@wanted)){
66fd4e
	    print "|$entry ";
90ba31
    }
66fd4e
        print "|\n";
66fd4e
        print "|---:|--- ";
66fd4e
    foreach my $entry (sort(@wanted)){
66fd4e
            print "|--- ";
66fd4e
    }
66fd4e
        print "|\n";
90ba31
90ba31
    for my $file (sort (@$sls_files)){
90ba31
	my $hostname=basename("$file",'.sls');
90ba31
	$hostname=~ s/_/./g;
66fd4e
   	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)){
66fd4e
		print "|";
90ba31
		if (defined($grains->{'grains'}->{$entry})){
90ba31
			my $type=reftype $grains->{'grains'}->{$entry};
90ba31
			if (defined($type) && "$type" eq 'ARRAY'){
90ba31
				if (@{$grains->{'grains'}->{$entry}} > 0){
90ba31
					foreach my $string (sort(@{$grains->{'grains'}->{$entry}})){
90ba31
						if ("$string" =~ m/^http.*/){
66fd4e
							print "[$string]($string) ";
90ba31
						} 
90ba31
						else {
90ba31
							if ("$entry" eq "partners"){
66fd4e
								print "[$string](#$string) ";
90ba31
							}
90ba31
							elsif ("$entry" eq "responsible"){
66fd4e
								print "[$string]($freeipa_user_url/$string) ";
90ba31
							}
90ba31
							else {
66fd4e
								print "$string ";
90ba31
							}
90ba31
						}
90ba31
					}
90ba31
				}
90ba31
			}
90ba31
			else {
90ba31
				print "$grains->{'grains'}->{$entry}" if ($grains->{'grains'}->{$entry} ne "");
90ba31
			}
90ba31
		}
90ba31
		else {
90ba31
			print " ";
90ba31
		}
90ba31
	}
66fd4e
	print "|\n";
90ba31
	$i++;
90ba31
    }
90ba31
}