Skip to content
This repository has been archived by the owner on Aug 18, 2022. It is now read-only.

(maint) Pay down tech debt #349

Merged
2 changes: 1 addition & 1 deletion build/dsc.rake
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ eod
desc "Build #{item_name}"
task :build, [:module_path] do |t, args|
module_path = args[:module_path] || default_dsc_module_path
m = Dsc::Manager.new
m = Dsc::TypeBuilder.new
m.target_module_path = module_path
msgs = m.build_dsc_types
msgs.each{|m| puts "#{m}"}
Expand Down
1 change: 1 addition & 0 deletions build/dsc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
require "#{LIB_PATH}/dsc/resource"
require "#{LIB_PATH}/dsc/psmodule"
require "#{LIB_PATH}/dsc/manager"
require "#{LIB_PATH}/dsc/typebuilder"
require "#{LIB_PATH}/dsc/typecleaner"
require "#{LIB_PATH}/dsc/documentbuilder"

Expand Down
128 changes: 0 additions & 128 deletions build/dsc/manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,73 +26,6 @@ def initialize
@resources = nil
@cim_classes_with_path = nil

@spec_test_values = {
'string' => 'foo',
'string[]' => ['foo','bar','spec'],
'MSFT_Credential' => { 'user' => 'user', 'password' => 'password' },
'MSFT_KeyValuePair' => { 'somekey' => 'somevalue' },
'MSFT_KeyValuePair[]' => { 'somekey' => 'somevalue', 'somekey2' => 'somevalue2' },
'bool' => true,
'boolean' => true,
'munged_bools' => ['true','false','True', 'False', :true, :false],
'munged_ints' => ['16','-16','32', '-32'],
'munged_uints' => ['16','32','64'],
'int_not_bool_mungeable' => -16,
'uint_not_bool_mungeable' => 16,
'datetime' => '20140711',
'int8' => -16,
'int8[]' => [-128, 0, 127],
'int16' => -16,
'int16[]' => [-16, 32, -64],
'int32' => -32,
'int32[]' => [-32, 64, -128],
'int64' => -64,
'int64[]' => [-64, 128, -256],
'sint8' => -128,
'sint8[]' => [-128, 0, 127],
'sint16' => -16,
'sint16[]' => [-16, 32, -64],
'sint32' => -32,
'sint32[]' => [-32, 64, -128],
'sint64' => -64,
'sint64[]' => [-64, 128, -256],
'uint8' => 1,
'uint8[]' => [0, 16, 255],
'uint16' => 16,
'uint16[]' => [16, 32, 64],
'uint32' => 32,
'uint32[]' => [32, 64, 128],
'uint64' => 64,
'uint64[]' => [64, 128, 256],
'real32' => '32.000',
'real64' => '64.000',
}

end

def get_spec_test_value(type)
# generate a hash here for any EmbeddedInstance type not seen yet
if ! @spec_test_values[type]

type_name = type.gsub(/\[\]$/, '') # strip [] off end of MOF type name
values = cim_classes_with_path
.select{ |c| c[:klass].name == type_name }
.first[:klass]
.features
.map do |prop|
# use first value in ValueMap if present
if prop.qualifiers['Values']
val = prop.qualifiers['Values'].value.first
else
val = get_spec_test_value(prop.type.to_s)
end
[prop.name, val]
end

@spec_test_values[type] = Hash[ values ]
end

@spec_test_values[type]
end

def dsc_results
Expand Down Expand Up @@ -170,41 +103,6 @@ def embedded_cim_classes
cim_classes_with_path.select{|cc| embedded_class_names.include?(cc[:klass].name) }.collect{|cc| cc[:klass] }
end

