From c3ec4ff1ac4f14e707c2ad4f18505a759118842e Mon Sep 17 00:00:00 2001 From: Lars Vogdt Date: Jan 15 2020 21:11:22 +0000 Subject: Merge branch 'lrupp/add_generate_redmine_page_script' into 'production' Add generate redmine page script As described on https://progress.opensuse.org/projects/opensuse-admin-wiki/wiki/Machines, this script can be used to generate exactly this page from a local copy of this repository. See merge request infra/salt!309 --- diff --git a/bin/generate_redmine_page.pl b/bin/generate_redmine_page.pl new file mode 100755 index 0000000..6061a44 --- /dev/null +++ b/bin/generate_redmine_page.pl @@ -0,0 +1,296 @@ +#!/usr/bin/perl -w +# +# Copyright Lars Vogdt +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the +# Free Software Foundation, Inc., +# 51 Franklin Street, +# Fifth Floor, +# Boston, MA 02110-1301, +# USA. +# +use strict; +use warnings; +use Data::Dumper; +use File::Basename; +use Getopt::Long; +use Scalar::Util qw(reftype); +use YAML::Tiny; + +my $pillar_id_path='pillar/id'; +my @wanted=qw(aliases responsible virt_cluster weburls hostusage partners description documentation); +my $print_help=''; +my $full_html=''; +my $dumpforredmine=''; +my $header=''; +my $footer=''; +my $table_style="width='1820' border='1' style='table-layout:fixed;'"; +my $td_style="style='word-wrap:break-word; padding-left:.5em'"; +my $td_color_even='#f6f6f6'; +my $td_color_odd='fcf8e3'; +my $freeipa_user_url='https://freeipa.infra.opensuse.org/ipa/ui/#/e/user/details'; + +sub getFiles($$) { + my $path=shift; + my $ending=shift; + opendir(DIR,"$path") || die ("Could not open $path: $! $?!\n"); + my @Files= grep {/.*\Q$ending\E/} readdir(DIR); + closedir(DIR); + my $number = @Files; + if ( $number gt 0 ){ + my @return; + foreach (@Files){ + next if ( $_ eq "." ); + next if ( $_ eq ".." ); + push @return,$_; + } + return \@return; + } + return 0; +} + +sub usage($){ + my $exitcode=shift || 1; + print < | --pillardir : Path to the files with the Pillar information. + -t | --tableonly : Print only the HTML table (for inclusion in Redmine). + + -r | --dumpforredmine : Print including a short manual for direct copy and past into the Redmine wiki. + -h | --help : This help + +Use this script to generate a HTML page (or just a HTML table), based on the +static pillar information provided in YAML files. +Currently the list of tags that end up in the HTML is hardcoded in the script. If you +want to change it, please edit the line starting with + my \@wanted=... + + +EOF +; + + exit $exitcode; +} + +my $redmine_header=" +Machines +======== + +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/). + +You need a GIT checkout of the repository and the following perl modules installed on your system: + +```bash + sudo zypper in 'perl(File::Basename)' 'perl(Getopt::Long)' 'perl(Scalar::Util)' 'perl(YAML::Tiny)' +``` + +After that you should be able to call the script from the root of the checked out Salt repository: + +```bash + bin/generate_redmine_page.pl -t +``` + +* 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. +* If you are 'just interested' or needed to fix something on the machine, please get in contact with the administrator mentioned + +List of machines +================ +"; + +Getopt::Long::Configure('bundling'); + +GetOptions( + 'h|help' => \$print_help, + 'd|pillardir=s' => \$pillar_id_path, + 'r|dumpforredmine' => \$dumpforredmine, + 't|tableonly' => \$full_html, +); + +usage(0) if ($print_help); + + + +my $sls_files=getFiles($pillar_id_path,'.sls'); + +if (! $full_html){ +$header=" + + + + Machine list + + + + \n"; +$footer=" +"; +} + +if (! $dumpforredmine){ + print "$header"; +} + +my $javascript=" +"; + +if ( "$sls_files" ne "0" ){ + my $i=1; + if ( $dumpforredmine ){ + print "$redmine_header\n"; + } + print "$javascript\n"; + print "\n"; + print "\n"; + print ""; + print "\n"; + print "\n"; + my $td=1; + foreach my $entry (sort(@wanted)){ + my $width=''; + if ("$entry" =~ m/responsible/){ + $width="width='150px'"; + } + if ("$entry" =~ m/virt_cluster/){ + $width="width='50px'"; + } + $td++; + print "\n"; + } + print "\n"; + print "\n"; + + for my $file (sort (@$sls_files)){ + my $bgcolor="bgcolor='$td_color_odd'"; + if ($i % 2){ + $bgcolor="bgcolor='$td_color_even'"; + } + print ""; + my $hostname=basename("$file",'.sls'); + $hostname=~ s/_/./g; + print "\n"; + my $yaml = YAML::Tiny->read("$pillar_id_path/$file"); + my $grains=$yaml->[0]; + # print Data::Dumper->Dump([$grains])."\n"; + foreach my $entry (sort(@wanted)){ + print "\n"; + } + print "\n"; + $i++; + } + print "
#
Hostname
$entry
$i

$hostname

"; + if (defined($grains->{'grains'}->{$entry})){ + my $type=reftype $grains->{'grains'}->{$entry}; + if (defined($type) && "$type" eq 'ARRAY'){ + if (@{$grains->{'grains'}->{$entry}} > 0){ + print "
    \n"; + foreach my $string (sort(@{$grains->{'grains'}->{$entry}})){ + if ("$string" =~ m/^http.*/){ + print "
  • $string
  • "; + } + else { + if ("$entry" eq "partners"){ + print "
  • $string
  • \n"; + } + elsif ("$entry" eq "responsible"){ + print "
  • $string
  • \n"; + } + else { + print "
  • $string
  • \n"; + } + } + } + print "
\n"; + } + } + else { + print "$grains->{'grains'}->{$entry}" if ($grains->{'grains'}->{$entry} ne ""); + } + } + else { + print " "; + } + print "
\n"; +} +print "$footer"; +