diff options
Diffstat (limited to 'lib/chef/util')
-rw-r--r-- | lib/chef/util/backup.rb | 4 | ||||
-rw-r--r-- | lib/chef/util/dsc/configuration_generator.rb | 8 | ||||
-rw-r--r-- | lib/chef/util/dsc/lcm_output_parser.rb | 6 | ||||
-rw-r--r-- | lib/chef/util/dsc/local_configuration_manager.rb | 4 | ||||
-rw-r--r-- | lib/chef/util/dsc/resource_info.rb | 30 | ||||
-rw-r--r-- | lib/chef/util/dsc/resource_store.rb | 139 | ||||
-rw-r--r-- | lib/chef/util/editor.rb | 1 | ||||
-rw-r--r-- | lib/chef/util/powershell/cmdlet.rb | 228 | ||||
-rw-r--r-- | lib/chef/util/powershell/cmdlet_result.rb | 66 | ||||
-rw-r--r-- | lib/chef/util/selinux.rb | 2 | ||||
-rw-r--r-- | lib/chef/util/windows/net_group.rb | 1 | ||||
-rw-r--r-- | lib/chef/util/windows/net_use.rb | 4 | ||||
-rw-r--r-- | lib/chef/util/windows/net_user.rb | 7 |
13 files changed, 249 insertions, 251 deletions
diff --git a/lib/chef/util/backup.rb b/lib/chef/util/backup.rb index e9b4ed2fe3..8bf2b3f25b 100644 --- a/lib/chef/util/backup.rb +++ b/lib/chef/util/backup.rb @@ -83,11 +83,11 @@ class Chef fn = Regexp.escape(::File.basename(path)) Dir.entries(::File.dirname(backup_path)).select do |f| !!(f =~ /\A#{fn}.chef-[0-9.]*\B/) - end.map {|f| ::File.join(::File.dirname(backup_path), f)} + end.map { |f| ::File.join(::File.dirname(backup_path), f) } end def sorted_backup_files - unsorted_backup_files.sort { |a,b| b <=> a } + unsorted_backup_files.sort { |a, b| b <=> a } end end end diff --git a/lib/chef/util/dsc/configuration_generator.rb b/lib/chef/util/dsc/configuration_generator.rb index bec9c69ff7..af9a71b246 100644 --- a/lib/chef/util/dsc/configuration_generator.rb +++ b/lib/chef/util/dsc/configuration_generator.rb @@ -70,7 +70,7 @@ class Chef::Util::DSC def get_merged_configuration_flags!(configuration_flags, configuration_name) merged_configuration_flags = { :outputpath => configuration_document_directory(configuration_name) } if configuration_flags - configuration_flags.map do | switch, value | + configuration_flags.map do |switch, value| if merged_configuration_flags.key?(switch.to_s.downcase.to_sym) raise ArgumentError, "The `flags` attribute for the dsc_script resource contained a command line switch :#{switch} that is disallowed." end @@ -114,7 +114,7 @@ Configuration '#{configuration_name}' def write_document_generation_script(code, configuration_name, imports) script_path = "#{@config_directory}/chef_dsc_config.ps1" - ::File.open(script_path, "wt") do | script | + ::File.open(script_path, "wt") do |script| script.write(configuration_code(code, configuration_name, imports)) end script_path @@ -122,7 +122,7 @@ Configuration '#{configuration_name}' def find_configuration_document(configuration_name) document_directory = configuration_document_directory(configuration_name) - document_file_name = ::Dir.entries(document_directory).find { | path | path =~ /.*.mof/ } + document_file_name = ::Dir.entries(document_directory).find { |path| path =~ /.*.mof/ } ::File.join(document_directory, document_file_name) if document_file_name end @@ -131,7 +131,7 @@ Configuration '#{configuration_name}' end def get_configuration_document(document_path) - ::File.open(document_path, "rb") do | file | + ::File.open(document_path, "rb") do |file| file.read end end diff --git a/lib/chef/util/dsc/lcm_output_parser.rb b/lib/chef/util/dsc/lcm_output_parser.rb index 8f62a27c8a..2e81b363e0 100644 --- a/lib/chef/util/dsc/lcm_output_parser.rb +++ b/lib/chef/util/dsc/lcm_output_parser.rb @@ -73,7 +73,7 @@ class Chef if current_resource[:name] resources.push(current_resource) end - current_resource = {:name => info} + current_resource = { :name => info } else Chef::Log.debug("Ignoring op_action #{op_action}: Read line #{line}") end @@ -108,8 +108,8 @@ class Chef # If the line looks like # What If: [machinename]: LCM: [op_action op_type] message # extract op_action, op_type, and message - operation, info = match.captures - op_action, op_type = operation.strip.split(" ").map {|m| m.downcase.to_sym} + operation, info = match.captures + op_action, op_type = operation.strip.split(" ").map { |m| m.downcase.to_sym } else op_action = op_type = :info if match = line.match(/^.*?:.*?: \s+(.*)/) diff --git a/lib/chef/util/dsc/local_configuration_manager.rb b/lib/chef/util/dsc/local_configuration_manager.rb index 6db8f40eae..3b4eaa92a9 100644 --- a/lib/chef/util/dsc/local_configuration_manager.rb +++ b/lib/chef/util/dsc/local_configuration_manager.rb @@ -111,7 +111,7 @@ EOH def save_configuration_document(configuration_document) ::FileUtils.mkdir_p(@configuration_path) - ::File.open(configuration_document_path, "wb") do | file | + ::File.open(configuration_document_path, "wb") do |file| file.write(configuration_document) end end @@ -121,7 +121,7 @@ EOH end def configuration_document_path - File.join(@configuration_path,"..mof") + File.join(@configuration_path, "..mof") end def clear_execution_time diff --git a/lib/chef/util/dsc/resource_info.rb b/lib/chef/util/dsc/resource_info.rb index 4a32451721..d6dfcff59a 100644 --- a/lib/chef/util/dsc/resource_info.rb +++ b/lib/chef/util/dsc/resource_info.rb @@ -2,25 +2,25 @@ class Chef class Util class DSC - class ResourceInfo - # The name is the text following [Start Set] - attr_reader :name + class ResourceInfo + # The name is the text following [Start Set] + attr_reader :name - # A list of all log messages between [Start Set] and [End Set]. - # Each line is an element in the list. - attr_reader :change_log + # A list of all log messages between [Start Set] and [End Set]. + # Each line is an element in the list. + attr_reader :change_log - def initialize(name, sets, change_log) - @name = name - @sets = sets - @change_log = change_log || [] - end + def initialize(name, sets, change_log) + @name = name + @sets = sets + @change_log = change_log || [] + end - # Does this resource change the state of the system? - def changes_state? - @sets - end + # Does this resource change the state of the system? + def changes_state? + @sets end + end end end end diff --git a/lib/chef/util/dsc/resource_store.rb b/lib/chef/util/dsc/resource_store.rb index df8cfa84ed..bb3480d105 100644 --- a/lib/chef/util/dsc/resource_store.rb +++ b/lib/chef/util/dsc/resource_store.rb @@ -21,90 +21,89 @@ require "chef/util/powershell/cmdlet_result" require "chef/exceptions" class Chef -class Util -class DSC - class ResourceStore + class Util + class DSC + class ResourceStore - def self.instance - @@instance ||= ResourceStore.new.tap do |store| - store.send(:populate_cache) - end - end + def self.instance + @@instance ||= ResourceStore.new.tap do |store| + store.send(:populate_cache) + end + end - def resources - @resources ||= [] - end + def resources + @resources ||= [] + end - def find(name, module_name=nil) - found = find_resources(name, module_name, resources) + def find(name, module_name = nil) + found = find_resources(name, module_name, resources) - # We don't have it, query for the resource...it might - # have been added since we last queried - if found.length == 0 - rs = query_resource(name) - add_resources(rs) - found = find_resources(name, module_name, rs) - end + # We don't have it, query for the resource...it might + # have been added since we last queried + if found.length == 0 + rs = query_resource(name) + add_resources(rs) + found = find_resources(name, module_name, rs) + end - found - end - - private + found + end - def add_resource(new_r) - count = resources.count do |r| - r["ResourceType"].casecmp(new_r["ResourceType"]) == 0 - end - if count == 0 - resources << new_r - end - end + private - def add_resources(rs) - rs.each do |r| - add_resource(r) - end - end + def add_resource(new_r) + count = resources.count do |r| + r["ResourceType"].casecmp(new_r["ResourceType"]) == 0 + end + if count == 0 + resources << new_r + end + end - def populate_cache - @resources = query_resources - end + def add_resources(rs) + rs.each do |r| + add_resource(r) + end + end - def find_resources(name, module_name, rs) - found = rs.find_all do |r| - name_matches = r["Name"].casecmp(name) == 0 - if name_matches - module_name == nil || (r["Module"] and r["Module"]["Name"].casecmp(module_name) == 0) - else - false + def populate_cache + @resources = query_resources end - end - end + def find_resources(name, module_name, rs) + found = rs.find_all do |r| + name_matches = r["Name"].casecmp(name) == 0 + if name_matches + module_name == nil || (r["Module"] and r["Module"]["Name"].casecmp(module_name) == 0) + else + false + end + end + end - # Returns a list of dsc resources - def query_resources - cmdlet = Chef::Util::Powershell::Cmdlet.new(nil, "get-dscresource", - :object) - result = cmdlet.run - result.return_value - end + # Returns a list of dsc resources + def query_resources + cmdlet = Chef::Util::Powershell::Cmdlet.new(nil, "get-dscresource", + :object) + result = cmdlet.run + result.return_value + end - # Returns a list of dsc resources matching the provided name - def query_resource(resource_name) - cmdlet = Chef::Util::Powershell::Cmdlet.new(nil, "get-dscresource #{resource_name}", - :object) - result = cmdlet.run - ret_val = result.return_value - if ret_val.nil? - [] - elsif ret_val.is_a? Array - ret_val - else - [ret_val] + # Returns a list of dsc resources matching the provided name + def query_resource(resource_name) + cmdlet = Chef::Util::Powershell::Cmdlet.new(nil, "get-dscresource #{resource_name}", + :object) + result = cmdlet.run + ret_val = result.return_value + if ret_val.nil? + [] + elsif ret_val.is_a? Array + ret_val + else + [ret_val] + end + end end end end end -end -end diff --git a/lib/chef/util/editor.rb b/lib/chef/util/editor.rb index 4073a0be70..fa4f0ec12e 100644 --- a/lib/chef/util/editor.rb +++ b/lib/chef/util/editor.rb @@ -89,4 +89,3 @@ class Chef end end end - diff --git a/lib/chef/util/powershell/cmdlet.rb b/lib/chef/util/powershell/cmdlet.rb index d9b53f6c7c..6ab380c071 100644 --- a/lib/chef/util/powershell/cmdlet.rb +++ b/lib/chef/util/powershell/cmdlet.rb @@ -21,150 +21,150 @@ require "chef/mixin/windows_architecture_helper" require "chef/util/powershell/cmdlet_result" class Chef -class Util -class Powershell - class Cmdlet - def initialize(node, cmdlet, output_format=nil, output_format_options={}) - @output_format = output_format - @node = node - - case output_format - when nil - @json_format = false - when :json - @json_format = true - when :text - @json_format = false - when :object - @json_format = true - else - raise ArgumentError, "Invalid output format #{output_format} specified" - end - - @cmdlet = cmdlet - @output_format_options = output_format_options - end - - attr_reader :output_format + class Util + class Powershell + class Cmdlet + def initialize(node, cmdlet, output_format = nil, output_format_options = {}) + @output_format = output_format + @node = node + + case output_format + when nil + @json_format = false + when :json + @json_format = true + when :text + @json_format = false + when :object + @json_format = true + else + raise ArgumentError, "Invalid output format #{output_format} specified" + end - def run(switches={}, execution_options={}, *arguments) - streams = { :json => CmdletStream.new("json"), - :verbose => CmdletStream.new("verbose"), - } + @cmdlet = cmdlet + @output_format_options = output_format_options + end - arguments_string = arguments.join(" ") + attr_reader :output_format - switches_string = command_switches_string(switches) + def run(switches = {}, execution_options = {}, *arguments) + streams = { :json => CmdletStream.new("json"), + :verbose => CmdletStream.new("verbose"), + } - json_depth = 5 + arguments_string = arguments.join(" ") - if @json_format && @output_format_options.has_key?(:depth) - json_depth = @output_format_options[:depth] - end + switches_string = command_switches_string(switches) - json_command = @json_format ? " | convertto-json -compress -depth #{json_depth} "\ - "> #{streams[:json].path}" : "" - redirections = "4> '#{streams[:verbose].path}'" - command_string = "powershell.exe -executionpolicy bypass -noprofile -noninteractive "\ - "-command \"trap [Exception] {write-error -exception "\ - "($_.Exception.Message);exit 1};#{@cmdlet} #{switches_string} "\ - "#{arguments_string} #{redirections}"\ - "#{json_command}\";if ( ! $? ) { exit 1 }" + json_depth = 5 - augmented_options = {:returns => [0], :live_stream => false}.merge(execution_options) - command = Mixlib::ShellOut.new(command_string, augmented_options) + if @json_format && @output_format_options.has_key?(:depth) + json_depth = @output_format_options[:depth] + end - status = nil + json_command = @json_format ? " | convertto-json -compress -depth #{json_depth} "\ + "> #{streams[:json].path}" : "" + redirections = "4> '#{streams[:verbose].path}'" + command_string = "powershell.exe -executionpolicy bypass -noprofile -noninteractive "\ + "-command \"trap [Exception] {write-error -exception "\ + "($_.Exception.Message);exit 1};#{@cmdlet} #{switches_string} "\ + "#{arguments_string} #{redirections}"\ + "#{json_command}\";if ( ! $? ) { exit 1 }" - with_os_architecture(@node) do - status = command.run_command - end + augmented_options = { :returns => [0], :live_stream => false }.merge(execution_options) + command = Mixlib::ShellOut.new(command_string, augmented_options) - CmdletResult.new(status, streams, @output_format) - end + status = nil - def run!(switches={}, execution_options={}, *arguments) - result = run(switches, execution_options, arguments) + with_os_architecture(@node) do + status = command.run_command + end - if ! result.succeeded? - raise Chef::Exceptions::PowershellCmdletException, "Powershell Cmdlet failed: #{result.stderr}" - end + CmdletResult.new(status, streams, @output_format) + end - result - end + def run!(switches = {}, execution_options = {}, *arguments) + result = run(switches, execution_options, arguments) - protected + if ! result.succeeded? + raise Chef::Exceptions::PowershellCmdletException, "Powershell Cmdlet failed: #{result.stderr}" + end - include Chef::Mixin::WindowsArchitectureHelper + result + end - def validate_switch_name!(switch_parameter_name) - if !!(switch_parameter_name =~ /\A[A-Za-z]+[_a-zA-Z0-9]*\Z/) == false - raise ArgumentError, "`#{switch_parameter_name}` is not a valid PowerShell cmdlet switch parameter name" - end - end + protected - def escape_parameter_value(parameter_value) - parameter_value.gsub(/(`|'|"|#)/,'`\1') - end + include Chef::Mixin::WindowsArchitectureHelper - def escape_string_parameter_value(parameter_value) - "'#{escape_parameter_value(parameter_value)}'" - end + def validate_switch_name!(switch_parameter_name) + if !!(switch_parameter_name =~ /\A[A-Za-z]+[_a-zA-Z0-9]*\Z/) == false + raise ArgumentError, "`#{switch_parameter_name}` is not a valid PowerShell cmdlet switch parameter name" + end + end - def command_switches_string(switches) - command_switches = switches.map do | switch_name, switch_value | - if switch_name.class != Symbol - raise ArgumentError, "Invalid type `#{switch_name} `for PowerShell switch '#{switch_name}'. The switch must be specified as a Symbol'" + def escape_parameter_value(parameter_value) + parameter_value.gsub(/(`|'|"|#)/, '`\1') end - validate_switch_name!(switch_name) - - switch_argument = "" - switch_present = true - - case switch_value - when Numeric - switch_argument = switch_value.to_s - when Float - switch_argument = switch_value.to_s - when FalseClass - switch_present = false - when TrueClass - when String - switch_argument = escape_string_parameter_value(switch_value) - else - raise ArgumentError, "Invalid argument type `#{switch_value.class}` specified for PowerShell switch `:#{switch_name}`. Arguments to PowerShell must be of type `String`, `Numeric`, `Float`, `FalseClass`, or `TrueClass`" + def escape_string_parameter_value(parameter_value) + "'#{escape_parameter_value(parameter_value)}'" end - switch_present ? ["-#{switch_name.to_s.downcase}", switch_argument].join(" ").strip : "" - end + def command_switches_string(switches) + command_switches = switches.map do |switch_name, switch_value| + if switch_name.class != Symbol + raise ArgumentError, "Invalid type `#{switch_name} `for PowerShell switch '#{switch_name}'. The switch must be specified as a Symbol'" + end + + validate_switch_name!(switch_name) + + switch_argument = "" + switch_present = true + + case switch_value + when Numeric + switch_argument = switch_value.to_s + when Float + switch_argument = switch_value.to_s + when FalseClass + switch_present = false + when TrueClass + when String + switch_argument = escape_string_parameter_value(switch_value) + else + raise ArgumentError, "Invalid argument type `#{switch_value.class}` specified for PowerShell switch `:#{switch_name}`. Arguments to PowerShell must be of type `String`, `Numeric`, `Float`, `FalseClass`, or `TrueClass`" + end + + switch_present ? ["-#{switch_name.to_s.downcase}", switch_argument].join(" ").strip : "" + end - command_switches.join(" ") - end + command_switches.join(" ") + end - class CmdletStream - def initialize(name) - @filename = Dir::Tmpname.create(name) {} - ObjectSpace.define_finalizer(self, self.class.destroy(@filename)) - end + class CmdletStream + def initialize(name) + @filename = Dir::Tmpname.create(name) {} + ObjectSpace.define_finalizer(self, self.class.destroy(@filename)) + end - def path - @filename - end + def path + @filename + end - def read - if File.exist? @filename - File.open(@filename, "rb:bom|UTF-16LE") do |f| - f.read.encode("UTF-8") + def read + if File.exist? @filename + File.open(@filename, "rb:bom|UTF-16LE") do |f| + f.read.encode("UTF-8") + end + end end - end - end - def self.destroy(name) - proc { File.delete(name) if File.exists? name } + def self.destroy(name) + proc { File.delete(name) if File.exists? name } + end + end end end end end -end -end diff --git a/lib/chef/util/powershell/cmdlet_result.rb b/lib/chef/util/powershell/cmdlet_result.rb index 4f97658a58..82aef4da40 100644 --- a/lib/chef/util/powershell/cmdlet_result.rb +++ b/lib/chef/util/powershell/cmdlet_result.rb @@ -19,43 +19,43 @@ require "chef/json_compat" class Chef -class Util -class Powershell - class CmdletResult - attr_reader :output_format - - def initialize(status, streams, output_format) - @status = status - @output_format = output_format - @streams = streams - end + class Util + class Powershell + class CmdletResult + attr_reader :output_format - def stdout - @status.stdout - end - - def stderr - @status.stderr - end + def initialize(status, streams, output_format) + @status = status + @output_format = output_format + @streams = streams + end - def stream(name) - @streams[name].read - end + def stdout + @status.stdout + end - def return_value - if output_format == :object - Chef::JSONCompat.parse(stream(:json)) - elsif output_format == :json - stream(:json) - else - @status.stdout - end - end + def stderr + @status.stderr + end - def succeeded? - @succeeded = @status.status.exitstatus == 0 + def stream(name) + @streams[name].read + end + + def return_value + if output_format == :object + Chef::JSONCompat.parse(stream(:json)) + elsif output_format == :json + stream(:json) + else + @status.stdout + end + end + + def succeeded? + @succeeded = @status.status.exitstatus == 0 + end + end end end end -end -end diff --git a/lib/chef/util/selinux.rb b/lib/chef/util/selinux.rb index 46b4dfce36..1aac7eeeca 100644 --- a/lib/chef/util/selinux.rb +++ b/lib/chef/util/selinux.rb @@ -71,7 +71,7 @@ class Chef def check_selinux_enabled? if selinuxenabled_path - cmd = shell_out!(selinuxenabled_path, :returns => [0,1]) + cmd = shell_out!(selinuxenabled_path, :returns => [0, 1]) case cmd.exitstatus when 1 return false diff --git a/lib/chef/util/windows/net_group.rb b/lib/chef/util/windows/net_group.rb index cfb06ed1f1..6dd6e39f2b 100644 --- a/lib/chef/util/windows/net_group.rb +++ b/lib/chef/util/windows/net_group.rb @@ -72,7 +72,6 @@ class Chef::Util::Windows::NetGroup rescue Chef::Exceptions::Win32APIError => e raise ArgumentError, e end - end def local_delete diff --git a/lib/chef/util/windows/net_use.rb b/lib/chef/util/windows/net_use.rb index 89b17a9687..b9c3ecc783 100644 --- a/lib/chef/util/windows/net_use.rb +++ b/lib/chef/util/windows/net_use.rb @@ -29,7 +29,7 @@ class Chef::Util::Windows::NetUse < Chef::Util::Windows end def to_ui2_struct(use_info) - use_info.inject({}) do |memo, (k,v)| + use_info.inject({}) do |memo, (k, v)| memo["ui2_#{k}".to_sym] = v memo end @@ -52,7 +52,7 @@ class Chef::Util::Windows::NetUse < Chef::Util::Windows end def from_use_info_struct(ui2_hash) - ui2_hash.inject({}) do |memo, (k,v)| + ui2_hash.inject({}) do |memo, (k, v)| memo[k.to_s.sub("ui2_", "").to_sym] = v memo end diff --git a/lib/chef/util/windows/net_user.rb b/lib/chef/util/windows/net_user.rb index 3c28ce6e22..f2edeacf17 100644 --- a/lib/chef/util/windows/net_user.rb +++ b/lib/chef/util/windows/net_user.rb @@ -26,6 +26,7 @@ require "chef/win32/security" class Chef::Util::Windows::NetUser < Chef::Util::Windows private + NetUser = Chef::ReservedNames::Win32::NetUser Security = Chef::ReservedNames::Win32::Security @@ -62,7 +63,7 @@ class Chef::Util::Windows::NetUser < Chef::Util::Windows } def transform_usri3(args) - args.inject({}) do |memo, (k,v)| + args.inject({}) do |memo, (k, v)| memo[USER_INFO_3_TRANSFORM[k]] = v memo end @@ -70,7 +71,7 @@ class Chef::Util::Windows::NetUser < Chef::Util::Windows def usri3_to_hash(usri3) t = USER_INFO_3_TRANSFORM.invert - usri3.inject({}) do |memo, (k,v)| + usri3.inject({}) do |memo, (k, v)| memo[t[k]] = v memo end @@ -128,7 +129,7 @@ class Chef::Util::Windows::NetUser < Chef::Util::Windows def update(args) user_modify do |user| - args.each do |key,val| + args.each do |key, val| user[key] = val end end |