Checking in changes prior to tagging of version 2.30.
[MogileFS-Utils.git] / lib / MogileFS / Utils.pm
bloba8ebe909ba3db50b530621f81127a9604628193e
1 #!/usr/bin/perl
2 package MogileFS::Utils;
4 our $VERSION = '2.30';
6 use Getopt::Long;
7 use MogileFS::Client;
9 use fields (
10 'config'
13 # Helper object for the individual utilities.
14 sub new {
15 my MogileFS::Utils $self = shift;
16 $self = fields::new($self) unless ref $self;
17 $self->_init(@_);
19 return $self;
22 # Predefine some options via configuration.
23 sub _init {
24 my MogileFS::Utils $self = shift;
26 $self->{config} = {};
29 sub _readconf {
30 my MogileFS::Utils $self = shift;
31 my $args = shift;
33 # Liftedish from mogadm, but we can refactor mogadm to use this instead.
34 my @configs = ($args->{conf}, $ENV{MOGUTILSCONF},
35 "$ENV{HOME}/.mogilefs.conf",
36 "/etc/mogilefs/mogilefs.conf");
37 my %opts = ();
38 for my $fn (reverse @configs) {
39 next unless $fn && -e $fn;
40 open my $file, "<$fn"
41 or die "unable to open $fn: $!";
42 while (<$file>) {
43 s/\#.*//;
44 next unless m/^\s*(\w+)\s*=\s*(.+?)\s*$/;
45 $opts{$1} = $2 unless ( defined $opts{$1} );
47 close $file;
50 return \%opts;
53 sub config {
54 my MogileFS::Utils $self = shift;
55 return $self->{config};
58 sub getopts {
59 my MogileFS::Utils $self = shift;
60 my $usage = shift;
61 my @want = @_;
63 my %opts = ();
64 $self->abort_usage($usage) unless @ARGV;
65 GetOptions(\%opts, @want, qw/help trackers=s domain=s conf=s/)
66 or $self->abort_usage($usage);
67 my $config = $self->_readconf(\%opts);
69 $self->{config} = {%$config, %opts};
70 $self->_verify_config;
71 $self->abort_usage($usage) if $self->{config}->{help};
73 return $self->{config};
76 sub _verify_config {
77 my MogileFS::Utils $self = shift;
78 my $conf = $self->{config};
80 while (my ($k, $v) = each %$conf) {
81 if ($k =~ m/^trackers/) {
82 my @tr = split /,/, $v;
83 for (@tr) {
84 # Client is obnoxious about requiring a port.
85 if ($_ !~ m/:\d+/) {
86 $_ = $_ . ':7001';
89 $conf->{$k} = \@tr;
90 } elsif ($k =~ m/class/) {
91 # "" means "default". Might have to remove this if people have
92 # been adding "default" classes, which I don't think is possible?
93 if ($v eq 'default') {
94 $conf->{$k} = '';
100 # Do we want to be fancier here?
101 sub abort_usage {
102 my MogileFS::Utils $self = shift;
103 my $usage = shift;
104 print "Usage: $0 $usage\n";
105 exit;
108 sub client {
109 my MogileFS::Utils $self = shift;
110 my $c = $self->{config};
111 return MogileFS::Client->new(domain => $c->{domain},
112 hosts => $c->{trackers});
115 =head1 NAME
117 MogileFS::Utils - Command line utilities for the MogileFS distributed file system.
119 =head1 SYNOPSIS
121 L<mogadm>
123 L<mogstats>
125 L<mogupload>
127 L<mogfetch>
129 L<mogdelete>
131 L<mogfileinfo>
133 L<moglistkeys>
135 L<moglistfids>
137 L<mogfiledebug>
139 L<mogtool> (DEPRECATED: Do not use!)
141 =head1 SUMMARY
143 Please refer to the documentation for the tools included in this distribution.
145 =head1 CONFIGURATION FILE
147 Most of the utilities in this package support a configuration file. Common
148 options can be pushed into the config file, such as trackers, domain, or
149 class. The file is in B</etc/mogilefs/mogilefs.conf> and B<~/.mogilefs.conf>
150 by default. You may also specify a configuration via B<--conf=filename>
152 Example:
154 trackers = 10.0.0.1:7001,10.0.0.3:7001
155 domain = foo
157 =head1 AUTHOR
159 Brad Fitzpatrick E<lt>L<brad@danga.com>E<gt>
161 Dormando E<lt>L<dormando@rydia.net>E<gt>
163 =head1 BUGS
165 Please report any on the MogileFS mailing list: L<http://groups.google.com/group/mogile/>.
167 =head1 LICENSE
169 Licensed for use and redistribution under the same terms as Perl itself.
171 =cut