From 7860617a23fab4377fac2e6a7d799fe6766c5530 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Magnus=20Rakv=C3=A5g?= Date: Fri, 8 Jun 2018 11:23:25 +0200 Subject: initial resource shell MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Tor Magnus Rakvåg --- lib/chef/resource/powershell_package_source.rb | 88 ++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 lib/chef/resource/powershell_package_source.rb (limited to 'lib') diff --git a/lib/chef/resource/powershell_package_source.rb b/lib/chef/resource/powershell_package_source.rb new file mode 100644 index 0000000000..6b969d67fb --- /dev/null +++ b/lib/chef/resource/powershell_package_source.rb @@ -0,0 +1,88 @@ +# Author:: Tor Magnus Rakvåg (tm@intility.no) +# Copyright:: 2015-2018 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. +# + +class Chef + class Resource + class PowershellPackageSource < Chef::Resource + resource_name "powershell_package_source" + provides(:powershell_package_source) { true } + + description "Use the powershell_package_source resource to register a powershell package repository" + introduced "15.0" + + property :name, String, + description: "", + name_property: true + + property :url, String, + description: "", + required: true + + property :trusted, [true, false], + description: "", + default: false + + property :package_management_provider, String, + equal_to: %w{ Programs msi NuGet msu PowerShellGet psl chocolatey }, + validation_message: "The following providers are supported: 'Programs', 'msi', 'NuGet', 'msu', 'PowerShellGet', 'psl' or 'chocolatey'", + description: "", + default: "NuGet" + + property :publish_location, String, + description: "", + required: false + + property :script_source_location, String, + description: "", + required: false + + property :script_publish_location, String, + description: "", + required: false + + action :register do + register_cmd = "Register-PackageSource -Name '#{new_resource.name}' -Location '#{new_resource.url}'" + register_cmd << " -Trusted" if new_resource.trusted + register_cmd << " -PublishLocation '#{new_resource.publish_location}'" if new_resource.publish_location + register_cmd << " -ScriptSourceLocation '#{new_resource.script_source_location}'" if new_resource.script_source_location + register_cmd << " -ScriptPublishLocation '#{new_resource.script_publish_location}'" if new_resource.script_publish_location + register_cmd << " -Force -ForceBootstrap" + + powershell_script "register package source: #{new_resource.name}" do + code register_cmd + not_if { package_source_exists? } + end + end + + action :unregister do + unregister_cmd = "Get-PackageSource -Name '#{new_resource.name}' | Unregister-PackageSource" + + powershell_script "unregister package source: #{new_resource.name}" do + code unregister_cmd + only_if { package_source_exists? } + end + end + + action_class do + def package_source_exists? + cmd = powershell_out!("(Get-PackageSource -Name '#{new_resource.source}').Name") + cmd.stdout.downcase.strip == new_resource.name.downcase + end + end + end + end +end -- cgit v1.2.1 From 2870d0e263d4effe21ce33400f3cd9d7ad8c6616 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Magnus=20Rakv=C3=A5g?= Date: Fri, 8 Jun 2018 11:54:04 +0200 Subject: wire up the resource MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Tor Magnus Rakvåg --- lib/chef/resources.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'lib') diff --git a/lib/chef/resources.rb b/lib/chef/resources.rb index c232ce04e1..cc1af2a5df 100644 --- a/lib/chef/resources.rb +++ b/lib/chef/resources.rb @@ -68,6 +68,7 @@ require "chef/resource/pacman_package" require "chef/resource/paludis_package" require "chef/resource/perl" require "chef/resource/portage_package" +require "chef/resource/powershell_package_source" require "chef/resource/powershell_script" require "chef/resource/osx_profile" require "chef/resource/python" -- cgit v1.2.1 From fad49ed6abb5036a71e22ac96031431c994b2458 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Magnus=20Rakv=C3=A5g?= Date: Fri, 8 Jun 2018 11:55:43 +0200 Subject: fix refactor error MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Tor Magnus Rakvåg --- lib/chef/resource/powershell_package_source.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/chef/resource/powershell_package_source.rb b/lib/chef/resource/powershell_package_source.rb index 6b969d67fb..5703e9fe97 100644 --- a/lib/chef/resource/powershell_package_source.rb +++ b/lib/chef/resource/powershell_package_source.rb @@ -79,7 +79,7 @@ class Chef action_class do def package_source_exists? - cmd = powershell_out!("(Get-PackageSource -Name '#{new_resource.source}').Name") + cmd = powershell_out!("(Get-PackageSource -Name '#{new_resource.name}').Name") cmd.stdout.downcase.strip == new_resource.name.downcase end end -- cgit v1.2.1 From 679ca53aea02e706494874c759635137fe07ea65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Magnus=20Rakv=C3=A5g?= Date: Fri, 8 Jun 2018 11:59:18 +0200 Subject: wire up package_management_provider MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Tor Magnus Rakvåg --- lib/chef/resource/powershell_package_source.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'lib') diff --git a/lib/chef/resource/powershell_package_source.rb b/lib/chef/resource/powershell_package_source.rb index 5703e9fe97..2cde896144 100644 --- a/lib/chef/resource/powershell_package_source.rb +++ b/lib/chef/resource/powershell_package_source.rb @@ -57,6 +57,7 @@ class Chef action :register do register_cmd = "Register-PackageSource -Name '#{new_resource.name}' -Location '#{new_resource.url}'" register_cmd << " -Trusted" if new_resource.trusted + register_cmd << " -PackageManagementProvider '#{new_resource.package_management_provider}'" register_cmd << " -PublishLocation '#{new_resource.publish_location}'" if new_resource.publish_location register_cmd << " -ScriptSourceLocation '#{new_resource.script_source_location}'" if new_resource.script_source_location register_cmd << " -ScriptPublishLocation '#{new_resource.script_publish_location}'" if new_resource.script_publish_location -- cgit v1.2.1 From 9cbaaa5ee48c8a6a9b77d17751b91fab03163397 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Magnus=20Rakv=C3=A5g?= Date: Mon, 11 Jun 2018 10:47:47 +0200 Subject: use psrepository cmdlet under the hood for powershellget MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Tor Magnus Rakvåg --- lib/chef/resource/powershell_package_source.rb | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) (limited to 'lib') diff --git a/lib/chef/resource/powershell_package_source.rb b/lib/chef/resource/powershell_package_source.rb index 2cde896144..5d6e7bb07b 100644 --- a/lib/chef/resource/powershell_package_source.rb +++ b/lib/chef/resource/powershell_package_source.rb @@ -36,7 +36,7 @@ class Chef description: "", default: false - property :package_management_provider, String, + property :provider_name, String, equal_to: %w{ Programs msi NuGet msu PowerShellGet psl chocolatey }, validation_message: "The following providers are supported: 'Programs', 'msi', 'NuGet', 'msu', 'PowerShellGet', 'psl' or 'chocolatey'", description: "", @@ -55,13 +55,19 @@ class Chef required: false action :register do - register_cmd = "Register-PackageSource -Name '#{new_resource.name}' -Location '#{new_resource.url}'" - register_cmd << " -Trusted" if new_resource.trusted - register_cmd << " -PackageManagementProvider '#{new_resource.package_management_provider}'" - register_cmd << " -PublishLocation '#{new_resource.publish_location}'" if new_resource.publish_location - register_cmd << " -ScriptSourceLocation '#{new_resource.script_source_location}'" if new_resource.script_source_location - register_cmd << " -ScriptPublishLocation '#{new_resource.script_publish_location}'" if new_resource.script_publish_location - register_cmd << " -Force -ForceBootstrap" + if psrepository_cmdlet_appropriate? + register_cmd = "Install-PackageProvider -Name 'NuGet';" + register_cmd << " Register-PSRepository -Name '#{new_resource.name}' -SourceLocation '#{new_resource.url}'" + register_cmd << " -PublishLocation '#{new_resource.publish_location}'" if new_resource.publish_location + register_cmd << " -ScriptSourceLocation '#{new_resource.script_source_location}'" if new_resource.script_source_location + register_cmd << " -ScriptPublishLocation '#{new_resource.script_publish_location}'" if new_resource.script_publish_location + register_cmd << " -InstallationPolicy Trusted" if new_resource.trusted + else + register_cmd = "Register-PackageSource -Name '#{new_resource.name}' -Location '#{new_resource.url}'" + register_cmd << " -ProviderName '#{new_resource.provider_name}'" + register_cmd << " -Force -ForceBootstrap" + register_cmd << " -Trusted" if new_resource.trusted + end powershell_script "register package source: #{new_resource.name}" do code register_cmd @@ -83,6 +89,10 @@ class Chef cmd = powershell_out!("(Get-PackageSource -Name '#{new_resource.name}').Name") cmd.stdout.downcase.strip == new_resource.name.downcase end + + def psrepository_cmdlet_appropriate? + new_resource.provider_name == "PowerShellGet" + end end end end -- cgit v1.2.1 From 107ecc7bdff947dd55864952b28d6d495d501dfb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Magnus=20Rakv=C3=A5g?= Date: Mon, 11 Jun 2018 10:48:44 +0200 Subject: fix copyright MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Tor Magnus Rakvåg --- lib/chef/resource/powershell_package_source.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/chef/resource/powershell_package_source.rb b/lib/chef/resource/powershell_package_source.rb index 5d6e7bb07b..c20e735c0a 100644 --- a/lib/chef/resource/powershell_package_source.rb +++ b/lib/chef/resource/powershell_package_source.rb @@ -1,5 +1,5 @@ # Author:: Tor Magnus Rakvåg (tm@intility.no) -# Copyright:: 2015-2018 Chef Software, Inc. +# Copyright:: 2018, Intility AS # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); -- cgit v1.2.1 From 3dd4ed856f1e8044452d3e931e540c88e57a9ede Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Magnus=20Rakv=C3=A5g?= Date: Tue, 12 Jun 2018 10:12:35 +0200 Subject: make properties idempotent MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Tor Magnus Rakvåg --- lib/chef/resource/powershell_package_source.rb | 103 ++++++++++++++++++++----- 1 file changed, 83 insertions(+), 20 deletions(-) (limited to 'lib') diff --git a/lib/chef/resource/powershell_package_source.rb b/lib/chef/resource/powershell_package_source.rb index c20e735c0a..53de31084e 100644 --- a/lib/chef/resource/powershell_package_source.rb +++ b/lib/chef/resource/powershell_package_source.rb @@ -54,33 +54,62 @@ class Chef description: "", required: false + load_current_value do + cmd = load_resource_state_script(name) + repo = powershell_out!(cmd) + status = {} + repo.stdout.split(/\r\n/).each do |line| + kv = line.strip.split(/\s*:\s*/, 2) + status[kv[0]] = kv[1] if kv.length == 2 + end + url status["url"].nil? ? "not_set" : status["url"] + trusted (status["trusted"] == "True" ? true : false) + provider_name status["provider_name"] + publish_location status["publish_location"] + script_source_location status["script_source_location"] + script_publish_location status["script_publish_location"] + end + action :register do + # TODO: Ensure package provider is installed? if psrepository_cmdlet_appropriate? - register_cmd = "Install-PackageProvider -Name 'NuGet';" - register_cmd << " Register-PSRepository -Name '#{new_resource.name}' -SourceLocation '#{new_resource.url}'" - register_cmd << " -PublishLocation '#{new_resource.publish_location}'" if new_resource.publish_location - register_cmd << " -ScriptSourceLocation '#{new_resource.script_source_location}'" if new_resource.script_source_location - register_cmd << " -ScriptPublishLocation '#{new_resource.script_publish_location}'" if new_resource.script_publish_location - register_cmd << " -InstallationPolicy Trusted" if new_resource.trusted + if package_source_exists? + converge_if_changed :url, :trusted, :publish_location, :script_source_location, :script_publish_location do + update_cmd = build_ps_repository_command("Set", new_resource) + res = powershell_out(update_cmd) + raise "Failed to update #{new_resource.name}: #{res.stderr}" unless res.stderr.empty? + end + else + converge_by("register source: #{new_resource.name}") do + register_cmd = build_ps_repository_command("Register", new_resource) + res = powershell_out(register_cmd) + raise "Failed to register #{new_resource.name}: #{res.stderr}" unless res.stderr.empty? + end + end else - register_cmd = "Register-PackageSource -Name '#{new_resource.name}' -Location '#{new_resource.url}'" - register_cmd << " -ProviderName '#{new_resource.provider_name}'" - register_cmd << " -Force -ForceBootstrap" - register_cmd << " -Trusted" if new_resource.trusted - end - - powershell_script "register package source: #{new_resource.name}" do - code register_cmd - not_if { package_source_exists? } + if package_source_exists? + converge_if_changed :url, :trusted, :provider_name do + update_cmd = build_package_source_command("Set", new_resource) + res = powershell_out(update_cmd) + raise "Failed to update #{new_resource.name}: #{res.stderr}" unless res.stderr.empty? + end + else + converge_by("register source: #{new_resource.name}") do + register_cmd = build_package_source_command("Register", new_resource) + res = powershell_out(register_cmd) + raise "Failed to register #{new_resource.name}: #{res.stderr}" unless res.stderr.empty? + end + end end end action :unregister do - unregister_cmd = "Get-PackageSource -Name '#{new_resource.name}' | Unregister-PackageSource" - - powershell_script "unregister package source: #{new_resource.name}" do - code unregister_cmd - only_if { package_source_exists? } + if package_source_exists? + unregister_cmd = "Get-PackageSource -Name '#{new_resource.name}' | Unregister-PackageSource" + converge_by("unregister source: #{new_resource.name}") do + res = powershell_out(unregister_cmd) + raise "Failed to unregister #{new_resource.name}: #{res.stderr}" unless res.stderr.empty? + end end end @@ -93,7 +122,41 @@ class Chef def psrepository_cmdlet_appropriate? new_resource.provider_name == "PowerShellGet" end + + def build_ps_repository_command(cmdlet_type, new_resource) + cmd = "#{cmdlet_type}-PSRepository -Name '#{new_resource.name}'" + cmd << " -SourceLocation '#{new_resource.url}'" if new_resource.url + cmd << " -InstallationPolicy '#{new_resource.trusted ? "Trusted" : "Untrusted"}'" + cmd << " -PublishLocation '#{new_resource.publish_location}'" if new_resource.publish_location + cmd << " -ScriptSourceLocation '#{new_resource.script_source_location}'" if new_resource.script_source_location + cmd << " -ScriptPublishLocation '#{new_resource.script_publish_location}'" if new_resource.script_publish_location + cmd + end + + def build_package_source_command(cmdlet_type, new_resource) + cmd = "#{cmdlet_type}-PackageSource -Name '#{new_resource.name}'" + cmd << " -Location '#{new_resource.url}'" if new_resource.url + cmd << " -Trusted:#{new_resource.trusted ? "$true" : "$false"}" + cmd << " -ProviderName '#{new_resource.provider_name}'" if new_resource.provider_name + cmd + end end end + + private + + def load_resource_state_script(name) + <<-EOH + if ((Get-PackageSource -Name '#{name}' -ErrorAction SilentlyContinue).ProviderName -eq 'PowerShellGet') { + (Get-PSRepository -Name '#{name}') | Select @{n='name';e={$_.Name}}, @{n='url';e={$_.SourceLocation}}, + @{n='trusted';e={$_.Trusted}}, @{n='provider_name';e={$_.PackageManagementProvider}}, @{n='publish_location';e={$_.PublishLocation}}, + @{n='script_source_location';e={$_.ScriptSourceLocation}}, @{n='script_publish_location';e={$_.ScriptPublishLocation}} | fl + } + else { + (Get-PackageSource -Name '#{name}'-ErrorAction SilentlyContinue) | Select @{n='name';e={$_.Name}}, @{n='url';e={$_.Location}}, + @{n='provider_name';e={$_.ProviderName}}, @{n='trusted';e={$_.IsTrusted}} | fl + } + EOH + end end end -- cgit v1.2.1 From 8970dd3b7225e9abb96f7ebda2c2aff534ef2b36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Magnus=20Rakv=C3=A5g?= Date: Tue, 12 Jun 2018 13:37:50 +0200 Subject: add missing descriptions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Tor Magnus Rakvåg --- lib/chef/resource/powershell_package_source.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'lib') diff --git a/lib/chef/resource/powershell_package_source.rb b/lib/chef/resource/powershell_package_source.rb index 53de31084e..100ee8d19e 100644 --- a/lib/chef/resource/powershell_package_source.rb +++ b/lib/chef/resource/powershell_package_source.rb @@ -25,15 +25,15 @@ class Chef introduced "15.0" property :name, String, - description: "", + description: "The name of the package source", name_property: true property :url, String, - description: "", + description: "The url to the package source", required: true property :trusted, [true, false], - description: "", + description: "Whether or not to trust packages from this source", default: false property :provider_name, String, @@ -43,15 +43,15 @@ class Chef default: "NuGet" property :publish_location, String, - description: "", + description: "The url where modules will be published to for this source. Only valid if the provider is 'PowerShellGet'.", required: false property :script_source_location, String, - description: "", + description: "The url where scripts are located for this source. Only valid if the provider is 'PowerShellGet'.", required: false property :script_publish_location, String, - description: "", + description: "The location where scripts will be published to for this source. Only valid if the provider is 'PowerShellGet'.", required: false load_current_value do -- cgit v1.2.1 From fae8378ca0a7646a69a5683d086a841dce591ba6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Magnus=20Rakv=C3=A5g?= Date: Wed, 13 Jun 2018 09:42:39 +0200 Subject: fix some properties MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Tor Magnus Rakvåg --- lib/chef/resource/powershell_package_source.rb | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) (limited to 'lib') diff --git a/lib/chef/resource/powershell_package_source.rb b/lib/chef/resource/powershell_package_source.rb index 100ee8d19e..cf84d0a828 100644 --- a/lib/chef/resource/powershell_package_source.rb +++ b/lib/chef/resource/powershell_package_source.rb @@ -22,7 +22,7 @@ class Chef provides(:powershell_package_source) { true } description "Use the powershell_package_source resource to register a powershell package repository" - introduced "15.0" + introduced "14.3" property :name, String, description: "The name of the package source", @@ -32,27 +32,25 @@ class Chef description: "The url to the package source", required: true - property :trusted, [true, false], + property :trusted, [TrueClass, FalseClass], description: "Whether or not to trust packages from this source", default: false property :provider_name, String, equal_to: %w{ Programs msi NuGet msu PowerShellGet psl chocolatey }, validation_message: "The following providers are supported: 'Programs', 'msi', 'NuGet', 'msu', 'PowerShellGet', 'psl' or 'chocolatey'", - description: "", + description: "The package management provider for the source. It supports the following providers: "\ + "'Programs', 'msi', 'NuGet', 'msu', 'PowerShellGet', 'psl' and 'chocolatey'.", default: "NuGet" property :publish_location, String, - description: "The url where modules will be published to for this source. Only valid if the provider is 'PowerShellGet'.", - required: false + description: "The url where modules will be published to for this source. Only valid if the provider is 'PowerShellGet'." property :script_source_location, String, - description: "The url where scripts are located for this source. Only valid if the provider is 'PowerShellGet'.", - required: false + description: "The url where scripts are located for this source. Only valid if the provider is 'PowerShellGet'." property :script_publish_location, String, - description: "The location where scripts will be published to for this source. Only valid if the provider is 'PowerShellGet'.", - required: false + description: "The location where scripts will be published to for this source. Only valid if the provider is 'PowerShellGet'." load_current_value do cmd = load_resource_state_script(name) -- cgit v1.2.1 From b49ca16636b77426e615adb30cc106d961575572 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Magnus=20Rakv=C3=A5g?= Date: Wed, 13 Jun 2018 09:51:51 +0200 Subject: rename name property to source_name MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Tor Magnus Rakvåg --- lib/chef/resource/powershell_package_source.rb | 28 +++++++++++++------------- 1 file changed, 14 insertions(+), 14 deletions(-) (limited to 'lib') diff --git a/lib/chef/resource/powershell_package_source.rb b/lib/chef/resource/powershell_package_source.rb index cf84d0a828..58a831a698 100644 --- a/lib/chef/resource/powershell_package_source.rb +++ b/lib/chef/resource/powershell_package_source.rb @@ -24,7 +24,7 @@ class Chef description "Use the powershell_package_source resource to register a powershell package repository" introduced "14.3" - property :name, String, + property :source_name, String, description: "The name of the package source", name_property: true @@ -75,13 +75,13 @@ class Chef converge_if_changed :url, :trusted, :publish_location, :script_source_location, :script_publish_location do update_cmd = build_ps_repository_command("Set", new_resource) res = powershell_out(update_cmd) - raise "Failed to update #{new_resource.name}: #{res.stderr}" unless res.stderr.empty? + raise "Failed to update #{new_resource.source_name}: #{res.stderr}" unless res.stderr.empty? end else - converge_by("register source: #{new_resource.name}") do + converge_by("register source: #{new_resource.source_name}") do register_cmd = build_ps_repository_command("Register", new_resource) res = powershell_out(register_cmd) - raise "Failed to register #{new_resource.name}: #{res.stderr}" unless res.stderr.empty? + raise "Failed to register #{new_resource.source_name}: #{res.stderr}" unless res.stderr.empty? end end else @@ -89,13 +89,13 @@ class Chef converge_if_changed :url, :trusted, :provider_name do update_cmd = build_package_source_command("Set", new_resource) res = powershell_out(update_cmd) - raise "Failed to update #{new_resource.name}: #{res.stderr}" unless res.stderr.empty? + raise "Failed to update #{new_resource.source_name}: #{res.stderr}" unless res.stderr.empty? end else - converge_by("register source: #{new_resource.name}") do + converge_by("register source: #{new_resource.source_name}") do register_cmd = build_package_source_command("Register", new_resource) res = powershell_out(register_cmd) - raise "Failed to register #{new_resource.name}: #{res.stderr}" unless res.stderr.empty? + raise "Failed to register #{new_resource.source_name}: #{res.stderr}" unless res.stderr.empty? end end end @@ -103,18 +103,18 @@ class Chef action :unregister do if package_source_exists? - unregister_cmd = "Get-PackageSource -Name '#{new_resource.name}' | Unregister-PackageSource" - converge_by("unregister source: #{new_resource.name}") do + unregister_cmd = "Get-PackageSource -Name '#{new_resource.source_name}' | Unregister-PackageSource" + converge_by("unregister source: #{new_resource.source_name}") do res = powershell_out(unregister_cmd) - raise "Failed to unregister #{new_resource.name}: #{res.stderr}" unless res.stderr.empty? + raise "Failed to unregister #{new_resource.source_name}: #{res.stderr}" unless res.stderr.empty? end end end action_class do def package_source_exists? - cmd = powershell_out!("(Get-PackageSource -Name '#{new_resource.name}').Name") - cmd.stdout.downcase.strip == new_resource.name.downcase + cmd = powershell_out!("(Get-PackageSource -Name '#{new_resource.source_name}').Name") + cmd.stdout.downcase.strip == new_resource.source_name.downcase end def psrepository_cmdlet_appropriate? @@ -122,7 +122,7 @@ class Chef end def build_ps_repository_command(cmdlet_type, new_resource) - cmd = "#{cmdlet_type}-PSRepository -Name '#{new_resource.name}'" + cmd = "#{cmdlet_type}-PSRepository -Name '#{new_resource.source_name}'" cmd << " -SourceLocation '#{new_resource.url}'" if new_resource.url cmd << " -InstallationPolicy '#{new_resource.trusted ? "Trusted" : "Untrusted"}'" cmd << " -PublishLocation '#{new_resource.publish_location}'" if new_resource.publish_location @@ -132,7 +132,7 @@ class Chef end def build_package_source_command(cmdlet_type, new_resource) - cmd = "#{cmdlet_type}-PackageSource -Name '#{new_resource.name}'" + cmd = "#{cmdlet_type}-PackageSource -Name '#{new_resource.source_name}'" cmd << " -Location '#{new_resource.url}'" if new_resource.url cmd << " -Trusted:#{new_resource.trusted ? "$true" : "$false"}" cmd << " -ProviderName '#{new_resource.provider_name}'" if new_resource.provider_name -- cgit v1.2.1 From 4b4cd8ae20cc56e2881ef38b9d5679a0552796ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Magnus=20Rakv=C3=A5g?= Date: Wed, 13 Jun 2018 10:38:07 +0200 Subject: refactor state fetching to use json MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Tor Magnus Rakvåg --- lib/chef/resource/powershell_package_source.rb | 27 ++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) (limited to 'lib') diff --git a/lib/chef/resource/powershell_package_source.rb b/lib/chef/resource/powershell_package_source.rb index 58a831a698..095adcb292 100644 --- a/lib/chef/resource/powershell_package_source.rb +++ b/lib/chef/resource/powershell_package_source.rb @@ -15,6 +15,8 @@ # limitations under the License. # +require "chef/json_compat" + class Chef class Resource class PowershellPackageSource < Chef::Resource @@ -55,13 +57,9 @@ class Chef load_current_value do cmd = load_resource_state_script(name) repo = powershell_out!(cmd) - status = {} - repo.stdout.split(/\r\n/).each do |line| - kv = line.strip.split(/\s*:\s*/, 2) - status[kv[0]] = kv[1] if kv.length == 2 - end + status = Chef::JSONCompat.from_json(repo.stdout) url status["url"].nil? ? "not_set" : status["url"] - trusted (status["trusted"] == "True" ? true : false) + trusted status["trusted"] provider_name status["provider_name"] publish_location status["publish_location"] script_source_location status["script_source_location"] @@ -145,14 +143,19 @@ class Chef def load_resource_state_script(name) <<-EOH - if ((Get-PackageSource -Name '#{name}' -ErrorAction SilentlyContinue).ProviderName -eq 'PowerShellGet') { - (Get-PSRepository -Name '#{name}') | Select @{n='name';e={$_.Name}}, @{n='url';e={$_.SourceLocation}}, - @{n='trusted';e={$_.Trusted}}, @{n='provider_name';e={$_.PackageManagementProvider}}, @{n='publish_location';e={$_.PublishLocation}}, - @{n='script_source_location';e={$_.ScriptSourceLocation}}, @{n='script_publish_location';e={$_.ScriptPublishLocation}} | fl + if(Get-PackageSource -Name '#{name}' -ErrorAction SilentlyContinue) { + if ((Get-PackageSource -Name '#{name}').ProviderName -eq 'PowerShellGet') { + (Get-PSRepository -Name '#{name}') | Select @{n='source_name';e={$_.Name}}, @{n='url';e={$_.SourceLocation}}, + @{n='trusted';e={$_.Trusted}}, @{n='provider_name';e={$_.PackageManagementProvider}}, @{n='publish_location';e={$_.PublishLocation}}, + @{n='script_source_location';e={$_.ScriptSourceLocation}}, @{n='script_publish_location';e={$_.ScriptPublishLocation}} | ConvertTo-Json + } + else { + (Get-PackageSource -Name '#{name}') | Select @{n='source_name';e={$_.Name}}, @{n='url';e={$_.Location}}, + @{n='provider_name';e={$_.ProviderName}}, @{n='trusted';e={$_.IsTrusted}} | ConvertTo-Json + } } else { - (Get-PackageSource -Name '#{name}'-ErrorAction SilentlyContinue) | Select @{n='name';e={$_.Name}}, @{n='url';e={$_.Location}}, - @{n='provider_name';e={$_.ProviderName}}, @{n='trusted';e={$_.IsTrusted}} | fl + "" | Select source_name, url, provider_name, trusted | ConvertTo-Json } EOH end -- cgit v1.2.1 From 355eb2261997423b0137ff77695f12b0086892e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Magnus=20Rakv=C3=A5g?= Date: Wed, 13 Jun 2018 10:42:27 +0200 Subject: set the resource to preview MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Tor Magnus Rakvåg --- lib/chef/resource/powershell_package_source.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/chef/resource/powershell_package_source.rb b/lib/chef/resource/powershell_package_source.rb index 095adcb292..85ce8dd415 100644 --- a/lib/chef/resource/powershell_package_source.rb +++ b/lib/chef/resource/powershell_package_source.rb @@ -20,8 +20,8 @@ require "chef/json_compat" class Chef class Resource class PowershellPackageSource < Chef::Resource + preview_resource true resource_name "powershell_package_source" - provides(:powershell_package_source) { true } description "Use the powershell_package_source resource to register a powershell package repository" introduced "14.3" -- cgit v1.2.1 From 3d9f9731fcb6c5d02c57e0dffd9df21f168ef8fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Magnus=20Rakv=C3=A5g?= Date: Thu, 14 Jun 2018 11:19:51 +0200 Subject: add descriptions to actions, require chef/resource MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Tor Magnus Rakvåg --- lib/chef/resource/powershell_package_source.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/chef/resource/powershell_package_source.rb b/lib/chef/resource/powershell_package_source.rb index 85ce8dd415..9fa4bc8497 100644 --- a/lib/chef/resource/powershell_package_source.rb +++ b/lib/chef/resource/powershell_package_source.rb @@ -15,6 +15,7 @@ # limitations under the License. # +require "chef/resource" require "chef/json_compat" class Chef @@ -41,8 +42,7 @@ class Chef property :provider_name, String, equal_to: %w{ Programs msi NuGet msu PowerShellGet psl chocolatey }, validation_message: "The following providers are supported: 'Programs', 'msi', 'NuGet', 'msu', 'PowerShellGet', 'psl' or 'chocolatey'", - description: "The package management provider for the source. It supports the following providers: "\ - "'Programs', 'msi', 'NuGet', 'msu', 'PowerShellGet', 'psl' and 'chocolatey'.", + description: "The package management provider for the source. It supports the following providers: 'Programs', 'msi', 'NuGet', 'msu', 'PowerShellGet', 'psl' and 'chocolatey'.", default: "NuGet" property :publish_location, String, @@ -67,6 +67,7 @@ class Chef end action :register do + description "Registers and updates the powershell package source." # TODO: Ensure package provider is installed? if psrepository_cmdlet_appropriate? if package_source_exists? @@ -100,6 +101,7 @@ class Chef end action :unregister do + description "Unregisters the powershell package source." if package_source_exists? unregister_cmd = "Get-PackageSource -Name '#{new_resource.source_name}' | Unregister-PackageSource" converge_by("unregister source: #{new_resource.source_name}") do -- cgit v1.2.1