| 1 | package MYDan::Deploy::Ctrl; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | MYDan::Deploy::Ctrl - Controls maintenance via a SQLite database | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | use MYDan::Deploy::Ctrl; | ||
| 10 | |||
| 11 | my $ctrl = MYDan::Deploy::Ctrl->new( $name => '/sqlite/file' ); | ||
| 12 | |||
| 13 | $ctrl->clear(); | ||
| 14 | $ctrl->pause(); | ||
| 15 | $ctrl->resume(); | ||
| 16 | $ctrl->exclude(); | ||
| 17 | sleep 3 if $ctrl->stuck(); | ||
| 18 | |||
| 19 | =cut | ||
| 20 | use strict; | ||
| 21 | use warnings; | ||
| 22 | |||
| 23 | use MIME::Base64; | ||
| 24 | use base qw( MYDan::Util::SQLiteDB ); | ||
| 25 | |||
| 26 | =head1 DATABASE | ||
| 27 | |||
| 28 | A SQLITE db has a I<watcher> table of I<four> columns: | ||
| 29 | |||
| 30 | name : name of maintenance | ||
| 31 | ctrl : 'error', 'pause' or 'exclude' | ||
| 32 | node : stage name or node name | ||
| 33 | info : additional information, if any | ||
| 34 | |||
| 35 | =cut | ||
| 36 | our ( $TABLE, $EXC, $ANY ) = qw( deploy exclude ANY ); | ||
| 37 | |||
| 38 | sub define | ||
| 39 | { | ||
| 40 | ctrl => 'TEXT NOT NULL', | ||
| 41 | name => 'TEXT NOT NULL', | ||
| 42 | node => 'TEXT NOT NULL', | ||
| 43 | info => 'BLOB'; | ||
| 44 | }; | ||
| 45 | |||
| 46 | sub new | ||
| 47 | { | ||
| 48 | my ( $class, $name, $db ) = splice @_; | ||
| 49 | my $self = $class->SUPER::new( "$db/$name", $TABLE ); | ||
| 50 | $self->{name} = $name; | ||
| 51 | return $self; | ||
| 52 | } | ||
| 53 | |||
| 54 | =head1 METHODS | ||
| 55 | |||
| 56 | =head3 pause( $job, $stage, $stop, $ctrl = 'pause' ) | ||
| 57 | |||
| 58 | Insert a record that cause stuck. | ||
| 59 | |||
| 60 | =cut | ||
| 61 | sub pause | ||
| 62 | { | ||
| 63 | my $self = shift; | ||
| 64 | splice @_, 0, 0 , 'pause' if @_ == 3; | ||
| 65 | return if @_ != 4; | ||
| 66 | $self->insert( $TABLE, splice( @_, 0,3 ), encode_base64( pop @_ ) ); | ||
| 67 | } | ||
| 68 | |||
| 69 | =head3 stuck( name, step ) | ||
| 70 | |||
| 71 | Return records that cause @stage to be stuck. Return all records if @stage | ||
| 72 | is not defined. | ||
| 73 | stuck( ) | ||
| 74 | stuck( name ) | ||
| 75 | stuck( name, step ) | ||
| 76 | |||
| 77 | =cut | ||
| 78 | sub stuck | ||
| 79 | { | ||
| 80 | my ( $self, %term ) = shift; | ||
| 81 | map{ $term{$_} = [ 1, $ANY, shift ] if @_ }qw( name node ); | ||
| 82 | $self->select( $TABLE => '*', %term, ctrl => [ 0, $EXC ] ); | ||
| 83 | } | ||
| 84 | |||
| 85 | =head3 resume( name, step ) | ||
| 86 | |||
| 87 | Clear records that cause @stage to be stuck. Clear all records if @stage | ||
| 88 | is not defined. | ||
| 89 | |||
| 90 | |||
| 91 | =cut | ||
| 92 | sub resume | ||
| 93 | { | ||
| 94 | my ( $self, %term ) = shift; | ||
| 95 | map{ $term{$_} = [ 1, $ANY, shift ] if @_ }qw( name node ); | ||
| 96 | $self->delete( $TABLE, %term, ctrl => [ 0, $EXC ] ); | ||
| 97 | } | ||
| 98 | |||
| 99 | =head3 exclude( $node, $info ) | ||
| 100 | |||
| 101 | Exclude $node with a $info. | ||
| 102 | |||
| 103 | =cut | ||
| 104 | sub exclude | ||
| 105 | { | ||
| 106 | my $self = shift; | ||
| 107 | map{ $self->insert( $TABLE, $EXC, $ANY, $ANY, $_ ) }@_; | ||
| 108 | } | ||
| 109 | |||
| 110 | =head3 excluded() | ||
| 111 | |||
| 112 | Return ARRAY ref of excluded nodes. | ||
| 113 | |||
| 114 | =cut | ||
| 115 | sub excluded | ||
| 116 | { | ||
| 117 | my $self = shift; | ||
| 118 | my @exc = $self->select( $TABLE => 'info', ctrl => [ 1, $EXC ] ); | ||
| 119 | return [ map { @$_ } @exc ]; | ||
| 120 | } | ||
| 121 | |||
| 122 | =head3 dump() | ||
| 123 | |||
| 124 | Return ARRAY ref of *. | ||
| 125 | |||
| 126 | =cut | ||
| 127 | sub dump | ||
| 128 | { | ||
| 129 | my $self = shift; | ||
| 130 | my @exc = $self->select( $TABLE => '*' ); | ||
| 131 | return [ map { @$_ } @exc ]; | ||
| 132 | } | ||
| 133 | |||
| 134 | =head3 clear() | ||
| 135 | |||
| 136 | clear all records. | ||
| 137 | |||
| 138 | =cut | ||
| 139 | sub clear | ||
| 140 | { | ||
| 141 | my $self = shift; | ||
| 142 | $self->delete( $TABLE ); | ||
| 143 | } | ||
| 144 | |||
| 145 | sub any | ||
| 146 | { | ||
| 147 | my $self = shift; | ||
| 148 | return $ANY; | ||
| 149 | } | ||
| 150 | |||
| 151 | 1; |