Ansible
view release on metacpan or search on metacpan
lib/Ansible.pm view on Meta::CPAN
18192021222324252627282930313233343536373839404142434445464748our
$allow_plus_one_indent
=
qr/service-policy |quit$/
;
our
$bad_indent_policy
=
'DIE'
;
BEGIN {
eval
" use IO::String "
;
$iostrings
= $@ ? 0 : 1;
}
my
$debug_get
= 0;
my
$debug_mget
= 0;
my
$debug_set
= 0;
my
$debug_context
= 0;
my
$debug_text
= 0;
my
$ddata
=
$debug_get
||
$debug_mget
||
$debug_set
||
$debug_context
||
$debug_text
|| 0;
# add debugging data to data structures
my
$spec
=
qr{^ }
;
my
$text
=
" text"
;
my
$subs
=
" subs"
;
my
$next
=
" next"
;
my
$cntx
=
" cntx"
;
my
$word
=
" word"
;
my
$seqn
=
" seqn"
;
my
$dupl
=
" dupl"
;
my
$debg
=
" debg"
;
lib/Ansible.pm view on Meta::CPAN
282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
return
$self
if
defined
(
$self
->{
$text
}) && !
@p
;
confess
unless
@p
;
return
$self
->{
$p
[0]}->endpt;
}
sub
text {
my
(
$self
) =
@_
;
return
''
unless
$self
;
if
(
defined
$self
->{
$text
} ) {
return
$debug_text
?
$self
->{
$word
} .
" "
.
$self
->{
$text
}
:
$self
->{
$text
};
}
my
(
@p
) =
$self
->sortit(
grep
(! /
$spec
/o,
keys
%$self
));
if
(
@p
> 1 ) {
#
# This is nasty because the lines may not be ordered
# in the tree-hiearchy used by Cisco::Reconfig
#
my
%temp
=
map
{
$self
->{
$_
}->sequenced_text(0) }
@p
;
return
join
(
''
,
map
{
$temp
{
$_
} }
sort
keys
%temp
);
}
elsif
(
$self
->{
$dupl
} ) {
return
join
(
''
,
map
{
$_
->{
$word
} .
" "
.
$_
->{
$text
} } @{
$self
->{
$dupl
} })
if
$debug_text
;
return
join
(
''
,
map
{
$_
->{
$text
} } @{
$self
->{
$dupl
} });
}
confess
unless
@p
;
return
$self
->{
$p
[0]}->text;
}
sub
sequenced_text {
my
(
$self
,
$all
) =
@_
;
my
@t
= ();
if
(
defined
$self
->{
$text
} ) {
push
(
@t
,
$debug_text
? (
$self
->
seqn
=>
$self
->{
$word
} .
" "
.
$self
->{
$text
})
: (
$self
->
seqn
=>
$self
->{
$text
}));
}
if
(
exists
$self
->{
$dupl
} ) {
push
(
@t
,
$debug_text
?
map
{
$_
->
seqn
=>
$_
->{
$word
} .
" "
.
$_
->{
$text
} } @{
$self
->{
$dupl
} }
:
map
{
$_
->
seqn
=>
$_
->{
$text
} } @{
$self
->{
$dupl
} });
}
my
(
@p
) =
$self
->sortit(
grep
(! /
$spec
/o,
keys
%$self
));
if
(
@p
) {
#
# This is nasty because the lines may not be ordered
# in the tree-hiearchy used by Cisco::Reconfig
#
return
(
@t
,
map
{
$self
->{
$_
}->sequenced_text(
$all
) }
@p
);
lib/Ansible.pm view on Meta::CPAN
390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
|| (
$_
[0] ? confess
"$_[0]"
:
$undef
)
};
#
# interface Loopback7
# ip address x y
#
sub
setcontext {
my
(
$self
,
@extras
) =
@_
;
STDERR
"\nSETCONTEXT\n"
if
$debug_context
;
unless
(
$self
->block ) {
STDERR
"\nNOT_A_BLOCK $self->{$debg}\n"
if
$debug_context
;
$self
=
$self
->context;
}
printf
STDERR
"\nSELF %sCONTEXT %sCCONTEXT %sEXTRAS$#extras @extras\n"
,
$self
->{
$debg
},
$self
->context->{
$debg
},
$self
->context->context->{
$debg
}
if
$debug_context
;
my
$x
=
$self
->context;
return
(
grep
defined
,
$x
->context->setcontext,
trim(
$x
->zoom->{
$text
}),
@extras
)
if
$x
;
return
@extras
;
}
sub
contextcount {
my
$self
=
shift
;
my
(
@a
) =
$self
->setcontext(
@_
);
printf
STDERR
"CONTEXTCOUNT = %d\n"
,
scalar
(
@a
)
if
$debug_context
;
STDERR
map
{
"CC: $_\n"
}
@a
if
$debug_context
;
return
scalar
(
@a
);
}
sub
unsetcontext {
my
$self
=
shift
;
return
((
"exit"
) x
$self
->contextcount(
@_
));
}
sub
teql {
my
(
$self
,
$b
) =
@_
;
lib/Ansible.pm view on Meta::CPAN
440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
$b
=~ s/ $//;
chomp
(
$b
);
return
$a
eq
$b
;
}
sub
set {
my
$self
=
shift
;
my
$new
=
pop
;
my
(
@designators
) =
@_
;
#my ($self, $designator, $new) = @_;
STDERR
"\nSET\n"
if
$debug_set
;
return
undef
unless
$self
;
my
$old
;
#my @designators;
STDERR
"\nSELF $self->{$debg}"
if
$debug_set
;
# move into the block if possible
$self
=
$self
->subs
if
$self
->subs;
STDERR
"\nSELF $self->{$debg}"
if
$debug_set
;
#if (ref $designator eq 'ARRAY') {
# @designators = @$designator;
# $old = $self->get(@designators);
# $designator = pop(@designators);
#} elsif ($designator) {
# $old = $self->get($designator);
#} else {
# $old = $self;
#}
my
$designator
;
if
(
@designators
) {
$old
=
$self
->get(
@designators
);
$designator
=
pop
(
@designators
);
}
else
{
$old
=
$self
;
}
STDERR
"\nOLD $old->{$debg}"
if
$debug_set
;
my
(
@lines
) = expand(
grep
(/./,
split
(/\n/,
$new
)));
if
(
$lines
[0] =~ /^(\s+)/ ) {
my
$ls
= $1;
my
$m
= 1;
map
{
substr
(
$_
, 0,
length
(
$ls
)) eq
$ls
or
$m
= 0 }
@lines
;
map
{
substr
(
$_
, 0,
length
(
$ls
)) =
''
}
@lines
if
$m
;
}
my
$indent
= (
' '
x
$self
->contextcount(
@designators
));
for
$_
(
@lines
) {
s/(\S)\s+/$1 /g;
s/\s+$//;
$_
=
'exit'
if
/^\s*!\s*$/;
$_
=
"$indent$_"
;
}
STDERR
"SET TO {\n@lines\n}\n"
if
$debug_set
;
my
$desig
=
shift
(
@lines
);
my
@o
;
undef
$old
if
!
$old
;
if
( !
$old
) {
STDERR
"NO OLD\n"
if
$debug_set
;
push
(
@o
, openangle(
$self
->setcontext(
@designators
)));
push
(
@o
,
$desig
);
}
elsif
( !
$designator
&& ! looks_like_a_block(
$desig
,
@lines
) ) {
if
(
$self
->block &&
$self
->context ) {
unshift
(
@lines
,
$desig
);
$old
=
$self
->context;
undef
$desig
;
}
else
{
unshift
(
@lines
,
$desig
);
STDERR
"IN NASTY BIT\n"
if
$debug_set
;
#
# this is a messy situation: we've got a random
# block of stuff to set inside a random block.
# In theorey we could avoid the die, I'll leave
# that as an exercise for the reader.
#
confess
"You cannot set nested configurations with set(undef, \$config) -- use a designator on the set method"
if
grep
(/^
$indent
\s/,
@lines
);
my
(
@t
) =
split
(/\n/,
$self
->text);
my
(
%t
);
lib/Ansible.pm view on Meta::CPAN
529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
}
}
for
my
$k
(
keys
%t
) {
unshift
(
@o
, iinvert(
$indent
,
$k
));
}
unshift
(
@o
,
$self
->setcontext)
if
@o
;
}
}
elsif
(
$old
->teql(
$desig
) ) {
STDERR
"DESIGNATOR EQUAL\n"
if
$debug_set
;
# okay
}
else
{
STDERR
"DESIGNATOR DIFERENT\n"
if
$debug_set
;
push
(
@o
, openangle(
$self
->setcontext(
@designators
)));
if
(
defined
$designator
) {
push
(
@o
, iinvert(
$indent
,
$designator
));
}
else
{
push
(
@o
, iinvert(
$indent
,
split
(/\n/,
$self
->text)));
}
push
(
@o
,
$desig
);
}
if
(
@lines
) {
if
(
$old
&& !
@o
&&
$old
->subs &&
$old
->subs->
next
) {
STDERR
"OLD= $old->{$debg}"
if
$debug_set
;
my
$ok
= 1;
my
$f
=
$old
->subs->
next
;
STDERR
"F= $f->{$debg}"
if
$debug_set
;
for
my
$l
(
@lines
) {
next
if
$l
=~ /^\s
*exit
\s*$/;
next
if
$f
->teql(
$l
);
STDERR
"LINE DIFF ON $l\n"
if
$debug_set
;
$ok
= 0;
last
;
}
continue
{
$f
=
$f
->
next
;
STDERR
"F= $f->{$debg}"
if
$debug_set
;
}
if
( !
$ok
||
$f
) {
push
(
@o
, openangle(
$self
->setcontext(
@designators
)));
push
(
@o
, iinvert(
$indent
,
$designator
));
push
(
@o
,
$desig
);
}
}
push
(
@o
,
@lines
)
if
@o
;
}
@o
=
grep
(
defined
,
@o
);
lib/Ansible.pm view on Meta::CPAN
609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
return
(
map
{
$self
->{
$_
} }
$self
->sortit(
grep
(/
$regex
/ && ! /
$spec
/o,
keys
%$self
)))
if
$regex
;
return
(
map
{
$self
->{
$_
} }
$self
->sortit(
grep
(! /
$spec
/o,
keys
%$self
)));
}
sub
get {
my
(
$self
,
@designators
) =
@_
;
return
$self
->mget(
@designators
)
if
wantarray
&&
@designators
> 1;
STDERR
"\nGET <@designators> $self->{$debg}"
if
$debug_get
;
return
$self
unless
$self
;
my
$zoom
=
$self
->zoom->subs;
$self
=
$zoom
if
$zoom
;
STDERR
"\nZOOMSUB $self->{$debg}"
if
$debug_get
;
while
(
@designators
) {
my
$designator
=
shift
(
@designators
);
# $self = $self->zoom;
# $self = $self->single || $self;
STDERR
"\nDESIGNATOR: $designator. ZOOMED: $self->{$debg}\n"
if
$debug_get
;
for
my
$d
(
split
(
' '
,
$designator
) ) {
STDERR
"\nDO WE HAVE A: $d?\n"
if
$debug_get
;
return
$undef
unless
$self
->{
$d
};
$self
=
$self
->{
$d
};
STDERR
"\nWE DO: $self->{$debg}\n"
if
$debug_get
;
}
last
unless
@designators
;
if
(
$self
->single ) {
$self
=
$self
->subs;
STDERR
"\nSINGLETON: $self->{$debg}\n"
if
$debug_get
;
}
else
{
STDERR
"\nNOT SINGLE\n"
if
$debug_get
;
return
$undef
;
}
}
STDERR
"\nDONE\n"
if
$debug_get
;
if
(
wantarray
) {
$self
=
$self
->zoom;
my
(
@k
) =
$self
->kids;
return
@k
if
@k
;
return
$self
;
}
return
$self
;
}
sub
strim {
lib/Ansible.pm view on Meta::CPAN
697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
return
$n
;
}
sub
mget {
my
(
$self
,
@designators
) =
@_
;
my
$cl
= callerlevels;
my
@newset
;
if
(
@designators
> 1 ) {
STDERR
"\nGET$cl $designators[0]----------\n"
if
$debug_mget
;
my
(
@set
) =
$self
->get(
shift
@designators
);
for
my
$item
(
@set
) {
STDERR
"\nMGET$cl $item ----------\n"
if
$debug_mget
;
STDERR
"\nMGET$cl $item->{$debg}\n"
if
$debug_mget
;
my
(
@got
) =
$item
->mget(
@designators
);
STDERR
map
{
"\nRESULTS$cl: $_->{$debg}\n"
}
@got
if
$debug_mget
;
push
(
@newset
,
@got
);
}
}
else
{
STDERR
"\nxGET$cl $designators[0] -------\n"
if
$debug_mget
;
(
@newset
) =
$self
->get(
shift
@designators
);
STDERR
map
{
"\nxRESULTS$cl: $_->{$debg}\n"
}
@newset
if
$debug_mget
;
}
return
@newset
;
}
sub
sortit {
my
$self
=
shift
;
return
sort
{
$self
->{
$a
}->seqn cmp
$self
->{
$b
}->seqn }
@_
;
}
( run in 0.307 second using v1.01-cache-2.11-cpan-454fe037f31 )