From 00b9d510b68e87b77e8d3105280b4e8c8d6d1dbf Mon Sep 17 00:00:00 2001 From: Tim Smith Date: Fri, 19 Jan 2018 21:41:25 -0800 Subject: Add windows auto_run, font, pagefile, printer, printer_port, and shortcut resources Ported from the Windows cookbook Signed-off-by: Tim Smith --- lib/chef/resource/windows_font.rb | 127 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 lib/chef/resource/windows_font.rb (limited to 'lib/chef/resource/windows_font.rb') diff --git a/lib/chef/resource/windows_font.rb b/lib/chef/resource/windows_font.rb new file mode 100644 index 0000000000..495abc787b --- /dev/null +++ b/lib/chef/resource/windows_font.rb @@ -0,0 +1,127 @@ +# +# Copyright:: 2014-2018, Schuberg Philis BV. +# Copyright:: 2017-2018, Chef Software, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require "chef/resource" + +class Chef + class Resource + class WindowsFont < Chef::Resource + require "chef/util/path_helper" + + resource_name :windows_font + provides :windows_font + + description "Use the windows_font resource to install or remove font files on Windows."\ + " Sources the font by default from the cookbook using the resource, but a URI"\ + " source can be specified as well." + introduced "14.0" + + property :font_name, String, + description: "The file name of the font file name to install if different than the resource name.", + name_property: true + + property :source, String, + description: "A local filesystem path or URI to source the font file from.", + coerce: proc { |x| x =~ /^.:.*/ ? x.tr('\\', "/").gsub("//", "/") : x } + + action :install do + description "Install a font to the system fonts directory." + + if font_exists? + Chef::Log.debug("Not installing font: #{new_resource.font_name} as font already installed.") + else + retrieve_cookbook_font + install_font + del_cookbook_font + end + end + + action_class do + # if a source is specified fetch using remote_file. If not use cookbook_file + def retrieve_cookbook_font + font_file = new_resource.font_name + if new_resource.source + declare_resource(:remote_file, font_file) do + action :nothing + source source_uri + path Chef::Util::PathHelper.join(ENV["TEMP"], font_file) + end.run_action(:create) + else + declare_resource(:cookbook_file, font_file) do + action :nothing + cookbook cookbook_name.to_s unless cookbook_name.nil? + path Chef::Util::PathHelper.join(ENV["TEMP"], font_file) + end.run_action(:create) + end + end + + # delete the temp cookbook file + def del_cookbook_font + file Chef::Util::PathHelper.join(ENV["TEMP"], new_resource.font_name) do + action :delete + end + end + + # install the font into the appropriate fonts directory + def install_font + require "win32ole" if RUBY_PLATFORM =~ /mswin|mingw32|windows/ + fonts_dir = WIN32OLE.new("WScript.Shell").SpecialFolders("Fonts") + folder = WIN32OLE.new("Shell.Application").Namespace(fonts_dir) + converge_by("install font #{new_resource.font_name} to #{fonts_dir}") do + folder.CopyHere(Chef::Util::PathHelper.join(ENV["TEMP"], new_resource.font_name)) + end + end + + # Check to see if the font is installed in the fonts dir + # + # @return [Boolean] Is the font is installed? + def font_exists? + require "win32ole" if RUBY_PLATFORM =~ /mswin|mingw32|windows/ + fonts_dir = WIN32OLE.new("WScript.Shell").SpecialFolders("Fonts") + Chef::Log.debug("Seeing if the font at #{Chef::Util::PathHelper.join(fonts_dir, new_resource.font_name)} exists") + ::File.exist?(Chef::Util::PathHelper.join(fonts_dir, new_resource.font_name)) + end + + # Parse out the schema provided to us to see if it's one we support via remote_file. + # We do this because URI will parse C:/foo as schema 'c', which won't work with remote_file + # + # @return [Boolean] + def remote_file_schema?(schema) + return true if %w{http https ftp}.include?(schema) + end + + # return new_resource.source if we have a proper URI specified + # if it's a local file listed as a source return it in file:// format + # + # @return [String] path to the font + def source_uri + begin + require "uri" + if remote_file_schema?(URI.parse(new_resource.source).scheme) + Chef::Log.debug("source property starts with ftp/http. Using source property unmodified") + return new_resource.source + end + rescue URI::InvalidURIError + Chef::Log.warn("source property of #{new_resource.source} could not be processed as a URI. Check the format you provided.") + end + Chef::Log.debug("source property does not start with ftp/http. Prepending with file:// as it appears to be a local file.") + "file://#{new_resource.source}" + end + end + end + end +end -- cgit v1.2.1