-
Notifications
You must be signed in to change notification settings - Fork 23
/
cups_lpadmin.py
1187 lines (962 loc) · 44.3 KB
/
cups_lpadmin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
(c) 2015, David Symons (Multimac) <[email protected]>
(c) 2016, Konstantin Shalygin <[email protected]>
(c) 2016, Hitesh Prabhakar <HP41@GitHub>
This file is part of Ansible
This module is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This software is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this software. If not, see <http://www.gnu.org/licenses/>.
"""
# ===========================================
DOCUMENTATION = '''
---
module: cups_lpadmin
author:
- "David Symons (Multimac) <[email protected]>"
- "Konstantin Shalygin <[email protected]>"
- "Hitesh Prabhakar <H41P@GitHub>"
short_description: Manages printers in CUPS printing system.
description:
- Creates, removes and sets options for printers in CUPS.
- Creates, removes and sets options for classes in CUPS.
- For class installation, the members are defined as a final state and therefore will only have the members defined.
version_added: "2.1"
notes: []
requirements:
- CUPS 1.7+
options:
name:
description:
- Name of the printer in CUPS.
required: false
default: null
purge:
description:
- Task to purge all printers in CUPS. Convenient before deploy.
required: false
default: false
choices: ["true", "false"]
state:
description:
- Whether the printer should or not be in CUPS.
required: false
default: present
choices: ["present", "absent"]
printer_or_class:
description:
- State whether the object/item we are working on is a printer or class.
required: false
default: printer
choices: ["printer", "class"]
driver:
description:
- System V interface or PPD file.
required: false
default: model
choices: ["model", "ppd"]
uri:
description:
- The URI to use when connecting to the printer. This is only required in the present state.
required: false
default: null
enabled:
description:
- Whether or not the printer should be enabled and accepting jobs.
required: false
default: true
choices: ["true", "false"]
shared:
description:
- Whether or not the printer should be shared on the network.
required: false
default: false
choices: ["true", "false"]
model:
description:
- The System V interface or PPD file to be used for the printer.
required: false
default: null
default:
description:
- Set default server printer. Only one printer can be default.
required: false
default: false
choices: ["true", "false"]
info:
description:
- The textual description of the printer.
required: false
default: null
location:
description:
- The textual location of the printer.
required: false
default: null
assign_cups_policy:
description:
- Assign a policy defined in /etc/cups/cupsd.conf to this printer.
required: false
default: null
class_members:
description:
- A list of printers to be added to this class.
required: false
default: []
type: list
report_ipp_supply_levels:
description:
- Whether or not the printer must report supply status via IPP.
required: false
default: true
choices: ["true", "false"]
report_snmp_supply_levels:
description:
- Whether or not the printer must report supply status via SNMP (RFC 3805).
required: false
default: true
choices: ["true", "false"]
job_kb_limit:
description:
- Limit jobs to this printer (in KB)
required: false
default: null
job_quota_limit:
description:
- Sets the accounting period for per-user quotas. The value is an integer number of seconds.
required: false
default: null
job_page_limit:
description:
- Sets the page limit for per-user quotas. The value is the integer number of pages that can be printed.
- Double sided pages are counted as 2.
required: false
default: null
options:
description:
- A dictionary of key-value pairs describing printer options and their required value.
default: {}
required: false
'''
# ===========================================
EXAMPLES = '''
# Creates HP MFP via ethernet, set default A4 paper size and make this printer
as server default.
- cups_lpadmin:
name: 'HP_M1536'
state: 'present'
printer_or_class: 'printer'
uri: 'hp:/net/HP_LaserJet_M1536dnf_MFP?ip=192.168.1.2'
model: 'drv:///hp/hpcups.drv/hp-laserjet_m1539dnf_mfp-pcl3.ppd'
default: 'true'
location: 'Room 404'
info: 'MFP, but duplex broken, as usual on this model'
printer_assign_policy: 'students'
report_ipp_supply_levels: 'true'
report_snmp_supply_levels: 'false'
options:
media: 'iso_a4_210x297mm'
# Creates HP Printer via IPP (shared USB printer in another CUPS instance).
Very important include 'snmp=false' to prevent adopt 'parent' driver,
because if 'parent' receive not raw job this job have fail (filter failed).
- cups_lpadmin:
name: 'HP_P2055'
state: 'present'
uri: 'ipp://192.168.2.127:631/printers/HP_P2055?snmp=false'
model: 'raw'
options:
media: 'iso_a4_210x297mm'
# Create CUPS Class.
- cups_lpadmin:
name: 'StudentClass'
state: 'present'
printer_or_class: 'class'
class_members:
- CampusPrinter1
- CampusPrinter2
info: 'Printers for students'
location: 'Room 404'
# Deletes the printers/classes.
- cups_lpadmin:
name: 'HP_P2055'
state: 'absent'
printer_or_class: 'printer'
- cups_lpadmin:
name: 'StudentClass'
state: 'absent'
printer_or_class: 'class'
# Purge all printers/classes. Useful when does not matter what we have now,
client always receive new configuration.
- cups_lpadmin: purge='true'
'''
# ===========================================
RETURN = '''
purge:
description: Whether to purge all printers on CUPS or not.
returned: when purge=True
type: string
sample: "True"
state:
description: The state as defined in the invocation of this script.
returned: when purge=False
type: string
sample: "present"
printer_or_class:
description: Printer or Class as defined when this script was invoked.
returned: when purge=False
type: string
sample: "class"
name:
description: The name of the destination (printer/class) as defined when the script was invoked.
returned: when purge=False
type: string
sample: "Test-Printer"
uri:
description: The uri of the printer.
returned: when purge=False and printer_or_class=printer
type: string
sample: "ipp://192.168.2.127:631/printers/HP_P2055?snmp=false"
class_members:
description: The members of the class.
returned: when purge=False and printer_or_class=class
type: string
sample: "[TestPrinter1,TestPrinter2]"
assign_cups_policy:
description: The CUPS policy to assign this printer or class.
returned: when purge=False and (printer_or_class=class or printer_or_class=printer)
type: string
sample: "[TestPrinter1,TestPrinter2]"
changed:
description: If any changes were made to the system when this script was run.
returned: always
type: boolean
sample: "False"
stdout:
description: Output from all the commands run concatenated. Only returned if any changes to the system were run.
returned: always
type: string
sample: "sample output"
cmd_history:
description: A concatenated string of all the commands run.
returned: always
type: string
sample: "\nlpstat -p TEST \nlpinfo -l -m \nlpoptions -p TEST \nlpstat -p TEST \nlpstat -p TEST \nlpadmin -p TEST -o cupsIPPSupplies=true -o cupsSNMPSupplies=true \nlpoptions -p TEST -l "
'''
# ===========================================
class CUPSCommand(object):
"""
This is the main class that directly deals with the lpadmin command.
Method naming methodology:
- Methods prefixed with 'cups_item' or '_cups_item' can be used with both printer and classes.
- Methods prefixed with 'class' or '_class' are meant to work with classes only.
- Methods prefixed with 'printer' or '_printer' are meant to work with printers only.
CUPSCommand handles printers like so:
- If state=absent:
- Printer exists: Deletes printer
- Printer doesn't exist: Does nothing and exits
- If state=present:
- Printer exists: Checks printer options and compares them to the ones stated:
- Options are different: Deletes the printer and installs it again with stated options.
- Options are same: Does nothing and exits.
- Printer doesn't exist: Installs printer with stated options.
- Mandatory options are set every time if the right variables are defined. They are:
- cupsIPPSupplies
- cupsSNMPSupplies
- printer-op-policy
- job-k-limit
- job-page-limit
- job-quota-period
CUPSCommand handles classes like so:
- If state=absent:
- Class exists: Deletes class
- Class doesn't exist: Does nothing and exits
- If state=present:
- Class exists: Checks class options and members and compares them to the ones stated:
- Options and members are different: Deletes the class and installs it again with
stated options and stated members.
- Options and members are same: Does nothing and exits.
- Class doesn't exist: Installs class with stated options and members.
- Mandatory options are set every time if the right variables are defined. They are:
- cupsIPPSupplies
- cupsSNMPSupplies
- printer-op-policy
- Notes about how classes are handled:
- Members stated will be the final list of printers in that class.
- It cannot add or remove printers from an existing list that might have more/other members defined.
- It'll uninstall the class and create it from scratch as defined in this script if the defined member
list and the actual member list don't match.
"""
def __init__(self, module):
"""
Assigns module vars to object.
"""
self.module = module
self.driver = CUPSCommand.strip_whitespace(module.params['driver'])
self.name = CUPSCommand.strip_whitespace(module.params['name'])
self.printer_or_class = module.params['printer_or_class']
self.state = module.params['state']
self.purge = module.params['purge']
self.uri = CUPSCommand.strip_whitespace(module.params['uri'])
self.enabled = module.params['enabled']
self.shared = module.params['shared']
self.default = module.params['default']
self.model = CUPSCommand.strip_whitespace(module.params['model'])
self.info = CUPSCommand.strip_whitespace(module.params['info'])
self.location = CUPSCommand.strip_whitespace(module.params['location'])
self.options = module.params['options']
self.assign_cups_policy = CUPSCommand.strip_whitespace(module.params['assign_cups_policy'])
self.class_members = module.params['class_members']
self.report_ipp_supply_levels = module.params['report_ipp_supply_levels']
self.report_snmp_supply_levels = module.params['report_snmp_supply_levels']
self.job_kb_limit = module.params['job_kb_limit']
self.job_quota_limit = module.params['job_quota_limit']
self.job_page_limit = module.params['job_page_limit']
self.out = ""
self.cmd_history = ""
self.changed = False
self.cups_current_options = {}
self.cups_expected_options = {}
self.class_current_members = []
self.printer_current_options = {}
self.check_mode = module.check_mode
self.check_settings()
def check_settings(self):
"""
Checks the values provided to the module and see if there are any missing/illegal settings.
Module fails and exits if it encounters an illegal combination of variables sent to the module.
:returns: None
"""
msgs = []
if self.state == 'printer':
if not self.printer_or_class:
msgs.append("When state=present printer or class must be defined.")
if self.printer_or_class == 'printer':
if not self.uri and not self.exists_self():
msgs.append("URI is required to install printer.")
if self.printer_or_class == 'class':
if not self.class_members and not self.exists_self():
self.module.fail_json(msg="Empty class cannot be created.")
if msgs:
"\n".join(msgs)
self.module.fail_json(msg=msgs)
@staticmethod
def strip_whitespace(text):
"""
A static method to help with stripping white space around object variables.
:returns: Trailing whitespace removed text or 'None' if input is 'None'.
"""
try:
return text.strip()
except:
return None
def append_cmd_out(self, cmd_out):
"""
Appends the out text from the command that was just run to the string with the out text of all the commands run.
:param cmd_out: The text that was outputted during last command that was run.
:returns: None
"""
if cmd_out:
self.out = "{0}{1}{2}".format(self.out, "\n", cmd_out)
def append_cmd_history(self, cmd):
"""
Appends the commands run into a single string.
:param cmd: The command to be appended into the command history string.
:returns: None
"""
safe_cmd = ""
for x in cmd:
x = str(x)
if " " in x:
if not ((x.startswith('"') and x.endswith('"')) or (x.startswith("'") and x.endswith("'"))):
x = '{0}{1}{0}'.format('"', x)
safe_cmd = "{0}{1}{2}".format(safe_cmd, x, " ")
self.cmd_history = "{0}{1}{2}".format(self.cmd_history, "\n", safe_cmd)
def _log_results(self, out):
"""
Method to log the details outputted from the command that was just run.
:param out: Output text from the command that was just run.
:returns: None
"""
self.append_cmd_out(out)
def process_info_command(self, cmd):
"""
Runs a command that's meant to poll information only.
Wraps around _process_command and ensures command output isn't logged as we're just fetching for information.
:param cmd: The command to run.
:returns: The output of _process_command which is return code, command output and error output.
"""
return self._process_command(cmd, log=False)
def process_change_command(self, cmd, err_msg, only_log_on_error=False):
"""
Runs a command that's meant to change CUPS state/settings.
Wraps around _process_command and ensures command output is logged as we're making changes to the system.
An optional only_log_on_error is provided for the install_mandatory_options methods that are always run
almost always and need not pollute the changed/output text with its information. This'll ensure the output
and error text is only recorded when there's an error (err != None) and (rc != 0).
It also is an easy way to centralize change command therefore making support_check_mode easier to implement.
:param cmd: The command to run.
:param err_msg: The error message with which to exit the module if an error occurred.
:param only_log_on_error: The optional flag to record output if there's an error. Default=False
:returns: The output of _process_command which is return code, command output and error output.
"""
(rc, out, err) = self._process_command(cmd, log=False)
if rc != 0 and err:
self.module.fail_json(msg="Error Message - {0}. Command Error Output - {1}.".format(err_msg, err))
if self.check_mode:
self.module.exit_json(changed=True)
if not only_log_on_error:
self._log_results(out)
self.changed = True
return rc, out, err
def _process_command(self, cmd, log=True):
"""
Runs a command given to it. Also logs the details if specified.
:param cmd: The command to run.
:param log: Boolean to specify if the command output should be logged. Default=True
:returns: Return code, command output and error output of the command that was run.
"""
self.append_cmd_history(cmd)
(rc, out, err) = self.module.run_command(cmd)
if log:
self._log_results(out)
return rc, out, err
def _printer_get_installed_drivers(self):
"""
Parses the output of lpinfo -l -m to provide a list of available drivers on machine.
Example output from lpinfo -l -m:
Model: name = gutenprint.5.2://xerox-wc_m118/expert
natural_language = en
make-and-model = Xerox WorkCentre M118 - CUPS+Gutenprint v5.2.11
device-id = MFG:XEROX;MDL:WorkCentre M118;DES:XEROX WorkCentre M118;
The output is parsed into a hash and then placed into the value of another hash where the key is the name field:
'gutenprint.5.2://xerox-wc_m118/expert': 'name': 'gutenprint.5.2://xerox-wc_m118/expert'
'natural_language': 'en'
'make-and-model': 'Xerox WorkCentre M118 - CUPS+Gutenprint v5.2.11'
'device-id': 'MFG:XEROX;MDL:WorkCentre M118;DES:XEROX WorkCentre M118;'
:returns: Hash defining all the drivers installed on the system.
"""
cmd = ['lpinfo', '-l', '-m']
(rc, out, err) = self.process_info_command(cmd)
# We want to split on sections starting with "Model:" as that specifies a new available driver
prog = re.compile("^Model:", re.MULTILINE)
cups_drivers = re.split(prog, out)
drivers = {}
for d in cups_drivers:
# Skip if the line contains only whitespace
if not d.strip():
continue
curr = {}
for l in d.splitlines():
kv = l.split('=', 1)
# Strip out any excess whitespace from the key/value
kv = tuple(map(str.strip, kv))
curr[kv[0]] = kv[1]
# Store drivers by their 'name' (i.e. path to driver file)
drivers[curr['name']] = curr
return drivers
def _printer_get_all_printers(self):
"""
Method to return all current printers and classes in CUPS.
:returns: list of printer or classes names.
"""
cmd = ['lpstat', '-a']
(rc, out, err) = self.process_info_command(cmd)
all_printers = []
if rc == 0:
# Match only 1st column, where placed printer name
all_printers = [line.split()[0] for line in out.splitlines()]
return all_printers
def cups_purge_all_items(self):
"""
Purge all printers and classes installed on CUPS.
"""
all_printers = self._printer_get_all_printers()
for printer in all_printers:
self.cups_item_uninstall(item_to_uninstall=printer)
def _printer_get_make_and_model(self):
"""
Method to return the make and model of the driver/printer that is supplied to the object.
If ppd is provided, ignore this as the ppd provided takes priority over finding a driver.
If not ppd is provided (default behaviour), the model specified is used.
It checks to see if the model specified is in the list of drivers installed on the system. If not, the whole
module fails out with an error message.
:returns: make-and-model of the model specified.
"""
if self.driver == 'model':
# Raw printer is defined
if not self.model or self.model == 'raw':
return "Remote Printer"
elif self.driver == 'ppd':
return
installed_drivers = self._printer_get_installed_drivers()
if self.model in installed_drivers:
return installed_drivers[self.model]['make-and-model']
self.module.fail_json(msg="Unable to determine printer make and model for printer '{0}'.".format(self.model))
def _printer_install(self):
"""
Installs the printer with the settings defined.
"""
cmd = ['lpadmin', '-p', self.name, '-v', self.uri]
if self.enabled:
cmd.append('-E')
if self.shared:
cmd.extend(['-o', 'printer-is-shared=true'])
else:
cmd.extend(['-o', 'printer-is-shared=false'])
if self.model:
if self.driver == 'model':
cmd.extend(['-m', self.model])
elif self.driver == 'ppd':
cmd.extend(['-P', self.model])
if self.info:
cmd.extend(['-D', self.info])
if self.location:
cmd.extend(['-L', self.location])
self.process_change_command(cmd,
err_msg="Installing printer '{0}' failed"
.format(self.name))
if self.default:
cmd = ['lpadmin', '-d', self.name]
self.process_change_command(cmd,
err_msg="Setting printer '{0}' as default failed"
.format(self.name))
def _printer_install_mandatory_options(self):
"""
Installs mandatory printer options.
cupsIPPSupplies, cupsSNMPSupplies, job-k-limit, job-page-limit, printer-op-policy,job-quota-period
cannot be checked via cups command-line tools yet. Therefore force set these options if they are defined.
If there's an error running the command, the whole module will fail with an error message.
"""
orig_cmd = ['lpadmin', '-p', self.name]
cmd = list(orig_cmd) # Making a copy of the list/array
if self.report_ipp_supply_levels:
cmd.extend(['-o', 'cupsIPPSupplies=true'])
else:
cmd.extend(['-o', 'cupsIPPSupplies=false'])
if self.report_snmp_supply_levels:
cmd.extend(['-o', 'cupsSNMPSupplies=true'])
else:
cmd.extend(['-o', 'cupsSNMPSupplies=false'])
if self.job_kb_limit:
cmd.extend(['-o', 'job-k-limit={0}'.format(self.job_kb_limit)])
if self.job_page_limit:
cmd.extend(['-o', 'job-page-limit={0}'.format(self.job_page_limit)])
if self.job_quota_limit:
cmd.extend(['-o', 'job-quota-period={0}'.format(self.job_quota_limit)])
if self.assign_cups_policy:
cmd.extend(['-o', 'printer-op-policy={0}'.format(self.assign_cups_policy)])
if cmd != orig_cmd:
self.process_change_command(cmd,
err_msg="Install mandatory options for printer '{0}'"
.format(self.name),
only_log_on_error=True)
def _printer_install_options(self):
"""
Installs any printer driver specific options defined.
:returns: rc, out, err. The output of the lpadmin installation command.
"""
cmd = ['lpadmin', '-p', self.name]
for k, v in self.options.iteritems():
cmd.extend(['-o', '{0}={1}'.format(k, v)])
if self.default:
cmd.extend(['-d', self.name])
return self.process_change_command(cmd,
err_msg="Install printer options for printer '{0}' failed".format(self.name))
def _class_install(self):
"""
Installs the class with the settings defined.
It loops through the list of printers that are supposed to be in the class and confirms if they exists and
adds them to the class. If any one of the printers don't exist, the whole module will fail with an error
message.
"""
for printer in self.class_members:
# Going through all the printers that are supposed to be in the class and adding them to said class
# Ensuring first the printer exists
if self.exists(item_to_check=printer):
cmd = ['lpadmin', '-p', printer, '-c', self.name]
self.process_change_command(cmd,
err_msg="Failed to add printer '{0}' to class '{1}'"
.format(printer, self.name))
else:
self.module.fail_json(msg="Printer '{0}' doesn't exist and cannot be added to class '{1}'."
.format(printer, self.name))
# Now that the printers are added to the class and the class created, we are setting up a few
# settings for the class itself
if self.exists_self():
cmd = ['lpadmin', '-p', self.name]
if self.enabled:
cmd.append('-E')
if self.shared:
cmd.extend(['-o', 'printer-is-shared=true'])
else:
cmd.extend(['-o', 'printer-is-shared=false'])
if self.info:
cmd.extend(['-D', self.info])
if self.location:
cmd.extend(['-L', self.location])
self.process_change_command(cmd,
err_msg="Failed to set Class options for class '{0}'"
.format(self.name))
def _class_install_mandatory_options(self):
"""
Installs mandatory class options.
cupsIPPSupplies, cupsSNMPSupplies, printer-op-policy,job-quota-period cannot be checked via
cups command-line tools yet. Therefore force set these options if they are defined.
If there's an error running the command, the whole module will fail with an error message.
"""
orig_cmd = ['lpadmin', '-p', self.name]
cmd = list(orig_cmd) # Making a copy of the list/array
if self.report_ipp_supply_levels:
cmd.extend(['-o', 'cupsIPPSupplies=true'])
else:
cmd.extend(['-o', 'cupsIPPSupplies=false'])
if self.report_snmp_supply_levels:
cmd.extend(['-o', 'cupsSNMPSupplies=true'])
else:
cmd.extend(['-o', 'cupsSNMPSupplies=false'])
if self.assign_cups_policy:
cmd.extend(['-o', 'printer-op-policy={0}'.format(self.assign_cups_policy)])
if cmd != orig_cmd:
self.process_change_command(cmd,
err_msg="Installing mandatory options for class '{0}' failed"
.format(self.name),
only_log_on_error=True)
def cups_item_uninstall_self(self):
"""
Uninstalls the printer or class defined in this class.
"""
self.cups_item_uninstall(item_to_uninstall=self.name)
def cups_item_uninstall(self, item_to_uninstall):
"""
Uninstalls a printer or class given in item_to_uninstall if it exists else do nothing.
:param item_to_uninstall: the CUPS Item (Printer or Class) that needs to be uninstalled.
"""
cmd = ['lpadmin', '-x']
if self.exists(item_to_check=item_to_uninstall):
if item_to_uninstall:
cmd.append(item_to_uninstall)
self.process_change_command(cmd,
err_msg="Uninstalling CUPS Item '{0}' failed"
.format(item_to_uninstall))
else:
self.module.fail_json(msg="Cannot delete/uninstall a cups item (printer/class) with no name.")
def exists_self(self):
"""
Checks to see if the printer or class defined in this class exists.
Using the lpstat command and based on if an error code is returned it can confirm if a printer or class exists.
:returns: The return value of self.exists()
"""
return self.exists(item_to_check=self.name)
def exists(self, item_to_check=None):
"""
Checks to see if a printer or class exists.
Using the lpstat command and based on if an error code is returned it can confirm if a printer or class exists.
:param item_to_check: The print or class name to check if it exists.
:returns: True if return code form the command is 0 and therefore there where no errors and printer/class
exists. Module exits if item_to_check is not defined.
"""
if item_to_check:
cmd = ['lpstat', '-p', item_to_check]
(rc, out, err) = self.process_info_command(cmd)
return rc == 0
else:
self.module.fail_json(msg="Cannot check if a cups item (printer/class) exists that has no name.")
def cups_item_get_cups_options(self):
"""
Returns a list of currently set options for the printer or class.
Uses lpoptions -p command to list all the options, eg:
copies=1 device-uri=socket://127.0.0.1:9100 finishings=3 job-cancel-after=10800
job-hold-until=no-hold job-priority=50 job-sheets=none,none marker-change-time=0 number-up=1
printer-commands=AutoConfigure,Clean,PrintSelfTestPage printer-info='HP LaserJet 4250 Printer Info'
printer-is-accepting-jobs=true printer-is-shared=true printer-location=PrinterLocation
printer-make-and-model='HP LaserJet 4250 Postscript (recommended)' printer-state=3
printer-state-change-time=1463902120 printer-state-reasons=none printer-type=8425668
printer-uri-supported=ipp://localhost/printers/TestPrinter
:returns: A hash of the above info.
"""
cmd = ['lpoptions', '-p', self.name]
(rc, out, err) = self.process_info_command(cmd)
options = {}
for s in shlex.split(out):
kv = s.split('=', 1)
if len(kv) == 1: # If we only have an option name, set it's value to None
options[kv[0]] = None
elif len(kv) == 2: # Otherwise set it's value to what we received
options[kv[0]] = kv[1]
self.cups_current_options = options
return options
def printer_check_cups_options(self):
"""
Creates a hash of the defined options sent to this module.
Polls and retrieves a hash of options currently set for the printer.
Compares them and returns True if the option values are satisfied or False if not satisfied.
:returns: 'True' if the option values match else 'False'.
"""
expected_cups_options = {
'printer-make-and-model': self._printer_get_make_and_model(),
'printer-is-shared': 'true' if self.shared else 'false',
}
if self.info:
expected_cups_options['printer-info'] = self.info
if self.uri:
expected_cups_options['device-uri'] = self.uri
if self.location:
expected_cups_options['printer-location'] = self.location
self.cups_expected_options = expected_cups_options
cups_options = self.cups_item_get_cups_options()
# Comparing expected options as stated above to the options of the actual printer object.
for k in expected_cups_options:
if k not in cups_options:
return False
if expected_cups_options[k] != cups_options[k]:
return False
return True
def class_check_cups_options(self):
"""
Creates a hash of the defined options sent to this module.
Polls and retrieves a hash of options currently set for the class.
Compares them and returns True if the option values are satisfied or False if not satisfied.
:returns: 'True' if the option values match else 'False'.
"""
expected_cups_options = {
'printer-location': self.location,
}
if self.info:
expected_cups_options['printer-info'] = self.info
if self.location:
expected_cups_options['printer-location'] = self.location
self.cups_expected_options = expected_cups_options
options = self.cups_item_get_cups_options()
options_status = True
# Comparing expected options as stated above to the options of the actual class object
for k in expected_cups_options:
if k not in options:
options_status = False
break
if expected_cups_options[k] != options[k]:
options_status = False
break
# Comparing expected class members and actual class members
class_members_status = sorted(self.class_members) == sorted(self.class_get_current_members())
return options_status and class_members_status
def class_get_current_members(self):
"""
Uses the lpstat -c command to get a list of members, eg:
members of class TestClass:
TestPrinter1
TestPrinter2
This is parsed into a list. The first line is skipped.
:returns: A list of members for class specified in the module.
"""
cmd = ['lpstat', '-c', self.name]
(rc, out, err) = self.process_info_command(cmd)
if rc != 0:
self.module.fail_json(
msg="Error occurred while trying to discern class '{0}' members.".format(self.name))
members = []
# Skip first line as it's an information line, it end with a ':'
(info, out) = out.split(':', 1)
out = shlex.split(out)
for m in out:
str.strip(m)
members.append(m)
self.class_current_members = members
return members
def printer_get_specific_options(self):
"""
Returns a hash of printer specific options with its current value, available values and its label.
Runs lpoptions -p <printer_name> -l, eg:
HPCollateSupported/Collation in Printer: True288 *False288
HPOption_500_Sheet_Feeder_Tray3/Tray 3: *True False
HPOption_Duplexer/Duplex Unit: *True False
HPOption_Disk/Printer Disk: True *False
HPOption_PaperPolicy/Paper Matching: *Prompt Scale Crop
HPServicesWeb/Services on the Web: *SupportAndTroubleshooting ProductManuals ColorPrintingAccessUsage
OrderSupplies ShowMeHow
HPServicesUtility/Device Maintenance: *DeviceAndSuppliesStatus
Resolution/Printer Resolution: *600dpi 1200dpi
PageSize/Page Size: *Letter Legal Executive HalfLetter w612h936 4x6 5x7 5x8 A4 A5 A6 RA4 B5 B6 W283H425
w553h765 w522h737 w558h774 DoublePostcard Postcard Env10 Env9 EnvMonarch EnvISOB5 EnvC5 EnvC6 EnvDL
Custom.WIDTHxHEIGHT
InputSlot/Paper Source: *Auto Tray1 Tray2 Tray3 Tray1_Man
Duplex/2-Sided Printing: *None DuplexNoTumble DuplexTumble
Collate/Collate: True *False
This is parsed into a hash with option name as key and value with currently selected option,
label of the option and available values eg:
'HPCollateSupported': 'current': 'False288'
'label': 'Collation in Printer'
'values': 'True288'
'False288'
:returns: A hash of printer options. It includes currently set option and other available options.
"""
cmd = ['lpoptions', '-p', self.name, '-l']
(rc, out, err) = self.process_info_command(cmd)
options = {}
for l in out.splitlines():
remaining = l
(name, remaining) = remaining.split('/', 1)
(label, remaining) = remaining.split(':', 1)
values = shlex.split(remaining)
current_value = None
for v in values:
# Current value is prepended with a '*'
if not v.startswith('*'):
continue
v = v[1:] # Strip the '*' from the value
current_value = v
break
options[name] = {