-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_hprcu_type.rb
executable file
·121 lines (96 loc) · 3.56 KB
/
gen_hprcu_type.rb
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
#!/usr/bin/ruby
#
# Use XML file generated by hprcu to generate hprcu type
#
# Steps:
# 1. Scan XML, extract names of options, convert into Puppet-friendly form
# (i.e. valid property names as per the Puppet grammar, defined in the
# Puppet code in grammar.ra)
# 2. (Optional) Invoke newproperty with each property name and check return
# code to confirm that the name is valid - emit helpful message if an
# invalid name has been generated or if there is a name clash
# 3. For each property, write to a new file the newproperty definition with
# appropriate name, valid and default values for the property
require 'puppet'
require 'rexml/document'
require 'erb'
# Parse command line
begin
if ! ( ARGV[0].nil? or ARGV[0] == '-' ) # Input
$stdin.reopen(ARGV[0], "r")
end
rescue Exception
STDERR.puts "Failed to open input file: #{$!}"
raise
end
begin
if ! ( ARGV[1].nil? or ARGV[1] == '-' ) # Output
$stdout.reopen(ARGV[1], "w")
end
rescue Exception
STDERR.puts "Failed to open output file: #{$!}"
raise
end
puts <<EOT
# hprcu type
Puppet::Type.newtype(:hprcu) do
@doc = "This type/provider uses the hprcu binary to modify BIOS settings on HP servers.
Usage:
puppet resource hprcu
puppet apply -e 'hprcu{\\\"default\\\": intelrhyperthreadingoptions => \\\"Disabled\\\"}'
Note: the name of the resource is hardcoded to 'default'. This is because each server has only one set of BIOS settings, when represented as a resource they needed a name, and 'default' seemed like a sensible choice.
The names of the settings supported by BIOS revisions varies. The gen_hprcu_type.rb script can be used to regenerate the Puppet type to reflect changes in the setting names:
hprcu -s -f /tmp/hprcu.xml
./gen_hprcu_type.rb /tmp/hprcu.xml > lib/puppet/type/hprcu.rb
Supported servers are known to include HP DL380 Gen8."
# Type must be ensurable as we must use exists?, because as per p. 46 of
# Puppet Types and Providers: properties other than ensure are only
# *individually* managed when ensure is set to present and the resource
# already exists. When a resource state is absent, Puppet ignores any
# specified # resource property.
ensurable
newparam(:name, :namevar => true) do
end
EOT
# Map from (e.g.) 'Intel(R) Hyperthreading Options' to 'intelrhyperthreadingoptions'
# Note that the names of the setters must be 'Puppet-friendly', i.e. valid as per
# grammar.ra in the Puppet source
$map2Valid = {}
def makeValid(invalid)
if ! $map2Valid.has_key?(invalid)
# Make into a valid puppet symbol by:
# 1) Changing to lowercase
# 2) Removing special characters
# 3) Prepending 'i' if it starts with digits
# 4) Removing dots if it ends with dots + numbers
valid = invalid.downcase.gsub(/[- ()_\/:;,]/,'').sub(/^([0-9]+)/, 'i\1').sub(/\.([0-9]+)$/, '\1')
$map2Valid[invalid] = valid
end
$map2Valid[invalid]
end
$newpropertyTemplate = <<EOT
newproperty(<%= propertyName %>) do
newvalues(<%= validValues.join(', ') %>)
end
EOT
hprcuXml = REXML::Document.new $stdin
hprcuXml.root.elements.each('/hprcu/feature') { |feature|
next unless feature.attributes['feature_type'] == 'option'
propertyName = ''
validValues = []
feature.elements.each('feature_name') { |feature_name|
propertyName = ':' + makeValid(feature_name.text)
}
feature.elements.each('option') { |option|
option.elements.each('option_name') { |on|
# validValues.push(':' + makeValid(on.text))
validValues.push('"' + on.text + '"')
}
}
puts ERB.new($newpropertyTemplate).result(binding)
}
puts <<EOT
end
# vim:sw=2:ts=2:et:
EOT
# vim:sw=2:ts=2:et: