cooking with chef on windows

37
Cooking with Chef on Microsoft Windows Julian C. Dunn Senior Consultant, Chef Software, Inc. [email protected]

Upload: julian-dunn

Post on 27-Jan-2015

119 views

Category:

Design


2 download

DESCRIPTION

The January 2014 Edition.

TRANSCRIPT

Page 1: Cooking with Chef on Windows

Cooking with Chef on Microsoft WindowsJulian C. DunnSenior Consultant, Chef Software, [email protected]

Page 2: Cooking with Chef on Windows

Chef and Windows Timeline• May 2011 – Knife plugin for Windows announced

• Oct 2011 – PowerShell, IIS, SQL Server, and Windows cookbooks

• Dec 2011 – Chef Client Installer MSI for Microsoft Windows

• Feb 2012 – Integration of the registry_key resource into core Chef from the Windows cookbook

• Aug 2013 – Chef 11.6.0 release. PowerShell and Batch scripting integrated into core Chef. Chef Client released as Windows service

• Aug 2013 - PowerShell Desired State Configuration support announced (for delivery in 2014)

Page 3: Cooking with Chef on Windows

Challenges to Chef on Windows• No real package manager

• COTS vendors don’t understand automation

• UAC

• WinRM Quotas

• Win32 Redirector

• Not all preferences/state stored in registry

Page 4: Cooking with Chef on Windows

Windows < 2012?• WinRM Memory Quota Hotfix required:

• http://support.microsoft.com/kb/2842230

Page 5: Cooking with Chef on Windows

Automating a .NET App on Windows

Page 6: Cooking with Chef on Windows

Automating a .NET App on Windows• The app: nopCommerce Shopping

Cart solution (www.nopcommerce.com)

• ASP.NET with SQL Server backend

• Available through WebPI

• WebPI install assumes a lot, however

• Full-featured app suitable to show off Chef resources on Windows

Page 7: Cooking with Chef on Windows

Resources Automated in Demo• Installing Windows Features and Roles

• IIS app pool

• IIS site

• IIS app

• Registry settings

• Deploying files onto the system

• Unzipping files 

• Windows filesystem rights management

Page 8: Cooking with Chef on Windows

Provisioning with Chef• Azure plugin for Knife

• Request new VM from Azure API

• Bootstrap it over WinRM

• Install and start Chef

• Register with Chef server

• Run through the “run list”

• Instant infrastructure with one command

Page 9: Cooking with Chef on Windows

Video

Page 10: Cooking with Chef on Windows

The Recipe Code

Page 11: Cooking with Chef on Windows

::Chef::Recipe.send(:include, Windows::Helper)

windows_feature 'IIS-WebServerRole' do action :installend

# Pre-requisite features for IIS-ASPNET45 that need to be installed first, in this order.%w{IIS-ISAPIFilter IIS-ISAPIExtensions NetFx3ServerFeatures NetFx4Extended-ASPNET45 IIS-NetFxExtensibility45}.each do |f| windows_feature f do action :install endend

windows_feature 'IIS-ASPNET45' do action :installend

service "iis" do service_name "W3SVC" action :nothingend

include_recipe "iis::remove_default_site"

nopCommerce Recipe Code: Install IIS, ASP.NET 4.5

Page 12: Cooking with Chef on Windows

windows_zipfile node['nopcommerce']['approot'] do source node['nopcommerce']['dist'] action :unzip not_if {::File.exists?(::File.join(node['nopcommerce']['approot'], "nopCommerce"))}end

%w{App_Data bin Content Content\\Images Content\\Images\\Thumbs Content\\Images\\Uploaded Content\\files\\ExportImport Plugins Plugins\\bin}.each do |d| directory win_friendly_path(::File.join(node['nopcommerce']['approot'], 'nopCommerce', d)) do rights :modify, 'IIS_IUSRS' endend

%w{Global.asax web.config}.each do |f| file win_friendly_path(::File.join(node['nopcommerce']['approot'], 'nopCommerce', f)) do rights :modify, 'IIS_IUSRS' endend

nopCommerce Recipe Code: Install nopCommerce

Page 13: Cooking with Chef on Windows

iis_pool node['nopcommerce']['poolname'] do runtime_version "4.0" action :addend

directory node['nopcommerce']['siteroot'] do rights :read, 'IIS_IUSRS' recursive true action :createend

iis_site 'nopCommerce' do protocol :http port 80 path node['nopcommerce']['siteroot'] application_pool node['nopcommerce']['poolname'] action [:add,:start]end

iis_app 'nopCommerce' do application_pool node['nopcommerce']['poolname'] path node['nopcommerce']['apppath'] physical_path "#{node['nopcommerce']['approot']}\\nopCommerce" action :addend

Set up IIS Pool, App, etc.

Page 14: Cooking with Chef on Windows

system32_path = node['kernel']['machine'] == 'x86_64' ? 'C:\Windows\Sysnative' : 'C:\Windows\System32'cookbook_file "#{system32_path}\\oemlogo.bmp" do source node['windowshacks']['oeminfo']['logofile'] rights :read, "Everyone" action :createend

registry_key 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\OEMInformation' do values [{:name => 'Logo', :type => :string, :data => 'C:\Windows\System32\oemlogo.bmp'}, {:name => 'Manufacturer', :type => :string, :data => node['windowshacks']['oeminfo']['manufacturer']}, {:name => 'SupportHours', :type => :string, :data => node['windowshacks']['oeminfo']['supporthours']}, {:name => 'SupportPhone', :type => :string, :data => node['windowshacks']['oeminfo']['supportphone']}, {:name => 'SupportURL', :type => :string, :data => node['windowshacks']['oeminfo']['supporturl']}] action :createend

