Gunnar Mills | bff5684 | 2017-09-25 15:49:16 -0500 | [diff] [blame] | 1 | #!/usr/bin/env perl |
| 2 | use strict; |
| 3 | use warnings; |
| 4 | |
| 5 | use mrw::Targets; # Set of APIs allowing access to parsed ServerWiz2 XML output |
| 6 | use Getopt::Long; # For parsing command line arguments |
| 7 | |
| 8 | # Globals |
| 9 | my $force = 0; |
| 10 | my $serverwizFile = ""; |
| 11 | my $debug = 0; |
| 12 | my $outputFile = ""; |
Gunnar Mills | 4576533 | 2017-09-26 16:19:32 -0500 | [diff] [blame^] | 13 | my $settingsFile = ""; |
Gunnar Mills | bff5684 | 2017-09-25 15:49:16 -0500 | [diff] [blame] | 14 | |
| 15 | # Command line argument parsing |
| 16 | GetOptions( |
| 17 | "f" => \$force, # numeric |
| 18 | "i=s" => \$serverwizFile, # string |
| 19 | "o=s" => \$outputFile, # string |
Gunnar Mills | 4576533 | 2017-09-26 16:19:32 -0500 | [diff] [blame^] | 20 | "s=s" => \$settingsFile, # string |
Gunnar Mills | bff5684 | 2017-09-25 15:49:16 -0500 | [diff] [blame] | 21 | "d" => \$debug, |
| 22 | ) |
| 23 | or printUsage(); |
| 24 | |
Gunnar Mills | 4576533 | 2017-09-26 16:19:32 -0500 | [diff] [blame^] | 25 | if (($serverwizFile eq "") or ($outputFile eq "") or ($settingsFile eq "") ) |
Gunnar Mills | bff5684 | 2017-09-25 15:49:16 -0500 | [diff] [blame] | 26 | { |
| 27 | printUsage(); |
| 28 | } |
| 29 | |
| 30 | # API used to access parsed XML data |
| 31 | my $targetObj = Targets->new; |
| 32 | if($debug == 1) |
| 33 | { |
| 34 | $targetObj->{debug} = 1; |
| 35 | } |
| 36 | |
| 37 | if($force == 1) |
| 38 | { |
| 39 | $targetObj->{force} = 1; |
| 40 | } |
| 41 | |
| 42 | $targetObj->loadXML($serverwizFile); |
| 43 | print "Loaded MRW XML: $serverwizFile \n"; |
| 44 | |
Gunnar Mills | 4576533 | 2017-09-26 16:19:32 -0500 | [diff] [blame^] | 45 | open(my $inFh, '<', $settingsFile) or die "Could not open file '$settingsFile' $!"; |
| 46 | open(my $outFh, '>', $outputFile) or die "Could not open file '$outputFile' $!"; |
| 47 | |
| 48 | # Process all the targets in the XML |
| 49 | foreach my $target (sort keys %{$targetObj->getAllTargets()}) |
| 50 | { |
| 51 | # A future improvement could be to specify the MRW target. |
| 52 | next if ("SYS" ne $targetObj->getType($target, "TYPE")); |
| 53 | # Read the settings YAML replacing any MRW_<variable name> with their |
| 54 | # MRW value |
| 55 | while (my $row = <$inFh>) |
| 56 | { |
| 57 | while ($row =~ /MRW_(.*?)\W/g) |
| 58 | { |
| 59 | my $setting = $1; |
| 60 | my $settingValue = $targetObj->getAttribute($target, $setting); |
| 61 | $row =~ s/MRW_${setting}/$settingValue/g; |
| 62 | } |
| 63 | print $outFh $row; |
| 64 | } |
| 65 | last; |
| 66 | close $inFh; |
| 67 | close $outFh; |
| 68 | } |
| 69 | |
Gunnar Mills | bff5684 | 2017-09-25 15:49:16 -0500 | [diff] [blame] | 70 | # Usage |
| 71 | sub printUsage |
| 72 | { |
| 73 | print " |
Gunnar Mills | 4576533 | 2017-09-26 16:19:32 -0500 | [diff] [blame^] | 74 | $0 -i [XML filename] -s [Settings YAML] -o [Output filename] [OPTIONS] |
| 75 | |
| 76 | Required: |
| 77 | -i = MRW XML filename |
| 78 | -s = The Setting YAML with MRW variables in MRW_<MRW variable name> format |
| 79 | -o = YAML output filename |
Gunnar Mills | bff5684 | 2017-09-25 15:49:16 -0500 | [diff] [blame] | 80 | Options: |
| 81 | -f = force output file creation even when errors |
| 82 | -d = debug mode |
Gunnar Mills | bff5684 | 2017-09-25 15:49:16 -0500 | [diff] [blame] | 83 | \n"; |
| 84 | exit(1); |
| 85 | } |