« blib/lib/Clustericious/Command/generate/app.pm
↑ Summary
blib/lib/Clustericious/Client/Object.pm »
| 1 | package Clustericious::Config; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 99 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 80 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 73 | ▶ | use 5.010; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 73 | ▶ | use Clustericious; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 73 | ▶ | use List::Util (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 72 | ▶ | use YAML::XS (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 72 | ▶ | use Mojo::Template; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 72 | ▶ | use Log::Log4perl qw( :nowarn ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 72 | ▶ | use Storable (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | 72 | ▶ | use Clustericious::Config::Helpers (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | 72 | ▶ | use Mojo::URL; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | 72 | ▶ | use File::Spec; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | 72 | ▶ | use File::Temp (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | 72 | ▶ | use Carp (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | # ABSTRACT: Configuration files for Clustericious nodes. | ||||||||||||||||||||
| 19 | 72 | our $VERSION = '1.29'; # VERSION | |||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | 72 | our %singletons; | |||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | 72 | our $class_suffix = {}; | |||||||||||||||||||
| 25 | 27 | ▶ | sub Clustericious::_config_uncache { | ||||||||||||||||||
|
Subroutine
_config_uncache: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 26 | 27 | my($class, $name) = @_; | |||||||||||||||||||
| 27 | 27 | delete $singletons{$name}; | |||||||||||||||||||
| 28 | 27 | ▶ | $class_suffix->{$name} //= 1; | ||||||||||||||||||
|
Condition100%2/2
$Clustericious::Config::class_suffix->{$name} //= 1
MC/DC100%1/1
$Clustericious::Config::class_suffix->{$name} //= 1
$Clustericious::Config::class_suffix->{$name}
Truth table100%2/2
A
A= $Clustericious::Config::class_suffix->{$name}
| |||||||||||||||||||||
| 29 | 27 | $class_suffix->{$name}++; | |||||||||||||||||||
| 30 | } | ||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | 150 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=21 SCAR=30.5
Pod
documented
| |||||||||||||||||||||
| 34 | 150 | my $class = shift; | |||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | 150 | my $logger = Log::Log4perl->get_logger(__PACKAGE__); | |||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | # (undocumented; for now) | ||||||||||||||||||||
| 39 | # callback is used by the configdebug command; | ||||||||||||||||||||
| 40 | # may be used elsewise at a later time | ||||||||||||||||||||
| 41 | 150 | ▶ | my $callback = ref $_[-1] eq 'CODE' ? pop : sub {}; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.4
Branch: 1 branch
| |||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | 150 | ▶ | my %t_args = (ref $_[-1] eq 'ARRAY' ? @{( pop )} : () ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | 150 | my $arg = $_[0]; | |||||||||||||||||||
| 46 | 150 | ▶ | ($arg = caller) =~ s/:.*$// unless $arg; # Determine from caller's class | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 47 | 150 | ▶ | return $singletons{$arg} if exists($singletons{$arg}); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | 150 | my $conf_data; | |||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | 150 | state $package_counter = 0; | |||||||||||||||||||
| 52 | 150 | my $namespace = "Clustericious::Config::TemplatePackage::Package$package_counter"; | |||||||||||||||||||
| 53 | 150 | ▶ | eval qq{ package $namespace; use Clustericious::Config::Helpers; }; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 54 | 150 | ▶ | die $@ if $@; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 55 | 150 | $package_counter++; | |||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | 150 | my $mt = Mojo::Template->new(namespace => $namespace)->auto_escape(0); | |||||||||||||||||||
| 58 | 150 | $mt->prepend( join "\n", map " my \$$_ = q{$t_args{$_}};", sort keys %t_args ); | |||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | 150 | ▶ | if(ref $arg eq 'HASH') | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 61 | { | ||||||||||||||||||||
| 62 | 62 | $conf_data = Storable::dclone $arg; | |||||||||||||||||||
| 63 | } | ||||||||||||||||||||
| 64 | else | ||||||||||||||||||||
| 65 | { | ||||||||||||||||||||
| 66 | 88 | my $filename; | |||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | 88 | ▶ | if($arg =~ /\.conf$/) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 69 | { | ||||||||||||||||||||
| 70 | 1 | $filename = $arg; | |||||||||||||||||||
| 71 | } | ||||||||||||||||||||
| 72 | else | ||||||||||||||||||||
| 73 | { | ||||||||||||||||||||
| 74 | 87 | my $name = $arg; | |||||||||||||||||||
| 75 | 87 | $name =~ s/::/-/g; | |||||||||||||||||||
| 76 | ($filename) = | ||||||||||||||||||||
| 77 | 94 | ▶ | List::Util::first { -f $_ } | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 78 | 87 | map { File::Spec->catfile($_, "$name.conf") } | |||||||||||||||||||
| 79 | Clustericious->_config_path; | ||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | 87 | ▶ | unless($filename) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 82 | { | ||||||||||||||||||||
| 83 | 20 | ▶ | $logger->trace("could not find $name file.") if $logger->is_trace; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 84 | 20 | $conf_data = {}; | |||||||||||||||||||
| 85 | } | ||||||||||||||||||||
| 86 | } | ||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | 88 | ▶ | if ($filename) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 89 | 68 | ▶ | $logger->trace("reading from config file $filename") if $logger->is_trace; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 90 | 68 | $callback->(pre_rendered => $filename); | |||||||||||||||||||
| 91 | 68 | my $rendered = $mt->render_file($filename); | |||||||||||||||||||
| 92 | 68 | $callback->(rendered => $filename => $rendered); | |||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | 68 | ▶ | die $rendered if ( (ref $rendered) =~ /Exception/ ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | 68 | $conf_data = eval { YAML::XS::Load($rendered) }; | |||||||||||||||||||
| 97 | 68 | ▶ | $logger->logdie("Could not parse\n-------\n$rendered\n---------\n$@\n") if $@; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 98 | } else { | ||||||||||||||||||||
| 99 | 20 | $callback->('not_found' => "$arg"); | |||||||||||||||||||
| 100 | } | ||||||||||||||||||||
| 101 | } | ||||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | 150 | ▶ | $conf_data ||= {}; | ||||||||||||||||||
|
Condition100%2/2
$conf_data ||= {}
MC/DC100%1/1
$conf_data ||= {}
$conf_data
Truth table100%2/2
A
A= $conf_data
| |||||||||||||||||||||
| 104 | 150 | Clustericious::Config::Helpers->_do_merges($conf_data); | |||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | # Use derived classes so that AUTOLOADING keeps namespaces separate | ||||||||||||||||||||
| 107 | # for various apps. | ||||||||||||||||||||
| 108 | 150 | ▶ | if ($class eq __PACKAGE__) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 109 | { | ||||||||||||||||||||
| 110 | 90 | ▶ | if(ref $arg) | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 111 | { | ||||||||||||||||||||
| 112 | 2 | $arg = "$arg"; | |||||||||||||||||||
| 113 | 2 | $arg =~ tr/a-zA-Z0-9//cd; | |||||||||||||||||||
| 114 | } | ||||||||||||||||||||
| 115 | elsif($arg =~ s/\.conf$//) | ||||||||||||||||||||
| 116 | { | ||||||||||||||||||||
| 117 | # NOTE: may revisit this later. | ||||||||||||||||||||
| 118 | 1 | ▶ | $arg = "cwd::$arg" unless $arg =~ s{^/+}{root::}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 119 | 1 | $arg =~ s{[\\/]}{::}g; | |||||||||||||||||||
| 120 | 1 | $arg =~ s{\.\.::}{__up__::}g; | |||||||||||||||||||
| 121 | 1 | $arg =~ tr/a-zA-Z0-0_://cd; | |||||||||||||||||||
| 122 | 1 | $arg =~ s/:{3,}/::/g; | |||||||||||||||||||
| 123 | } | ||||||||||||||||||||
| 124 | 90 | $arg =~ s/-/::/g; | |||||||||||||||||||
| 125 | 90 | $class = join '::', $class, 'App', $arg; | |||||||||||||||||||
| 126 | 90 | ▶ | $class .= $class_suffix->{$arg} if $class_suffix->{$arg}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 127 | 90 | my $dome = '# line '. __LINE__ . ' "' . __FILE__ . qq("\n) . '@'."$class"."::ISA = ('".__PACKAGE__. "')"; | |||||||||||||||||||
| 128 | 90 | eval $dome; | |||||||||||||||||||
| 129 | 90 | ▶ | die "error setting ISA : $@" if $@; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 130 | } | ||||||||||||||||||||
| 131 | 150 | bless $conf_data, $class; | |||||||||||||||||||
| 132 | } | ||||||||||||||||||||
| 133 | |||||||||||||||||||||
| 134 | # defined so that AUTOLOAD doesn't get called | ||||||||||||||||||||
| 135 | # when config falls out of scope. | ||||||||||||||||||||
| 136 | 0 | ▶ | sub DESTROY { | ||||||||||||||||||
|
Subroutine
DESTROY: not called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 137 | } | ||||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | 304 | ▶ | sub AUTOLOAD { | ||||||||||||||||||
|
Subroutine
AUTOLOAD: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 140 | 304 | my($self, %args) = @_; | |||||||||||||||||||
| 141 | |||||||||||||||||||||
| 142 | # NOTE: I hope to deprecated and later remove defining defaults in this way in the near future. | ||||||||||||||||||||
| 143 | 304 | my $default = $args{default}; | |||||||||||||||||||
| 144 | 304 | my $default_exists = exists $args{default}; | |||||||||||||||||||
| 145 | |||||||||||||||||||||
| 146 | 304 | our $AUTOLOAD; | |||||||||||||||||||
| 147 | 304 | my $called = $AUTOLOAD; | |||||||||||||||||||
| 148 | 304 | $called =~ s/.*:://g; | |||||||||||||||||||
| 149 | |||||||||||||||||||||
| 150 | 304 | my $value = $self->{$called}; | |||||||||||||||||||
| 151 | 304 | my $invocant = ref $self; | |||||||||||||||||||
| 152 | 304 | ▶ | my $obj = ref $value eq 'HASH' ? $invocant->new($value) : undef; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 153 | |||||||||||||||||||||
| 154 | 417 | ▶ | my $sub = sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=11 SCAR=24.0
| |||||||||||||||||||||
| 155 | 417 | my $self = shift; | |||||||||||||||||||
| 156 | 417 | my $value; | |||||||||||||||||||
| 157 | |||||||||||||||||||||
| 158 | 417 | ▶ | if(exists $self->{$called}) | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 159 | { | ||||||||||||||||||||
| 160 | 213 | $value = $self->{$called}; | |||||||||||||||||||
| 161 | } | ||||||||||||||||||||
| 162 | elsif($default_exists) | ||||||||||||||||||||
| 163 | { | ||||||||||||||||||||
| 164 | 201 | ▶ | $value = $self->{$called} = ref $default eq 'CODE' ? $default->() : $default; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 165 | 201 | ▶ | $obj = ref $value eq 'HASH' ? $invocant->new($value) : undef; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 166 | } | ||||||||||||||||||||
| 167 | else | ||||||||||||||||||||
| 168 | { | ||||||||||||||||||||
| 169 | 3 | Carp::croak "'$called' configuration item not found. Values present: @{[keys %$self]}"; | |||||||||||||||||||
| 170 | } | ||||||||||||||||||||
| 171 | |||||||||||||||||||||
| 172 | 414 | my $ref = ref $value; | |||||||||||||||||||
| 173 | 414 | ▶ | if($ref) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 174 | { | ||||||||||||||||||||
| 175 | 233 | ▶ | if(wantarray) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 176 | { | ||||||||||||||||||||
| 177 | 143 | ▶ | return %$value if $ref eq 'HASH'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 178 | 140 | ▶ | return @$value if $ref eq 'ARRAY'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 179 | } | ||||||||||||||||||||
| 180 | 90 | ▶ | return $obj if $obj; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 181 | 27 | ▶ | $value = $value->execute if eval { $value->can('execute') }; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 182 | } | ||||||||||||||||||||
| 183 | 208 | $value; | |||||||||||||||||||
| 184 | 304 | }; | |||||||||||||||||||
| 185 | 72 | ▶ | do { no strict 'refs'; *{ $invocant . "::$called" } = $sub }; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 186 | 304 | $sub->($self); | |||||||||||||||||||
| 187 | } | ||||||||||||||||||||
| 188 | |||||||||||||||||||||
| 189 | |||||||||||||||||||||
| 190 | package Clustericious::Config::Callback; | ||||||||||||||||||||
| 191 | |||||||||||||||||||||
| 192 | 72 | ▶ | use JSON::MaybeXS qw( encode_json ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 193 | |||||||||||||||||||||
| 194 | sub new | ||||||||||||||||||||
| 195 | 4 | ▶ | { | ||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 196 | 4 | my($class, @args) = @_; | |||||||||||||||||||
| 197 | 4 | bless [@args], $class; | |||||||||||||||||||
| 198 | } | ||||||||||||||||||||
| 199 | |||||||||||||||||||||
| 200 | 1 | ▶ | sub args { @{ shift() } } | ||||||||||||||||||
|
Subroutine
args: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 201 | |||||||||||||||||||||
| 202 | 1 | ▶ | sub execute { '' } | ||||||||||||||||||
|
Subroutine
execute: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 203 | |||||||||||||||||||||
| 204 | sub to_yaml | ||||||||||||||||||||
| 205 | 6 | ▶ | { | ||||||||||||||||||
|
Subroutine
to_yaml: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 206 | 6 | my($self) = @_; | |||||||||||||||||||
| 207 | 6 | "!!perl/array:@{[ ref $self ]} @{[ encode_json [@$self] ]}"; | |||||||||||||||||||
| 208 | } | ||||||||||||||||||||
| 209 | |||||||||||||||||||||
| 210 | package Clustericious::Config::Callback::Password; | ||||||||||||||||||||
| 211 | |||||||||||||||||||||
| 212 | 72 | ▶ | use base qw( Clustericious::Config::Callback ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 213 | |||||||||||||||||||||
| 214 | sub execute | ||||||||||||||||||||
| 215 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
execute: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 216 | 0 | state $pass; | |||||||||||||||||||
| 217 | 0 | ▶ | $pass //= do { require Term::Prompt; Term::Prompt::prompt('p', 'Password:', '', '') }; | ||||||||||||||||||
|
Condition0%0/3
$pass //= do {
require Term::Prompt;
Term::Prompt::prompt("p", "Password:", "", "")
}
MC/DC0%0/2
$pass //= do {
require Term::Prompt;
Term::Prompt::prompt("p", "Password:", "", "")
}
$pass
do {
require Term::Prompt;
Term::Prompt::prompt("p", "Password:", "", "")
}
Truth table0%0/3
A //= B
A= $passB= do {
require Term::Prompt;
Term::Prompt::prompt("p", "Password:", "", "")
}
| |||||||||||||||||||||
| 218 | 0 | $pass; | |||||||||||||||||||
| 219 | } | ||||||||||||||||||||
| 220 | |||||||||||||||||||||
| 221 | 72 | 1; | |||||||||||||||||||
| 222 | |||||||||||||||||||||
| 223 | __END__ | ||||||||||||||||||||