Skip to content

Commit

Permalink
Merge pull request #63 from chef-cookbooks/smurawski/merging_prs_57_6…
Browse files Browse the repository at this point in the history
…0_61

Merging PRs - #57, #60, #61
  • Loading branch information
smurawski committed Dec 11, 2015
2 parents fbf4ad0 + e73841e commit 94bc591
Show file tree
Hide file tree
Showing 25 changed files with 634 additions and 593 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@ powershell Cookbook CHANGELOG
=============================
This file is used to list changes made in each version of the powershell cookbook.

v3.2.0 (unreleased)
-------------------
- [**Annih**](https://github.com/Annih)
[PR #61](http://github.com/chef-cookbooks/powershell/pull/61) - use the all in one ms_dotnet cookbook
- [**RyanJarv**](https://github.com/RyanJarv)
[PR #60](http://github.com/chef-cookbooks/powershell/pull/60) - Use Ruby to unzip to support 2008 R2 Server Core
- [**Aliasgar16**](https://Aliasgar16)
[PR #57](http://github.com/chef-cookbooks/powershell/pull/57) - Add LCM configuration recipes

v3.1.0 (2015-04-27)
-------------------
- [**webframp**](https://github.com/webframp)
Expand Down
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,7 @@ Not every version of Windows supports every version of Powershell. The following

PowerShell also requires the appropriate version of the Microsoft .NET Framework to be installed, if the operating system does not ship with that version. The following community cookbooks are used to install the correct version of the .NET Framework:

* ms_dotnet2
* ms_dotnet4
* ms_dotnet45
* ms_dotnet

Resource/Provider
-----------------
Expand Down Expand Up @@ -171,7 +169,8 @@ end

### `powershell_module`

Installs or uninstalls a Powershell module
Installs or uninstalls a Powershell module. You either need to install rubyzip with chef_gem or
include the default recipe before using this resource.

#### Actions

Expand All @@ -188,6 +187,8 @@ Installs or uninstalls a Powershell module
#### Examples

```ruby
include_recipe 'powershell::default'

# Install module from local directory path
# change the package_name and source
powershell_module "PsUrl" do
Expand Down Expand Up @@ -231,7 +232,7 @@ Usage

### default

The default recipe does nothing.
The default recipe is needs to be included before using the powershell_module resource.

### powershell2

Expand Down
9 changes: 9 additions & 0 deletions attributes/config_lcm.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

## temporary directory related attributes ##
default['lcm']['mof']['temp_dir'] = "#{Chef::Config[:file_cache_path]}\\lcm_mof"

## lcm configuration related attributes ##
default['lcm']['config']['enable']['config_mode'] = 'ApplyOnly'
default['lcm']['config']['enable']['reboot_node'] = false
default['lcm']['config']['enable']['refresh_mode'] = 'Push'
default['lcm']['config']['disable']['refresh_mode'] = 'Disabled'
54 changes: 41 additions & 13 deletions libraries/powershell_module_provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,7 @@ def install_module
FileUtils.cp(filename, ps_module_path)
end
elsif @new_resource.source =~ URI.regexp # Check for valid URL
downloaded_file = download_extract_module

if ::File.exist?(downloaded_file)
Chef::Log.debug("Powershell Module '#{@powershell_module.package_name}' removing download #{downloaded_file}")
FileUtils.rm_f(downloaded_file)
end

download_extract_module
end
end

Expand All @@ -85,6 +79,39 @@ def uninstall_module
end
end

def download(download_url, target)
uri = URI(download_url)

Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
f = open(target, 'wb')
http.request_get(uri.path) do |resp|
resp.read_body do |segment|
f.write(segment)
end
end

f.close
end
end

def unzip(zip_file, target_directory)
begin
require 'zip'
rescue LoadError
raise(
'Could not load the rubyzip gem, please make sure this gem is installed with the "chef_gem" resource ' \
'or include the powershell::default recipe before using powershell_module.'
)
end

Zip::File.open(zip_file) do |zip|
zip.each do |entry|
FileUtils.mkdir_p(::File.join(target_directory, ::File.dirname(entry.name)))
entry.extract(::File.join(target_directory, entry.name))
end
end
end

def download_extract_module(download_url = nil, target = nil)
filename = @new_resource.package_name + '.zip'

Expand All @@ -97,19 +124,20 @@ def download_extract_module(download_url = nil, target = nil)
ps_module_path = sanitize! @new_resource.destination
Chef::Log.debug("Powershell Module ps_module_path is #{ps_module_path}")

cmd_str = "powershell.exe Invoke-WebRequest #{download_url} -OutFile #{target}; $shell = new-object -com shell.application;$zip = $shell.NameSpace('#{target.gsub('/', '\\\\')}'); $shell.Namespace('#{ps_module_path}').copyhere($zip.items(), 0x14);write-host $shell"

installed_module = module_exists?(ps_module_path, "*#{@new_resource.package_name}*")

if installed_module
Chef::Log.info("Powershell Module #{@new_resource.package_name} already installed.")
Chef::Log.info("Remove path at #{ps_module_path}\\#{installed_module} to reinstall.")
else
ps_cmd = Mixlib::ShellOut.new(cmd_str)
ps_cmd.run_command
download(download_url, target)
unzip(target, ps_module_path)
remove_download(target)
end
end

target
def remove_download(target)
Chef::Log.debug("Powershell Module '#{@powershell_module.package_name}' removing download #{downloaded_file}")
FileUtils.rm_f(downloaded_file) if ::File.exist?(target)
end

def module_path_name
Expand Down
10 changes: 5 additions & 5 deletions metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@
license 'Apache 2.0'
description 'Installs/Configures PowerShell on the Windows platform'
long_description IO.read(File.join(File.dirname(__FILE__), 'README.md'))
version '3.1.0'
version '3.2.0'

recipe 'powershell::default', 'Does nothing; choose the right version of Powershell by selecting the correct recipe'
recipe 'powershell::default', 'Makes sure RubyZip is installed (for powershell_module)'
recipe 'powershell::powershell2', 'Installs PowerShell 2.0'
recipe 'powershell::powershell3', 'Installs PowerShell 3.0'
recipe 'powershell::powershell4', 'Installs PowerShell 4.0'
recipe 'powershell::powershell5', 'Installs PowerShell 5.0'
recipe 'powershell::winrm', 'Configures WinRM'
recipe 'powershell::dsc', 'Desired State Configuration'
recipe 'powershell::enable_lcm', 'Enable the DSC Local Configuration Manager'
recipe 'powershell::disable_lcm', 'Disable the DSC Local Configuration Manager'

supports 'windows'
depends 'windows', '>= 1.2.8'
depends 'ms_dotnet45'
depends 'ms_dotnet4'
depends 'ms_dotnet2'
depends 'ms_dotnet', '>= 2.6'
depends 'chef_handler'

source_url 'https://github.com/chef-cookbooks/powershell' if respond_to?(:source_url)
Expand Down
2 changes: 2 additions & 0 deletions recipes/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#

chef_gem 'rubyzip'
63 changes: 63 additions & 0 deletions recipes/disable_lcm.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#
# Author:: Aliasgar Batterywala (<[email protected]>)
# Cookbook Name:: powershell
# Recipe:: disable_lcm
#
# Copyright:: Copyright (c) 2015 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.

include_recipe 'powershell::powershell5'
include_recipe 'powershell::dsc'

case node['platform']
when 'windows'

directory 'Creating temporary directory to store LCM MOF files' do
path node['lcm']['mof']['temp_dir']
rights :read, 'Everyone'
action :create
end

powershell_script 'Disable LCM' do
code <<-EOH
Configuration DisableLCM
{
Node "localhost"
{
LocalConfigurationManager
{
RefreshMode = "#{node['lcm']['config']['disable']['refresh_mode']}"
}
}
}
DisableLCM -OutputPath "#{node['lcm']['mof']['temp_dir']}"
Set-DscLocalConfigurationManager -Path "#{node['lcm']['mof']['temp_dir']}"
EOH
not_if <<-EOH
$LCM = (Get-DscLocalConfigurationManager)
$LCM.RefreshMode -eq "#{node['lcm']['config']['disable']['refresh_mode']}"
EOH
end

directory 'Deleting temporary directory which stored LCM MOF files' do
path node['lcm']['mof']['temp_dir']
recursive true
action :delete
end
else
Chef::Log.warn('LCM configuration can only be executed on the Windows platform.')
end
20 changes: 20 additions & 0 deletions recipes/enable_dsc_script.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#
# Author:: Aliasgar Batterywala (<[email protected]>)
# Cookbook Name:: powershell
# Recipe:: enable_lcm
#
# Copyright:: Copyright (c) 2015 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.

include_recipe 'powershell::enable_lcm'
73 changes: 73 additions & 0 deletions recipes/enable_lcm.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#
# Author:: Aliasgar Batterywala (<[email protected]>)
# Cookbook Name:: powershell
# Recipe:: enable_lcm
#
# Copyright:: Copyright (c) 2015 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.

include_recipe 'powershell::dsc'

case node['platform']
when 'windows'

directory 'Creating temporary directory to store LCM MOF files' do
path node['lcm']['mof']['temp_dir']
rights :read, 'Everyone'
action :create
guard_interpreter :powershell_script
not_if <<-EOH
$LCM = (Get-DscLocalConfigurationManager)
$LCM.ConfigurationMode -eq "#{node['lcm']['config']['enable']['config_mode']}" -and
$LCM.RefreshMode -eq "#{node['lcm']['config']['enable']['refresh_mode']}"
EOH
end

directory 'Deleting temporary directory which stored LCM MOF files' do
path node['lcm']['mof']['temp_dir']
recursive true
action :nothing
end

powershell_script 'Configure and Enable LCM' do
code <<-EOH
Configuration EnableLCM
{
Node "localhost"
{
LocalConfigurationManager
{
ConfigurationMode = "#{node['lcm']['config']['enable']['config_mode']}"
RebootNodeIfNeeded = $#{node['lcm']['config']['enable']['reboot_node']}
RefreshMode = "#{node['lcm']['config']['enable']['refresh_mode']}"
}
}
}
EnableLCM -OutputPath "#{node['lcm']['mof']['temp_dir']}"
Set-DscLocalConfigurationManager -Path "#{node['lcm']['mof']['temp_dir']}"
EOH
not_if <<-EOH
$LCM = (Get-DscLocalConfigurationManager)
$LCM.ConfigurationMode -eq "#{node['lcm']['config']['enable']['config_mode']}" -and
$LCM.RefreshMode -eq "#{node['lcm']['config']['enable']['refresh_mode']}"
EOH
notifies :delete, 'directory[Deleting temporary directory which stored LCM MOF files]', :immediately
end

else
Chef::Log.warn('LCM configuration can only be executed on the Windows platform.')
end
42 changes: 12 additions & 30 deletions recipes/powershell2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,48 +23,30 @@

case node['platform']
when 'windows'
nt_version = ::Windows::VersionHelper.nt_version(node)

require 'chef/win32/version'
windows_version = Chef::ReservedNames::Win32::Version.new
include_recipe 'ms_dotnet::ms_dotnet2'

if (windows_version.windows_server_2012? || windows_version.windows_8?) && windows_version.core?
# Windows Server 2012 Core does not come with Powershell 2.0 enabled
if nt_version.between?(6.1, 6.2) && ::Windows::VersionHelper.core_version?(node)
feature_suffix = 'V2' if nt_version == 6.2

windows_feature 'MicrosoftWindowsPowerShellV2' do
windows_feature "MicrosoftWindowsPowerShell#{feature_suffix}" do
action :install
end
windows_feature 'MicrosoftWindowsPowerShellV2-WOW64' do
action :install
only_if { node['kernel']['machine'] == 'x86_64' }
end

elsif (windows_version.windows_server_2008_r2? || windows_version.windows_7?) && windows_version.core?
# Windows Server 2008 R2 Core does not come with .NET or Powershell 2.0 enabled

windows_feature 'NetFx2-ServerCore' do
action :install
end
windows_feature 'NetFx2-ServerCore-WOW64' do
windows_feature "MicrosoftWindowsPowerShell#{feature_suffix}-WOW64" do
action :install
only_if { node['kernel']['machine'] == 'x86_64' }
end
windows_feature 'MicrosoftWindowsPowerShell' do
action :install
end
windows_feature 'MicrosoftWindowsPowerShell-WOW64' do
action :install
only_if { node['kernel']['machine'] == 'x86_64' }
end

elsif windows_version.windows_server_2008? || windows_version.windows_server_2003_r2? ||
windows_version.windows_server_2003? || windows_version.windows_xp?

include_recipe 'ms_dotnet2'

# Reboot if user specifies doesn't specify no_reboot
# WMF 2.0 is required and only compatible with:
# * Windows NT 5.1 & 5.2 (Windows Server 2003 & Windows XP)
# * Windows NT 6.0 server (Windows Server 2008 SP2 not vista)
elsif nt_version.between?(5.1, 5.2) || (nt_version == 6.0 && ::Windows::VersionHelper.server_version?(node))
# Reboot if user doesn't specify no_reboot
include_recipe 'powershell::windows_reboot' unless node['powershell']['installation_reboot_mode'] == 'no_reboot'

windows_package 'Windows Management Framework Core' do
windows_package 'Windows Management Framework Core' do # ~FC009
source node['powershell']['powershell2']['url']
checksum node['powershell']['powershell2']['checksum']
installer_type :custom
Expand Down
Loading

0 comments on commit 94bc591

Please sign in to comment.