| 1 | package Brackup::Target::GoogleAppEngine; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use base 'Brackup::Target'; | ||
| 6 | use Carp qw(croak); | ||
| 7 | use LWP::ConnCache; | ||
| 8 | use LWP::UserAgent; | ||
| 9 | use HTTP::Request; | ||
| 10 | use HTTP::Request::Common; | ||
| 11 | |||
| 12 | # fields in object: | ||
| 13 | # user_email | ||
| 14 | # password | ||
| 15 | # url | ||
| 16 | |||
| 17 | sub new { | ||
| 18 | my ($class, $confsec) = @_; | ||
| 19 | my $self = $class->SUPER::new($confsec); | ||
| 20 | |||
| 21 | $self->{user_email} = $confsec->value("user_email") | ||
| 22 | or die "No 'user_email'"; | ||
| 23 | $self->{password} = $confsec->value("password") | ||
| 24 | or die "No 'password'"; | ||
| 25 | $self->{url} = $confsec->value("server_url") | ||
| 26 | or die "No 'server_url'"; | ||
| 27 | |||
| 28 | return $self->_init; | ||
| 29 | } | ||
| 30 | |||
| 31 | sub _init { | ||
| 32 | my $self = shift; | ||
| 33 | $self->{url} =~ s!/$!!; | ||
| 34 | my $conn_cache = LWP::ConnCache->new(total_capacity => 10); | ||
| 35 | $self->{ua} = LWP::UserAgent->new(conn_cache => $conn_cache); | ||
| 36 | |||
| 37 | $self->{upload_urls} = []; | ||
| 38 | return $self; | ||
| 39 | } | ||
| 40 | |||
| 41 | sub _prompt { | ||
| 42 | my ($q) = @_; | ||
| 43 | print $q if $q; | ||
| 44 | my $ans = <STDIN>; | ||
| 45 | $ans =~ s/^\s+//; | ||
| 46 | $ans =~ s/\s+$//; | ||
| 47 | return $ans; | ||
| 48 | } | ||
| 49 | |||
| 50 | sub backup_header { | ||
| 51 | my $self = shift; | ||
| 52 | return { | ||
| 53 | "UserEmail" => $self->{user_email}, | ||
| 54 | "URL" => $self->{url}, | ||
| 55 | }; | ||
| 56 | } | ||
| 57 | |||
| 58 | sub new_from_backup_header { | ||
| 59 | my ($class, $header) = @_; | ||
| 60 | my $password = _prompt("App Engine Target Server Password for $header->{UserEmail}: ") | ||
| 61 | or die "Password required.\n"; | ||
| 62 | my $self = bless { | ||
| 63 | user_email => $header->{UserEmail}, | ||
| 64 | url => $header->{URL}, | ||
| 65 | password => $password, | ||
| 66 | }, $class; | ||
| 67 | return $self->_init; | ||
| 68 | } | ||
| 69 | |||
| 70 | sub has_chunk { | ||
| 71 | my ($self, $chunk) = @_; | ||
| 72 | my $dig = $chunk->backup_digest; # "sha1:sdfsdf" format scalar | ||
| 73 | |||
| 74 | die "no impl"; | ||
| 75 | return 0; | ||
| 76 | } | ||
| 77 | |||
| 78 | sub load_chunk { | ||
| 79 | my ($self, $dig) = @_; | ||
| 80 | my $req = GET("$self->{url}/get_chunk?digest=$dig&" . | ||
| 81 | "password=" . _eurl($self->{password}) . "&" . | ||
| 82 | "user_email=" . $self->{user_email}); | ||
| 83 | my $res = $self->{ua}->request($req); | ||
| 84 | if ($res->is_success) { | ||
| 85 | my $content_type = $res->header("Content-Type"); | ||
| 86 | die "Expected x-danga/brackup-chunk content type but got $content_type." | ||
| 87 | unless $content_type eq "x-danga/brackup-chunk"; | ||
| 88 | my $content_ref = \ scalar $res->content; | ||
| 89 | # TODO: verify digest out of paranoia? | ||
| 90 | return $content_ref; | ||
| 91 | } else { | ||
| 92 | warn "Failed to get chunk $dig: " . $res->status_line . "\n" | ||
| 93 | . $res->content; | ||
| 94 | } | ||
| 95 | return 0; | ||
| 96 | } | ||
| 97 | |||
| 98 | sub _eurl { | ||
| 99 | my $a = defined $_[0] ? $_[0] : ""; | ||
| 100 | $a =~ s/([^a-zA-Z0-9_\,\-.\/\\\: ])/uc sprintf("%%%02x",ord($1))/eg; | ||
| 101 | $a =~ tr/ /+/; | ||
| 102 | return $a; | ||
| 103 | } | ||
| 104 | |||
| 105 | sub _get_upload_url { | ||
| 106 | my $self = shift; | ||
| 107 | my $for_backup = shift || 0; | ||
| 108 | |||
| 109 | if (!$for_backup && @{$self->{upload_urls}}) { | ||
| 110 | my $url = shift @{$self->{upload_urls}}; | ||
| 111 | die "Bogus URL: $url" unless $url =~ /^http/; | ||
| 112 | return $url; | ||
| 113 | } | ||
| 114 | |||
| 115 | my $count = $for_backup ? 1 : 10; | ||
| 116 | |||
| 117 | my $req = HTTP::Request->new("GET", | ||
| 118 | "$self->{url}/get_upload_urls?" . | ||
| 119 | "for_backup=$for_backup&" . | ||
| 120 | "count=$count&" . | ||
| 121 | "password=" . _eurl($self->{password}) . "&" . | ||
| 122 | "user_email=" . $self->{user_email}); | ||
| 123 | my $res = $self->{ua}->request($req); | ||
| 124 | if ($res->is_success) { | ||
| 125 | $self->{upload_urls} = [ split(/\s*\n\s*/, $res->content) ]; | ||
| 126 | } else { | ||
| 127 | die "Failed to get upload URLs: " . $res->status_line . "\n" . $res->content; | ||
| 128 | } | ||
| 129 | |||
| 130 | my $url = shift @{$self->{upload_urls}}; | ||
| 131 | die "Bogus URL: $url" unless $url =~ /^http/; | ||
| 132 | return $url; | ||
| 133 | } | ||
| 134 | |||
| 135 | sub store_chunk { | ||
| 136 | my ($self, $chunk) = @_; | ||
| 137 | my $dig = $chunk->backup_digest; | ||
| 138 | my $blen = $chunk->backup_length; | ||
| 139 | my $chunkref = $chunk->chunkref; | ||
| 140 | |||
| 141 | my $upload_url = $self->_get_upload_url | ||
| 142 | or die; | ||
| 143 | |||
| 144 | my $filename = $dig; | ||
| 145 | $filename =~ s/:/_/; | ||
| 146 | $filename .= ".chunk"; | ||
| 147 | |||
| 148 | print "Storing chunk: $dig\n"; | ||
| 149 | |||
| 150 | my $content = do { local $/; <$chunkref> }; | ||
| 151 | |||
| 152 | my $req = HTTP::Request::Common::POST($upload_url, | ||
| 153 | Content_Type => 'form-data', | ||
| 154 | Content => [ | ||
| 155 | "password" => $self->{password}, | ||
| 156 | "user_email" => $self->{user_email}, | ||
| 157 | "algo_digest" => $dig, | ||
| 158 | "size" => $blen, | ||
| 159 | "file" => [ undef, $filename, | ||
| 160 | "Content-Type" => "x-danga/brackup-chunk", | ||
| 161 | Content => $content ] | ||
| 162 | ]); | ||
| 163 | |||
| 164 | my $location = 0; | ||
| 165 | my $n_errors = 0; | ||
| 166 | while ($n_errors < 10) { | ||
| 167 | my $res = $self->{ua}->simple_request($req); | ||
| 168 | if ($res->status_line =~ /^500/) { | ||
| 169 | # AppEngine's datastore decided to time out on its | ||
| 170 | # un-contended transactions? Bleh. | ||
| 171 | $n_errors++; | ||
| 172 | warn "500 error from AppEngine (errors=$n_errors). Retrying after some sleep.\n"; | ||
| 173 | sleep(5); | ||
| 174 | next; | ||
| 175 | } | ||
| 176 | |||
| 177 | unless ($res->status_line =~ /^302/) { | ||
| 178 | # TODO: retries on 5xx? | ||
| 179 | die "Expected 302 redirect from AppEngine, got: " . $res->status_line; | ||
| 180 | } | ||
| 181 | |||
| 182 | my $location = $res->header("Location"); | ||
| 183 | return 1 if $location =~ m!/success$!; | ||
| 184 | |||
| 185 | warn "Got error message from AppEngine: $location\n"; | ||
| 186 | return 0; | ||
| 187 | } | ||
| 188 | |||
| 189 | warn "Too many failures."; | ||
| 190 | return 0; | ||
| 191 | } | ||
| 192 | |||
| 193 | sub delete_chunk { | ||
| 194 | my ($self, $dig) = @_; | ||
| 195 | die "no impl"; | ||
| 196 | return 0; | ||
| 197 | } | ||
| 198 | |||
| 199 | sub chunks { | ||
| 200 | my $self = shift; | ||
| 201 | |||
| 202 | } | ||
| 203 | |||
| 204 | sub store_backup_meta { | ||
| 205 | my ($self, $name, $fh, $meta) = @_; | ||
| 206 | $meta ||= {}; | ||
| 207 | |||
| 208 | print "Storing backup: $name\n"; | ||
| 209 | |||
| 210 | my $upload_url = $self->_get_upload_url(1) # for backup | ||
| 211 | or die; | ||
| 212 | |||
| 213 | my $content = do { local $/; <$fh> }; | ||
| 214 | |||
| 215 | my $req = HTTP::Request::Common::POST($upload_url, | ||
| 216 | Content_Type => 'form-data', | ||
| 217 | Content => [ | ||
| 218 | "password" => $self->{password}, | ||
| 219 | "user_email" => $self->{user_email}, | ||
| 220 | "encrypted" => $meta->{is_encrypted} ? 1 : 0, | ||
| 221 | "title" => $name, | ||
| 222 | "file" => [ undef, $name, | ||
| 223 | "Content-Type" => "x-danga/brackup-backup", | ||
| 224 | Content => $content ] | ||
| 225 | ]); | ||
| 226 | |||
| 227 | my $res = $self->{ua}->simple_request($req); | ||
| 228 | unless ($res->status_line =~ /^302/) { | ||
| 229 | # TODO: retries on 5xx? | ||
| 230 | die "Expected 302 redirect from AppEngine, got: " . $res->status_line; | ||
| 231 | } | ||
| 232 | |||
| 233 | my $location = $res->header("Location"); | ||
| 234 | return 1 if $location =~ m!/success$!; | ||
| 235 | |||
| 236 | warn "Got error message from AppEngine: $location\n"; | ||
| 237 | return 0; | ||
| 238 | } | ||
| 239 | |||
| 240 | sub backups { | ||
| 241 | my $self = shift; | ||
| 242 | die "no impl"; | ||
| 243 | return (); | ||
| 244 | } | ||
| 245 | |||
| 246 | sub get_backup { | ||
| 247 | my $self = shift; | ||
| 248 | my ($name, $output_file) = @_; | ||
| 249 | die "no impl" | ||
| 250 | } | ||
| 251 | |||
| 252 | sub delete_backup { | ||
| 253 | my $self = shift; | ||
| 254 | my $name = shift; | ||
| 255 | die "no impl" | ||
| 256 | } | ||
| 257 | |||
| 258 | ############################################################# | ||
| 259 | # These functions are for the brackup-verify-inventory script | ||
| 260 | ############################################################# | ||
| 261 | |||
| 262 | sub chunkpath { | ||
| 263 | my $self = shift; | ||
| 264 | my $dig = shift; | ||
| 265 | die "no impl"; | ||
| 266 | #return $dig; | ||
| 267 | } | ||
| 268 | |||
| 269 | sub size { | ||
| 270 | my $self = shift; | ||
| 271 | my $dig = shift; | ||
| 272 | |||
| 273 | die "no impl"; | ||
| 274 | #return $size; | ||
| 275 | } | ||
| 276 | |||
| 277 | 1; | ||
| 278 | |||
| 279 | =head1 NAME | ||
| 280 | |||
| 281 | Brackup::Target::GoogleAppEngine - backup to the App Engine target server | ||
| 282 | |||
| 283 | =head1 WARNING WARNING WARNING | ||
| 284 | |||
| 285 | This isn't totally done yet. B<Don't trust it quite yet>. Restore should | ||
| 286 | work now, but storing and re-retrieving metafiles isn't done yet, for instance. | ||
| 287 | |||
| 288 | =head1 EXAMPLE | ||
| 289 | |||
| 290 | In your ~/.brackup.conf file: | ||
| 291 | |||
| 292 | [TARGET:google] | ||
| 293 | type = GoogleAppEngine | ||
| 294 | user_email = .... | ||
| 295 | password = .... | ||
| 296 | server_url = .... | ||
| 297 | |||
| 298 | =head1 CONFIG OPTIONS | ||
| 299 | |||
| 300 | =over | ||
| 301 | |||
| 302 | =item B<type> | ||
| 303 | |||
| 304 | Must be "B<GoogleAppEngine>". | ||
| 305 | |||
| 306 | =item B<user_email> | ||
| 307 | |||
| 308 | Email address that you've logged into your brackup-gae-server instance | ||
| 309 | with and configured uploading. | ||
| 310 | |||
| 311 | =item B<password> | ||
| 312 | |||
| 313 | Your brackup-gae-server password. B<NOT> your Google account's password. | ||
| 314 | |||
| 315 | You should make a separate password just for this. | ||
| 316 | |||
| 317 | =item B<server_url> | ||
| 318 | |||
| 319 | URL to your brackup-gae-server instance. | ||
| 320 | |||
| 321 | Source code to run your own instance is at: L<http://github.com/bradfitz/brackup-gae-server> | ||
| 322 | |||
| 323 | =back | ||
| 324 | |||
| 325 | =head1 WARRANTY AND SUPPORT | ||
| 326 | |||
| 327 | None. Use this at your own risk. I'm a Google employee, but I'm not | ||
| 328 | writing this as a Google employee, and this is not a Google product. | ||
| 329 | |||
| 330 | This comes with no warranty, neither expressed nor implied. | ||
| 331 | |||
| 332 | Also, it doesn't even work yet. It's still in development. See the | ||
| 333 | WARNING WARNING WARNING section at top. | ||
| 334 | |||
| 335 | =head1 SEE ALSO | ||
| 336 | |||
| 337 | L<Brackup::Target> | ||
| 338 | |||
| 339 | =head1 AUTHOR | ||
| 340 | |||
| 341 | Brad Fitzpatrick E<lt>brad@danga.comE<gt> | ||
| 342 | |||
| 343 |