Other Code You Might Have Noticed

Page 15: Cooking with Chef on Windows

64

Page 16: Cooking with Chef on Windows

The Result

Page 17: Cooking with Chef on Windows

Overview of Chef Resources on Windows

Page 18: Cooking with Chef on Windows

Same as UNIX/Linux• file, remote_file, cookbook_file, template

• directory, remote_directory

• user, group

• mount (can take CIFS paths)

• env

• service

• execute

• ruby_block

• many others...

Page 19: Cooking with Chef on Windows

Unique to Windows• registry_key (new in Chef 11.0.0)

• powershell_script (new in Chef 11.6.0)

• batch (new in Chef 11.6.0)

• Automatic architecture handling (:i386 vs. :x86_64)

• Automatic Windows filesystem redirector handling (Wow64)

• Long-term roadmap: move more resources to core and out of ‘windows’ cookbook

Page 20: Cooking with Chef on Windows

Windows-Only Cookbooks• By Chef:

• 7-zip

• iis

• powershell

• sql_server

• webpi

• windows

• wix

Page 21: Cooking with Chef on Windows

Windows Community Cookbooks• ms_dotnet2 / 4 / 45

• windows_ad (by TAMU)

• msoffice

• azure

Page 22: Cooking with Chef on Windows

# Set system’s proxy settings to be the same as used for Chefproxy = URI.parse(Chef::Config[:http_proxy])registry_key 'HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings' do values [{:name => 'ProxyEnable', :type => :reg_dword, :data => 1}, {:name => 'ProxyServer', :data => "#{proxy.host}:#{proxy.port}"}, {:name => 'ProxyOverride', :type => :reg_string, :data => '<local>'}] action :createend

registry_key example

Page 23: Cooking with Chef on Windows

powershell_script "rename hostname" do code <<-EOH $computer_name = Get-Content env:computername $new_name = 'test-hostname' $sysInfo = Get-WmiObject -Class Win32_ComputerSystem $sysInfo.Rename($new_name) EOHend

powershell_script example

Page 24: Cooking with Chef on Windows

Registry Helpers• Resources like powershell_script are not idempotent by default

• We provide some helpers for checking the registry:

• registry_data_exists?

• registry_get_subkeys

• registry_get_values

• registry_has_subkeys?

• registry_key_exists?

• registry_value_exists?

Page 25: Cooking with Chef on Windows

:windows_8_1?:windows_server_2012_r2?:windows_8?:windows_server_2012?

etc.

:marketing_name

:cluster?:core?:datacenter?

Version Helpers

• Methods on Chef::ReservedNames::Win32

Page 26: Cooking with Chef on Windows

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

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

Example Usage

• https://github.com/juliandunn/ms_dotnet2/blob/master/recipes/default.rb

Page 27: Cooking with Chef on Windows

Special File/Directory Handling• Parameters that don’t make sense

are ignored

• DOMAIN\user, DOMAIN\group work

• Filesystem ACLs are different on Windows

• mode parameter semantics

• rights parameter only for Windows

Page 28: Cooking with Chef on Windows

The ‘windows’ Cookbook• The windows cookbook includes a number of resources

and providers, and helper libraries.

• See https://github.com/opscode-cookbooks/windows for a full list

• Highlights:

• windows_auto_run

• windows_feature

• windows_package

• windows_path

• windows_reboot

• windows_zipfile

• Other: windows_printer, windows_printer_port, windows_task

Page 29: Cooking with Chef on Windows

Windows Report Handlers• Windows cookbook:

• WindowsRebootHandler

• windows_reboot resource

• windows::reboot_handler recipe

• Eventlog cookbook:

• Send Chef output to Windows Event Log

Page 30: Cooking with Chef on Windows

Desired State Configuration (DSC)•New in Windows 2012R2 / WMF4

•“Chef-like” declarative system

•Compiles to intermediate format (MOF)

•Provides reliable automation hooks into Windows

Page 31: Cooking with Chef on Windows

dsc_resource 'IIS' do name 'Webserver' resource :component action :installend

Potential DSC Integration

• 1:1 mapping DSC resources to Chef resources

• Challenges: DSC transactional, Chef is not

• Thoughts? See me after

Page 32: Cooking with Chef on Windows

Windows Roadmap 2014• Moar resources in core chef-client

• Package (e.g. msi), feature, reboot, etc.

• PowerShell DSC resource integration

• Easy WinRM setup, bootstrap

• Cookbooks: WSUS, AD, Group Policy, etc.

• Miscellaneus: Anonymous Resource RFC

• http://tinyurl.com/anonymous-resource-rfc

Page 33: Cooking with Chef on Windows

Testing on Windows

Page 34: Cooking with Chef on Windows

As a Host• Berkshelf, Test-Kitchen, ChefSpec work on Windows

• You need Git Bash or a UNIX-like environment

Page 35: Cooking with Chef on Windows

As a Guest• vagrant-windows

• Monkeypatch to Vagrant to support WinRM

• Works adequately, but fragile

• Packer images to generate Windows VMs

• https://github.com/misheska/basebox-packer

• ServerSpec supports Windows, but limited assertions

Page 36: Cooking with Chef on Windows

Questions?• Much more than what’s shown here!

• Questions?

• Thank you!

• E: [email protected]

• W: www.getchef.com

• T: @julian_dunn

• G: github.com/juliandunn

Page 37: Cooking with Chef on Windows