summaryrefslogtreecommitdiffstats
path: root/gen_settings.pl
blob: 2c975863a19e39664f44dc4fa923ed0140c8b444 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/bin/env perl

#This script replaces MRW attribute names with their values from the
#MRW XML in any file.  In addition, it can evaluate mathematical
#expressions if they are in [[ ]]s and can use variables passed in from
#the command line in those expressions.
#
#For example, if the attribute FOO has a value of 50 in the MRW, and
#the program was started with: -v "MY_VAR1=200 MY_VAR2=400"
#
#then the line
#  [[(MRW_FOO * MY_VAR1) + 5]]..[[MRW_FOO * MY_VAR2]]
#
#would get written out as:
#  10005..20000
#

use strict;
use warnings;

use mrw::Targets; # Set of APIs allowing access to parsed ServerWiz2 XML output
use Getopt::Long; # For parsing command line arguments

# Globals
my $force          = 0;
my $serverwizFile  = "";
my $debug          = 0;
my $outputFile     = "";
my $settingsFile   = "";
my $expressionVars = "";
my %exprVars;

# Command line argument parsing
GetOptions(
"f"   => \$force,            # numeric
"i=s" => \$serverwizFile,    # string
"o=s" => \$outputFile,       # string
"s=s" => \$settingsFile,     # string
"v=s" => \$expressionVars,   # string
"d"   => \$debug,
)
or printUsage();

if (($serverwizFile eq "") or ($outputFile eq "") or ($settingsFile eq "") )
{
    printUsage();
}

# API used to access parsed XML data
my $targetObj = Targets->new;
if($debug == 1)
{
    $targetObj->{debug} = 1;
}

if($force == 1)
{
    $targetObj->{force} = 1;
}

$targetObj->loadXML($serverwizFile);
print "Loaded MRW XML: $serverwizFile \n";

open(my $inFh, '<', $settingsFile) or die "Could not open file '$settingsFile' $!";
open(my $outFh, '>', $outputFile) or die "Could not open file '$outputFile' $!";

if (length($expressionVars) > 0)
{
    loadVars($expressionVars);
}

# Process all the targets in the XML
foreach my $target (sort keys %{$targetObj->getAllTargets()})
{
    # A future improvement could be to specify the MRW target.
    next if ("SYS" ne $targetObj->getType($target, "TYPE"));
    # Read the settings YAML replacing any MRW_<variable name> with their
    # MRW value
    while (my $row = <$inFh>)
    {
        while ($row =~ /MRW_(.*?)\W/g)
        {
            my $setting = $1;
            my $settingValue = $targetObj->getAttribute($target, $setting);
            $row =~ s/MRW_${setting}/$settingValue/g;
        }

        #If there are [[ ]] expressions, evaluate them and replace the
        #[[expression]] with the value
        while ($row =~ /\[\[(.*?)\]\]/)
        {
            my $expr = $1;
            my $value = evaluate($expr);

            #Break the row apart, remove the [[ ]]s, and put the
            #value in the middle when putting back together.
            my $exprStart = index($row, $expr);
            my $front = substr($row, 0, $exprStart - 2);
            my $back = substr($row, $exprStart + length($expr) + 2);

            $row = $front . $value . $back;
        }

        print $outFh $row;
    }
    last;
    close $inFh;
    close $outFh;
}

#Evaluate the expression passed in.  Substitute any variables with
#their values passed in on the command line.
sub evaluate
{
    my $expr = shift;

    #Put in the value for the variable.
    for my $var (keys %exprVars)
    {
        $expr =~ s/$var/$exprVars{$var}/;
    }

    my $value = eval($expr);
    if (not defined $value)
    {
        die "Invalid expression found: $expr\n";
    }

    #round it to an integer
    $value = sprintf("%.0f", $value);
    return $value;
}

#Parse the variable=value string passed in from the
#command line and load it into %exprVars.
sub loadVars
{
    my $varString = shift;

    #Example: "VAR1=VALUE1 VAR2=VALUE2"
    my @entries = split(' ', $varString);

    for my $entry (@entries)
    {
        my ($var, $value) = $entry =~ /(.+)=(.+)/;

        if ((not defined $var) || (not defined $value))
        {
            die "Could not parse expression variable string $varString\n";
        }

        $exprVars{$var} = $value;
    }
}

# Usage
sub printUsage
{
    print "
    $0 -i [XML filename] -s [Settings YAML] -o [Output filename] -v [expr vars] [OPTIONS]

Required:
    -i = MRW XML filename
    -s = The Setting YAML with MRW variables in MRW_<MRW variable name> format
    -o = YAML output filename
Optional:
    -v = Variables and values for any [[expression]] evaluation
         in the form: \"VAR1=VALUE1 VAR2=VALUE2\"
Options:
    -f = force output file creation even when errors
    -d = debug mode
    \n";
    exit(1);
}
OpenPOWER on IntegriCloud