6 $stderr.sync
= $stdout.sync
= true
8 trap('INT') { exit
130 }
9 trap('PIPE') { exit
0 }
10 if md5_trailer_nodes
= ENV["MD5_TRAILER_NODES"]
11 md5_trailer_nodes
.split(/\s*,\s*/).each
do |host
|
12 MogileFS
::HTTPFile::MD5_TRAILER_NODES[host
] = true
16 # this is to be compatible with config files used by the Perl tools
17 def parse_config_file
!(path
, overwrite
= false)
19 File
.open(path
).each_line
do |line
|
21 when /^(domain|class)\s*=\s*(\S+)/
23 when /^(?:trackers|hosts)\s*=\s*(.*)/
24 dest
[:hosts] = $1.split(/\s*,\s*/)
25 when /^timeout\s*=\s*(.*)/
26 dest
[:timeout] = $1.to_f
27 when /^noclobber\s*=\s*true\s*/
28 dest
[:noclobber] = true
30 warn
"Ignored configuration line: #{line}" unless /^#/.match(line
)
36 # parse the default config file if one exists
37 def_file
= File
.expand_path("~/.mogilefs-client.conf")
38 def_cfg
= File
.exist
?(def_file
) ? parse_config_file
!(def_file
) : {}
40 # parse the command-line first, these options take precedence over all else
47 cat
= { :raw => false }
50 x
.banner
= "Usage: #{$0} [options] <command> [<arguments>]"
53 x
.on('-c', '--config=/path/to/config',
54 'config file to load') { |file
| config_file
= file
}
56 x
.on('-t', '--trackers=host1[,host2]', '--hosts=host1[,host2]', Array
,
57 'hostnames/IP addresses of trackers') do |trackers
|
58 cli_cfg
[:hosts] = trackers
61 x
.on('-e', 'True if key exists') { test
[:e] = true }
62 x
.on('-r', '--raw', 'show raw big_info file information') { cat
[:raw] = true }
63 x
.on('-n', '--no-clobber', 'do not clobber existing key') do
64 cli_cfg
[:noclobber] = true
67 x
.on('-C', '--class=s', 'class') { |klass
| cli_cfg
[:class] = klass
}
68 x
.on('-d', '--domain=s', 'domain') { |domain
| cli_cfg
[:domain] = domain
}
69 x
.on('-l', "long listing format (`ls' command)") { ls_l
= true }
70 x
.on('-h', '--human-readable',
71 "print sizes in human-readable format (`ls' command)") { ls_h
= true }
72 x
.on('--chunk', "chunk uploads (`tee' command)") { chunk
= true }
74 x
.on('--help', 'Show this help message.') { puts x
; exit
}
75 x
.on('--version', 'Show --version') { puts
"#$0 #{MogileFS::VERSION}"; exit
}
79 # parse the config file specified at the command-line
80 file_cfg
= config_file
? parse_config_file
!(config_file
, true) : {}
82 # read environment variables, too. This Ruby API favors the term
83 # "hosts", however upstream MogileFS teminology favors "trackers" instead.
84 # Favor the term more consistent with what the MogileFS inventors used.
86 if ENV["MOG_TRACKERS"]
87 env_cfg
[:hosts] = ENV["MOG_TRACKERS"].split(/\s*,\s*/)
89 if ENV["MOG_HOSTS"] && (env_cfg
[:hosts] || []).empty
?
90 env_cfg
[:hosts] = ENV["MOG_HOSTS"].split(/\s*,\s*/)
92 env_cfg
[:domain] = ENV["MOG_DOMAIN"] if ENV["MOG_DOMAIN"]
93 env_cfg
[:class] = ENV["MOG_CLASS"] if ENV["MOG_CLASS"]
95 # merge the configs, favoring them in order specified:
96 cfg
= {}.merge(def_cfg
).merge(env_cfg
).merge(file_cfg
).merge(cli_cfg
)
100 err
<< "trackers must be specified" if cfg
[:hosts].nil? || cfg
[:hosts].empty
?
101 err
<< "domain must be specified" unless cfg
[:domain]
103 warn
"Errors:\n #{err.join("\n ")}"
108 unless cmd
= ARGV.shift
113 cfg
[:timeout] ||= 30 # longer timeout for interactive use
114 mg
= MogileFS
::MogileFS.new(cfg
)
116 def store_file_retry(mg
, key
, storage_class
, filepath
)
119 mg
.store_file(key
, storage_class
, filepath
)
120 rescue MogileFS
::UnreadableSocketError,
121 MogileFS
::Backend::NoDevicesError => err
122 if ((tries
+= 1) < 10)
123 warn
"Retrying on error: #{err}: #{err.message} tries: #{tries}"
126 warn
"FATAL: #{err}: #{err.message} tries: #{tries}"
134 %w(K M G
).each
do |s
|
141 sprintf("%.1f%s", size
, suff
)
147 filename
= ARGV.shift
or raise ArgumentError
, '<filename> <key>'
148 dkey
= ARGV.shift
or raise ArgumentError
, '<filename> <key>'
149 ARGV.shift
and raise ArgumentError
, '<filename> <key>'
150 cfg
[:noclobber] && mg
.exist
?(dkey
) and
151 abort
"`#{dkey}' already exists and -n/--no-clobber was specified"
152 store_file_retry(mg
, dkey
, cfg
[:class], filename
)
154 ARGV.empty
? and raise ArgumentError
, '<key1> [<key2> ...]'
156 if (!cat
[:raw] && key
=~
/^_big_info:/)
157 mg
.bigfile_write(key
, $stdout, {:verify => true})
159 mg
.get_file_data(key
, $stdout)
163 prefixes
= ARGV.empty
? ? [ nil ] : ARGV
165 each_key
= lambda
do |key
, size
, devcount
|
166 size
= ls_h
&& size
> 1024 ? human_size(size
) : size
.to_s
167 size
= (' ' * (12 - size
.length
)) << size
# right justify
168 puts
[ sprintf("% 2d", devcount
), size
, key
].pack("A4 A16 A*")
171 each_key
= lambda
{ |key
| puts key
}
173 prefixes
.each
{ |prefix
| mg
.each_key(prefix
, &each_key
) }
175 ARGV.empty
? and raise ArgumentError
, '<key1> [<key2>]'
176 ARGV.each
{ |key
| mg
.delete(key
) }
178 from
= ARGV.shift
or raise ArgumentError
, '<from> <to>'
179 to
= ARGV.shift
or raise ArgumentError
, '<from> <to>'
180 ARGV.shift
and raise ArgumentError
, '<from> <to>'
182 when 'stat' # this outputs a RFC822-like format
183 ARGV.empty
? and raise ArgumentError
, '<key1> [<key2>]'
185 ARGV.each_with_index
do |key
,j
|
187 info
= mg
.file_info(key
)
189 puts
"Size: #{info['length']}"
190 puts
"Class: #{info['class']}"
191 checksum
= info
['checksum'] and puts
"Checksum: #{checksum}"
192 o
= { :pathcount => info
["devcount"] }
193 mg
.get_paths(key
, o
).each_with_index
do |path
,i
|
194 puts
"URL-#{i}: #{path}"
196 puts
"" if ARGV.size
!= (j
+ 1)
197 rescue MogileFS
::Backend::UnknownKeyError
198 warn
"No such key: #{key}"
205 dkey
= ARGV.shift
or raise ArgumentError
, '<key>'
206 ARGV.shift
and raise ArgumentError
, '<key>'
207 cfg
[:noclobber] && mg
.exist
?(dkey
) and
208 abort
"`#{dkey}' already exists and -n/--no-clobber was specified"
209 skip_tee
= File
.stat('/dev/null') == $stdout.stat
215 tee_obj
= lambda
do |*args
|
216 buf
= $stdin.readpartial(*args
)
221 alias readpartial call
224 mg
.store_file(dkey
, cfg
[:class], tee_obj
)
225 else # buffer input, first
226 tmp
= Tempfile
.new('mog-tee')
229 # if stdout is pointing to /dev/null, don't bother installing the filter.
232 tee_obj
= lambda
do |buf
|
241 MogileFS
.io
.copy_stream($stdin, tee_obj
)
242 store_file_retry(mg
, dkey
, cfg
[:class], tmp
.path
)
248 truth
, ok
= true, nil
249 raise ArgumentError
, "-e must be specified" unless (test
.size
== 1)
251 truth
, key
= case ARGV.size
256 raise ArgumentError
, "#{ARGV[0]}: binary operator expected"
260 raise ArgumentError
, "Too many arguments"
263 test
[:e] or raise ArgumentError
, "Unknown flag: -#{test.keys.first}"
268 raise ArgumentError
, "Unknown command: #{cmd}"
270 rescue ArgumentError
=> err
271 warn
"Usage: #{$0} #{cmd} #{err.message}"