File Coverage

blib/lib/Rex/Config.pm
Criterion Covered Total %
statement 315 498 63.2
branch 81 162 50.0
condition 43 115 37.3
subroutine 94 142 66.2
pod 107 123 86.9
total 640 1040 61.5


line stmt bran cond sub pod time code
1             #
2             # (c) Jan Gehring
3             #
4              
5             =head1 NAME
6              
7             Rex::Config - Handles Rex configuration
8              
9             =head1 SYNOPSIS
10              
11             use Rex::Config;
12              
13             # set a config option
14             Rex::Config->set_exec_autodie(TRUE);
15              
16             # get value of a config option
17             my $user = Rex::Config->get_user();
18              
19             =head1 DESCRIPTION
20              
21             This module holds all configuration options for Rex, and also allows you to specify your own ones for your modules.
22              
23             Please take a look at L first, which provides convenience wrappers for many of these options.
24              
25             While it's possible to use the methods below to set a configuration option directly, their main intended purpose is to be used as internal plumbing, and to provide an escape hatch in case there are no better alternatives.
26              
27             =head1 EXPORTED METHODS
28              
29             =cut
30              
31             package Rex::Config;
32              
33 105     105   1214454 use v5.12.5;
  105         478  
34 105     105   881 use warnings;
  105         247  
  105         5162  
35              
36             our $VERSION = '1.14.2.3'; # TRIAL VERSION
37              
38 105     105   43438 use Rex::Helper::File::Spec;
  105         298  
  105         3665  
39 105     105   22906 use Rex::Logger;
  105         297  
  105         3286  
40 105     105   48042 use YAML;
  105         922074  
  105         5513  
41 105     105   11002 use Data::Dumper;
  105         111458  
  105         5420  
42 105     105   43898 use Rex::Require;
  105         270  
  105         1025  
43 105     105   39884 use Symbol;
  105         65578  
  105         539468  
