forked from hachi/MogileFS-Utils
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
what used to be mogtool, is now split up into multiple unix-y utils. "bigfile" by default was very confusing to folks testing the utils. new commands are added and more will be added around troubleshooting
- Loading branch information
Showing
9 changed files
with
353 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,9 @@ lib/MogileFS/Utils.pm | |
mogtool | ||
mogadm | ||
mogstats | ||
mogupload | ||
mogdelete | ||
mogfetch | ||
mogfileinfo | ||
moglistfids | ||
moglistkeys |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,10 @@ WriteMakefile( | |
VERSION_FROM => 'lib/MogileFS/Utils.pm', | ||
AUTHOR => 'Brad Fitzpatrick <[email protected]>', | ||
ABSTRACT => 'MogileFS utilities', | ||
EXE_FILES => ['mogtool', 'mogadm', 'mogstats'], | ||
EXE_FILES => ['mogtool', 'mogadm', 'mogstats', | ||
'mogupload', 'mogfetch', 'mogdelete', 'mogfileinfo', 'moglistkeys', | ||
'moglistfids', | ||
], | ||
PREREQ_PM => { | ||
'LWP::Simple' => 0, | ||
'Compress::Zlib' => 0, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#!/usr/bin/perl | ||
|
||
use strict; | ||
use warnings; | ||
|
||
use lib './lib'; | ||
use MogileFS::Utils; | ||
|
||
my $util = MogileFS::Utils->new; | ||
my $usage = "--trackers=host --domain=foo --key='/hello.jpg'"; | ||
my $c = $util->getopts($usage, 'key=s'); | ||
|
||
my $mogc = $util->client; | ||
|
||
$mogc->delete($c->{key}); | ||
if ($mogc->errcode) { | ||
print STDERR "Error deleting file: ", $mogc->errstr, "\n"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#!/usr/bin/perl | ||
|
||
use strict; | ||
use warnings; | ||
|
||
use lib './lib'; | ||
use MogileFS::Utils; | ||
|
||
my $util = MogileFS::Utils->new; | ||
my $usage = "--trackers=host --domain=foo --key='/hello.jpg' --file='./output'"; | ||
my $c = $util->getopts($usage, qw/key=s file=s/); | ||
|
||
my $mogc = $util->client; | ||
|
||
# Default to noverify, don't hang up the tracker. We'll try all paths. | ||
my @paths = $mogc->get_paths($c->{key}, { noverify => 1 }); | ||
if ($mogc->errcode) { | ||
die "Error fetching paths: " . $mogc->errstr; | ||
} | ||
|
||
die "No paths found or key does not exist" unless @paths; | ||
|
||
my $filename = $c->{file}; | ||
my @resses; | ||
for my $path (@paths) { | ||
next unless $path; # overparanoid? | ||
my $ua = LWP::UserAgent->new; | ||
$ua->timeout(10); | ||
|
||
my $file; | ||
if ($filename eq '-') { | ||
$file = *STDOUT; | ||
} else { | ||
open($file, "> $filename") or die "Could not open " . $filename; | ||
} | ||
|
||
my $writeout = sub { | ||
print $file $_[0]; | ||
}; | ||
my $res = $ua->get($path, ':content_cb' => $writeout, | ||
':read_size_hint' => 32768); | ||
|
||
if ($res->is_success) { | ||
last; | ||
} else { | ||
# print all the errors to be the most helpful | ||
push(@resses, $res); | ||
next; | ||
} | ||
} | ||
|
||
if (@resses) { | ||
for my $res (@resses) { | ||
print STDERR "Got errors while trying to fetch:\n"; | ||
print STDERR $res->status_line, "\n"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#!/usr/bin/perl | ||
|
||
use strict; | ||
use warnings; | ||
|
||
use lib './lib'; | ||
use MogileFS::Utils; | ||
|
||
my $util = MogileFS::Utils->new; | ||
my $usage = "--trackers=host --domain=foo --key='/hello.jpg'"; | ||
my $c = $util->getopts($usage, 'key=s'); | ||
|
||
my $mogc = $util->client; | ||
|
||
my $fid = $mogc->file_info($c->{key}); | ||
if ($mogc->errcode) { | ||
die "Error fetching file info: " . $mogc->errstr; | ||
} | ||
die "Key not found: " . $c->{key} unless $fid; | ||
|
||
# Might replace this with just fetching the devids from above... | ||
my @paths = $mogc->get_paths($c->{key}, { noverify => 1, pathcount => 99 }); | ||
if ($mogc->errcode) { | ||
die "Error fetching paths: " . $mogc->errstr; | ||
} | ||
die "No paths found or key does not exist" unless @paths; | ||
|
||
print "- file: ", $c->{key}, "\n"; | ||
for my $item (sort keys %$fid) { | ||
printf(" %8s: %20s\n", $item, $fid->{$item}); | ||
} | ||
|
||
for my $path (@paths) { | ||
print " - ", $path, "\n"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#!/usr/bin/perl | ||
# Example for building into a backup program. | ||
|
||
use strict; | ||
use warnings; | ||
|
||
use MogileFS::Admin; | ||
use lib './lib'; | ||
use MogileFS::Utils; | ||
|
||
my $util = MogileFS::Utils->new; | ||
my $usage = "--trackers=host --fromfid=123 --count=5000"; | ||
my $c = $util->getopts($usage, qw/fromfid=i count=i/); | ||
|
||
my $moga = MogileFS::Admin->new(hosts => $c->{trackers}); | ||
|
||
my $fromfid = $c->{fromfid} || 0; | ||
my $count = $c->{count} || 100; | ||
|
||
while ($count) { | ||
# Try to fetch the max, but we will likely get less. | ||
my $fids_chunk = $moga->list_fids($fromfid, $count); | ||
if ($moga->errcode) { | ||
die "Error listing fids: ", $moga->errstr, "\n"; | ||
} | ||
my @fids = sort { $a <=> $b } keys %$fids_chunk; | ||
last unless @fids; | ||
$fromfid = $fids[-1]; | ||
$count -= @fids; | ||
for my $fid (@fids) { | ||
my $file = $fids_chunk->{$fid}; | ||
print "fid ", $fid, "\n"; | ||
for my $key (sort keys %$file) { | ||
my $val = $file->{$key}; | ||
$val = _escape_url_string($val) if $key eq 'dkey'; | ||
print $key, " ", $val, "\n"; | ||
} | ||
print "\n"; | ||
} | ||
} | ||
|
||
sub _escape_url_string { | ||
my $str = shift; | ||
$str =~ s/([^a-zA-Z0-9_\,\-.\/\\\: ])/uc sprintf("%%%02x",ord($1))/eg; | ||
$str =~ tr/ /+/; | ||
return $str; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#!/usr/bin/perl | ||
# TODO: Add ways to limit # of keys displayed | ||
|
||
use strict; | ||
use warnings; | ||
|
||
use lib './lib'; | ||
use MogileFS::Utils; | ||
|
||
my $util = MogileFS::Utils->new; | ||
my $usage = "--trackers=host --domain=foo --key_prefix='bar/'"; | ||
my $c = $util->getopts($usage, 'key_prefix=s'); | ||
|
||
my $mogc = $util->client; | ||
|
||
$mogc->foreach_key(prefix => $c->{key_prefix}, sub { | ||
my $key = shift; | ||
print $key, "\n"; | ||
}); | ||
|
||
if ($mogc->errcode) { | ||
print STDERR "Error listing files: ", $mogc->errstr, "\n"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#!/usr/bin/perl | ||
|
||
use strict; | ||
use warnings; | ||
|
||
use lib './lib'; | ||
use MogileFS::Utils; | ||
|
||
my $util = MogileFS::Utils->new; | ||
my $usage = "--trackers=host --domain=foo --key='/hello.jpg' --file='./hello.jpg'"; | ||
my $c = $util->getopts($usage, qw/class=s key=s file=s/); | ||
|
||
my $mogc = $util->client; | ||
|
||
my $filename = $c->{file}; | ||
|
||
my $fh; | ||
my $size; | ||
if ($filename eq '-') { | ||
# Can't upload files without a known filesize? | ||
die "STDIN mode not yet supported"; | ||
$fh = *STDIN; | ||
} else { | ||
$size = -s $filename; | ||
die "Could not stat " . $filename unless defined $size; | ||
open($fh, "< $filename") or die "Could not open " . $filename; | ||
} | ||
|
||
my $mf = $mogc->new_file($c->{key}, $c->{class}, $size); | ||
if ($mogc->errcode) { | ||
die "Error opening MogileFS file: " . $mogc->errstr; | ||
} | ||
|
||
my $buf; | ||
while (my $read = read($fh, $buf, 32768)) { | ||
die "error reading file" unless defined $read; | ||
last if $read == 0; | ||
print $mf $buf; | ||
} | ||
|
||
unless ($mf->close) { | ||
die "Error writing file: " . $mogc->errcode . ": " . $mogc->errstr; | ||
} |