summaryrefslogtreecommitdiff
path: root/lib/chef
diff options
context:
space:
mode:
authorTor Magnus Rakvåg <tm@intility.no>2018-06-12 10:12:35 +0200
committerTor Magnus Rakvåg <tm@intility.no>2018-06-12 10:12:35 +0200
commit3dd4ed856f1e8044452d3e931e540c88e57a9ede (patch)
tree71801bd21684c49fba7525405fccb3083730b03a /lib/chef
parent107ecc7bdff947dd55864952b28d6d495d501dfb (diff)
downloadchef-3dd4ed856f1e8044452d3e931e540c88e57a9ede.tar.gz
make properties idempotent
Signed-off-by: Tor Magnus Rakvåg <tm@intility.no>
Diffstat (limited to 'lib/chef')
-rw-r--r--lib/chef/resource/powershell_package_source.rb103
1 files changed, 83 insertions, 20 deletions
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