summaryrefslogtreecommitdiff
path: root/lib/chef
diff options
context:
space:
mode:
Diffstat (limited to 'lib/chef')
-rw-r--r--lib/chef/provider/package.rb3
-rw-r--r--lib/chef/provider/package/powershell.rb125
-rw-r--r--lib/chef/providers.rb1
-rw-r--r--lib/chef/resource/powershell_package.rb41
-rw-r--r--lib/chef/resources.rb1
5 files changed, 171 insertions, 0 deletions
diff --git a/lib/chef/provider/package.rb b/lib/chef/provider/package.rb
index 048807dd05..f22d20dbbc 100644
--- a/lib/chef/provider/package.rb
+++ b/lib/chef/provider/package.rb
@@ -453,6 +453,9 @@ class Chef
elsif current_version.nil?
Chef::Log.debug("#{new_resource} #{package_name} not installed, installing #{candidate_version}")
target_version_array.push(candidate_version)
+ elsif !version_requirement_satisfied?(current_version, candidate_version)
+ Chef::Log.debug("#{new_resource} #{package_name} #{candidate_version} not installed, installing #{candidate_version}")
+ target_version_array.push(candidate_version)
else
Chef::Log.debug("#{new_resource} #{package_name} #{current_version} already installed")
target_version_array.push(nil)
diff --git a/lib/chef/provider/package/powershell.rb b/lib/chef/provider/package/powershell.rb
new file mode 100644
index 0000000000..84e8c53fc1
--- /dev/null
+++ b/lib/chef/provider/package/powershell.rb
@@ -0,0 +1,125 @@
+# Author:: Dheeraj Dubey(dheeraj.dubey@msystechnologies.com)
+# Copyright:: Copyright 2015-2016, 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 "chef/provider/package"
+require "chef/resource/powershell_package"
+require "chef/mixin/powershell_out"
+
+class Chef
+ class Provider
+ class Package
+ class Powershell < Chef::Provider::Package
+ include Chef::Mixin::PowershellOut
+
+ provides :powershell_package, os: "windows"
+
+ def load_current_resource
+ @current_resource = Chef::Resource::PowershellPackage.new(new_resource.name)
+ current_resource.package_name(new_resource.package_name)
+ current_resource.version(build_current_versions)
+ current_resource
+ end
+
+ def define_resource_requirements
+ super
+ if powershell_out("$PSVersionTable.PSVersion.Major").stdout.strip().to_i < 5
+ raise "Minimum installed Powershell Version required is 5"
+ end
+ requirements.assert(:install) do |a|
+ a.assertion { candidates_exist_for_all_uninstalled? }
+ a.failure_message(Chef::Exceptions::Package, "No candidate version available for #{packages_missing_candidates.join(", ")}")
+ a.whyrun("Assuming a repository that offers #{packages_missing_candidates.join(", ")} would have been configured")
+ end
+ end
+
+ def candidate_version
+ @candidate_version ||= build_candidate_versions
+ end
+
+ def install_package(names, versions)
+ # Installs the package specified with the version passed else latest version will be installed
+ names.each_with_index do |name,index|
+ powershell_out("Install-Package #{name} -Force -ForceBootstrap -RequiredVersion #{versions[index]}", { :timeout => @new_resource.timeout }).stdout
+ end
+ end
+
+ def remove_package(names, versions)
+ # Removes the package, if no version is passed, all installed version will be removed
+ names.each_with_index do |name,index|
+ if !versions.nil?
+ if versions.length > index
+ powershell_out( "Uninstall-Package #{name} -Force -RequiredVersion #{versions[index]}", { :timeout => @new_resource.timeout }).stdout
+ end
+ end
+ if new_resource.version.nil?
+ stdout = 0
+ while stdout!="" do
+ stdout = powershell_out( "Uninstall-Package #{name} -Force", { :timeout => @new_resource.timeout }).stdout
+ if !stdout.empty?
+ stdout = stdout.split(" ")
+ Chef::Log.debug("Removed package #{name} with version #{stdout[stdout.index(name)+1]}")
+ end
+ end
+ end
+ end
+ end
+
+ # return array of latest available packages online
+ def build_candidate_versions
+ versions = []
+ new_resource.package_name.map do |name|
+ stdout = powershell_out("Find-Package #{name}", { :timeout => @new_resource.timeout }).stdout
+ versions.push(find_version(stdout))
+ end
+ versions
+ end
+
+ #return array of currently installed version
+ def build_current_versions
+ version_list = []
+ new_resource.package_name.each_with_index do |name,index|
+ if !new_resource.version.nil?
+ if new_resource.version.length >= index+1
+ stdout = powershell_out("Get-Package -Name #{name} -RequiredVersion #{new_resource.version[index]}", { :timeout => @new_resource.timeout }).stdout
+ else
+ stdout = powershell_out("Get-Package -Name #{name}", { :timeout => @new_resource.timeout }).stdout
+ end
+ else
+ stdout = powershell_out("Get-Package -Name #{name}", { :timeout => @new_resource.timeout }).stdout
+ end
+ if stdout.empty?
+ version_list.push(nil)
+ else
+ stdout = stdout.split(" ")
+ version_list.push(stdout[stdout.index(name)+1])
+ end
+ end
+ version_list
+ end
+
+ def find_version(stdout)
+ if stdout.empty?
+ raise "Version speicified by user not found for the package"
+ else
+ stdout.split(" ")[-2]
+ end
+ end
+
+ end
+ end
+ end
+end
diff --git a/lib/chef/providers.rb b/lib/chef/providers.rb
index 596629290b..8067643bab 100644
--- a/lib/chef/providers.rb
+++ b/lib/chef/providers.rb
@@ -84,6 +84,7 @@ require "chef/provider/package/solaris"
require "chef/provider/package/smartos"
require "chef/provider/package/aix"
require "chef/provider/package/cab"
+require "chef/provider/package/powershell"
require "chef/provider/service/arch"
require "chef/provider/service/freebsd"
diff --git a/lib/chef/resource/powershell_package.rb b/lib/chef/resource/powershell_package.rb
new file mode 100644
index 0000000000..17d0fed555
--- /dev/null
+++ b/lib/chef/resource/powershell_package.rb
@@ -0,0 +1,41 @@
+# Author:: Dheeraj Dubey(dheeraj.dubey@msystechnologies.com)
+# Copyright:: Copyright 2008-2016, 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 "chef/resource/package"
+require "chef/mixin/uris"
+
+class Chef
+ class Resource
+ class PowershellPackage < Chef::Resource::Package
+ include Chef::Mixin::Uris
+
+ provides :powershell_package, os: "windows"
+
+ allowed_actions :install, :remove
+
+ def initialize(name, run_context = nil)
+ super
+ @resource_name = :powershell_package
+ end
+
+ property :package_name, [String, Array], coerce: proc { |x| [x].flatten }
+
+ property :version, [String, Array], coerce: proc { |x| [x].flatten }
+
+ end
+ end
+end
diff --git a/lib/chef/resources.rb b/lib/chef/resources.rb
index 9ff772353b..6634a298d1 100644
--- a/lib/chef/resources.rb
+++ b/lib/chef/resources.rb
@@ -96,3 +96,4 @@ require "chef/resource/lwrp_base"
require "chef/resource/bff_package"
require "chef/resource/zypper_package"
require "chef/resource/cab_package"
+require "chef/resource/powershell_package"