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
40 cat
= { :raw => false }
43 x
.banner
= "Usage: #{$0} [options] <command> [<arguments>]"
46 x
.on('-c', '--config=/path/to/config',
47 'config file to load') { |file
| config_file
= file
}
49 x
.on('-t', '--trackers=host1[,host2]', '--hosts=host1[,host2]', Array
,
50 'hostnames/IP addresses of trackers') do |trackers
|
51 cli_cfg
[:hosts] = trackers
54 x
.on('-e', 'True if key exists') { test
[:e] = true }
55 x
.on('-r', '--raw', 'show raw big_info file information') { cat
[:raw] = true }
56 x
.on('-n', '--no-clobber', 'do not clobber existing key') do
57 cli_cfg
[:noclobber] = true
60 x
.on('-C', '--class=s', 'class') { |klass
| cli_cfg
[:class] = klass
}
61 x
.on('-d', '--domain=s', 'domain') { |domain
| cli_cfg
[:domain] = domain
}
62 x
.on('-l', "long listing format (`ls' command)") { ls_l
= true }
63 x
.on('-h', '--human-readable',
64 "print sizes in human-readable format (`ls' command)") { ls_h
= true }
65 x
.on('--chunk', "chunk uploads (`tee' command)") { chunk
= true }
67 x
.on('--help', 'Show this help message.') { puts x
; exit
}
68 x
.on('--version', 'Show --version') { puts
"#$0 #{MogileFS::VERSION}"; exit
}
72 # parse the config file specified at the command-line
73 file_cfg
= config_file
? parse_config_file
!(config_file
, true) : {}
75 # read environment variables, too. This Ruby API favors the term
76 # "hosts", however upstream MogileFS teminology favors "trackers" instead.
77 # Favor the term more consistent with what the MogileFS inventors used.
79 if ENV["MOG_TRACKERS"]
80 env_cfg
[:hosts] = ENV["MOG_TRACKERS"].split(/\s*,\s*/)
82 if ENV["MOG_HOSTS"] && (env_cfg
[:hosts] || []).empty
?
83 env_cfg
[:hosts] = ENV["MOG_HOSTS"].split(/\s*,\s*/)
85 env_cfg
[:domain] = ENV["MOG_DOMAIN"] if ENV["MOG_DOMAIN"]
86 env_cfg
[:class] = ENV["MOG_CLASS"] if ENV["MOG_CLASS"]
88 # merge the configs, favoring them in order specified:
89 cfg
= {}.merge(def_cfg
).merge(env_cfg
).merge(file_cfg
).merge(cli_cfg
)
93 err
<< "trackers must be specified" if cfg
[:hosts].nil? || cfg
[:hosts].empty
?
94 err
<< "domain must be specified" unless cfg
[:domain]
96 warn
"Errors:\n #{err.join("\n ")}"
101 unless cmd
= ARGV.shift
106 cfg
[:timeout] ||= 30 # longer timeout for interactive use
107 mg
= MogileFS
::MogileFS.new(cfg
)
109 def store_file_retry(mg
, key
, storage_class
, filepath
)
112 mg
.store_file(key
, storage_class
, filepath
)
113 rescue MogileFS
::UnreadableSocketError,
114 MogileFS
::Backend::NoDevicesError => err
115 if ((tries
+= 1) < 10)
116 warn
"Retrying on error: #{err}: #{err.message} tries: #{tries}"
119 warn
"FATAL: #{err}: #{err.message} tries: #{tries}"
127 %w(K M G
).each
do |s
|
134 sprintf("%.1f%s", size
, suff
)
140 filename
= ARGV.shift
or raise ArgumentError
, '<filename> <key>'
141 dkey
= ARGV.shift
or raise ArgumentError
, '<filename> <key>'
142 ARGV.shift
and raise ArgumentError
, '<filename> <key>'
143 cfg
[:noclobber] && mg
.exist
?(dkey
) and
144 abort
"`#{dkey}' already exists and -n/--no-clobber was specified"
145 store_file_retry(mg
, dkey
, cfg
[:class], filename
)
147 ARGV.empty
? and raise ArgumentError
, '<key1> [<key2> ...]'
149 if (!cat
[:raw] && key
=~
/^_big_info:/)
150 mg
.bigfile_write(key
, $stdout, {:verify => true})
152 mg
.get_file_data(key
, $stdout)
156 prefixes
= ARGV.empty
? ? [ nil ] : ARGV
158 each_key
= lambda
do |key
, size
, devcount
|
159 size
= ls_h
&& size
> 1024 ? human_size(size
) : size
.to_s
160 size
= (' ' * (12 - size
.length
)) << size
# right justify
161 puts
[ sprintf("% 2d", devcount
), size
, key
].pack("A4 A16 A*")
164 each_key
= lambda
{ |key
| puts key
}
166 prefixes
.each
{ |prefix
| mg
.each_key(prefix
, &each_key
) }
168 ARGV.empty
? and raise ArgumentError
, '<key1> [<key2>]'
169 ARGV.each
{ |key
| mg
.delete(key
) }
171 from
= ARGV.shift
or raise ArgumentError
, '<from> <to>'
172 to
= ARGV.shift
or raise ArgumentError
, '<from> <to>'
173 ARGV.shift
and raise ArgumentError
, '<from> <to>'
175 when 'stat' # this outputs a RFC822-like format
176 ARGV.empty
? and raise ArgumentError
, '<key1> [<key2>]'
178 ARGV.each_with_index
do |key
,j
|
180 info
= mg
.file_info(key
)
182 puts
"Size: #{info['length']}"
183 puts
"Class: #{info['class']}"
184 o
= { :pathcount => info
["devcount"] }
185 mg
.get_paths(key
, o
).each_with_index
do |path
,i
|
186 puts
"URL-#{i}: #{path}"
188 puts
"" if ARGV.size
!= (j
+ 1)
189 rescue MogileFS
::Backend::UnknownKeyError
190 warn
"No such key: #{key}"
197 dkey
= ARGV.shift
or raise ArgumentError
, '<key>'
198 ARGV.shift
and raise ArgumentError
, '<key>'
199 cfg
[:noclobber] && mg
.exist
?(dkey
) and
200 abort
"`#{dkey}' already exists and -n/--no-clobber was specified"
201 skip_tee
= File
.stat('/dev/null') == $stdout.stat
207 tee_obj
= lambda
do |*args
|
208 buf
= $stdin.readpartial(*args
)
213 alias readpartial call
216 mg
.store_file(dkey
, cfg
[:class], tee_obj
)
217 else # buffer input, first
218 tmp
= Tempfile
.new('mog-tee')
221 # if stdout is pointing to /dev/null, don't bother installing the filter.
224 tee_obj
= lambda
do |buf
|
233 MogileFS
.io
.copy_stream($stdin, tee_obj
)
234 store_file_retry(mg
, dkey
, cfg
[:class], tmp
.path
)
240 truth
, ok
= true, nil
241 raise ArgumentError
, "-e must be specified" unless (test
.size
== 1)
243 truth
, key
= case ARGV.size
248 raise ArgumentError
, "#{ARGV[0]}: binary operator expected"
252 raise ArgumentError
, "Too many arguments"
255 test
[:e] or raise ArgumentError
, "Unknown flag: -#{test.keys.first}"
260 raise ArgumentError
, "Unknown command: #{cmd}"
262 rescue ArgumentError
=> err
263 warn
"Usage: #{$0} #{cmd} #{err.message}"