def build_dsc_types
type_pathes = []
whitelist = ['archive','environment','file','group','groupset','log','package',
'processset','registry','script','service','serviceset','user','windowsfeature',
'windowsfeatureset','windowsoptionalfeature','windowsoptionalfeatureset','windowsprocess']
resources.each do |resource|
type_template = File.open(@type_template_file, 'r').read
type_erb = ERB.new(type_template, nil, '-')
type_spec_template = File.open(@type_spec_template_file, 'r').read
type_spec_erb = ERB.new(type_spec_template, nil, '-')
if resource.friendlyname
puppet_type_path = "#{@target_module_path}/#{@puppet_type_subpath}"
FileUtils.mkdir_p(puppet_type_path) unless File.exists?(puppet_type_path)
File.open("#{puppet_type_path}/dsc_#{resource.friendlyname.downcase}.rb", 'w+') do |file|
file.write(type_erb.result(binding))
pn = Pathname.new(file.path).expand_path.relative_path_from(@module_path)
type_pathes << "Add type - #{pn.to_s}"
end
puppet_type_spec_path = "#{@target_module_path}/#{@puppet_type_spec_subpath}"
FileUtils.mkdir_p(puppet_type_spec_path) unless File.exists?(puppet_type_spec_path)
if whitelist.include?(resource.friendlyname.downcase)
File.open("#{puppet_type_spec_path}/dsc_#{resource.friendlyname.downcase}_spec.rb", 'w+') do |file|
file.write(type_spec_erb.result(binding))
pn = Pathname.new(file.path).expand_path.relative_path_from(@module_path)
type_pathes << "Add type spec - #{pn.to_s}"
end
end
else
puts "#{resource.name} from #{resource.dsc_module} has invalid mof (no friendlyname defined)"
puts "#{resource.name} will not be usable with puppet"
end
end
type_pathes
end

def get_dsc_types
dsc_types = []
resources.each do |resource|
Expand All @@ -218,32 +116,6 @@ def import_dmtf_mofs
Dsc::Import.download(@dmtf_cim_mof_zip_url, @dmtf_cim_mof_zip_path)
Dsc::Import.unzip(@dmtf_cim_mof_zip_path, @dmtf_mof_folder)
end

def format_type_value(type_value)
case
when type_value.class.name == 'Symbol'
":#{type_value}"
when type_value.class.name == 'String'
"'#{type_value}'"
when type_value.class.name == 'Numeric'
"#{type_value}"
else
type_value
end
end

def format_newvalues(values)
output = []
values.each do |v|
if v.respond_to?('downcase')
output << format_type_value(v)
output << format_type_value(v.downcase)
else
output << format_type_value(v)
end
end
output.join(', ')
end

def find_valid_files(directory)
valid_files = Dir.glob("#{directory}/**/*").reject do |f|
Expand Down
146 changes: 146 additions & 0 deletions build/dsc/typebuilder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
module Dsc
class TypeBuilder < Manager

def build_dsc_types
type_pathes = []
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: typo. Won't stop a merge though

resources.each do |resource|
if resource.friendlyname
build_dsc_type(resource, @type_template_file, @target_module_path, @puppet_type_subpath, @module_path, type_pathes)
build_dsc_type_spec(resource, @type_spec_template_file, @target_module_path, @puppet_type_spec_subpath, @module_path, type_pathes)
else
puts "#{resource.name} from #{resource.dsc_module} has invalid mof (no friendlyname defined)"
puts "#{resource.name} will not be usable with puppet"
end
end
type_pathes
end

def build_dsc_type(resource, type_template_file, target_module_path, puppet_type_subpath, module_path, type_pathes)
type_template = File.open(type_template_file, 'r').read
type_erb = ERB.new(type_template, nil, '-')
puppet_type_path = "#{target_module_path}/#{puppet_type_subpath}"

FileUtils.mkdir_p(puppet_type_path) unless File.exists?(puppet_type_path)

File.open("#{puppet_type_path}/dsc_#{resource.friendlyname.downcase}.rb", 'w+') do |file|
file.write(type_erb.result(binding))
pn = Pathname.new(file.path).expand_path.relative_path_from(module_path)
type_pathes << "Add type - #{pn.to_s}"
end
end