44              
45             our (
46             $user, $password,
47             $port, $timeout,
48             $max_connect_fails, $password_auth,
49             $key_auth, $krb5_auth,
50             $public_key, $private_key,
51             $parallelism, $log_filename,
52             $log_facility, $sudo_password,
53             $ca_file, $ca_cert,
54             $ca_key, $path,
55             $no_path_cleanup, $set_param,
56             $environment, $connection_type,
57             $distributor, $template_function,
58             $SET_HANDLER, $HOME_CONFIG,
59             $HOME_CONFIG_YAML, %SSH_CONFIG_FOR,
60             $sudo_without_locales, $sudo_without_sh,
61             $no_tty, $source_global_profile,
62             $source_profile, %executor_for,
63             $allow_empty_groups, $use_server_auth,
64             $tmp_dir, %openssh_opt,
65             $use_cache, $cache_type,
66             $use_sleep_hack, $report_type,
67             $do_reporting, $say_format,
68             $exec_autodie, $verbose_run,
69             $disable_taskname_warning, $proxy_command,
70             $task_call_by_method, $fallback_auth,
71             $register_cmdb_template, $check_service_exists,
72             $set_no_append, $use_net_openssh_if_present,
73             $use_template_ng, $use_rex_kvm_agent,
74             $autodie, $task_chaining_cmdline_args,
75             $waitpid_blocking_sleep_time, $write_utf8_files,
76             $default_auth,
77             );
78              
79             # some defaults
80             %executor_for = (
81             perl => "perl",
82             python => "python",
83             ruby => "ruby",
84             bash => "bash",
85             );
86              
87             =head2 set_autodie
88              
89             =head2 get_autodie
90              
91             Sets and gets the value of the C<$autodie> configuration variable.
92              
93             This controls whether Rex should C if there's an error while executing L.
94              
95             Default is C.
96              
97             =cut
98              
99             sub set_autodie {
100 1     1 1 2 my $class = shift;
101 1         4 $autodie = shift;
102             }
103              
104             sub get_autodie {
105 2     2 1 43 return $autodie;
106             }
107              
108             =head2 set_use_net_openssh_if_present
109              
110             =head2 get_use_net_openssh_if_present
111              
112             Sets and gets the value of the C<$use_net_openssh_if_present> configuration variable.
113              
114             This controls whether Rex should use L for connections if that is available.
115              
116             Default is C.
117              
118             =cut
119              
120             sub set_use_net_openssh_if_present {
121 685     685 1 1683 my $class = shift;
122 685         2132 $use_net_openssh_if_present = shift;
123             }
124              
125             sub get_use_net_openssh_if_present {
126 0     0 1 0 return $use_net_openssh_if_present;
127             }
128              
129             =head2 set_use_rex_kvm_agent
130              
131             =head2 get_use_rex_kvm_agent
132              
133             Sets and gets the value of the C<$use_rex_kvm_agent> configuration variable.
134              
135             This controls whether Rex should setup and use a serial device for the experimental L for managed VMs.
136              
137             Default is C.
138              
139             =cut
140              
141             sub set_use_rex_kvm_agent {
142 0     0 1 0 my $class = shift;
143 0         0 $use_rex_kvm_agent = shift;
144             }
145              
146             sub get_use_rex_kvm_agent {
147 0     0 1 0 return $use_rex_kvm_agent;
148             }
149              
150             =head2 set_use_template_ng
151              
152             =head2 get_use_template_ng
153              
154             Sets and gets the value of the C<$use_template_ng> configuration variable.
155              
156             This controls whether Rex should use L to render templates.
157              
158             Default is C.
159              
160             =cut
161              
162             sub set_use_template_ng {
163 0     0 1 0 my $class = shift;
164 0         0 $use_template_ng = shift;
165             }
166              
167             sub get_use_template_ng {
168 117     117 1 519 return $use_template_ng;
169             }
170              
171             =head2 set_set_no_append
172              
173             =head2 get_set_no_append
174              
175             Sets and gets the value of the C<$set_no_append> configuration variable.
176              
177             This controls whether Rex should overwrite or append values of configuration options when using the L command.
178              
179             Default is C.
180              
181             =cut
182              
183             sub set_set_no_append {
184 1     1 1 2 my $class = shift;
185 1         3 $set_no_append = shift;
186             }
187              
188             sub get_set_no_append {
189 0     0 1 0 return $set_no_append;
190             }
191              
192             =head2 set_check_service_exists
193              
194             =head2 get_check_service_exists
195              
196             Sets and gets the value of the C<$check_service_exists> configuration variable.
197              
198             This controls whether Rex should C early if it is asked to manage a service that doesn't exist.
199              
200             Default is C.
201              
202             =cut
203              
204             sub set_check_service_exists {
205 1     1 1 5 my $class = shift;
206 1         5 $check_service_exists = shift;
207             }
208              
209             sub get_check_service_exists {
210 0     0 1 0 return $check_service_exists;
211             }
212              
213             =head2 set_register_cmdb_template
214              
215             =head2 get_register_cmdb_template
216              
217             Sets and gets the value of the C<$register_cmdb_template> configuration variable.
218              
219             This controls whether Rex should make L data available to be used in templates as variables.
220              
221             Default is C.
222              
223             =cut
224              
225             sub set_register_cmdb_template {
226 3     3 1 31095 my $class = shift;
227 3         15 $register_cmdb_template = shift;
228             }
229              
230             sub get_register_cmdb_template {
231 4     4 1 24 return $register_cmdb_template;
232             }
233              
234             =head2 set_fallback_auth
235              
236             =head2 get_fallback_auth
237              
238             Sets and gets the value of the C<$fallback_auth> configuration variable.
239              
240             This can be used to define an array of hash references, each of them containing L to be tried during connection attempts when the directly specified ones fail.
241              
242             Default is C.
243              
244             =cut
245              
246             sub set_fallback_auth {
247 0     0 1 0 my $class = shift;
248 0         0 $fallback_auth = [@_];
249             }
250              
251             sub get_fallback_auth {
252 0     0 1 0 return $fallback_auth;
253             }
254              
255             =head2 set_task_call_by_method
256              
257             =head2 get_task_call_by_method
258              
259             Sets and gets the value of the C<$task_call_by_method> configuration variable.
260              
261             This controls whether calling tasks as a method is allowed or not.
262              
263             Default is C.
264              
265             =cut
266              
267             sub set_task_call_by_method {
268 1     1 1 3 my $class = shift;
269 1         2 $task_call_by_method = shift;
270             }
271              
272             sub get_task_call_by_method {
273 0     0 1 0 return $task_call_by_method;
274             }
275              
276             =head2 set_disable_taskname_warning
277              
278             =head2 get_disable_taskname_warning
279              
280             Sets and gets the value of the C<$disable_taskname_warning> configuration variable.
281              
282             This controls whether Rex should show or suppress the warning message about task names that can not be used as Perl identifiers.
283              
284             Default is C.
285              
286             =cut
287              
288             sub set_disable_taskname_warning {
289 0     0 1 0 my $class = shift;
290 0         0 $disable_taskname_warning = shift;
291             }
292              
293             sub get_disable_taskname_warning {
294 0     0 1 0 return $disable_taskname_warning;
295             }
296              
297             =head2 set_task_chaining_cmdline_args
298              
299             =head2 get_task_chaining_cmdline_args
300              
301             Sets and gets the value of the C<$task_chaining_cmdline_args> configuration variable.
302              
303             This controls whether Rex should parse task arguments on the command line per task, or should pass all arguments to all tasks.
304              
305             Default is C.
306              
307             =cut
308              
309             sub set_task_chaining_cmdline_args {
310 11     11 1 4256 my $class = shift;
311 11         114 $task_chaining_cmdline_args = shift;
312             }
313              
314             sub get_task_chaining_cmdline_args {
315 40     40 1 298 return $task_chaining_cmdline_args;
316             }
317              
318             =head2 set_verbose_run
319              
320             =head2 get_verbose_run
321              
322             Sets and gets the value of the C<$verbose_run> configuration variable.
323              
324             This controls whether Rex should show verbose output about executed L commands. This means an error message if the command is not found, a warning message if the exit code indicates an error, and an informational message upon success.
325              
326             Default is C.
327              
328             =cut
329              
330             sub set_verbose_run {
331 0     0 1 0 my $class = shift;
332 0         0 $verbose_run = shift;
333             }
334              
335             sub get_verbose_run {
336 11     11 1 110 return $verbose_run;
337             }
338              
339             =head2 set_exec_autodie
340              
341             =head2 get_exec_autodie
342              
343             Sets and gets the value of the C<$exec_autodie> configuration variable.
344              
345             This controls whether Rex should C or not when the exit code of executed L command indicate an error.
346              
347             Default is C.
348              
349             =cut
350              
351             sub set_exec_autodie {
352 14     14 1 29554 my $class = shift;
353 14         67 $exec_autodie = shift;
354             }
355              
356             sub get_exec_autodie {
357 10     10 1 61 return $exec_autodie;
358             }
359              
360             =head2 set_no_path_cleanup
361              
362             =head2 get_no_path_cleanup
363              
364             Sets and gets the value of the C<$no_path_cleanup> configuration variable.
365              
366             This controls whether Rex should use either the default or the explicitly configured C settings (via L or L) when executing commands or not.
367              
368             Default is C.
369              
370             =cut
371              
372             sub set_no_path_cleanup {
373 0     0 1 0 my $class = shift;
374 0         0 $no_path_cleanup = shift;
375             }
376              
377             sub get_no_path_cleanup {
378 920     920 1 4050 return $no_path_cleanup;
379             }
380              
381             =head2 set_source_profile
382              
383             =head2 get_source_profile
384              
385             Sets and gets the value of the C<$source_profile> configuration variable.
386              
387             This controls whether Rex should source shell-specific profile files before executing commands.
388              
389             Default is C.
390              
391             =cut
392              
393             sub set_source_profile {
394 0     0 1 0 my $class = shift;
395 0         0 $source_profile = shift;
396             }
397              
398             sub get_source_profile {
399 0     0 1 0 return $source_profile;
400             }
401              
402             =head2 set_say_format
403              
404             =head2 get_say_format
405              
406             Sets and gets the value of the C<$say_format> configuration variable.
407              
408             This controls the output format of the built-in C command (see also L).
409              
410             Default is C.
411              
412             =cut
413              
414             sub set_say_format {
415 0     0 1 0 my $class = shift;
416 0         0 $say_format = shift;
417             }
418              
419             sub get_say_format {
420 0     0 1 0 return $say_format;
421             }
422              
423             =head2 set_do_reporting
424              
425             =head2 get_do_reporting
426              
427             Sets and gets the value of the C<$do_reporting> configuration variable.
428              
429             This controls whether Rex should do reporting on executed resources where it is supported. This only affects the data structures returned internally.
430              
431             Default is C.
432              
433             =cut
434              
435             sub set_do_reporting {
436 5     5 1 17 my $class = shift;
437 5         22 $do_reporting = shift;
438             }
439              
440             sub get_do_reporting {
441 0     0 1 0 return $do_reporting;
442             }
443              
444             =head2 set_report_type
445              
446             =head2 get_report_type
447              
448             Sets and gets the value of the C<$report_type> configuration variable, which can also be controlled via the C environment variable.
449              
450             This selects the reporting type (format) Rex should use, e.g. C<'YAML'>.
451              
452             Default is C.
453              
454             =cut
455              
456             sub set_report_type {
457 1     1 1 5 my $class = shift;
458 1         10 $report_type = shift;
459             }
460              
461             sub get_report_type {
462 40 50   40 1 264 if ( exists $ENV{REX_REPORT_TYPE} ) {
463 0         0 return $ENV{REX_REPORT_TYPE};
464             }
465              
466 40         449 return $report_type;
467             }
468              
469             =head2 set_sleep_hack
470              
471             =head2 get_sleep_hack
472              
473             Sets and gets the value of the C<$sleep_hack> configuration variable.
474              
475             This controls whether Rex should use or not an extra 10 ns long sleep after executed commands.
476              
477             This might help working around an issue when Rex runs inside a KVM virtualized host and L/L is used to connect to another VM on the same hardware.
478              
479             Default is C.
480              
481             =cut
482              
483             sub set_sleep_hack {
484 0     0 1 0 my $class = shift;
485 0         0 $use_sleep_hack = shift;
486             }
487              
488             sub get_sleep_hack {
489 0     0 1 0 return $use_sleep_hack;
490             }
491              
492             =head2 set_cache_type
493              
494             =head2 get_cache_type
495              
496             Sets and gets the value of the C<$cache_type> configuration variable, which can also be controlled via the C environment variable.
497              
498             This selects the cache type Rex should use, e.g. C<'YAML'>.
499              
500             Default is C<'Base'>.
501              
502             =cut
503              
504             sub set_cache_type {
505 0     0 1 0 my $class = shift;
506 0         0 $cache_type = shift;
507             }
508              
509             sub get_cache_type {
510 113 50   113 1 627 if ( exists $ENV{REX_CACHE_TYPE} ) {
511 0         0 return $ENV{REX_CACHE_TYPE};
512             }
513              
514 113   50     1431 return $cache_type || "Base";
515             }
516              
517             =head2 set_use_cache
518              
519             =head2 get_use_cache
520              
521             Sets and gets the value of the C<$use_cache> configuration variable.
522              
523             This controls whether Rex should use caching or not for runtime information like CMDB contents, hardware and operating system information, or the shell type that is being used to execute commands on the managed endpoint.
524              
525             Default is C.
526              
527             =cut
528              
529             sub set_use_cache {
530 2     2 1 21418 my $class = shift;
531 2         19 $use_cache = shift;
532             }
533              
534             sub get_use_cache {
535 1253     1253 1 6031 return $use_cache;
536             }
537              
538             =head2 set_openssh_opt
539              
540             =head2 get_openssh_opt
541              
542             Sets and gets the value of the C<$openssh_opt> configuration variable, which holds a hash of the SSH configuration options used for the connection. See the L for the available options.
543              
544             Rex::Config->set_openssh_opt( $option => $value, );
545              
546             There is a custom option named C specific to Rex, which can be used to pass a hash reference describing the Lnew($host,-%opts)> for the underlying L object:
547              
548             Rex::Config->set_openssh_opt( initialize_options => { $parameter => $value, } );
549              
550             Default is C.
551              
552             =cut
553              
554             sub set_openssh_opt {
555 0     0 1 0 my ( $class, %opt ) = @_;
556              
557 0         0 for my $key ( keys %opt ) {
558 0 0       0 if ( !defined $opt{$key} ) {
559 0         0 $openssh_opt{$key} = undef;
560 0         0 delete $openssh_opt{$key};
561 0         0 next;
562             }
563              
564 0         0 $openssh_opt{$key} = $opt{$key};
565             }
566             }
567              
568             sub get_openssh_opt {
569 0     0 1 0 return %openssh_opt;
570             }
571              
572             =head2 set_sudo_without_locales
573              
574             =head2 get_sudo_without_locales
575              
576             Sets and gets the value of the C<$sudo_without_locales> configuration variable.
577              
578             This controls whether Rex should execute L commands without setting any locales via the C environment variable.
579              
580             B if the locale is something else than C or C, then things will break!
581              
582             Default is C.
583              
584             =cut
585              
586             sub set_sudo_without_locales {
587 0     0 1 0 my $class = shift;
588 0         0 $sudo_without_locales = shift;
589             }
590              
591             sub get_sudo_without_locales {
592 0     0 1 0 return $sudo_without_locales;
593             }
594              
595             =head2 set_sudo_without_sh
596              
597             =head2 get_sudo_without_sh
598              
599             Sets and gets the value of the C<$sudo_without_sh> configuration variable.
600              
601             This controls whether Rex should run L commands without C. This might break things.
602              
603             Default is C.
604              
605             =cut
606              
607             sub set_sudo_without_sh {
608 0     0 1 0 my $class = shift;
609 0         0 $sudo_without_sh = shift;
610             }
611              
612             sub get_sudo_without_sh {
613 0     0 1 0 return $sudo_without_sh;
614             }
615              
616             =head2 set_executor_for
617              
618             =head2 get_executor_for
619              
620             Sets and gets the keys and values of the C<%executor_for> configuration variable.
621              
622             This sets the executor for a given file type when using the C function of L module.
623              
624             Default is:
625              
626             (
627             perl => 'perl',
628             python => 'python',
629             ruby => 'ruby',
630             bash => 'bash',
631             )
632              
633             =cut
634              
635             sub set_executor_for {
636 0     0 1 0 my $class = shift;
637 0         0 my $for = shift;
638 0         0 my $e = shift;
639              
640 0         0 $executor_for{$for} = $e;
641             }
642              
643             sub get_executor_for {
644 0     0 1 0 my $class = shift;
645 0         0 my $e = shift;
646              
647 0         0 return $executor_for{$e};
648             }
649              
650             =head2 set_tmp_dir
651              
652             =head2 get_tmp_dir
653              
654             Sets and gets the value of the C<$tmp_dir> configuration variable.
655              
656             This controls which directory Rex should use for temporary files.
657              
658             Default is determined by the following logic:
659              
660             =over 4
661              
662             =item * try to use what C<< File::Spec->tmpdir >> would return on the managed endpoint
663              
664             =item * fall back to C<'/tmp'>
665              
666             =back
667              
668             =cut
669              
670             sub set_tmp_dir {
671 0     0 1 0 my ( $class, $dir ) = @_;
672 0 0       0 if ( $class eq "Rex::Config" ) {
673 0         0 $tmp_dir = $dir;
674             }
675             else {
676 0         0 $tmp_dir = $class;
677             }
678             }
679              
680             sub get_tmp_dir {
681 922     922 1 4756 my $cache = Rex::get_cache();
682 922 100       7123 if ( my $cached_tmp = $cache->get("tmpdir") ) {
683 8         216 return $cached_tmp;
684             }
685              
686 914 50       2837 if ( !$tmp_dir ) {
687 914 50       2908 if ( my $ssh = Rex::is_ssh() ) {
688 0         0 my $exec;
689 0 0       0 if ( Rex::is_sudo() ) {
690 0 0       0 if ( ref $ssh eq "Net::OpenSSH" ) {
691 0         0 $exec = Rex::Interface::Exec->create("OpenSSH");
692             }
693             else {
694 0         0 $exec = Rex::Interface::Exec->create("SSH");
695             }
696             }
697             else {
698 0         0 $exec = Rex::Interface::Exec->create;
699             }
700 0         0 my ($out) =
701             $exec->exec("perl -MFile::Spec -le 'print File::Spec->tmpdir'");
702              
703 0 0 0     0 if ( $? == 0 && $out ) {
704 0         0 $out =~ s/[\r\n]//gms;
705              
706 0         0 $cache->set( "tmpdir", $out );
707 0         0 return $out;
708             }
709 0         0 $cache->set( "tmpdir", "/tmp" );
710 0         0 return "/tmp";
711             }
712             else {
713 914         25920 $cache->set( "tmpdir", Rex::Helper::File::Spec->tmpdir );
714 914         6459 return Rex::Helper::File::Spec->tmpdir;
715             }
716             }
717 0         0 return $tmp_dir;
718             }
719              
720             =head2 set_path
721              
722             =head2 get_path
723              
724             Sets and gets the value of the C<$path> configuration variable.
725              
726             This controls which C Rex should use when executing commands via the L module.
727              
728             The value should be set as an array reference, and will be dereferenced as such before returned by C. The L command is a convenience wrapper for C, and accepts an array.
729              
730             Default is
731              
732             qw(
733             /bin
734             /sbin
735             /usr/bin
736             /usr/sbin
737             /usr/local/bin
738             /usr/local/sbin
739             /usr/pkg/bin
740             /usr/pkg/sbin
741             )
742              
743             =cut
744              
745             sub set_path {
746 1     1 1 3 my $class = shift;
747 1         3 $path = shift;
748             }
749              
750             sub get_path {
751 921 100   921 1 3060 if ( !$path ) {
752             return (
753 920         11580 "/bin", "/sbin", "/usr/bin",
754             "/usr/sbin", "/usr/local/bin", "/usr/local/sbin",
755             "/usr/pkg/bin", "/usr/pkg/sbin"
756             );
757             }
758 1         2 return @{$path};
  1         8  
759             }
760              
761             =head2 set_user
762              
763             =head2 get_user
764              
765             Sets and gets the value of the C<$user> configuration variable, which also can be set via the C environment variable.
766              
767             This controls which L Rex should use for authentication.
768              
769             Default is determined by the following logic:
770              
771             =over 4
772              
773             =item * value of C environment variable
774              
775             =item * user set by L command
776              
777             =item * user running Rex
778              
779             =back
780              
781             =cut
782              
783             sub set_user {
784 8     8 1 29 my $class = shift;
785 8         33 $user = shift;
786             }
787              
788             sub has_user {
789 70     70 0 293 my $class = shift;
790 70         838 return $user;
791             }
792              
793             sub get_user {
794 279     279 1 930 my $class = shift;
795              
796 279 100       1463 if ( exists $ENV{REX_USER} ) {
797 1         7 return $ENV{REX_USER};
798             }
799              
800 278 100       1214 if ($user) {
801 44         160 return $user;
802             }
803              
804 234 50       1725 if ( $^O =~ m/^MSWin/ ) {
805 0         0 return getlogin;
806             }
807             else {
808 234         61581 return scalar getpwuid($<);
809             }
810             }
811              
812             =head2 set_password
813              
814             =head2 get_password
815              
816             Sets and gets the value of the C<$password> configuration variable, which also can be set via the C environment variable.
817              
818             This controls what L Rex should use for authentication or as passphrase when using private keys.
819              
820             Default is C.
821              
822             =cut
823              
824             sub set_password {
825 8     8 1 21 my $class = shift;
826 8         24 $password = shift;
827             }
828              
829             sub get_password {
830 278     278 1 1014 my $class = shift;
831              
832 278 50       1129 if ( exists $ENV{REX_PASSWORD} ) {
833 0         0 return $ENV{REX_PASSWORD};
834             }
835              
836 278         2362 return $password;
837             }
838              
839             =head2 set_port
840              
841             =head2 get_port
842              
843             Sets and gets the value of the C<$port> configuration variable.
844              
845             This controls which L Rex should connect to.
846              
847             C accepts an optional C<< server => $server >> argument to return the C setting for the given C<$server> as optionally set in group files.
848              
849             Default is C.
850              
851             =cut
852              
853             sub set_port {
854 0     0 1 0 my $class = shift;
855 0         0 $port = shift;
856             }
857              
858             sub get_port {
859 88     88 1 216 my $class = shift;
860 88         308 my $param = {@_};
861              
862 88 0 33     463 if ( exists $param->{server}
      33        
863             && exists $SSH_CONFIG_FOR{ $param->{server} }
864             && exists $SSH_CONFIG_FOR{ $param->{server} }->{port} )
865             {
866 0         0 return $SSH_CONFIG_FOR{ $param->{server} }->{port};
867             }
868              
869 88         408 return $port;
870             }
871              
872             =head2 set_sudo_password
873              
874             =head2 get_sudo_password
875              
876             Sets and gets the value of the C<$sudo_password> configuration variable, which can also be controlled via the C environment variable.
877              
878             This controls what L Rex should use.
879              
880             Default is determined by the following logic:
881              
882             =over 4
883              
884             =item * value of C environment variable
885              
886             =item * sudo password set by the L command
887              
888             =item * password set by the L command
889              
890             =item * empty string (C<''>)
891              
892             =back
893              
894             =cut
895              
896             sub set_sudo_password {
897 1     1 1 3 my $class = shift;
898 1         4 $sudo_password = shift;
899             }
900              
901             sub get_sudo_password {
902 296     296 1 755 my $class = shift;
903              
904 296 50       927 if ( exists $ENV{REX_SUDO_PASSWORD} ) {
905 0         0 return $ENV{REX_SUDO_PASSWORD};
906             }
907              
908 296 100       1211 if ($sudo_password) {
    50          
909 1         6 return $sudo_password;
910             }
911             elsif ( !defined $sudo_password ) {
912 295         2517 return "";
913             }
914             else {
915 0         0 return $password;
916             }
917              
918 0         0 return "";
919             }
920              
921             =head2 set_source_global_profile
922              
923             =head2 get_source_global_profile
924              
925             Sets and gets the value of the C<$source_global_profile> configuration variable.
926              
927             This controls whether Rex should source C before executing commands.
928              
929             Default is C.
930              
931             =cut
932              
933             sub set_source_global_profile {
934 0     0 1 0 my $class = shift;
935 0         0 $source_global_profile = shift;
936             }
937              
938             sub get_source_global_profile {
939 1151     1151 1 4501 return $source_global_profile;
940             }
941              
942             =head2 set_max_connect_fails
943              
944             =head2 get_max_connect_fails
945              
946             Sets and gets the value of the C<$max_connect_fails> configuration variable.
947              
948             This controls how many times Rex should retry to connect before giving up.
949              
950             C accepts an optional C<< server => $server >> argument to C setting for the given C<$server> as optionally set in group files.
951              
952             Default is C.
953              
954             =cut
955              
956             sub set_max_connect_fails {
957 1     1 1 5 my $class = shift;
958 1         4 $max_connect_fails = shift;
959             }
960              
961             sub get_max_connect_fails {
962 1     1 1 2 my $class = shift;
963 1         2 my $param = {@_};
964              
965 1 0 33     5 if ( exists $param->{server}
      33        
966             && exists $SSH_CONFIG_FOR{ $param->{server} }
967             && exists $SSH_CONFIG_FOR{ $param->{server} }->{connectionattempts} )
968             {
969 0         0 return $SSH_CONFIG_FOR{ $param->{server} }->{connectionattempts};
970             }
971              
972 1   50     7 return $max_connect_fails || 3;
973             }
974              
975             =head2 set_proxy_command
976              
977             =head2 get_proxy_command
978              
979             Sets and gets the value of the C<$proxy_command> configuration variable.
980              
981             This controls the SSH ProxyCommand Rex should set for connections when L is used.
982              
983             C accepts an optional C<< server => $server >> argument to return the C setting for the given C<$server> as optionally set in group files.
984              
985             Default is C.
986              
987             =cut
988              
989             sub set_proxy_command {
990 0     0 1 0 my $class = shift;
991 0         0 $proxy_command = shift;
992             }
993              
994             sub get_proxy_command {
995 0     0 1 0 my $class = shift;
996 0         0 my $param = {@_};
997              
998 0 0 0     0 if ( exists $param->{server}
      0        
999             && exists $SSH_CONFIG_FOR{ $param->{server} }
1000             && exists $SSH_CONFIG_FOR{ $param->{server} }->{proxycommand} )
1001             {
1002 0         0 return $SSH_CONFIG_FOR{ $param->{server} }->{proxycommand};
1003             }
1004              
1005 0         0 return $proxy_command;
1006             }
1007              
1008             =head2 set_timeout
1009              
1010             =head2 get_timeout
1011              
1012             Sets and gets the value of the C<$timeout> configuration variable.
1013              
1014             This controls how many seconds Rex should wait for connections to succeed when using SSH or L.
1015              
1016             C accepts an optional C<< server => $server >> argument to return the C setting for the given C<$server> as optionally set in group files.
1017              
1018             Default is C.
1019              
1020             =cut
1021              
1022             sub set_timeout {
1023 7     7 1 25 my $class = shift;
1024 7         23 $timeout = shift;
1025             }
1026              
1027             sub get_timeout {
1028 1     1 1 2 my $class = shift;
1029 1         3 my $param = {@_};
1030              
1031 1 0 33     6 if ( exists $param->{server}
      33        
1032             && exists $SSH_CONFIG_FOR{ $param->{server} }
1033             && exists $SSH_CONFIG_FOR{ $param->{server} }->{connecttimeout} )
1034             {
1035 0         0 return $SSH_CONFIG_FOR{ $param->{server} }->{connecttimeout};
1036             }
1037              
1038 1   50     6 return $timeout || 2;
1039             }
1040              
1041             =head2 set_password_auth
1042              
1043             =head2 get_password_auth
1044              
1045             Sets and gets the value of the C<$password_auth> configuration variable, which can also be set by setting the C environment variable to C.
1046              
1047             This controls whether Rex should use the L method.
1048              
1049             Default is C.
1050              
1051             =cut
1052              
1053             sub set_password_auth {
1054 3     3 1 10 my $class = shift;
1055 3         9 $key_auth = 0;
1056 3         6 $krb5_auth = 0;
1057 3   50     13 $password_auth = shift || 1;
1058             }
1059              
1060             sub get_password_auth {
1061 71 50 33 71 1 411 if ( exists $ENV{REX_AUTH_TYPE} && $ENV{REX_AUTH_TYPE} eq "pass" ) {
1062 0         0 return 1;
1063             }
1064 71         470 return $password_auth;
1065             }
1066              
1067             =head2 set_key_auth
1068              
1069             =head2 get_key_auth
1070              
1071             Sets and gets the value of the C<$key_auth> configuration variable, which can also be set by setting the C environment variable to C.
1072              
1073             This controls whether Rex should use the L method.
1074              
1075             Default is C.
1076              
1077             =cut
1078              
1079             sub set_key_auth {
1080 1     1 1 3 my $class = shift;
1081 1         2 $password_auth = 0;
1082 1         4 $krb5_auth = 0;
1083 1   50     5 $key_auth = shift || 1;
1084             }
1085              
1086             sub get_key_auth {
1087 63 50 33 63 1 284 if ( exists $ENV{REX_AUTH_TYPE} && $ENV{REX_AUTH_TYPE} eq "key" ) {
1088 0         0 return 1;
1089             }
1090 63         267 return $key_auth;
1091             }
1092              
1093             =head2 set_krb5_auth
1094              
1095             =head2 get_krb5_auth
1096              
1097             Sets and gets the value of the C<$krb5_auth> configuration variable, which can also be set by setting the C environment variable to C.
1098              
1099             This controls whether Rex should use the L authentication method.
1100              
1101             Default is C.
1102              
1103             =cut
1104              
1105             sub set_krb5_auth {
1106 0     0 1 0 my $class = shift;
1107 0         0 $password_auth = 0;
1108 0         0 $key_auth = 0;
1109 0   0     0 $krb5_auth = shift || 1;
1110             }
1111              
1112             sub get_krb5_auth {
1113 70 50 33 70 1 393 if ( exists $ENV{REX_AUTH_TYPE} && $ENV{REX_AUTH_TYPE} eq "krb5" ) {
1114 0         0 return 1;
1115             }
1116 70         554 return $krb5_auth;
1117             }
1118              
1119             =head2 set_public_key
1120              
1121             =head2 get_public_key
1122              
1123             Sets and gets the value of the C<$public_key> configuration variable.
1124              
1125             This controls which L Rex should use when using L for connections.
1126              
1127             Default is C.
1128              
1129             =cut
1130              
1131             sub set_public_key {
1132 7     7 1 37 my $class = shift;
1133 7         18 $public_key = shift;
1134             }
1135              
1136             sub has_public_key {
1137 88     88 0 305 return get_public_key();
1138             }
1139              
1140             sub get_public_key {
1141 472 50   472 1 1373 if ( exists $ENV{REX_PUBLIC_KEY} ) { return $ENV{REX_PUBLIC_KEY}; }
  0         0  
1142 472 100       1058 if ($public_key) {
1143 156         483 return $public_key;
1144             }
1145              
1146 316         2194 return;
1147             }
1148              
1149             =head2 set_private_key
1150              
1151             =head2 get_private_key
1152              
1153             Sets and gets the value of the C<$private_key> configuration variable.
1154              
1155             This controls which L Rex should use with L or when using L for connections.
1156              
1157             Default is C.
1158              
1159             =cut
1160              
1161             sub set_private_key {
1162 7     7 1 18 my $class = shift;
1163 7         21 $private_key = shift;
1164             }
1165              
1166             sub has_private_key {
1167 88     88 0 702 return get_private_key();
1168             }
1169              
1170             sub get_private_key {
1171 472 50   472 1 1348 if ( exists $ENV{REX_PRIVATE_KEY} ) { return $ENV{REX_PRIVATE_KEY}; }
  0         0  
1172 472 100       1139 if ($private_key) {
1173 156         551 return $private_key;
1174             }
1175              
1176 316         3083 return;
1177             }
1178              
1179             =head2 set_parallelism
1180              
1181             =head2 get_parallelism
1182              
1183             Sets and gets the value of the C<$parallelism> configuration variable.
1184              
1185             This controls how many hosts Rex should connect to in L.
1186              
1187             Default is C<1>.
1188              
1189             =cut
1190              
1191             sub set_parallelism {
1192 2     2 1 5 my $class = shift;
1193 2         3 $parallelism = $_[0];
1194             }
1195              
1196             sub get_parallelism {
1197 139     139 1 806 my $class = shift;
1198 139   100     1919 return $parallelism || 1;
1199             }
1200              
1201             =head2 set_log_filename
1202              
1203             =head2 get_log_filename
1204              
1205             Sets and gets the value of the C<$log_filename> configuration variable.
1206              
1207             This controls which file Rex should use for L.
1208              
1209             Default is C.
1210              
1211             =cut
1212              
1213             sub set_log_filename {
1214 2     2 1 15714 my $class = shift;
1215 2         8 $log_filename = shift;
1216             }
1217              
1218             sub get_log_filename {
1219 185     185 1 1986 my $class = shift;
1220 185         4252 return $log_filename;
1221             }
1222              
1223             =head2 set_log_facility
1224              
1225             =head2 get_log_facility
1226              
1227             Sets and gets the value of the C<$log_facility> configuration variable.
1228              
1229             This controls which log facility Rex should use when L to syslog.
1230              
1231             Default is C<'local0'>.
1232              
1233             =cut
1234              
1235             sub set_log_facility {
1236 0     0 1 0 my $class = shift;
1237 0         0 $log_facility = shift;
1238             }
1239              
1240             sub get_log_facility {
1241 100     100 1 808 my $class = shift;
1242 100   50     2764 return $log_facility || "local0";
1243             }
1244              
1245             =head2 set_environment
1246              
1247             =head2 get_environment
1248              
1249             Sets and gets the value of the C<$environment> configuration variable.
1250              
1251             This controls which L Rex should use.
1252              
1253             Default is C<''>.
1254              
1255             =cut
1256              
1257             sub set_environment {
1258 0     0 1 0 my ( $class, $env ) = @_;
1259 0         0 $environment = $env;
1260             }
1261              
1262             sub get_environment {
1263 270   50 270 1 4176 return $environment || "";
1264             }
1265              
1266             sub get_ssh_config_username {
1267 36     36 0 199 my $class = shift;
1268 36         219 my $param = {@_};
1269              
1270 36 0 33     733 if ( exists $param->{server}
      33        
1271             && exists $SSH_CONFIG_FOR{ $param->{server} }
1272             && exists $SSH_CONFIG_FOR{ $param->{server} }->{user} )
1273             {
1274 0         0 return $SSH_CONFIG_FOR{ $param->{server} }->{user};
1275             }
1276              
1277 36         357 return 0;
1278             }
1279              
1280             sub get_ssh_config_hostname {
1281 0     0 0 0 my $class = shift;
1282 0         0 my $param = {@_};
1283              
1284 0 0 0     0 if ( exists $param->{server}
      0        
1285             && exists $SSH_CONFIG_FOR{ $param->{server} }
1286             && exists $SSH_CONFIG_FOR{ $param->{server} }->{hostname} )
1287             {
1288 0 0       0 if ( $SSH_CONFIG_FOR{ $param->{server} }->{hostname} =~ m/^\%h(\.(.*))?/ ) {
1289 0         0 return $param->{server} . $1;
1290             }
1291             else {
1292 0         0 return $SSH_CONFIG_FOR{ $param->{server} }->{hostname};
1293             }
1294             }
1295              
1296 0         0 return 0;
1297             }
1298              
1299             sub get_ssh_config_private_key {
1300 39     39 0 145 my $class = shift;
1301 39         184 my $param = {@_};
1302              
1303 39 0 33     658 if ( exists $param->{server}
      33        
1304             && exists $SSH_CONFIG_FOR{ $param->{server} }
1305             && exists $SSH_CONFIG_FOR{ $param->{server} }->{identityfile} )
1306             {
1307              
1308 0         0 my $file = $SSH_CONFIG_FOR{ $param->{server} }->{identityfile};
1309 0         0 my $home_dir = _home_dir();
1310 0         0 $file =~ s/^~/$home_dir/;
1311              
1312 0         0 return $file;
1313             }
1314              
1315 39         409 return 0;
1316             }
1317              
1318             sub get_ssh_config_public_key {
1319 39     39 0 157 my $class = shift;
1320 39         222 my $param = {@_};
1321              
1322 39 0 33     681 if ( exists $param->{server}
      33        
1323             && exists $SSH_CONFIG_FOR{ $param->{server} }
1324             && exists $SSH_CONFIG_FOR{ $param->{server} }->{identityfile} )
1325             {
1326 0         0 my $file = $SSH_CONFIG_FOR{ $param->{server} }->{identityfile} . ".pub";
1327 0         0 my $home_dir = _home_dir();
1328 0         0 $file =~ s/^~/$home_dir/;
1329 0         0 return $file;
1330             }
1331              
1332 39         406 return 0;
1333             }
1334              
1335             sub get_connection_type {
1336 217     217 0 514 my $class = shift;
1337              
1338 217 50 66     2139 if ( $^O !~ m/^MSWin/ && !$connection_type && $use_net_openssh_if_present ) {
      66        
1339 55         210 my $has_net_openssh = 0;
1340 55         179 eval {
1341 55         1070 Net::OpenSSH->require;
1342 55         533 Net::SFTP::Foreign->require;
1343 55         183 $has_net_openssh = 1;
1344 55         141 1;
1345             };
1346              
1347 55 50       270 if ($has_net_openssh) {
1348 55         439 Rex::Logger::debug(
1349             "Found Net::OpenSSH and Net::SFTP::Foreign - using it as default");
1350 55         357 $connection_type = "OpenSSH";
1351 55         1018 return "OpenSSH";
1352             }
1353             }
1354              
1355 162 50       423 if ( !$connection_type ) {
1356 0         0 my $has_net_ssh2 = 0;
1357 0         0 eval {
1358 0         0 Net::SSH2->require;
1359 0         0 $has_net_ssh2 = 1;
1360 0         0 1;
1361             };
1362              
1363 0 0       0 if ($has_net_ssh2) {
1364 0         0 $connection_type = "SSH";
1365 0         0 return "SSH";
1366             }
1367             }
1368              
1369 162   50     1609 return $connection_type || "SSH";
1370             }
1371              
1372             sub get_ca {
1373 0     0 0 0 my $class = shift;
1374 0   0     0 return $ca_file || "";
1375             }
1376              
1377             sub get_ca_cert {
1378 0     0 0 0 my $class = shift;
1379 0   0     0 return $ca_cert || "";
1380             }
1381              
1382             sub get_ca_key {
1383 0     0 0 0 my $class = shift;
1384 0   0     0 return $ca_key || "";
1385             }
1386              
1387             =head2 set_distributor
1388              
1389             =head2 get_distributor
1390              
1391             Sets and gets the value of the C<$distributor> configuration variable.
1392              
1393             This controls which method Rex should use for distributing tasks for parallel execution.
1394              
1395             Default is C<'Base'>.
1396              
1397             =cut
1398              
1399             sub set_distributor {
1400 9     9 1 11862 my $class = shift;
1401 9         27 $distributor = shift;
1402             }
1403              
1404             sub get_distributor {
1405 67     67 1 54215 my $class = shift;
1406 67   100     545 return $distributor || "Base";
1407             }
1408              
1409             =head2 set_template_function
1410              
1411             =head2 get_template_function
1412              
1413             Sets and gets the value of the C<$template_function> configuration variable.
1414              
1415             This controls the function to be used for rendering L. The value should be a subroutine reference that will be called with passing two scalar references as positional arguments: first is template content, second is template variables.
1416              
1417             Default is determined by the following logic:
1418              
1419             =over 4
1420              
1421             =item * if L is loadable and L is true, use that
1422              
1423             =item * fall back to L otherwise
1424              
1425             =back
1426              
1427             =cut
1428              
1429             sub set_template_function {
1430 0     0 1 0 my $class = shift;
1431 0         0 ($template_function) = @_;
1432             }
1433              
1434             sub get_template_function {
1435 117 50   117 1 579 if ( ref($template_function) eq "CODE" ) {
1436             return sub {
1437 0     0   0 my ( $content, $template_vars ) = @_;
1438             $template_vars = {
1439             Rex::Commands::task()->get_opts,
1440             (
1441             Rex::Resource->is_inside_resource
1442 0         0 ? %{ Rex::Resource->get_current_resource()->get_all_parameters }
1443             : ()
1444             ),
1445 0 0       0 %{ $template_vars || {} }
  0 0       0  
    0          
1446             }
1447             if ( Rex::Commands::task() );
1448 0         0 return $template_function->( $content, $template_vars );
1449 0         0 };
1450             }
1451              
1452 117 50 33     1439 if ( Rex::Template::NG->is_loadable && get_use_template_ng() ) {
1453              
1454             # new template engine
1455             return sub {
1456 0     0   0 my ( $content, $template_vars ) = @_;
1457             $template_vars = {
1458             Rex::Commands::task()->get_opts,
1459             (
1460             Rex::Resource->is_inside_resource
1461 0         0 ? %{ Rex::Resource->get_current_resource()->get_all_parameters }
1462             : ()
1463             ),
1464 0 0       0 %{ $template_vars || {} }
  0 0       0  
    0          
1465             }
1466             if ( Rex::Commands::task() );
1467 0         0 Rex::Template::NG->require;
1468 0         0 my $t = Rex::Template::NG->new;
1469 0         0 return $t->parse( $content, %{$template_vars} );
  0         0  
1470 0         0 };
1471             }
1472              
1473             return sub {
1474 117     117   479 my ( $content, $template_vars ) = @_;
1475             $template_vars = {
1476             Rex::Commands::task()->get_opts,
1477             (
1478             Rex::Resource->is_inside_resource
1479 12         88 ? %{ Rex::Resource->get_current_resource()->get_all_parameters }
1480             : ()
1481             ),
1482 117 100       965 %{ $template_vars || {} }
  29 50       360  
    100          
1483             }
1484             if ( Rex::Commands::task() );
1485 105     105   53273 use Rex::Template;
  105         297  
  105         1195  
1486 117         1347 my $template = Rex::Template->new;
1487 117         777 return $template->parse( $content, $template_vars );
1488 117         1581 };
1489             }
1490              
1491             =head2 set_no_tty
1492              
1493             =head2 get_no_tty
1494              
1495             Sets and gets the value of the C<$no_tty> configuration variable.
1496              
1497             This controls whether Rex should request a terminal when using L or allocate a pseudo-tty for the remote process when using L.
1498              
1499             Default is C.
1500              
1501             =cut
1502              
1503             sub set_no_tty {
1504 4     4 1 5213 shift;
1505 4         41 $no_tty = shift;
1506             }
1507              
1508             sub get_no_tty {
1509 1151     1151 1 6304 return $no_tty;
1510             }
1511              
1512             =head2 set_allow_empty_groups
1513              
1514             =head2 get_allow_empty_groups
1515              
1516             Sets and gets the value of the C<$allow_empty_groups> configuration variable.
1517              
1518             This controls whether Rex should allow empty L or not.
1519              
1520             Default is C<0>.
1521              
1522             =cut
1523              
1524             sub set_allow_empty_groups {
1525 0     0 1 0 my ( $class, $set ) = @_;
1526 0 0       0 if ($set) {
1527 0         0 $allow_empty_groups = 1;
1528             }
1529             else {
1530 0         0 $allow_empty_groups = 0;
1531             }
1532             }
1533              
1534             sub get_allow_empty_groups {
1535 0 0   0 1 0 if ($allow_empty_groups) {
1536 0         0 return 1;
1537             }
1538              
1539 0         0 return 0;
1540             }
1541              
1542             =head2 set_use_server_auth
1543              
1544             =head2 get_use_server_auth
1545              
1546             Sets and gets the value of the C<$use_server_auth> configuration variable.
1547              
1548             This controls whether Rex should use server-specific authentication information from group files.
1549              
1550             Default is C<0>.
1551              
1552             =cut
1553              
1554             sub set_use_server_auth {
1555 1     1 1 1789 my ( $class, $set ) = @_;
1556 1 50       5 if ($set) {
1557 1         3 $use_server_auth = 1;
1558             }
1559             else {
1560 0         0 $use_server_auth = 0;
1561             }
1562             }
1563              
1564             sub get_use_server_auth {
1565 105 100   105 1 530 if ($use_server_auth) {
1566 4         15 return 1;
1567             }
1568              
1569 101         384 return 0;
1570             }
1571              
1572             =head2 set_waitpid_blocking_sleep_time
1573              
1574             =head2 get_waitpid_blocking_sleep_time
1575              
1576             Sets and gets the value of the C<$waitpid_blocking_sleep_time> configuration variable.
1577              
1578             This controls how many seconds Rex should sleep between checking forks.
1579              
1580             Default is C<0.1>.
1581              
1582             =cut
1583              
1584             sub set_waitpid_blocking_sleep_time {
1585 1     1 1 4 my $self = shift;
1586 1         4 $waitpid_blocking_sleep_time = shift;
1587             }
1588              
1589             sub get_waitpid_blocking_sleep_time {
1590 6512   100 6512 1 652236632 return $waitpid_blocking_sleep_time // 0.1;
1591             }
1592              
1593             =head2 set_write_utf8_files
1594              
1595             =head2 get_write_utf8_files
1596              
1597             Sets and gets the value of the C<$write_utf8_files> configuration variable.
1598              
1599             This controls whether Rex should force C encoding when writing files.
1600              
1601             Default is C.
1602              
1603             =cut
1604              
1605             sub set_write_utf8_files {
1606 0     0 1 0 my $self = shift;
1607 0         0 $write_utf8_files = shift;
1608             }
1609              
1610             sub get_write_utf8_files {
1611 268     268 1 885 return $write_utf8_files;
1612             }
1613              
1614             =head2 set_default_auth
1615              
1616             =head2 get_default_auth
1617              
1618             Sets and gets the value of the C<$default_auth> configuration variable.
1619              
1620             This controls whether Rex should attach default authentication info to tasks.
1621              
1622             Default is C<1>.
1623              
1624             =cut
1625              
1626             sub set_default_auth {
1627 3     3 1 12 my $self = shift;
1628 3         9 $default_auth = shift;
1629             }
1630              
1631             sub get_default_auth {
1632 61   100 61 1 555 return $default_auth // 1;
1633             }
1634              
1635             =head2 register_set_handler($handler_name, $code)
1636              
1637             Register a handler that gets called by I.
1638              
1639             Rex::Config->register_set_handler("foo", sub {
1640             my ($value) = @_;
1641             print "The user set foo -> $value\n";
1642             });
1643              
1644             And now you can use this handler in your I like this:
1645              
1646             set foo => "bar";
1647              
1648             =cut
1649              
1650             sub register_set_handler {
1651 1936     1936 1 3508 my ( $class, $handler_name, $code ) = @_;
1652 1936         4958 $SET_HANDLER->{$handler_name} = $code;
1653             }
1654              
1655             sub set {
1656 139     139 1 1869 my ( $class, $var, $data ) = @_;
1657              
1658 139 100       744 if ( exists( $SET_HANDLER->{$var} ) ) {
1659 15         64 shift;
1660 15         54 shift;
1661 15         62 return &{ $SET_HANDLER->{$var} }(@_);
  15         194  
1662             }
1663              
1664 124 100       441 if ($set_no_append) {
1665 6         22 $set_param->{$var} = $data;
1666             }
1667             else {
1668 118 100       554 if ( ref($data) eq "HASH" ) {
    100          
1669 98 100       444 if ( !ref( $set_param->{$var} ) ) {
1670 96         454 $set_param->{$var} = {};
1671             }
1672 98         222 for my $key ( keys %{$data} ) {
  98         578  
1673 8         28 $set_param->{$var}->{$key} = $data->{$key};
1674             }
1675             }
1676             elsif ( ref($data) eq "ARRAY" ) {
1677 4         10 push( @{ $set_param->{$var} }, @{$data} );
  4         12  
  4         16  
1678             }
1679             else {
1680 16         92 $set_param->{$var} = $data;
1681             }
1682             }
1683             }
1684              
1685             sub unset {
1686 0     0 0 0 my ( $class, $var ) = @_;
1687 0         0 $set_param->{$var} = undef;
1688 0         0 delete $set_param->{$var};
1689             }
1690              
1691             sub get {
1692 119     119 0 494 my ( $class, $var ) = @_;
1693 119 100       509 $var or return;
1694 106 100       695 if ( exists $set_param->{$var} ) {
1695 60         384 return $set_param->{$var};
1696             }
1697             }
1698              
1699             sub get_all {
1700 52     52 0 250 my ($class) = @_;
1701 52         362 return $set_param;
1702             }
1703              
1704             =head2 register_config_handler($topic, $code)
1705              
1706             With this function it is possible to register own sections in the users config file (C<$HOME/.rex/config.yml>).
1707              
1708             Example:
1709              
1710             Rex::Config->register_config_handler("foo", sub {
1711             my ($param) = @_;
1712             print "bar is: " . $param->{bar} . "\n";
1713             });
1714              
1715             And now the user can set this in his configuration file:
1716              
1717             base:
1718             user: theuser
1719             password: thepassw0rd
1720             foo:
1721             bar: baz
1722              
1723             =cut
1724              
1725             sub register_config_handler {
1726 109     109 1 404 my ( $class, $topic, $code ) = @_;
1727              
1728 109 100       485 if ( !ref($HOME_CONFIG) ) { $HOME_CONFIG = {}; }
  105         248  
1729 109         365 $HOME_CONFIG->{$topic} = $code;
1730              
1731 109 0 33     706 if ( ref($HOME_CONFIG_YAML) && exists $HOME_CONFIG_YAML->{$topic} ) {
1732 0         0 &$code( $HOME_CONFIG_YAML->{$topic} );
1733             }
1734             }
1735              
1736             sub read_config_file {
1737 789     789 0 1665 my ($config_file) = @_;
1738 789   66     3416 $config_file ||= _home_dir() . "/.rex/config.yml";
1739              
1740 789 100       437574 if ( -f $config_file ) {
1741 1         4 my $yaml = eval { local ( @ARGV, $/ ) = ($config_file); <>; };
  1         7  
  1         87  
1742 1         4 eval { $HOME_CONFIG_YAML = Load($yaml); };
  1         5  
1743              
1744 1 50       16474 if ($@) {
1745 0         0 print STDERR "Error loading $config_file\n";
1746 0         0 print STDERR "$@\n";
1747 0         0 exit 2;
1748             }
1749              
1750 1         3 for my $key ( keys %{$HOME_CONFIG} ) {
  1         8  
1751 1 50       4 if ( exists $HOME_CONFIG_YAML->{$key} ) {
1752 1         3 my $code = $HOME_CONFIG->{$key};
1753 1         3 &$code( $HOME_CONFIG_YAML->{$key} );
1754             }
1755             }
1756             }
1757             }
1758              
1759             sub read_ssh_config_file {
1760 790     790 0 8056 my ($config_file) = @_;
1761 790   66     4784 $config_file ||= _home_dir() . '/.ssh/config';
1762              
1763 790 100       11025 if ( -f $config_file ) {
1764 2         6 my @lines = eval { local (@ARGV) = ($config_file); <>; };
  2         9  
  2         122  
1765 2         9 %SSH_CONFIG_FOR = _parse_ssh_config(@lines);
1766             }
1767             }
1768              
1769             sub _parse_ssh_config {
1770 3     3   3200 my (@lines) = @_;
1771              
1772 3         5 my %ret = ();
1773              
1774 3         6 my ( @host, $in_host );
1775 3         7 for my $line (@lines) {
1776 27         43 chomp $line;
1777 27 100       54 next if ( $line =~ m/^\s*#/ );
1778 26 100       70 next if ( $line =~ m/^\s*$/ );
1779              
1780 19 100       58 if ( $line =~ m/^Host(?:\s*=\s*|\s+)(.*)$/i ) {
    50          
1781 6         16 my $host_tmp = $1;
1782 6         20 @host = split( /\s+/, $host_tmp );
1783 6         9 $in_host = 1;
1784 6         10 for my $h (@host) {
1785 8         21 $ret{$h} = {};
1786             }
1787 6         12 next;
1788             }
1789             elsif ($in_host) {
1790              
1791             #my ($key, $val) = ($line =~ m/^\s*([^\s]+)\s+=?\s*(.*)$/);
1792 13         55 $line =~ s/^\s*//g;
1793 13         51 my ( $key, $val_tmp ) = split( /[\s=]/, $line, 2 );
1794 13         32 $val_tmp =~ s/^[\s=]+//g;
1795 13         18 my $val = $val_tmp;
1796              
1797 13         21 $val =~ s/^\s+//;
1798 13         22 $val =~ s/\s+$//;
1799 13         20 for my $h (@host) {
1800 15         43 $ret{$h}->{ lc($key) } = $val;
1801             }
1802             }
1803             }
1804              
1805 3         25 return %ret;
1806             }
1807              
1808             sub import {
1809 788     788   5146 read_ssh_config_file();
1810 788         2720 read_config_file();
1811             }
1812              
1813             _register_config_handlers();
1814              
1815             sub _register_config_handlers {
1816             __PACKAGE__->register_config_handler(
1817             base => sub {
1818 1     1   4 my ($param) = @_;
1819              
1820 1         2 for my $key ( keys %{$param} ) {
  1         5  
1821              
1822 1 50       4 if ( $key eq "keyauth" ) {
1823 0         0 $key_auth = $param->{keyauth};
1824 0         0 next;
1825             }
1826              
1827 1 50       4 if ( $key eq "passwordauth" ) {
1828 0         0 $password_auth = $param->{passwordauth};
1829 0         0 next;
1830             }
1831              
1832 1 50       3 if ( $key eq "passauth" ) {
1833 0         0 $password_auth = $param->{passauth};
1834 0         0 next;
1835             }
1836              
1837 1         6 my $ref_to_key = qualify_to_ref( $key, __PACKAGE__ );
1838 1         42 my $ref_to_key_scalar = *{$ref_to_key}{SCALAR};
  1         5  
1839              
1840 1         2 ${$ref_to_key_scalar} = $param->{$key};
  1         5  
1841             }
1842             }
1843 105     105   994 );
1844             }
1845              
1846             _register_set_handlers();
1847              
1848             sub _register_set_handlers {
1849 105     105   415 my @set_handler =
1850             qw/user password private_key public_key -keyauth -passwordauth -passauth
1851             parallelism sudo_password connection ca cert key distributor
1852             template_function port waitpid_blocking_sleep_time/;
1853 105         273 for my $hndl (@set_handler) {
1854             __PACKAGE__->register_set_handler(
1855             $hndl => sub {
1856 3     3   8 my ($val) = @_;
1857 3 50       13 if ( $hndl =~ m/^\-/ ) {
1858 0         0 $hndl = substr( $hndl, 1 );
1859             }
1860 3 50       10 if ( $hndl eq "keyauth" ) { $hndl = "key_auth"; $val = 1; }
  0         0  
  0         0  
1861 3 50 33     14 if ( $hndl eq "passwordauth" || $hndl eq "passauth" ) {
1862 0         0 $hndl = "password_auth";
1863 0         0 $val = 1;
1864             }
1865 3 100       7 if ( $hndl eq "connection" ) { $hndl = "connection_type"; }
  1         3  
1866 3 50       7 if ( $hndl eq "ca" ) { $hndl = "ca_file"; }
  0         0  
1867 3 50       8 if ( $hndl eq "cert" ) { $hndl = "ca_cert"; }
  0         0  
1868 3 50       9 if ( $hndl eq "key" ) { $hndl = "ca_key"; }
  0         0  
1869              
1870 3         11 my $ref_to_hndl = qualify_to_ref( $hndl, __PACKAGE__ );
1871 3         73 my $ref_to_hndl_scalar = *{$ref_to_hndl}{SCALAR};
  3         8  
1872              
1873 3         5 ${$ref_to_hndl_scalar} = $val;
  3         8  
1874             }
1875 1785         5626 );
1876             }
1877             }
1878              
1879             sub _home_dir {
1880 1576 50   1576   6406 if ( $^O =~ m/^MSWin/ ) {
1881 0         0 return $ENV{'USERPROFILE'};
1882             }
1883              
1884 1576   50     10411 return $ENV{'HOME'} || "";
1885             }
1886              
1887             1;