| 1 | package MYDan::Deploy::Conf; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | MYDan::Deploy::Conf - Load/Inspect maintenance configs | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | use MYDan::Deploy::Conf; | ||
| 10 | |||
| 11 | my $conf = MYDan::Deploy::Conf->new( $name )->dump( \%macro ); | ||
| 12 | |||
| 13 | =cut | ||
| 14 | use strict; | ||
| 15 | use warnings; | ||
| 16 | |||
| 17 | use Data::Dumper; | ||
| 18 | |||
| 19 | use Carp; | ||
| 20 | use YAML::XS; | ||
| 21 | use Tie::File; | ||
| 22 | |||
| 23 | =head1 CONFIGURATION | ||
| 24 | |||
| 25 | YAML file that defines sets of maintenance parameters index by names. | ||
| 26 | Each set defines the following parameters: | ||
| 27 | |||
| 28 | target : targets of maintenance, to be devided into batches. | ||
| 29 | maint : name of maintainance code. | ||
| 30 | batch : name of batch code. | ||
| 31 | param : ( optional ) parameters of batch code. | ||
| 32 | |||
| 33 | =cut | ||
| 34 | #our @PARAM = qw( target maint batch ); | ||
| 35 | |||
| 36 | sub new | ||
| 37 | { | ||
| 38 | my ( $class, $name, $path ) = splice @_; | ||
| 39 | |||
| 40 | map{ confess "no path $path->{$_}" unless -d $path->{$_} }qw( mould conf ); | ||
| 41 | |||
| 42 | my $conf = eval { YAML::XS::LoadFile( "$path->{conf}/$name" ) }; | ||
| 43 | confess "load conf error: $@" if $@; | ||
| 44 | |||
| 45 | confess "tie conf fail: $!" unless tie my @conf, 'Tie::File', "$path->{conf}/$name"; | ||
| 46 | my $s_conf = join "\n", @conf; | ||
| 47 | |||
| 48 | return bless +{ path => $path, conf => $conf, s_conf => $s_conf }, ref $class || $class; | ||
| 49 | } | ||
| 50 | |||
| 51 | sub dump | ||
| 52 | { | ||
| 53 | my ( $this, $macro ) = @_; | ||
| 54 | my ( $path, $conf, $s_conf ) = @$this{qw( path conf s_conf )}; | ||
| 55 | |||
| 56 | if( my $dm = delete $conf->{macro} ) | ||
| 57 | { | ||
| 58 | confess "default macro no HASH in conf" if ref $dm ne 'HASH'; | ||
| 59 | map{ $macro->{$_} = $dm->{$_} unless defined $macro->{$_} }keys %$dm; | ||
| 60 | } | ||
| 61 | |||
| 62 | $conf = $s_conf; | ||
| 63 | |||
| 64 | map{ $conf =~ s/\$env\{$_\}/$macro->{$_}/g; }keys %$macro if $macro; | ||
| 65 | |||
| 66 | my @env = $conf =~ /(\$env\{.*\})/g; | ||
| 67 | |||
| 68 | exit 0 if @env && printf "env no replace on conf: %s\n", join ' ', @env; | ||
| 69 | |||
| 70 | $conf = eval{ YAML::XS::Load $conf }; | ||
| 71 | confess "load conf error: $@" if $@; | ||
| 72 | |||
| 73 | map{ confess "$_ undef in conf" unless $conf->{$_} }qw( batch maint ); | ||
| 74 | confess "batch's code undef in conf" unless $conf->{batch}{code}; | ||
| 75 | confess "maint's mould undef in conf" unless $conf->{maint}{mould}; | ||
| 76 | |||
| 77 | |||
| 78 | my $mouldfile = "$path->{mould}/$conf->{maint}{mould}"; | ||
| 79 | confess "no the mould file\n" unless -f $mouldfile; | ||
| 80 | confess "tie mould fail: $!" unless tie my @maint, 'Tie::File', $mouldfile; | ||
| 81 | |||
| 82 | my $maint = join "\n", @maint; | ||
| 83 | |||
| 84 | my $m = $conf->{maint}{macro}; | ||
| 85 | map{ $maint =~ s/\$env\{$_\}/$m->{$_}/g; }keys %$m if $m; | ||
| 86 | my @e = $maint =~ /(\$env\{.*\})/g; | ||
| 87 | |||
| 88 | confess sprintf "env no replace on maint: %s\n", join ' ', @e if @e; | ||
| 89 | |||
| 90 | $maint = eval{ YAML::XS::Load $maint }; | ||
| 91 | confess "load conf error: $@" if $@; | ||
| 92 | |||
| 93 | confess "maint no ARRAY" if ref $maint ne 'ARRAY'; | ||
| 94 | |||
| 95 | my %title; | ||
| 96 | my %code = ( $conf->{batch}{code} => 1 ); | ||
| 97 | map | ||
| 98 | { | ||
| 99 | $title{ $maint->[$_-1]->{title} ||= "job.$_" } ++; | ||
| 100 | $code{$maint->[$_-1]->{code}} = 1; | ||
| 101 | }1.. @$maint; | ||
| 102 | |||
| 103 | my @redef = grep{ $title{$_} > 1 }keys %title; | ||
| 104 | confess sprintf "maint title ref: %s", join ' ' , @redef if @redef; | ||
| 105 | |||
| 106 | return $conf, $maint, \%code; | ||
| 107 | } | ||
| 108 | |||
| 109 | # | ||
| 110 | #sub check | ||
| 111 | #{ | ||
| 112 | |||
| 113 | |||
| 114 | # my ( $self, $conf ) = splice @_; | ||
| 115 | # map { die "$_ not defined" if ! $conf->{$_} } @PARAM; | ||
| 116 | #} | ||
| 117 | # | ||
| 118 | #=head1 METHODS | ||
| 119 | # | ||
| 120 | #=head3 dump( @name ) | ||
| 121 | # | ||
| 122 | #Returns configurations indexed by @name. | ||
| 123 | # | ||
| 124 | #=cut | ||
| 125 | #sub dump | ||
| 126 | #{ | ||
| 127 | # my $self = shift; | ||
| 128 | # my @conf = return @$self{@_}; | ||
| 129 | # return wantarray ? @conf : shift @conf; | ||
| 130 | #} | ||
| 131 | # | ||
| 132 | #=head3 names | ||
| 133 | # | ||
| 134 | #Returns names of all maintenance. | ||
| 135 | # | ||
| 136 | #=cut | ||
| 137 | #sub names | ||
| 138 | #{ | ||
| 139 | # my $self = shift; | ||
| 140 | # return keys %$self; | ||
| 141 | #} | ||
| 142 | # | ||
| 143 | 1; |