6 $stderr.sync
= $stdout.sync
= true
8 trap('INT') { exit
130 }
9 trap('PIPE') { exit
0 }
11 # this is to be compatible with config files used by the Perl tools
12 def parse_config_file
!(path
, overwrite
= false)
14 File
.open(path
).each_line
do |line
|
16 if /^(domain|class)\s*=\s*(\S+)/.match(line
)
18 elsif m
= /^(?:trackers|hosts)\s*=\s*(.*)/.match(line
)
19 dest
[:hosts] = $1.split(/\s*,\s*/)
20 elsif m
= /^timeout\s*=\s*(.*)/.match(line
)
21 dest
[:timeout] = m
[1].to_f
23 warn
"Ignored configuration line: #{line}" unless /^#/.match(line
)
29 # parse the default config file if one exists
30 def_file
= File
.expand_path("~/.mogilefs-client.conf")
31 def_cfg
= File
.exist
?(def_file
) ? parse_config_file
!(def_file
) : {}
33 # parse the command-line first, these options take precedence over all else
39 cat
= { :raw => false }
42 x
.banner
= "Usage: #{$0} [options] <command> [<arguments>]"
45 x
.on('-c', '--config=/path/to/config',
46 'config file to load') { |file
| config_file
= file
}
48 x
.on('-t', '--trackers=host1[,host2]', '--hosts=host1[,host2]', Array
,
49 'hostnames/IP addresses of trackers') do |trackers
|
50 cli_cfg
[:hosts] = trackers
53 x
.on('-e', 'True if key exists') { test
[:e] = true }
54 x
.on('-r', '--raw', 'show raw big_info file information') { cat
[:raw] = true }
56 x
.on('-C', '--class=s', 'class') { |klass
| cli_cfg
[:class] = klass
}
57 x
.on('-d', '--domain=s', 'domain') { |domain
| cli_cfg
[:domain] = domain
}
58 x
.on('-l', "long listing format (`ls' command)") { ls_l
= true }
59 x
.on('-h', '--human-readable',
60 "print sizes in human-readable format (`ls' command)") { ls_h
= true }
63 x
.on('--help', 'Show this help message.') { puts x
; exit
}
67 # parse the config file specified at the command-line
68 file_cfg
= config_file
? parse_config_file
!(config_file
, true) : {}
70 # read environment variables, too. This Ruby API favors the term
71 # "hosts", however upstream MogileFS teminology favors "trackers" instead.
72 # Favor the term more consistent with what the MogileFS inventors used.
74 if ENV["MOG_TRACKERS"]
75 env_cfg
[:hosts] = ENV["MOG_TRACKERS"].split(/\s*,\s*/)
77 if ENV["MOG_HOSTS"] && (env_cfg
[:hosts] || []).empty
?
78 env_cfg
[:hosts] = ENV["MOG_HOSTS"].split(/\s*,\s*/)
80 env_cfg
[:domain] = ENV["MOG_DOMAIN"] if ENV["MOG_DOMAIN"]
81 env_cfg
[:class] = ENV["MOG_CLASS"] if ENV["MOG_CLASS"]
83 # merge the configs, favoring them in order specified:
84 cfg
= {}.merge(def_cfg
).merge(env_cfg
).merge(file_cfg
).merge(cli_cfg
)
88 err
<< "trackers must be specified" if cfg
[:hosts].nil? || cfg
[:hosts].empty
?
89 err
<< "domain must be specified" unless cfg
[:domain]
91 warn
"Errors:\n #{err.join("\n ")}"
96 unless cmd
= ARGV.shift
101 cfg
[:timeout] ||= 30 # longer timeout for interactive use
102 mg
= MogileFS
::MogileFS.new(cfg
)
104 def store_file_retry(mg
, key
, storage_class
, filepath
)
107 mg
.store_file(key
, storage_class
, filepath
)
108 rescue MogileFS
::UnreadableSocketError,
109 MogileFS
::Backend::NoDevicesError => err
110 if ((tries
+= 1) < 10)
111 warn
"Retrying on error: #{err}: #{err.message} tries: #{tries}"
114 warn
"FATAL: #{err}: #{err.message} tries: #{tries}"
122 %w(K M G
).each
do |s
|
129 sprintf("%.1f%s", size
, suff
)
135 filename
= ARGV.shift
or raise ArgumentError
, '<filename> <key>'
136 key
= ARGV.shift
or raise ArgumentError
, '<filename> <key>'
137 ARGV.shift
and raise ArgumentError
, '<filename> <key>'
138 store_file_retry(mg
, key
, cfg
[:class], filename
)
140 ARGV.empty
? and raise ArgumentError
, '<key1> [<key2> ...]'
142 if (!cat
[:raw] && key
=~
/^_big_info:/)
143 mg
.bigfile_write(key
, $stdout, {:verify => true})
145 mg
.get_file_data(key
, $stdout)
149 prefixes
= ARGV.empty
? ? [ nil ] : ARGV
151 each_key
= lambda
do |key
, size
, devcount
|
152 size
= ls_h
&& size
> 1024 ? human_size(size
) : size
.to_s
153 size
= (' ' * (12 - size
.length
)) << size
# right justify
154 puts
[ sprintf("% 2d", devcount
), size
, key
].pack("A4 A16 A*")
157 each_key
= lambda
{ |key
| puts key
}
159 prefixes
.each
{ |prefix
| mg
.each_key(prefix
, &each_key
) }
161 ARGV.empty
? and raise ArgumentError
, '<key1> [<key2>]'
162 ARGV.each
{ |key
| mg
.delete(key
) }
164 from
= ARGV.shift
or raise ArgumentError
, '<from> <to>'
165 to
= ARGV.shift
or raise ArgumentError
, '<from> <to>'
166 ARGV.shift
and raise ArgumentError
, '<from> <to>'
168 when 'stat' # this outputs a RFC822-like format
169 ARGV.empty
? and raise ArgumentError
, '<key1> [<key2>]'
170 ARGV.each_with_index
do |key
, i
|
171 if size
= mg
.size(key
)
174 mg
.get_paths(key
).each_with_index
do |path
,i
|
175 puts
"URL-#{i}: #{path}"
179 warn
"No such key: #{key}"
184 key
= ARGV.shift
or raise ArgumentError
, '<key>'
185 ARGV.shift
and raise ArgumentError
, '<key>'
186 cfg
[:class] or raise ArgumentError
, 'E: --class must be specified'
188 tmp
= Tempfile
.new('mog-tee') # TODO: explore Transfer-Encoding:chunked :)
190 # if stdout is pointing to /dev/null, don't bother installing the filter.
192 if File
.stat('/dev/null') != $stdout.stat
193 tee_obj
= lambda
do |buf
|
195 [ $stdout, tmp
].each
{ |io
| rv
= io
.write(buf
) }
198 def tee_obj
.write(buf
)
203 MogileFS
::X.copy_stream($stdin, tee_obj
)
204 store_file_retry(mg
, key
, cfg
[:class], tmp
.path
)
209 truth
, ok
= true, nil
210 raise ArgumentError
, "-e must be specified" unless (test
.size
== 1)
212 truth
, key
= case ARGV.size
217 raise ArgumentError
, "#{ARGV[0]}: binary operator expected"
221 raise ArgumentError
, "Too many arguments"
224 paths
= mg
.get_paths(key
)
226 ok
= !!(paths
&& paths
.size
> 0)
228 raise ArgumentError
, "Unknown flag: -#{test.keys.first}"
234 raise ArgumentError
, "Unknown command: #{cmd}"
236 rescue ArgumentError
=> err
237 warn
"Usage: #{$0} #{cmd} #{err.message}"