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 }
57 x
.on('-C', '--class=s', 'class') { |klass
| cli_cfg
[:class] = klass
}
58 x
.on('-d', '--domain=s', 'domain') { |domain
| cli_cfg
[:domain] = domain
}
59 x
.on('-l', "long listing format (`ls' command)") { ls_l
= true }
60 x
.on('-h', '--human-readable',
61 "print sizes in human-readable format (`ls' command)") { ls_h
= true }
62 x
.on('--chunk', "chunk uploads (`tee' command)") { chunk
= true }
64 x
.on('--help', 'Show this help message.') { puts x
; exit
}
65 x
.on('--version', 'Show --version') { puts
"#$0 #{MogileFS::VERSION}"; exit
}
69 # parse the config file specified at the command-line
70 file_cfg
= config_file
? parse_config_file
!(config_file
, true) : {}
72 # read environment variables, too. This Ruby API favors the term
73 # "hosts", however upstream MogileFS teminology favors "trackers" instead.
74 # Favor the term more consistent with what the MogileFS inventors used.
76 if ENV["MOG_TRACKERS"]
77 env_cfg
[:hosts] = ENV["MOG_TRACKERS"].split(/\s*,\s*/)
79 if ENV["MOG_HOSTS"] && (env_cfg
[:hosts] || []).empty
?
80 env_cfg
[:hosts] = ENV["MOG_HOSTS"].split(/\s*,\s*/)
82 env_cfg
[:domain] = ENV["MOG_DOMAIN"] if ENV["MOG_DOMAIN"]
83 env_cfg
[:class] = ENV["MOG_CLASS"] if ENV["MOG_CLASS"]
85 # merge the configs, favoring them in order specified:
86 cfg
= {}.merge(def_cfg
).merge(env_cfg
).merge(file_cfg
).merge(cli_cfg
)
90 err
<< "trackers must be specified" if cfg
[:hosts].nil? || cfg
[:hosts].empty
?
91 err
<< "domain must be specified" unless cfg
[:domain]
93 warn
"Errors:\n #{err.join("\n ")}"
98 unless cmd
= ARGV.shift
103 cfg
[:timeout] ||= 30 # longer timeout for interactive use
104 mg
= MogileFS
::MogileFS.new(cfg
)
106 def store_file_retry(mg
, key
, storage_class
, filepath
)
109 mg
.store_file(key
, storage_class
, filepath
)
110 rescue MogileFS
::UnreadableSocketError,
111 MogileFS
::Backend::NoDevicesError => err
112 if ((tries
+= 1) < 10)
113 warn
"Retrying on error: #{err}: #{err.message} tries: #{tries}"
116 warn
"FATAL: #{err}: #{err.message} tries: #{tries}"
124 %w(K M G
).each
do |s
|
131 sprintf("%.1f%s", size
, suff
)
137 filename
= ARGV.shift
or raise ArgumentError
, '<filename> <key>'
138 key
= ARGV.shift
or raise ArgumentError
, '<filename> <key>'
139 ARGV.shift
and raise ArgumentError
, '<filename> <key>'
140 store_file_retry(mg
, key
, cfg
[:class], filename
)
142 ARGV.empty
? and raise ArgumentError
, '<key1> [<key2> ...]'
144 if (!cat
[:raw] && key
=~
/^_big_info:/)
145 mg
.bigfile_write(key
, $stdout, {:verify => true})
147 mg
.get_file_data(key
, $stdout)
151 prefixes
= ARGV.empty
? ? [ nil ] : ARGV
153 each_key
= lambda
do |key
, size
, devcount
|
154 size
= ls_h
&& size
> 1024 ? human_size(size
) : size
.to_s
155 size
= (' ' * (12 - size
.length
)) << size
# right justify
156 puts
[ sprintf("% 2d", devcount
), size
, key
].pack("A4 A16 A*")
159 each_key
= lambda
{ |key
| puts key
}
161 prefixes
.each
{ |prefix
| mg
.each_key(prefix
, &each_key
) }
163 ARGV.empty
? and raise ArgumentError
, '<key1> [<key2>]'
164 ARGV.each
{ |key
| mg
.delete(key
) }
166 from
= ARGV.shift
or raise ArgumentError
, '<from> <to>'
167 to
= ARGV.shift
or raise ArgumentError
, '<from> <to>'
168 ARGV.shift
and raise ArgumentError
, '<from> <to>'
170 when 'stat' # this outputs a RFC822-like format
171 ARGV.empty
? and raise ArgumentError
, '<key1> [<key2>]'
172 ARGV.each_with_index
do |key
, i
|
173 if size
= mg
.size(key
)
176 mg
.get_paths(key
).each_with_index
do |path
,i
|
177 puts
"URL-#{i}: #{path}"
181 warn
"No such key: #{key}"
186 key
= ARGV.shift
or raise ArgumentError
, '<key>'
187 ARGV.shift
and raise ArgumentError
, '<key>'
188 skip_tee
= File
.stat('/dev/null') == $stdout.stat
194 tee_obj
= lambda
do |*args
|
195 buf
= $stdin.readpartial(*args
)
200 alias readpartial call
203 mg
.store_file(key
, cfg
[:class], tee_obj
)
204 else # buffer input, first
205 tmp
= Tempfile
.new('mog-tee')
208 # if stdout is pointing to /dev/null, don't bother installing the filter.
211 tee_obj
= lambda
do |buf
|
220 MogileFS
::X.copy_stream($stdin, tee_obj
)
221 store_file_retry(mg
, key
, cfg
[:class], tmp
.path
)
227 truth
, ok
= true, nil
228 raise ArgumentError
, "-e must be specified" unless (test
.size
== 1)
230 truth
, key
= case ARGV.size
235 raise ArgumentError
, "#{ARGV[0]}: binary operator expected"
239 raise ArgumentError
, "Too many arguments"
243 paths
= mg
.get_paths(key
)
245 ok
= !!(paths
&& paths
.size
> 0)
247 raise ArgumentError
, "Unknown flag: -#{test.keys.first}"
249 rescue MogileFS
::Backend::UnknownKeyError
256 raise ArgumentError
, "Unknown command: #{cmd}"
258 rescue ArgumentError
=> err
259 warn
"Usage: #{$0} #{cmd} #{err.message}"