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
48 cat
= { :raw => false }
51 x
.banner
= "Usage: #{$0} [options] <command> [<arguments>]"
54 x
.on('-c', '--config=/path/to/config',
55 'config file to load') { |file
| config_file
= file
}
57 x
.on('-t', '--trackers=host1[,host2]', '--hosts=host1[,host2]', Array
,
58 'hostnames/IP addresses of trackers') do |trackers
|
59 cli_cfg
[:hosts] = trackers
62 x
.on('-e', 'True if key exists') { test
[:e] = true }
63 x
.on('-r', '--raw', 'show raw big_info file information') { cat
[:raw] = true }
64 x
.on('-n', '--no-clobber', 'do not clobber existing key') do
65 cli_cfg
[:noclobber] = true
68 x
.on('-C', '--class=s', 'class') { |klass
| cli_cfg
[:class] = klass
}
69 x
.on('-d', '--domain=s', 'domain') { |domain
| cli_cfg
[:domain] = domain
}
70 x
.on('-l', "long listing format (`ls' command)") { ls_l
= true }
71 x
.on('-h', '--human-readable',
72 "print sizes in human-readable format (`ls' command)") { ls_h
= true }
73 x
.on('--chunk', "chunk uploads (`tee' command)") { chunk
= true }
74 x
.on('--range', "stream partial uploads (`tee' command)") { range
= true }
76 x
.on('--help', 'Show this help message.') { puts x
; exit
}
77 x
.on('--version', 'Show --version') { puts
"#$0 #{MogileFS::VERSION}"; exit
}
81 # parse the config file specified at the command-line
82 file_cfg
= config_file
? parse_config_file
!(config_file
, true) : {}
84 # read environment variables, too. This Ruby API favors the term
85 # "hosts", however upstream MogileFS teminology favors "trackers" instead.
86 # Favor the term more consistent with what the MogileFS inventors used.
88 if ENV["MOG_TRACKERS"]
89 env_cfg
[:hosts] = ENV["MOG_TRACKERS"].split(/\s*,\s*/)
91 if ENV["MOG_HOSTS"] && (env_cfg
[:hosts] || []).empty
?
92 env_cfg
[:hosts] = ENV["MOG_HOSTS"].split(/\s*,\s*/)
94 env_cfg
[:domain] = ENV["MOG_DOMAIN"] if ENV["MOG_DOMAIN"]
95 env_cfg
[:class] = ENV["MOG_CLASS"] if ENV["MOG_CLASS"]
97 # merge the configs, favoring them in order specified:
98 cfg
= {}.merge(def_cfg
).merge(env_cfg
).merge(file_cfg
).merge(cli_cfg
)
102 err
<< "trackers must be specified" if cfg
[:hosts].nil? || cfg
[:hosts].empty
?
103 err
<< "domain must be specified" unless cfg
[:domain]
105 warn
"Errors:\n #{err.join("\n ")}"
110 unless cmd
= ARGV.shift
115 cfg
[:timeout] ||= 30 # longer timeout for interactive use
116 mg
= MogileFS
::MogileFS.new(cfg
)
118 def store_file_retry(mg
, key
, storage_class
, filepath
)
121 mg
.store_file(key
, storage_class
, filepath
)
122 rescue MogileFS
::UnreadableSocketError,
123 MogileFS
::Backend::NoDevicesError => err
124 if ((tries
+= 1) < 10)
125 warn
"Retrying on error: #{err}: #{err.message} tries: #{tries}"
128 warn
"FATAL: #{err}: #{err.message} tries: #{tries}"
136 %w(K M G
).each
do |s
|
143 sprintf("%.1f%s", size
, suff
)
149 filename
= ARGV.shift
or raise ArgumentError
, '<filename> <key>'
150 dkey
= ARGV.shift
or raise ArgumentError
, '<filename> <key>'
151 ARGV.shift
and raise ArgumentError
, '<filename> <key>'
152 cfg
[:noclobber] && mg
.exist
?(dkey
) and
153 abort
"`#{dkey}' already exists and -n/--no-clobber was specified"
154 store_file_retry(mg
, dkey
, cfg
[:class], filename
)
156 ARGV.empty
? and raise ArgumentError
, '<key1> [<key2> ...]'
158 if (!cat
[:raw] && key
=~
/^_big_info:/)
159 mg
.bigfile_write(key
, $stdout, {:verify => true})
161 mg
.get_file_data(key
, $stdout)
165 prefixes
= ARGV.empty
? ? [ nil ] : ARGV
167 each_key
= lambda
do |key
, size
, devcount
|
168 size
= ls_h
&& size
> 1024 ? human_size(size
) : size
.to_s
169 size
= (' ' * (12 - size
.length
)) << size
# right justify
170 puts
[ sprintf("% 2d", devcount
), size
, key
].pack("A4 A16 A*")
173 each_key
= lambda
{ |key
| puts key
}
175 prefixes
.each
{ |prefix
| mg
.each_key(prefix
, &each_key
) }
177 ARGV.empty
? and raise ArgumentError
, '<key1> [<key2>]'
178 ARGV.each
{ |key
| mg
.delete(key
) }
180 from
= ARGV.shift
or raise ArgumentError
, '<from> <to>'
181 to
= ARGV.shift
or raise ArgumentError
, '<from> <to>'
182 ARGV.shift
and raise ArgumentError
, '<from> <to>'
184 when 'stat' # this outputs a RFC822-like format
185 ARGV.empty
? and raise ArgumentError
, '<key1> [<key2>]'
187 ARGV.each_with_index
do |key
,j
|
189 info
= mg
.file_info(key
)
191 puts
"Size: #{info['length']}"
192 puts
"Class: #{info['class']}"
193 checksum
= info
['checksum'] and puts
"Checksum: #{checksum}"
194 o
= { :pathcount => info
["devcount"] }
195 mg
.get_paths(key
, o
).each_with_index
do |path
,i
|
196 puts
"URL-#{i}: #{path}"
198 puts
"" if ARGV.size
!= (j
+ 1)
199 rescue MogileFS
::Backend::UnknownKeyError
200 warn
"No such key: #{key}"
206 abort
"--range and --chunk are incompatible" if range
&& chunk
207 dkey
= ARGV.shift
or raise ArgumentError
, '<key>'
208 ARGV.shift
and raise ArgumentError
, '<key>'
209 cfg
[:noclobber] && mg
.exist
?(dkey
) and
210 abort
"`#{dkey}' already exists and -n/--no-clobber was specified"
211 skip_tee
= File
.stat('/dev/null') == $stdout.stat
212 largefile
= :tempfile
213 largefile
= :content_range if range
214 largefile
= :chunked if chunk
216 io
= mg
.new_file(dkey
, :class => cfg
[:class], :largefile => largefile
)
218 buf
= $stdin.readpartial(16384)
221 $stdout.write(buf
) unless skip_tee
222 $stdin.readpartial(16384, buf
)
228 truth
, ok
= true, nil
229 raise ArgumentError
, "-e must be specified" unless (test
.size
== 1)
231 truth
, key
= case ARGV.size
236 raise ArgumentError
, "#{ARGV[0]}: binary operator expected"
240 raise ArgumentError
, "Too many arguments"
243 test
[:e] or raise ArgumentError
, "Unknown flag: -#{test.keys.first}"
248 raise ArgumentError
, "Unknown command: #{cmd}"
250 rescue ArgumentError
=> err
251 warn
"Usage: #{$0} #{cmd} #{err.message}"