diff options
author | Joshua Timberman <joshua@opscode.com> | 2013-01-08 06:34:53 -0800 |
---|---|---|
committer | Joshua Timberman <joshua@opscode.com> | 2013-01-08 06:34:53 -0800 |
commit | 2072aa03ec5af04cdde97324c647113906916873 (patch) | |
tree | 568566e63489e834f421f2d81f71999e188e90e2 | |
parent | 73a7919842b45812f623163afef7bae7c18badb9 (diff) | |
parent | ed4eee5e0ce0cb3f44f4e78df1f0f1ce0b3cdde7 (diff) | |
download | mixlib-shellout-2072aa03ec5af04cdde97324c647113906916873.tar.gz |
Merge pull request #19 from Nordstrom/cook-2142
Cook 2142: Add Windows Printer and Printer Port LWRPs
-rw-r--r-- | README.md | 89 | ||||
-rw-r--r-- | metadata.rb | 1 | ||||
-rw-r--r-- | providers/printer.rb | 100 | ||||
-rw-r--r-- | providers/printer_port.rb | 102 | ||||
-rw-r--r-- | resources/printer.rb | 41 | ||||
-rw-r--r-- | resources/printer_port.rb | 40 |
6 files changed, 373 insertions, 0 deletions
@@ -23,6 +23,7 @@ Cookbooks --------- * chef_handler (`windows::reboot_handler` leverages the chef_handler LWRP) +* powershell - The Printer and Printer Port LWRP require Powershell Attributes ========== @@ -230,6 +231,92 @@ For maximum flexibility the `source` attribute supports both remote and local in end +windows\_printer\_port +---------------------- + +Create and delete TCP/IPv4 printer ports. + +### Actions + +- :create: Create a TCIP/IPv4 printer port. This is the default action. +- :delete: Delete a TCIP/IPv4 printer port + +### Attribute Parameters + +- :ipv4_address: Name attribute. Required. IPv4 address, e.g. "10.0.24.34" +- :port_name: Port name. Optional. Defaults to "IP_" + :ipv4_address +- :port_number: Port number. Optional. Defaults to 9100. +- :port_description: Port description. Optional. +- :snmp_enabled: Boolean. Optional. Defaults to false. +- :port_protocol: Port protocol, 1 (RAW), or 2 (LPR). Optional. Defaults to 1. + +### Examples + + # simplest example. Creates a TCP/IP printer port named "IP_10.4.64.37" + # with all defaults + windows_printer_port '10.4.64.37' do + end + + # delete a printer port + windows_printer_port '10.4.64.37' do + action :delete + end + + # delete a port with a custom port_name + windows_printer_port '10.4.64.38' do + port_name "My awesome port" + action :delete + end + + # Create a port with more options + windows_printer_port '10.4.64.39' do + port_name "My awesome port" + snmp_enabled true + port_protocol 2 + end + + +windows\_printer +---------------- + +Create Windows printer. Note that this doesn't currently install a printer +driver. You must already have the driver installed on the system. + +The Windows Printer LWRP will automatically create a TCP/IP printer port for you using the `ipv4_address` property. If you want more granular control over the printer port, just create it using the `windows_printer_port` LWRP before creating the printer. + +### Actions + +- :create: Create a new printer +- :delete: Delete a new printer + +### Attribute Parameters + +- :device_id: Name attribute. Required. Printer queue name, e.g. "HP LJ 5200 in fifth floor copy room" +- :comment: Optional string describing the printer queue. +- :default: Boolean. Optional. Defaults to false. Note that Windows sets the first printer defined to the default printer regardless of this setting. +- :driver_name: String. Required. Exact name of printer driver. Note that the printer driver must already be installed on the node. +- :location: Printer location, e.g. "Fifth floor copy room", or "US/NYC/Floor42/Room4207" +- :shared: Boolean. Defaults to false. +- :share_name: Printer share name. +- :ipv4_address: Printer IPv4 address, e.g. "10.4.64.23". You don't have to be able to ping the IP addresss to set it. Required. + + +### Examples + + # create a printer + windows_printer 'HP LaserJet 5th Floor' do + driver_name 'HP LaserJet 4100 Series PCL6' + ipv4_address '10.4.64.38' + end + + # delete a printer + # Note: this doesn't delete the associated printer port. + # See `windows_printer_port` above for how to delete the port. + windows_printer 'HP LaserJet 5th Floor' do + action :delete + end + + windows\_reboot --------------- @@ -481,10 +568,12 @@ License and Author Author:: Seth Chisamore (<schisamo@opscode.com>) Author:: Doug MacEachern (<dougm@vmware.com>) Author:: Paul Morton (<pmorton@biaprotect.com>) +Author:: Doug Ireton (<doug.ireton@nordstrom.com>) Copyright:: 2011, Opscode, Inc. Copyright:: 2010, VMware, Inc. Copyright:: 2011, Business Intelligence Associates, Inc +Copyright:: 2012, Nordstrom, Inc. Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/metadata.rb b/metadata.rb index 5e81f4e..1a5afaa 100644 --- a/metadata.rb +++ b/metadata.rb @@ -7,3 +7,4 @@ long_description IO.read(File.join(File.dirname(__FILE__), 'README.md')) version "1.7.0" supports "windows" depends "chef_handler" +depends "powershell" diff --git a/providers/printer.rb b/providers/printer.rb new file mode 100644 index 0000000..95a2c85 --- /dev/null +++ b/providers/printer.rb @@ -0,0 +1,100 @@ +# +# Author:: Doug Ireton (<doug.ireton@nordstrom.com>) +# Cookbook Name:: windows +# Provider:: printer +# +# Copyright:: 2012, Nordstrom, 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. +# + +# Support whyrun +def whyrun_supported? + true +end + +action :create do + if @current_resource.exists + Chef::Log.info "#{ @new_resource } already exists - nothing to do." + else + converge_by("Create #{ @new_resource }") do + create_printer + end + end +end + +action :delete do + if @current_resource.exists + converge_by("Delete #{ @new_resource }") do + delete_printer + end + else + Chef::Log.info "#{ @current_resource } doesn't exist - can't delete." + end +end + +def load_current_resource + @current_resource = Chef::Resource::WindowsPrinter.new(@new_resource.name) + @current_resource.name(@new_resource.name) + + if printer_exists?(@current_resource.name) + # TODO: Set @current_resource printer properties from registry + @current_resource.exists = true + end +end + + +private + +PRINTERS_REG_KEY = 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print\Printers\\' + +def printer_exists?(name) + printer_reg_key = PRINTERS_REG_KEY + name + Chef::Log.debug "Checking to see if this reg key exists: '#{ printer_reg_key }'" + Registry.key_exists?(printer_reg_key) +end + +def create_printer + + # Create the printer port first + windows_printer_port new_resource.ipv4_address do + end + + port_name = "IP_#{ new_resource.ipv4_address }" + + powershell "Creating printer: #{ new_resource.name }" do + code <<-EOH + + Set-WmiInstance -class Win32_Printer ` + -EnableAllPrivileges ` + -Argument @{ DeviceID = "#{ new_resource.device_id }"; + Comment = "#{ new_resource.comment }"; + Default = "$#{ new_resource.default }"; + DriverName = "#{ new_resource.driver_name }"; + Location = "#{ new_resource.location }"; + PortName = "#{ port_name }"; + Shared = "$#{ new_resource.shared }"; + ShareName = "#{ new_resource.share_name }"; + } + EOH + end +end + +def delete_printer + powershell "Deleting printer: #{ new_resource.name }" do + code <<-EOH + $printer = Get-WMIObject -class Win32_Printer -EnableAllPrivileges -Filter "name = '#{ new_resource.name }'" + $printer.Delete() + EOH + end +end diff --git a/providers/printer_port.rb b/providers/printer_port.rb new file mode 100644 index 0000000..d6147f8 --- /dev/null +++ b/providers/printer_port.rb @@ -0,0 +1,102 @@ +# +# Author:: Doug Ireton (<doug.ireton@nordstrom.com>) +# Cookbook Name:: windows +# Provider:: printer_port +# +# Copyright:: 2012, Nordstrom, 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. +# + +# Support whyrun +def whyrun_supported? + true +end + +action :create do + if @current_resource.exists + Chef::Log.info "#{ @new_resource } already exists - nothing to do." + else + converge_by("Create #{ @new_resource }") do + create_printer_port + end + end +end + +action :delete do + if @current_resource.exists + converge_by("Delete #{ @new_resource }") do + delete_printer_port + end + else + Chef::Log.info "#{ @current_resource } doesn't exist - can't delete." + end +end + +def load_current_resource + @current_resource = Chef::Resource::WindowsPrinterPort.new(@new_resource.name) + @current_resource.name(@new_resource.name) + @current_resource.ipv4_address(@new_resource.ipv4_address) + @current_resource.port_name(@new_resource.port_name || "IP_#{ @new_resource.ipv4_address }") + + if port_exists?(@current_resource.port_name) + # TODO: Set @current_resource port properties from registry + @current_resource.exists = true + end +end + + +private + +PORTS_REG_KEY = 'HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Monitors\Standard TCP/IP Port\Ports\\' + +def port_exists?(name) + port_reg_key = PORTS_REG_KEY + name + + Chef::Log.debug "Checking to see if this reg key exists: '#{ port_reg_key }'" + Registry.key_exists?(port_reg_key) +end + + +def create_printer_port + + port_name = new_resource.port_name || "IP_#{ new_resource.ipv4_address }" + + # create the printer port using PowerShell + powershell "Creating printer port #{ new_resource.port_name }" do + code <<-EOH + + Set-WmiInstance -class Win32_TCPIPPrinterPort ` + -EnableAllPrivileges ` + -Argument @{ HostAddress = "#{ new_resource.ipv4_address }"; + Name = "#{ port_name }"; + Description = "#{ new_resource.port_description }"; + PortNumber = "#{ new_resource.port_number }"; + Protocol = "#{ new_resource.port_protocol }"; + SNMPEnabled = "$#{ new_resource.snmp_enabled }"; + } + EOH + end +end + +def delete_printer_port + + port_name = new_resource.port_name || "IP_#{ new_resource.ipv4_address }" + + powershell "Deleting printer port: #{ new_resource.port_name }" do + code <<-EOH + $port = Get-WMIObject -class Win32_TCPIPPrinterPort -EnableAllPrivileges -Filter "name = '#{ port_name }'" + $port.Delete() + EOH + end +end diff --git a/resources/printer.rb b/resources/printer.rb new file mode 100644 index 0000000..5effa33 --- /dev/null +++ b/resources/printer.rb @@ -0,0 +1,41 @@ +# +# Author:: Doug Ireton (<doug.ireton@nordstrom.com>) +# Cookbook Name:: windows +# Resource:: printer +# +# Copyright:: 2012, Nordstrom, 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. +# +# See here for more info: +# http://msdn.microsoft.com/en-us/library/windows/desktop/aa394492(v=vs.85).aspx + +require 'resolv' + +actions :create, :delete + +default_action :create + +attribute :device_id, :kind_of => String, :name_attribute => true, + :required => true +attribute :comment, :kind_of => String + +attribute :default, :kind_of => [ TrueClass, FalseClass ], :default => false +attribute :driver_name, :kind_of => String, :required => true +attribute :location, :kind_of => String +attribute :shared, :kind_of => [ TrueClass, FalseClass ], :default => false +attribute :share_name, :kind_of => String + +attribute :ipv4_address, :kind_of => String, :regex => Resolv::IPv4::Regex + +attr_accessor :exists diff --git a/resources/printer_port.rb b/resources/printer_port.rb new file mode 100644 index 0000000..b79a6fc --- /dev/null +++ b/resources/printer_port.rb @@ -0,0 +1,40 @@ +# +# Author:: Doug Ireton (<doug.ireton@nordstrom.com>) +# Cookbook Name:: windows +# Resource:: printer_port +# +# Copyright:: 2012, Nordstrom, 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. +# +# See here for more info: +# http://msdn.microsoft.com/en-us/library/windows/desktop/aa394492(v=vs.85).aspx + +require 'resolv' + +actions :create, :delete + +default_action :create + +attribute :ipv4_address, :name_attribute => true, :kind_of => String, + :required => true, :regex => Resolv::IPv4::Regex + +attribute :port_name , :kind_of => String +attribute :port_number , :kind_of => Fixnum, :default => 9100 +attribute :port_description, :kind_of => String +attribute :snmp_enabled , :kind_of => [ TrueClass, FalseClass ], + :default => false + +attribute :port_protocol, :kind_of => Fixnum, :default => 1, :equal_to => [1, 2] + +attr_accessor :exists |