| 1 | package Varnish::CLI; | ||||||||||||||||||||||||||||
| 2 | 2 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use Net::Telnet; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use Digest::SHA qw/sha256_hex/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | =head1 NAME | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | Varnish::CLI - An interface to the Varnish CLI | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | =head1 VERSION | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | Version 0.03 | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | =cut | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 2 | our $VERSION = '0.03'; | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | Varnish CLI is a great administration tool, but a bit difficult to script for. | ||||||||||||||||||||||||||||
| 23 | This module allows easy script interface to it. | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | use Varnish::CLI; | ||||||||||||||||||||||||||||
| 26 | my $varnish = Varnish::CLI->new( host => $host, | ||||||||||||||||||||||||||||
| 27 | port => $port, | ||||||||||||||||||||||||||||
| 28 | timeout => $timeout, | ||||||||||||||||||||||||||||
| 29 | secret => $secret, | ||||||||||||||||||||||||||||
| 30 | ); | ||||||||||||||||||||||||||||
| 31 | $varnish->send( 'url.purge .*' ); | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | The Varnish::CLI can be initialised without any parameters, and will usually work for the default | ||||||||||||||||||||||||||||
| 34 | Varnish settings: | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | my $varnish = Varnish::CLI->new(); | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | If you have started your Varnish CLI with a secret, you must will have to pass the contents | ||||||||||||||||||||||||||||
| 39 | of your secret file, otherwise authentication will fail... Makes sense!! :) | ||||||||||||||||||||||||||||
| 40 | Remember - complete contents of the secret file (including a newline if it exists!) | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | my $varnish = Varnish::CLI->new( secret => $secret ); | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | =head1 PROPERTIES | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | has host => ( is => 'ro', | ||||||||||||||||||||||||||||
| 47 | isa => 'Str', | ||||||||||||||||||||||||||||
| 48 | required => 1, | ||||||||||||||||||||||||||||
| 49 | default => 'localhost' ); | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | has port => ( is => 'ro', | ||||||||||||||||||||||||||||
| 52 | isa => 'Int', | ||||||||||||||||||||||||||||
| 53 | required => 1, | ||||||||||||||||||||||||||||
| 54 | default => 6082 ); | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | has timeout => ( is => 'rw', | ||||||||||||||||||||||||||||
| 57 | isa => 'Int', | ||||||||||||||||||||||||||||
| 58 | required => 1, | ||||||||||||||||||||||||||||
| 59 | default => 1 ); | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | has t => ( is => 'rw', | ||||||||||||||||||||||||||||
| 62 | isa => 'Net::Telnet', | ||||||||||||||||||||||||||||
| 63 | clearer => 'clear_t' ); | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | has secret => ( is => 'rw', | ||||||||||||||||||||||||||||
| 66 | isa => 'Str' ); | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | has connected => ( is => 'rw', | ||||||||||||||||||||||||||||
| 69 | isa => 'Int', | ||||||||||||||||||||||||||||
| 70 | default => 0, | ||||||||||||||||||||||||||||
| 71 | required => 1 ); | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | has last_lines => ( is => 'rw', | ||||||||||||||||||||||||||||
| 74 | isa => 'ArrayRef', | ||||||||||||||||||||||||||||
| 75 | default => sub{ [] } ); | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | has last_status => ( is => 'rw', | ||||||||||||||||||||||||||||
| 78 | isa => 'Int', | ||||||||||||||||||||||||||||
| 79 | ); | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | =cut | ||||||||||||||||||||||||||||
| 82 | 2 | has host => ( is => 'ro', | |||||||||||||||||||||||||||
| 83 | isa => 'Str', | ||||||||||||||||||||||||||||
| 84 | required => 1, | ||||||||||||||||||||||||||||
| 85 | default => 'localhost' ); | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 2 | has port => ( is => 'ro', | |||||||||||||||||||||||||||
| 88 | isa => 'Int', | ||||||||||||||||||||||||||||
| 89 | required => 1, | ||||||||||||||||||||||||||||
| 90 | default => 6082 ); | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 2 | has timeout => ( is => 'rw', | |||||||||||||||||||||||||||
| 93 | isa => 'Int', | ||||||||||||||||||||||||||||
| 94 | required => 1, | ||||||||||||||||||||||||||||
| 95 | default => 1 ); | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 2 | has secret => ( is => 'rw', | |||||||||||||||||||||||||||
| 98 | isa => 'Str' ); | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 2 | has t => ( is => 'rw', | |||||||||||||||||||||||||||
| 101 | isa => 'Net::Telnet', | ||||||||||||||||||||||||||||
| 102 | clearer => 'clear_t' ); | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 2 | has connected => ( is => 'rw', | |||||||||||||||||||||||||||
| 105 | isa => 'Int', | ||||||||||||||||||||||||||||
| 106 | default => 0, | ||||||||||||||||||||||||||||
| 107 | required => 1 ); | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | has last_lines => ( is => 'rw', | ||||||||||||||||||||||||||||
| 110 | isa => 'ArrayRef', | ||||||||||||||||||||||||||||
| 111 | 0 | ▶ | default => sub{ [] } ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 2 | has last_status => ( is => 'rw', | |||||||||||||||||||||||||||
| 114 | isa => 'Int', | ||||||||||||||||||||||||||||
| 115 | ); | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | =head1 SUBROUTINES/METHODS | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | =head2 connect | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | Connect to the Varnish CLI interface | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | =cut | ||||||||||||||||||||||||||||
| 125 | 0 | ▶ | sub connect{ | ||||||||||||||||||||||||||
|
Subroutine
connect: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||
| 126 | 0 | my( $self ) = shift; | |||||||||||||||||||||||||||
| 127 | 0 | ▶ | if( $self->t() and $self->connected() ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->t and $self->connected
MC/DC0%0/2
$self->t and $self->connected
$self->t
$self->connected
Truth table0%0/3
A and B
A= $self->tB= $self->connected
| |||||||||||||||||||||||||||||
| 128 | 0 | return 1; | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | 0 | my $t = Net::Telnet->new( | |||||||||||||||||||||||||||
| 131 | Host => $self->host(), | ||||||||||||||||||||||||||||
| 132 | Port => $self->port(), | ||||||||||||||||||||||||||||
| 133 | Timeout => $self->timeout(), | ||||||||||||||||||||||||||||
| 134 | Output_record_separator => "\n", | ||||||||||||||||||||||||||||
| 135 | Input_record_separator => "\n", | ||||||||||||||||||||||||||||
| 136 | ); | ||||||||||||||||||||||||||||
| 137 | 0 | $self->t( $t ); | |||||||||||||||||||||||||||
| 138 | 0 | $t->open(); | |||||||||||||||||||||||||||
| 139 | 0 | $self->_parse_response(); | |||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | # A 107 response on connection means the Varnish CLI expects authentication | ||||||||||||||||||||||||||||
| 142 | 0 | ▶ | if( $self->last_status() == 107 ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | 0 | ▶ | if( not $self->secret() ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | 0 | croak( "Connection failed: authentication required, but no secret given\n" ); | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 0 | my $challenge = substr( $self->last_lines()->[0], 0, 32 ); | |||||||||||||||||||||||||||
| 148 | 0 | my $auth = sha256_hex( $challenge . "\n" . $self->secret() . $challenge . "\n" ); | |||||||||||||||||||||||||||
| 149 | 0 | $self->send( "auth $auth" ); | |||||||||||||||||||||||||||
| 150 | 0 | ▶ | if( $self->last_status != 200 ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | 0 | croak( "Authentication failed!\n" ); | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 0 | ▶ | if( $self->last_status() != 200 ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | croak( "Connection failed\nStatus: " . $self->last_status() . "\n". | ||||||||||||||||||||||||||||
| 157 | 0 | "Last lines: \n\t" . join( "\t", @{ $self->last_lines() } ) . "\n" ); | |||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | 0 | return 1; | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | =head2 close | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | Close the connection to the Varnish CLI interface | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | =cut | ||||||||||||||||||||||||||||
| 167 | 0 | ▶ | sub close{ | ||||||||||||||||||||||||||
|
Subroutine
close: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 168 | 0 | my( $self ) = shift; | |||||||||||||||||||||||||||
| 169 | 0 | ▶ | if( not $self->t() or not $self->connected() ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->t and $self->connected
MC/DC0%0/2
$self->t and $self->connected
$self->t
$self->connected
Truth table0%0/3
A and B
A= $self->tB= $self->connected
| |||||||||||||||||||||||||||||
| 170 | 0 | carp( "Close called, but not connected" ); | |||||||||||||||||||||||||||
| 171 | 0 | return 1; | |||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | 0 | my $t = $self->t(); | |||||||||||||||||||||||||||
| 174 | 0 | $t->print( 'quit' ); | |||||||||||||||||||||||||||
| 175 | 0 | $t->close(); | |||||||||||||||||||||||||||
| 176 | 0 | $self->clear_t(); | |||||||||||||||||||||||||||
| 177 | 0 | $self->connected( 0 ); | |||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | =head2 send | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | Send a command to the Varnish CLi | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | =cut | ||||||||||||||||||||||||||||
| 185 | 0 | ▶ | sub send{ | ||||||||||||||||||||||||||
|
Subroutine
send: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 186 | 0 | my( $self, $command ) = @_; | |||||||||||||||||||||||||||
| 187 | 0 | ▶ | if( ! $command ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 188 | 0 | croak( "Cannot call send without a command" ); | |||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||
| 190 | # Make sure we're connected | ||||||||||||||||||||||||||||
| 191 | 0 | $self->connect(); | |||||||||||||||||||||||||||
| 192 | 0 | $self->t->print( $command ); | |||||||||||||||||||||||||||
| 193 | 0 | $self->_parse_response(); | |||||||||||||||||||||||||||
| 194 | 0 | ▶ | if( $self->last_status() != 200 ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 195 | croak( "Command failed: $command\nStatus: " . $self->last_status() . "\n". | ||||||||||||||||||||||||||||
| 196 | 0 | "Last lines: \n\t" . join( "\t", @{ $self->last_lines() } ) . "\n" ); | |||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | # Private method to parse the response from the CLI | ||||||||||||||||||||||||||||
| 201 | 0 | ▶ | sub _parse_response{ | ||||||||||||||||||||||||||
|
Subroutine
_parse_response: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 202 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 203 | 0 | my $t = $self->t(); | |||||||||||||||||||||||||||
| 204 | 0 | my $line = $t->getline(); | |||||||||||||||||||||||||||
| 205 | 0 | ▶ | if( $line !~ m/^(\d+)\s*(\d+)\s*$/ ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 206 | 0 | $self->connected( 0 ); | |||||||||||||||||||||||||||
| 207 | 0 | print "Next line:\n"; | |||||||||||||||||||||||||||
| 208 | 0 | print $t->getline(); | |||||||||||||||||||||||||||
| 209 | 0 | croak( "Unexpected line:\n($line)" ); | |||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||
| 212 | 0 | my $status = $1; | |||||||||||||||||||||||||||
| 213 | 0 | my $chars = $2; | |||||||||||||||||||||||||||
| 214 | 0 | my $got_chars = 0; | |||||||||||||||||||||||||||
| 215 | 0 | $self->connected( 1 ); | |||||||||||||||||||||||||||
| 216 | 0 | my @lines; | |||||||||||||||||||||||||||
| 217 | 0 | ▶ | while( $got_chars < $chars ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 218 | 0 | push( @lines, $t->getline() ); | |||||||||||||||||||||||||||
| 219 | 0 | $got_chars += length( $lines[-1] ); | |||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||
| 221 | # There's always one empty line after | ||||||||||||||||||||||||||||
| 222 | 0 | push( @lines, $t->getline() ); | |||||||||||||||||||||||||||
| 223 | 0 | $self->last_lines( \@lines ); | |||||||||||||||||||||||||||
| 224 | 0 | $self->last_status( $status ); | |||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | Robin Clarke, C<< <perl at robinclarke.net> >> | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | =head1 BUGS | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | Please report any bugs or feature requests to C<bug-varnish at rt.cpan.org>, or through | ||||||||||||||||||||||||||||
| 234 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Varnish>. I will be notified, and then you'll | ||||||||||||||||||||||||||||
| 235 | automatically be notified of progress on your bug as I make changes. | ||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | perldoc Varnish::CLI | ||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | You can also look for information at: | ||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | =over 4 | ||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | =item * Repository on Github | ||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||
| 252 | L<https://github.com/robin13/Varnish-CLI> | ||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | =item * RT: CPAN's request tracker | ||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Varnish> | ||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||
| 258 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||
| 260 | L<http://annocpan.org/dist/Varnish> | ||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||
| 262 | =item * CPAN Ratings | ||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | L<http://cpanratings.perl.org/d/Varnish> | ||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||
| 266 | =item * Search CPAN | ||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||
| 268 | L<http://search.cpan.org/dist/Varnish/> | ||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||
| 270 | =back | ||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||
| 273 | =head1 ACKNOWLEDGEMENTS | ||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | L<http://www.varnish-cache.org/> | ||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||
| 279 | Copyright 2011 Robin Clarke. | ||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||
| 281 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||
| 282 | under the terms of either: the GNU General Public License as published | ||||||||||||||||||||||||||||
| 283 | by the Free Software Foundation; or the Artistic License. | ||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||
| 285 | See http://dev.perl.org/licenses/ for more information. | ||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||
| 288 | =cut | ||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||
| 290 | 2 | 1; # End of Varnish | |||||||||||||||||||||||||||