def build_dsc_type_spec(resource, type_spec_template_file, target_module_path, puppet_type_spec_subpath, module_path, type_pathes)
whitelist = ['archive','environment','file','group','groupset','log','package',
'processset','registry','script','service','serviceset','user','windowsfeature',
'windowsfeatureset','windowsoptionalfeature','windowsoptionalfeatureset','windowsprocess']

type_spec_template = File.open(type_spec_template_file, 'r').read
type_spec_erb = ERB.new(type_spec_template, nil, '-')
puppet_type_spec_path = "#{target_module_path}/#{puppet_type_spec_subpath}"

FileUtils.mkdir_p(puppet_type_spec_path) unless File.exists?(puppet_type_spec_path)

if whitelist.include?(resource.friendlyname.downcase)
File.open("#{puppet_type_spec_path}/dsc_#{resource.friendlyname.downcase}_spec.rb", 'w+') do |file|
file.write(type_spec_erb.result(binding))
pn = Pathname.new(file.path).expand_path.relative_path_from(module_path)
type_pathes << "Add type spec - #{pn.to_s}"
end
end
end

def format_type_value(type_value)
case
when type_value.class.name == 'Symbol'
":#{type_value}"
when type_value.class.name == 'String'
"'#{type_value}'"
when type_value.class.name == 'Numeric'
"#{type_value}"
else
type_value
end
end

def format_newvalues(values)
output = []
values.each do |v|
if v.respond_to?('downcase')
output << format_type_value(v)
output << format_type_value(v.downcase)
else
output << format_type_value(v)
end
end
output.join(', ')
end

def get_spec_test_value(type)
@spec_test_values = {
'string' => 'foo',
'string[]' => ['foo','bar','spec'],
'MSFT_Credential' => { 'user' => 'user', 'password' => 'password' },
'MSFT_KeyValuePair' => { 'somekey' => 'somevalue' },
'MSFT_KeyValuePair[]' => { 'somekey' => 'somevalue', 'somekey2' => 'somevalue2' },
'bool' => true,
'boolean' => true,
'munged_bools' => ['true','false','True', 'False', :true, :false],
'munged_ints' => ['16','-16','32', '-32'],
'munged_uints' => ['16','32','64'],
'int_not_bool_mungeable' => -16,
'uint_not_bool_mungeable' => 16,
'datetime' => '20140711',
'int8' => -16,
'int8[]' => [-128, 0, 127],
'int16' => -16,
'int16[]' => [-16, 32, -64],
'int32' => -32,
'int32[]' => [-32, 64, -128],
'int64' => -64,
'int64[]' => [-64, 128, -256],
'sint8' => -128,
'sint8[]' => [-128, 0, 127],
'sint16' => -16,
'sint16[]' => [-16, 32, -64],
'sint32' => -32,
'sint32[]' => [-32, 64, -128],
'sint64' => -64,
'sint64[]' => [-64, 128, -256],
'uint8' => 1,
'uint8[]' => [0, 16, 255],
'uint16' => 16,
'uint16[]' => [16, 32, 64],
'uint32' => 32,
'uint32[]' => [32, 64, 128],
'uint64' => 64,
'uint64[]' => [64, 128, 256],
'real32' => '32.000',
'real64' => '64.000',
}
# generate a hash here for any EmbeddedInstance type not seen yet
if ! @spec_test_values[type]

type_name = type.gsub(/\[\]$/, '') # strip [] off end of MOF type name
values = cim_classes_with_path
.select{ |c| c[:klass].name == type_name }
.first[:klass]
.features
.map do |prop|
# use first value in ValueMap if present
if prop.qualifiers['Values']
val = prop.qualifiers['Values'].value.first
else
val = get_spec_test_value(prop.type.to_s)
end
[prop.name, val]
end

@spec_test_values[type] = Hash[ values ]
end

@spec_test_values[type]
end

end

end