summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDoug Ireton <doug.ireton@nordstrom.com>2012-12-30 19:28:33 -0800
committerDoug Ireton <doug.ireton@nordstrom.com>2013-01-01 16:43:46 -0800
commitd1ab9cbd983ee063183c1f1ac3132037352d96bb (patch)
tree4cc54c9a2d87a2b03c54c5c1c939681655ae3da0
parent1601558f5fce30bed47bfc6d458a80891e127c61 (diff)
downloadmixlib-shellout-d1ab9cbd983ee063183c1f1ac3132037352d96bb.tar.gz
Add printer and printer_port LWRPs
Conflicts: providers/printer.rb resources/printer.rb
-rw-r--r--README.md89
-rw-r--r--providers/printer.rb74
-rw-r--r--providers/printer_port.rb76
-rw-r--r--resources/printer.rb19
-rw-r--r--resources/printer_port.rb22
5 files changed, 280 insertions, 0 deletions
diff --git a/README.md b/README.md
index a19c7cd..69e670e 100644
--- a/README.md
+++ b/README.md
@@ -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
---------------
@@ -480,10 +567,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/providers/printer.rb b/providers/printer.rb
new file mode 100644
index 0000000..b926d3f
--- /dev/null
+++ b/providers/printer.rb
@@ -0,0 +1,74 @@
+action :create do
+ if @current_resource.exists
+ Chef::Log.info "#{ @new_resource } already exists - nothing to do."
+ else
+ create_printer
+ end
+end
+
+action :delete do
+ if @current_resource.exists
+ delete_printer
+ 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 = @new_resource.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
+
+ @new_resource.updated_by_last_action(true)
+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
+
+ @new_resource.updated_by_last_action(true)
+end
diff --git a/providers/printer_port.rb b/providers/printer_port.rb
new file mode 100644
index 0000000..0d5c8d5
--- /dev/null
+++ b/providers/printer_port.rb
@@ -0,0 +1,76 @@
+action :create do
+ if @current_resource.exists
+ Chef::Log.info "#{ @new_resource } already exists - nothing to do."
+ else
+ create_printer_port
+ end
+end
+
+action :delete do
+ if @current_resource.exists
+ delete_printer_port
+ 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 using PowerShell
+ powershell "Creating printer port #{ 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
+
+ @new_resource.updated_by_last_action(true)
+end
+
+def delete_printer_port
+ port_name = @new_resource.port_name || "IP_#{ @new_resource.ipv4_address }"
+
+ powershell "Deleting printer port: #{ port_name }" do
+ code <<-EOH
+ $port = Get-WMIObject -class Win32_TCPIPPrinterPort -EnableAllPrivileges -Filter "name = '#{ port_name }'"
+ $port.Delete()
+ EOH
+ end
+
+ @new_resource.updated_by_last_action(true)
+end
diff --git a/resources/printer.rb b/resources/printer.rb
new file mode 100644
index 0000000..768ea59
--- /dev/null
+++ b/resources/printer.rb
@@ -0,0 +1,19 @@
+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..c07a9a3
--- /dev/null
+++ b/resources/printer_port.rb
@@ -0,0 +1,22 @@
+
+# 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