From 04ade41b2c654288e579ef32bc162eeeb668f5b0 Mon Sep 17 00:00:00 2001 From: John McCrae Date: Fri, 14 May 2021 08:52:50 -0700 Subject: creating a temp branch to work from Signed-off-by: John McCrae --- lib/chef/provider/package/winget.rb | 219 ++++++++++++++++++++++ lib/chef/provider/winget_source.rb | 188 +++++++++++++++++++ lib/chef/providers.rb | 1 + lib/chef/resource/windows_package_manager.rb | 190 ------------------- lib/chef/resource/windows_package_manager_hold.rb | 201 ++++++++++++++++++++ lib/chef/resource/winget_package.rb | 100 ++++++++++ lib/chef/resource/winget_source.rb | 59 ++++++ lib/chef/resources.rb | 3 +- spec/functional/resource/winget_package2_spec.rb | 60 ++++++ spec/functional/resource/winget_package_spec.rb | 175 +++++++++++++++++ 10 files changed, 1005 insertions(+), 191 deletions(-) create mode 100644 lib/chef/provider/package/winget.rb create mode 100644 lib/chef/provider/winget_source.rb delete mode 100644 lib/chef/resource/windows_package_manager.rb create mode 100644 lib/chef/resource/windows_package_manager_hold.rb create mode 100644 lib/chef/resource/winget_package.rb create mode 100644 lib/chef/resource/winget_source.rb create mode 100644 spec/functional/resource/winget_package2_spec.rb create mode 100644 spec/functional/resource/winget_package_spec.rb diff --git a/lib/chef/provider/package/winget.rb b/lib/chef/provider/package/winget.rb new file mode 100644 index 0000000000..453730a04d --- /dev/null +++ b/lib/chef/provider/package/winget.rb @@ -0,0 +1,219 @@ +# +# Authors:: Adam Jacob () +# Ionuț Arțăriși () +# Copyright:: Copyright (c) Chef Software Inc. +# Copyright:: 2013-2016, SUSE Linux GmbH +# License:: Apache License, Version 2.0 +# +# 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_relative "../package" +require_relative "../../resource/winget_package" + +class Chef + class Provider + class Package + class Winget < Chef::Provider::Package + use_multipackage_api + allow_nils + + provides :package, platform_family: "windows" + provides :winget_package + + def load_current_resource + @current_resource = Chef::Resource::WingetPackage.new(new_resource.name) + current_resource.package_name(new_resource.package_name) + current_resource.version(get_current_versions) + current_resource + end + + def install_package(name, version) + puts "here is the version string I was passed by install_package : #{version}" + actual_version = version + arguments = build_argument_string + winget_package("install", name, "--version #{version}", arguments ) + end + + def upgrade_package(name, version) + # `zypper install` upgrades packages, we rely on the idempotency checks to get action :install behavior + install_package(name, version) + end + + private + + def build_argument_string + build_arguments = "" + build_arguments << " --source #{new_resource.source_name}" if new_resource.source_name + build_arguments << " --scope #{new_resource.scope}" if new_resource.scope + build_arguments << " --override:#{new_resource.options}" if new_resource.options + build_arguments << " --location #{new_resource.location}" if new_resource.location + build_arguments << " --force" if new_resource.force + build_arguments + end + + def get_current_versions + puts "The installed version and index are : " + package_name_array.each_with_index.map { |pkg, i| installed_version(i) } + end + + def candidate_version + @candidate_version ||= package_name_array.each_with_index.map { |pkg, i| available_version(i) } + end + + def is_installed?(package_name) + ps_results = powershell_exec!("winget list #{package_name}").result + ps_results.each do |line| + if line =~ /No installed/ + puts "returning false" + return false + elsif line =~ /\d+\.\d+\.\d+\.\d+|\d+\.\d+\.\d+/ + current_version = line.split(' ') + puts "I think I found this version : #{current_version[-1]}" + return current_version[-1] + end + end + end + + def get_latest_version(package_name) + # return a version number + end + + def resolve_current_version(package_name) + puts "resolving current version for : #{package_name}" + latest_version = current_version = nil + is_installed = false + # latest version is the one on the Internet + # current version is the one installed + # is the damned thing installed locally? If yes, gimme the version from the Internet + # + logger.trace("#{new_resource} checking winget") + status = is_installed?(package_name) + if status != false + is_installed = true + current_version = status + puts "current version is : #{current_version}" + end + + latest_version = get_latest_version + + # status = shell_out!("winget", "list", package_name) + # puts "What is my status : #{status.stdout}" + # status.stdout.each_line do |line| + # if line =~ /^No installed package *: (.+) *$/ + + # # installed_version = $1.strip + # # logger.trace("#{new_resource} version #{installed_version}") + # # is_installed = true + # elsif line =~ /^Name *: (.+) *$/ + # end + puts "Is #{package_name} installed? : #{is_installed}" + current_version ||= latest_version if is_installed + current_version + end + + # def resolve_current_version(package_name) + # latest_version = current_version = nil + # is_installed = false + # logger.trace("#{new_resource} checking zypper") + # status = shell_out!("zypper", "--non-interactive", "info", package_name) + # status.stdout.each_line do |line| + # case line + # when /^Version *: (.+) *$/ + # latest_version = $1.strip + # logger.trace("#{new_resource} version #{latest_version}") + # when /^Installed *: Yes.*$/ # http://rubular.com/r/9StcAMjOn6 + # is_installed = true + # logger.trace("#{new_resource} is installed") + # when /^Status *: out-of-date \(version (.+) installed\) *$/ + # current_version = $1.strip + # logger.trace("#{new_resource} out of date version #{current_version}") + # end + # end + # current_version ||= latest_version if is_installed + # current_version + # end + + + + def resolve_available_version(package_name, new_version) + puts "checking available versions for #{package_name}" + search_string = new_version.nil? ? package_name : "#{package_name}=#{new_version}" + so = shell_out!("winget", "search", search_string) + so.stdout.each_line do |line| + if line =~ /#{search_string}\s+\w+\.\w+\s+\d+\.\d+\.\d+/ + version = line.split(" ")[2] + return version + end + end + nil + end + + def available_version(index) + @available_version ||= [] + @available_version[index] ||= resolve_available_version(package_name_array[index], safe_version_array[index]) + @available_version[index] + end + + def installed_version(index) + puts "here is the installed version #{resolve_current_version(package_name_array[index])} " + @installed_version ||= [] + @installed_version[index] ||= resolve_current_version(package_name_array[index]) + @installed_version[index] + end + + def zip(names, versions) + names.zip(versions).map do |n, v| + (v.nil? || v.empty?) ? n : "#{n}=#{v}" + end.compact + end + + def winget_version + @winget_version ||= + `winget --version`.scan(/\d+/).join(".").to_f + end + + def winget_package(command, name, version, arguments) + # zipped_names = zip(names, versions) + # shell_out!("winget", global_options, "--non-interactive", gpg_checks, command, *options, zipped_names) + shell_out!("winget", command, name, version, arguments) + end + + # def global_options + # new_resource.global_options + # end + + # def locked_packages + # @locked_packages ||= + # begin + # locked = shell_out!("zypper", "locks") + # locked.stdout.each_line.map do |line| + # line.split("|").shift(2).last.strip + # end + # end + # end + + def safe_version_array + if new_resource.version.is_a?(Array) + new_resource.version + elsif new_resource.version.nil? + package_name_array.map { nil } + else + [ new_resource.version ] + end + end + + end + end + end +end diff --git a/lib/chef/provider/winget_source.rb b/lib/chef/provider/winget_source.rb new file mode 100644 index 0000000000..61c920bfd0 --- /dev/null +++ b/lib/chef/provider/winget_source.rb @@ -0,0 +1,188 @@ +# +# Author:: Tim Smith () +# Copyright:: Copyright (c) Chef Software Inc. +# License:: Apache License, Version 2.0 +# +# 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_relative "../resource" +require_relative "../dsl/declare_resource" +require_relative "noop" +# require "shellwords" unless defined?(Shellwords) +require "chef-utils/dist" unless defined?(ChefUtils::Dist) + +class Chef + class Provider + class WingetSource < Chef::Provider + provides :winget_source, platform_family: "windows" + + def load_current_resource; end + + action :create do + if new_resource.gpgautoimportkeys + install_gpg_key(new_resource.gpgkey) + else + logger.debug("'gpgautoimportkeys' property is set to false. Skipping key import.") + end + + declare_resource(:template, "/etc/zypp/repos.d/#{escaped_repo_name}.repo") do + if template_available?(new_resource.source) + source new_resource.source + else + source ::File.expand_path("support/zypper_repo.erb", __dir__) + local true + end + sensitive new_resource.sensitive + variables(config: new_resource) + mode new_resource.mode + notifies :refresh, new_resource, :immediately if new_resource.refresh_cache + end + end + + action :delete do + declare_resource(:execute, "zypper --quiet --non-interactive removerepo #{escaped_repo_name}") do + only_if "zypper --quiet lr #{escaped_repo_name}" + end + end + + action :refresh do + declare_resource(:execute, "zypper --quiet --non-interactive refresh --force #{escaped_repo_name}") do + only_if "zypper --quiet lr #{escaped_repo_name}" + end + end + + alias_method :action_add, :action_create + alias_method :action_remove, :action_delete + + # zypper repos are allowed to have spaces in the names + # @return [String] escaped repo string + def escaped_repo_name + Shellwords.escape(new_resource.repo_name) + end + + # return the specified cookbook name or the cookbook containing the + # resource. + # + # @return [String] name of the cookbook + def cookbook_name + new_resource.cookbook || new_resource.cookbook_name + end + + # determine if a template file is available in the current run + # @param [String] path the path to the template file + # + # @return [Boolean] template file exists or doesn't + def template_available?(path) + !path.nil? && run_context.has_template_in_cookbook?(cookbook_name, path) + end + + # determine if a cookbook file is available in the run + # @param [String] fn the path to the template file + # + # @return [Boolean] cookbook file exists or doesn't + def has_cookbook_file?(fn) + run_context.has_cookbook_file_in_cookbook?(cookbook_name, fn) + end + + # Given the provided key URI determine what kind of chef resource we need + # to fetch the key + # @param [String] uri the uri of the gpg key (local path or http URL) + # + # @raise [Chef::Exceptions::FileNotFound] Key isn't remote or found in the current run + # + # @return [Symbol] :remote_file or :cookbook_file + def key_type(uri) + if uri.start_with?("http") + logger.trace("Will use :remote_file resource to cache the gpg key locally") + :remote_file + elsif has_cookbook_file?(uri) + logger.trace("Will use :cookbook_file resource to cache the gpg key locally") + :cookbook_file + else + raise Chef::Exceptions::FileNotFound, "Cannot determine location of gpgkey. Must start with 'http' or be a file managed by #{ChefUtils::Dist::Infra::PRODUCT}." + end + end + + # the version of gpg installed on the system + # + # @return [Gem::Version] the version of GPG + def gpg_version + so = shell_out!("gpg --version") + # matches 2.0 and 2.2 versions from SLES 12 and 15: https://rubular.com/r/e6D0WfGK6SXvUp + version = /gpg \(GnuPG\)\s*(.*)/.match(so.stdout)[1] + logger.trace("GPG package version is #{version}") + Gem::Version.new(version) + end + + # is the provided key already installed + # @param [String] key_path the path to the key on the local filesystem + # + # @return [boolean] is the key already known by rpm + def key_installed?(key_path) + so = shell_out("/bin/rpm -qa gpg-pubkey*") + # expected output & match: http://rubular.com/r/RdF7EcXEtb + status = /gpg-pubkey-#{short_key_id(key_path)}/.match(so.stdout) + logger.trace("GPG key at #{key_path} is known by rpm? #{status ? "true" : "false"}") + status + end + + # extract the gpg key's short key id from a local file. Learning moment: This 8 hex value ID + # is sometimes incorrectly called the fingerprint. The fingerprint is the full length value + # and googling for that will just result in sad times. + # + # @param [String] key_path the path to the key on the local filesystem + # + # @return [String] the short key id of the key + def short_key_id(key_path) + if gpg_version >= Gem::Version.new("2.2") # SLES 15+ + so = shell_out!("gpg --import-options import-show --dry-run --import --with-colons #{key_path}") + # expected output and match: https://rubular.com/r/uXWJo3yfkli1qA + short_key_id = /fpr:*\h*(\h{8}):/.match(so.stdout)[1].downcase + else # SLES 12 and earlier + so = shell_out!("gpg --with-fingerprint #{key_path}") + # expected output and match: http://rubular.com/r/BpfMjxySQM + short_key_id = %r{pub\s*\S*/(\S*)}.match(so.stdout)[1].downcase + end + logger.trace("GPG short key ID of key at #{key_path} is #{short_key_id}") + short_key_id + end + + # install the provided gpg key + # @param [String] uri the uri of the local or remote gpg key + def install_gpg_key(uri) + unless uri + logger.debug("'gpgkey' property not provided or set to nil. Skipping key import.") + return + end + + cached_keyfile = ::File.join(Chef::Config[:file_cache_path], uri.split("/")[-1]) + + declare_resource(key_type(new_resource.gpgkey), cached_keyfile) do + source uri + mode "0644" + sensitive new_resource.sensitive + action :create + end + + declare_resource(:execute, "import gpg key from #{new_resource.gpgkey}") do + command "/bin/rpm --import #{cached_keyfile}" + not_if { key_installed?(cached_keyfile) } + action :run + end + end + end + end +end + +Chef::Provider::Noop.provides :winget_source diff --git a/lib/chef/providers.rb b/lib/chef/providers.rb index 331f224855..7761987386 100644 --- a/lib/chef/providers.rb +++ b/lib/chef/providers.rb @@ -74,6 +74,7 @@ require_relative "provider/package/cab" require_relative "provider/package/powershell" require_relative "provider/package/msu" require_relative "provider/package/snap" +require_relative "provider/package/winget" require_relative "provider/service/arch" require_relative "provider/service/freebsd" diff --git a/lib/chef/resource/windows_package_manager.rb b/lib/chef/resource/windows_package_manager.rb deleted file mode 100644 index 87e8e27b86..0000000000 --- a/lib/chef/resource/windows_package_manager.rb +++ /dev/null @@ -1,190 +0,0 @@ -# -# Author:: Richard Lavey (richard.lavey@calastone.com) -# -# Copyright:: 2015-2017, Calastone Ltd. -# Copyright:: Copyright (c) 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_relative "../resource" -require_relative "../exceptions" -# require_relative "../win32/error" if RUBY_PLATFORM.match?(/mswin|mingw|windows/) -# require "chef-utils/dist" unless defined?(ChefUtils::Dist) - -class Chef - class Resource - class WindowsPackageManager < Chef::Resource - unified_mode true - - provides :windows_package_manager - - description "Use the **windows_package_manager** resource allows you to add/remove/update windows packages using WinGet." - introduced "17.20" - examples <<~DOC - **Add a new package to a system** - - ```ruby - windows_package_manager 'Install 7zip' do - package_name 7zip - action :install - end - ``` - - **Add a package source to install from** - - ```ruby - windows_package_manager "Add New Source" do - source_name "my_package_source" - url https://foo/bar.com/packages - action :register - end - ``` - - **Remove a package source to install from** - - ```ruby - windows_package_manager "Add New Source" do - source_name "my_package_source" - action :unregister - end - ``` - - **Install a package from a custom source** - - ```ruby - windows_package_manager "Install 7zip from new source" do - package_name 7zip - source_name "my_package_source" - scope 'User' - location "C:\\Foo\\7Zip" - override "-o, -q, -h" - force true - action :install - end - ``` - - DOC - - property :package_name, String, - description: "The name of a single package to be installed." - - property :source_name, String, - description: "The name of a custom installation source.", - default: "winget" - - property :url, String, - description: "The url to a package or source" - - property :scope, String, - description: "Install the package for the current user or the whole machine.", - default: "user", equal_to: [user, machine] - - property :location, String, - description: "The location on the local system to install the package to. For example 'c:\foo\'." - - property :override, Array, - description: "An array containing command line switches to pass to your package. In the form of '-o, -foo, -bar, -blat'." - - property :force, [TrueClass, FalseClass], - description: "Tells WinGet to bypass hash-checking a package.", - default: false - - action :install, description: "Installs an item on a Windows node." do - local_arguments = build_argument_string - converge_by("install package: #{new_resource.package_name}") do - install_cmd = ps_execute_winget("install", package_name: new_resource.package_name, arguments: local_arguments) - res = powershell_exec(install_cmd) - raise "Failed to install #{new_resource.package_name}: #{res.errors}" if res.error? - end - end - - action :register, description: "Adds or updates a package source location to install a package from." do - if package_source_exists? - converge_if_changed :url do - update_cmd = build_ps_package_source_command("update", new_resource.source_name, new_resource.url) - res = powershell_exec(update_cmd) - raise "Failed to update #{new_resource.source_name}: #{res.errors}" if res.error? - end - else - converge_by("register source: #{new_resource.source_name}") do - register_cmd = build_ps_package_source_command("add", new_resource.source_name, new_resource.url) - res = powershell_exec!(register_cmd) - puts "what does my result say? #{res.result}" - raise "Failed to register #{new_resource.source_name}: #{res.errors}" if res.error? - end - end - end - - action :unregister, description: "Removes a package source location." do - if package_source_exists? - powershell_exec!("winget source remove --name #{new_resource.source_name} ") - end - end - - action_class do - def build_argument_string - build_arguments = "" - build_arguments << " --source #{new_resource.source_name}" if new_resource.source_name - build_arguments << " --scope #{new_resource.scope}" if new_resource.scope - build_arguments << " --override:#{new_resource.override}" if new_resource.override - build_arguments << " --location #{new_resource.location}" if new_resource.location - build_arguments << " --force" if new_resource.force - build_arguments - end - - def ps_execute_winget(cmd_type, package_name:, arguments:) - <<-CMD - winget #{cmd_type} --name #{package_name} #{arguments} - CMD - end - - def package_source_exists? - powershell_exec!(ps_package_sources_cmd).result - end - - def ps_package_sources_cmd - <<-CMD - $hash = new-object System.Collections.Hashtable - [System.Collections.ArrayList]$sources = Invoke-Expression "winget source list" - $sources += $sources.Remove("Name Argument") - $sources += $sources.Remove("-------------------------------------------------------") - - foreach($source in $sources){ - $break = $($source -replace '\s+', ' ').split() - $key = $break[0] - $value = $break[1] - $hash.Add($key, $value) - } - - foreach($key in $hash.Keys){ - if($key -contains "#{new_resource.source_name}"){ - return $true - } - else{ - return $false - } - } - CMD - end - - def build_ps_package_source_command(cmdlet_type, source, url) - cmd = "winget source #{cmdlet_type} --Name #{source} #{url}" - cmd - end - - end - - end - end -end diff --git a/lib/chef/resource/windows_package_manager_hold.rb b/lib/chef/resource/windows_package_manager_hold.rb new file mode 100644 index 0000000000..bd6aae9bc4 --- /dev/null +++ b/lib/chef/resource/windows_package_manager_hold.rb @@ -0,0 +1,201 @@ +# +# Author:: Richard Lavey (richard.lavey@calastone.com) +# +# Copyright:: 2015-2017, Calastone Ltd. +# Copyright:: Copyright (c) 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_relative "../resource" +require_relative "../exceptions" +# require_relative "../win32/error" if RUBY_PLATFORM.match?(/mswin|mingw|windows/) +# require "chef-utils/dist" unless defined?(ChefUtils::Dist) + +class Chef + class Resource + class WindowsPackageManager < Chef::Resource + unified_mode true + + provides :windows_package_manager + + description "Use the **windows_package_manager** resource allows you to add/remove/update windows packages using WinGet." + introduced "17.20" + examples <<~DOC + **Add a new package to a system** + + ```ruby + windows_package_manager 'Install 7zip' do + package_name 7zip + action :install + end + ``` + + **Add several packages on a system** + + ```ruby + windows_package_manager 'Install 7zip' do + package_name %[1Password MicroK8s] + action :install + end + ``` + + **Add a package source to install from** + + ```ruby + windows_package_manager "Add New Source" do + source_name "my_package_source" + url https://foo/bar.com/packages + action :register + end + ``` + + **Remove a package source to install from** + + ```ruby + windows_package_manager "Add New Source" do + source_name "my_package_source" + action :unregister + end + ``` + + **Install a package from a custom source** + + ```ruby + windows_package_manager "Install 7zip from new source" do + package_name 7zip + source_name "my_package_source" + scope 'User' + location "C:\\Foo\\7Zip" + override "-o, -q, -h" + force true + action :install + end + ``` + + DOC + + property :package_name, Array, + description: "The name of one or more packages to be installed." + + property :source_name, String, + description: "The name of a custom installation source.", + default: "winget" + + property :url, String, + description: "The url to a package or source" + + property :scope, String, + description: "Install the package for the current user or the whole machine.", + default: "user", equal_to: %w{user machine} + + property :location, String, + description: "The location on the local system to install the package to. For example 'c:\foo\'." + + property :override, Array, + description: "An array containing command line switches to pass to your package. In the form of '-o, -foo, -bar, -blat'." + + property :force, [TrueClass, FalseClass], + description: "Tells WinGet to bypass hash-checking a package.", + default: false + + action :install, description: "Installs an item on a Windows node." do + local_arguments = build_argument_string + new_resource.package_name.each do |package| + converge_by("install package: #{package}") do + install_cmd = ps_execute_winget("install", package_name: package, arguments: local_arguments) + res = powershell_exec(install_cmd) + raise "Failed to install #{new_resource.package_name}: #{res.errors}" if res.error? + end + end + end + + action :register, description: "Adds or updates a package source location to install a package from." do + if package_source_exists? + converge_if_changed :url do + update_cmd = build_ps_package_source_command("update", new_resource.source_name, new_resource.url) + res = powershell_exec(update_cmd) + raise "Failed to update #{new_resource.source_name}: #{res.errors}" if res.error? + end + else + converge_by("register source: #{new_resource.source_name}") do + register_cmd = build_ps_package_source_command("add", new_resource.source_name, new_resource.url) + res = powershell_exec!(register_cmd) + puts "what does my result say? #{res.result}" + raise "Failed to register #{new_resource.source_name}: #{res.errors}" if res.error? + end + end + end + + action :unregister, description: "Removes a package source location." do + if package_source_exists? + powershell_exec!("winget source remove --name #{new_resource.source_name} ") + end + end + + action_class do + def build_argument_string + build_arguments = "" + build_arguments << " --source #{new_resource.source_name}" if new_resource.source_name + build_arguments << " --scope #{new_resource.scope}" if new_resource.scope + build_arguments << " --override:#{new_resource.override}" if new_resource.override + build_arguments << " --location #{new_resource.location}" if new_resource.location + build_arguments << " --force" if new_resource.force + build_arguments + end + + def ps_execute_winget(cmd_type, package_name:, arguments:) + <<-CMD + winget #{cmd_type} --name #{package_name} #{arguments} + CMD + end + + def package_source_exists? + powershell_exec!(ps_package_sources_cmd).result + end + + def ps_package_sources_cmd + <<-CMD + $hash = new-object System.Collections.Hashtable + [System.Collections.ArrayList]$sources = Invoke-Expression "winget source list" + $sources += $sources.Remove("Name Argument") + $sources += $sources.Remove("-------------------------------------------------------") + + foreach($source in $sources){ + $break = $($source -replace '\s+', ' ').split() + $key = $break[0] + $value = $break[1] + $hash.Add($key, $value) + } + + foreach($key in $hash.Keys){ + if($key -contains "#{new_resource.source_name}"){ + return $true + } + else{ + return $false + } + } + CMD + end + + def build_ps_package_source_command(cmdlet_type, source, url) + cmd = "winget source #{cmdlet_type} --Name #{source} #{url}" + cmd + end + + end + + end + end +end diff --git a/lib/chef/resource/winget_package.rb b/lib/chef/resource/winget_package.rb new file mode 100644 index 0000000000..bb97dbf081 --- /dev/null +++ b/lib/chef/resource/winget_package.rb @@ -0,0 +1,100 @@ +# +# Author:: Joe Williams () +# Copyright:: Copyright 2009-2016, Joe Williams +# License:: Apache License, Version 2.0 +# +# 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_relative "package" + +class Chef + class Resource + class WingetPackage < Chef::Resource::Package + unified_mode true + + provides :winget_package + provides :package, platform_family: "windows" + + description "Use the **winget_package** resource to add/update windows packages using WinGet." + introduced "17.20" + examples <<~DOC + **Add a new package to a system** + + ```ruby + winget_package 'Install 7zip' do + package_name 7zip + action :install + end + ``` + + **Add multiple packages to a system** + + ```ruby + winget_package 'Install 7zip' do + package_name ["7zip", "notepad", "foo"] + package_version ["0.1.2", "5.4", "0.0.5"] + action :install + end + ``` + + **Add several packages on a system** + + ```ruby + winget_package 'Install 7zip' do + package_name %[1Password MicroK8s] + action :install + end + ``` + + **Install a package from a custom source** + + ```ruby + winget_package "Install 7zip from new source" do + package_name 7zip + source_name "my_package_source" + scope 'User' + location "C:\\Foo\\7Zip" + options "-o, -q, -h" + force true + action :install + end + ``` + DOC + + property :package_name, [ String, Array ], + description: "The name of one or more packages to be installed." + + property :package_version, [ String, Array ], + description: "The version of one or more packages to be installed. The position of the version corresponds to the name specified in the package_name array." + + property :source_name, String, + description: "The name of a custom installation source.", + default: "winget" + + property :scope, String, + description: "Install the package for the current user or the whole machine.", + default: "user", equal_to: %w{user machine} + + property :location, String, + description: "The location on the local system to install the package to. For example 'c:\\foo\\'." + + property :options, [ String, Array ], + description: "Command line switches to pass to your package. In the form of ['-o', '-foo', '-bar', '-blat']." + + property :force, [TrueClass, FalseClass], + description: "Tells WinGet to bypass hash-checking a package.", + default: false + end + end +end \ No newline at end of file diff --git a/lib/chef/resource/winget_source.rb b/lib/chef/resource/winget_source.rb new file mode 100644 index 0000000000..6fdfbb6d8a --- /dev/null +++ b/lib/chef/resource/winget_source.rb @@ -0,0 +1,59 @@ +# +# Author:: Tim Smith () +# Copyright:: Copyright (c) Chef Software Inc. +# License:: Apache License, Version 2.0 +# +# 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_relative "../resource" + +class Chef + class Resource + class WingetSource < Chef::Resource + unified_mode true + + provides(:winget_source) { true } + + description "Use the **winget_source** resource allows you to add/remove/update sources for your WinGet packages." + introduced "17.20" + examples <<~DOC + **Add a package source to install from** + + ```ruby + windows_package_manager "Add New Source" do + source_name "my_package_source" + url https://foo/bar.com/packages + action :register + end + ``` + + **Remove a package source to install from** + + ```ruby + windows_package_manager "Add New Source" do + source_name "my_package_source" + action :unregister + end + ``` + DOC + + property :source_name, String, + description: "The name of a custom installation source.", + default: "winget" + + property :url, String, + description: "The url to a source" + end + end +end diff --git a/lib/chef/resources.rb b/lib/chef/resources.rb index 8ae922a28b..6a894cb74b 100644 --- a/lib/chef/resources.rb +++ b/lib/chef/resources.rb @@ -169,4 +169,5 @@ require_relative "resource/windows_uac" require_relative "resource/windows_workgroup" require_relative "resource/timezone" require_relative "resource/windows_user_privilege" -require_relative "resource/windows_security_policy" \ No newline at end of file +require_relative "resource/windows_security_policy" +require_relative "resource/winget_package" \ No newline at end of file diff --git a/spec/functional/resource/winget_package2_spec.rb b/spec/functional/resource/winget_package2_spec.rb new file mode 100644 index 0000000000..8586cdc49c --- /dev/null +++ b/spec/functional/resource/winget_package2_spec.rb @@ -0,0 +1,60 @@ +require "spec_helper" +require "chef/mixin/powershell_exec" +require "chef/resource/hostname" +describe Chef::Resource::Hostname, :windows_only do + include Chef::Mixin::PowershellExec + + def get_domain_status + powershell_exec!("(Get-WmiObject -Class Win32_ComputerSystem).PartofDomain").result + end + + let(:package_name) { "7zip" } + let(:package_name2) { "pennywise" } + let(:package_version) { nil } + let(:source_name) { "winget" } + let(:scope) { "user" } + let(:options) { nil } + let(:force) { nil } + + # let(:run_context) do + # node = Chef::Node.new + # node.consume_external_attrs(OHAI_SYSTEM.data, {}) # node[:languages][:powershell][:version] + # node.automatic["os"] = "windows" + # node.automatic["platform"] = "windows" + # node.automatic["platform_version"] = "6.1" + # node.automatic["kernel"][:machine] = :x86_64 # Only 64-bit architecture is supported + # empty_events = Chef::EventDispatch::Dispatcher.new + # Chef::RunContext.new(node, {}, empty_events) + # end + + # subject do + # new_resource = Chef::Resource::WingetPackage.new("Winget", run_context) + # new_resource + # end + + # let(:provider) do + # provider = subject.provider_for_action(subject.action) + # provider + # end + + let(:run_context) do + Chef::RunContext.new(Chef::Node.new, {}, Chef::EventDispatch::Dispatcher.new) + end + + let(:winget_package) do + r = Chef::Resource::WingetPackage.new(package_name, run_context) + r + end + + + describe "Installing packages" do + context "Installing various Windows packages" do + it "installs a single windows package" do + winget_package.package_name package_name2 + winget_package.run_action(:install) + expect(winget_package).to be_updated_by_last_action + end + end + + end +end \ No newline at end of file diff --git a/spec/functional/resource/winget_package_spec.rb b/spec/functional/resource/winget_package_spec.rb new file mode 100644 index 0000000000..05dee7cdb0 --- /dev/null +++ b/spec/functional/resource/winget_package_spec.rb @@ -0,0 +1,175 @@ +# if winget not installed yet, install it + +# +# Author:: John McCrae () +# Copyright:: Copyright (c) Chef Software Inc. +# License:: Apache License, Version 2.0 +# +# 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 "spec_helper" +require "chef/mixin/powershell_exec" +require "chef/mixin/shell_out" + +describe Chef::Resource::WingetPackage, :requires_root, :windows_only do + include Chef::Mixin::PowershellExec + include Chef::Mixin::ShellOut + + let(:my_package_name) { ["pennywise"] } + let(:my_other_package_name) { "AWS Command Line Interface v2" } + let(:my_package_list) { ["1Password", "MicroK8s"] } + let(:my_source_name) { "ChefTest" } + let(:my_url ) { "https://testingchef.blob.core.windows.net/files/" } + let(:my_scope) { "machine" } + let(:my_location) { "C:\\Program Files\\Foo\\aws_cli" } + let(:my_override) { nil } + let(:my_force) { true } + + # let(:resource) do + # Chef::RunContext.new(Chef::Node.new, {}, Chef::EventDispatch::Dispatcher.new) + # end + + let(:run_context) do + node = Chef::Node.new + node.consume_external_attrs(OHAI_SYSTEM.data, {}) # node[:languages][:powershell][:version] + node.automatic["os"] = "windows" + node.automatic["platform"] = "windows" + node.automatic["platform_version"] = "6.1" + node.automatic["kernel"][:machine] = :x86_64 # Only 64-bit architecture is supported + empty_events = Chef::EventDispatch::Dispatcher.new + Chef::RunContext.new(node, {}, empty_events) + end + + subject do + new_resource = Chef::Resource::WingetPackage.new("Winget", run_context) + new_resource + end + + def package_source_exists? + powershell_exec!(ps_package_sources_cmd).result + end + + def ps_package_sources_cmd + <<-CMD + $hash = new-object System.Collections.Hashtable + [System.Collections.ArrayList]$sources = Invoke-Expression "winget source list" + $sources += $sources.Remove("Name Argument") + $sources += $sources.Remove("-------------------------------------------------------") + + foreach($source in $sources){ + $break = $($source -replace '\s+', ' ').split() + $key = $break[0] + $value = $break[1] + $hash.Add($key, $value) + } + + foreach($key in $hash.Keys){ + if($key -contains "#{my_source_name}"){ + return $true + } + else{ + return $false + } + } + CMD + end + + def ps_get_app_installer_bundle + <<-CMD + $url = "https://testingchef.blob.core.windows.net/sources/Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.appxbundle" + New-Item -ItemType Directory -Force -Path C:\\chef_download\\ + $download_path = "C:\\chef_download\\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.appxbundle" + Invoke-WebRequest -Uri $url -OutFile $download_path + Import-Module Appx + Add-AppxPackage -Path $download_path + CMD + end + + def ps_package_is_installed?(package_name:) + <<-CMD + $ErrorActionPreference = 'SilentlyContinue' + $result = get-command -Name "#{package_name}" + if ([string]::IsNullOrEmpty($result)){ + return $false + } + else { + return $true + } + CMD + end + + # before do + # ps_get_app_installer_bundle + # end + + # after { "winget source reset --force" } + + # context "manage package sources" do + # it "adds a new package source" do + # windows_package_manager "loading a new package source" do + # source_name my_source_name + # url my_url + # action :register + # end.should_be_updated + # expect(package_source_exists?).to be true + # end + + # it "removes a package source" do + # windows_package_manager "loading a new package source" do + # source_name my_source_name + # action :unregister + # end + # expect(package_source_exists?).to be false + # end + # end + + describe "adding packages to a windows node" do + context "manage packages" do + it "does not add a package that is already installed" do + subject.package_name my_package_name + subject.run_action(:install) + expect(subject.updated_by_last_action?).to be false + # winget_package "loading a new package" do + # package_name my_package_name + # action :install + # end.should_be_updated + expect(powershell_exec!(ps_package_is_installed?(package_name:'7z.exe')).result).to be true + end + end + end + + # it "adds more than one package" do + # puts "My Package Class is : #{my_package_list.class}" + # # my_package_list.each do |item| + # my_package_list.each do |item| + # windows_package_manager "install packages from an array" do + # source_name item + # action :install + # end.should_be_updated + # end + # expect(my_package_list).to be(Array) + # end + + # it "adds a package to the local node with extended settings" do + # windows_package_manager "loading a new package with parameters" do + # package_name my_other_package_name + # scope my_scope + # location my_location + # force my_force + # action :install + # end.should_be_updated + # # expect + # end + +end -- cgit v1.2.1