4 [ STDIN, STDOUT, STDERR].each
{ |io
| io
.binmode
}
6 trap('INT') { exit
130 }
7 trap('PIPE') { exit
0 }
9 # this is to be compatible with config files used by the Perl tools
10 def parse_config_file
!(path
, overwrite
= false)
12 File
.open(path
).each_line
do |line
|
14 if /^(domain|class)\s*=\s*(\S+)/.match(line
)
16 elsif m
= /^(?:trackers|hosts)\s*=\s*(.*)/.match(line
)
17 dest
[:hosts] = $1.split(/\s*,\s*/)
18 elsif m
= /^timeout\s*=\s*(.*)/.match(line
)
19 dest
[:timeout] = m
[1].to_f
21 STDERR.puts
"Ignored configuration line: #{line}" unless /^#/.match(line
)
27 # parse the default config file if one exists
28 def_file
= File
.expand_path("~/.mogilefs-client.conf")
29 def_cfg
= File
.exist
?(def_file
) ? parse_config_file
!(def_file
) : {}
31 # parse the command-line first, these options take precedence over all else
37 cat
= { :raw => false }
40 x
.banner
= "Usage: #{$0} [options] <command> [<arguments>]"
43 x
.on('-c', '--config=/path/to/config',
44 'config file to load') { |file
| config_file
= file
}
46 x
.on('-t', '--trackers=host1[,host2]', '--hosts=host1[,host2]', Array
,
47 'hostnames/IP addresses of trackers') do |trackers
|
48 cli_cfg
[:hosts] = trackers
51 x
.on('-e', 'True if key exists') { test
[:e] = true }
52 x
.on('-r', '--raw', 'show raw big_info file information') { cat
[:raw] = true }
54 x
.on('-C', '--class=s', 'class') { |klass
| cli_cfg
[:class] = klass
}
55 x
.on('-d', '--domain=s', 'domain') { |domain
| cli_cfg
[:domain] = domain
}
56 x
.on('-l', "long listing format (`ls' command)") { ls_l
= true }
57 x
.on('-h', '--human-readable',
58 "print sizes in human-readable format (`ls' command)") { ls_h
= true }
61 x
.on('--help', 'Show this help message.') { puts x
; exit
}
65 # parse the config file specified at the command-line
66 file_cfg
= config_file
? parse_config_file
!(config_file
, true) : {}
68 # read environment variables, too. This Ruby API favors the term
69 # "hosts", however upstream MogileFS teminology favors "trackers" instead.
70 # Favor the term more consistent with what the MogileFS inventors used.
72 if ENV["MOG_TRACKERS"]
73 env_cfg
[:hosts] = ENV["MOG_TRACKERS"].split(/\s*,\s*/)
75 if ENV["MOG_HOSTS"] && (env_cfg
[:hosts] || []).empty
?
76 env_cfg
[:hosts] = ENV["MOG_HOSTS"].split(/\s*,\s*/)
78 env_cfg
[:domain] = ENV["MOG_DOMAIN"] if ENV["MOG_DOMAIN"]
79 env_cfg
[:class] = ENV["MOG_CLASS"] if ENV["MOG_CLASS"]
81 # merge the configs, favoring them in order specified:
82 cfg
= {}.merge(def_cfg
).merge(env_cfg
).merge(file_cfg
).merge(cli_cfg
)
86 err
<< "trackers must be specified" if cfg
[:hosts].nil? || cfg
[:hosts].empty
?
87 err
<< "domain must be specified" unless cfg
[:domain]
89 STDERR.puts
"Errors:\n #{err.join("\n ")}"
90 STDERR.puts
ARGV.options
94 unless cmd
= ARGV.shift
95 STDERR.puts
ARGV.options
99 cfg
[:timeout] ||= 30 # longer timeout for interactive use
100 mg
= MogileFS
::MogileFS.new(cfg
)
102 def store_file_retry(mg
, key
, storage_class
, filepath
)
105 mg
.store_file(key
, storage_class
, filepath
)
106 rescue MogileFS
::UnreadableSocketError,
107 MogileFS
::Backend::NoDevicesError => err
108 if ((tries
+= 1) < 10)
109 STDERR.puts
"Retrying on error: #{err}: #{err.message} tries: #{tries}"
112 STDERR.puts
"FATAL: #{err}: #{err.message} tries: #{tries}"
120 %w(K M G
).each
do |s
|
127 sprintf("%.1f%s", size
, suff
)
133 filename
= ARGV.shift
or raise ArgumentError
, '<filename> <key>'
134 key
= ARGV.shift
or raise ArgumentError
, '<filename> <key>'
135 ARGV.shift
and raise ArgumentError
, '<filename> <key>'
136 store_file_retry(mg
, key
, cfg
[:class], filename
)
138 ARGV.empty
? and raise ArgumentError
, '<key1> [<key2> ...]'
140 if (!cat
[:raw] && key
=~
/^_big_info:/)
141 mg
.bigfile_write(key
, STDOUT, {:verify => true})
143 mg
.get_file_data(key
, STDOUT)
147 prefixes
= ARGV.empty
? ? [ nil ] : ARGV
149 each_key
= lambda
do |key
, size
, devcount
|
150 size
= ls_h
&& size
> 1024 ? human_size(size
) : size
.to_s
151 size
= (' ' * (12 - size
.length
)) << size
# right justify
152 puts
[ sprintf("% 2d", devcount
), size
, key
].pack("A4 A16 A*")
155 each_key
= lambda
{ |key
| puts key
}
157 prefixes
.each
{ |prefix
| mg
.each_key(prefix
, &each_key
) }
159 ARGV.empty
? and raise ArgumentError
, '<key1> [<key2>]'
160 ARGV.each
{ |key
| mg
.delete(key
) }
162 from
= ARGV.shift
or raise ArgumentError
, '<from> <to>'
163 to
= ARGV.shift
or raise ArgumentError
, '<from> <to>'
164 ARGV.shift
and raise ArgumentError
, '<from> <to>'
166 when 'stat' # this outputs a RFC822-like format
167 ARGV.empty
? and raise ArgumentError
, '<key1> [<key2>]'
168 ARGV.each_with_index
do |key
, i
|
169 if size
= mg
.size(key
)
172 mg
.get_paths(key
).each_with_index
do |path
,i
|
173 puts
"URL-#{i}: #{path}"
177 STDERR.puts
"No such key: #{key}"
182 key
= ARGV.shift
or raise ArgumentError
, '<key>'
183 ARGV.shift
and raise ArgumentError
, '<key>'
184 cfg
[:class] or raise ArgumentError
, 'E: --class must be specified'
186 tmp
= Tempfile
.new('mog-tee') # TODO: explore Transfer-Encoding:chunked :)
188 # if stdout is pointing to /dev/null, don't bother installing the filter.
191 if File
.stat('/dev/null') != STDOUT.stat
192 tee_obj
= lambda
do |buf
|
194 [ STDOUT, tmp
].each
{ |io
| rv
= io
.write(buf
) }
197 def tee_obj
.write(buf
)
202 MogileFS
::X.copy_stream(STDIN, tee_obj
)
203 store_file_retry(mg
, key
, cfg
[:class], tmp
.path
)
208 truth
, ok
= true, nil
209 raise ArgumentError
, "-e must be specified" unless (test
.size
== 1)
211 truth
, key
= case ARGV.size
216 raise ArgumentError
, "#{ARGV[0]}: binary operator expected"
220 raise ArgumentError
, "Too many arguments"
223 paths
= mg
.get_paths(key
)
225 ok
= !!(paths
&& paths
.size
> 0)
227 raise ArgumentError
, "Unknown flag: -#{test.keys.first}"
233 raise ArgumentError
, "Unknown command: #{cmd}"
235 rescue ArgumentError
=> err
236 STDERR.puts
"Usage: #{$0} #{cmd} #{err.message}"