backend: remember to require 'thread' here
[ruby-mogilefs-client.git] / bin / mog
blobdab03bbc2467eb8b19ffbde087ae8e368932f67c
1 #!/usr/bin/env ruby
2 require 'mogilefs'
3 require 'optparse'
5 trap('INT') { exit 130 }
6 trap('PIPE') { exit 0 }
8 # this is to be compatible with config files used by the Perl tools
9 def parse_config_file!(path, overwrite = false)
10 dest = {}
11 File.open(path).each_line do |line|
12 line.strip!
13 if /^(domain|class)\s*=\s*(\S+)/.match(line)
14 dest[$1.to_sym] = $2
15 elsif m = /^(?:trackers|hosts)\s*=\s*(.*)/.match(line)
16 dest[:hosts] = $1.split(/\s*,\s*/)
17 elsif m = /^timeout\s*=\s*(.*)/.match(line)
18 dest[:timeout] = m[1].to_f
19 else
20 STDERR.puts "Ignored configuration line: #{line}" unless /^#/.match(line)
21 end
22 end
23 dest
24 end
26 # parse the default config file if one exists
27 def_file = File.expand_path("~/.mogilefs-client.conf")
28 def_cfg = File.exist?(def_file) ? parse_config_file!(def_file) : {}
30 # parse the command-line first, these options take precedence over all else
31 cli_cfg = {}
32 config_file = nil
33 ls_l = false
34 ls_h = false
35 test = {}
36 cat = { :raw => false }
38 ARGV.options do |x|
39 x.banner = "Usage: #{$0} [options] <command> [<arguments>]"
40 x.separator ''
42 x.on('-c', '--config=/path/to/config',
43 'config file to load') { |file| config_file = file }
45 x.on('-t', '--trackers=host1[,host2]', '--hosts=host1[,host2]', Array,
46 'hostnames/IP addresses of trackers') do |trackers|
47 cli_cfg[:hosts] = trackers
48 end
50 x.on('-e', 'True if key exists') { test[:e] = true }
51 x.on('-r', '--raw', 'show raw big_info file information') { cat[:raw] = true }
53 x.on('-C', '--class=s', 'class') { |klass| cli_cfg[:class] = klass }
54 x.on('-d', '--domain=s', 'domain') { |domain| cli_cfg[:domain] = domain }
55 x.on('-l', "long listing format (`ls' command)") { ls_l = true }
56 x.on('-h', '--human-readable',
57 "print sizes in human-readable format (`ls' command)") { ls_h = true }
59 x.separator ''
60 x.on('--help', 'Show this help message.') { puts x; exit }
61 x.parse!
62 end
64 # parse the config file specified at the command-line
65 file_cfg = config_file ? parse_config_file!(config_file, true) : {}
67 # read environment variables, too. This Ruby API favors the term
68 # "hosts", however upstream MogileFS teminology favors "trackers" instead.
69 # Favor the term more consistent with what the MogileFS inventors used.
70 env_cfg = {}
71 if ENV["MOG_TRACKERS"]
72 env_cfg[:hosts] = ENV["MOG_TRACKERS"].split(/\s*,\s*/)
73 end
74 if ENV["MOG_HOSTS"] && env_cfg[:hosts].empty?
75 env_cfg[:hosts] = ENV["MOG_HOSTS"].split(/\s*,\s*/)
76 end
77 env_cfg[:domain] = ENV["MOG_DOMAIN"] if ENV["MOG_DOMAIN"]
78 env_cfg[:class] = ENV["MOG_CLASS"] if ENV["MOG_CLASS"]
80 # merge the configs, favoring them in order specified:
81 cfg = {}.merge(def_cfg).merge(env_cfg).merge(file_cfg).merge(cli_cfg)
83 # error-checking
84 err = []
85 err << "trackers must be specified" if cfg[:hosts].nil? || cfg[:hosts].empty?
86 err << "domain must be specified" unless cfg[:domain]
87 if err.any?
88 STDERR.puts "Errors:\n #{err.join("\n ")}"
89 STDERR.puts ARGV.options
90 exit 1
91 end
93 unless cmd = ARGV.shift
94 STDERR.puts ARGV.options
95 exit 1
96 end
98 cfg[:timeout] ||= 30 # longer timeout for interactive use
99 include MogileFS::Util
100 mg = MogileFS::MogileFS.new(cfg)
102 def store_file_retry(mg, key, storage_class, filepath)
103 tries = 0
104 begin
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}"
110 retry
111 else
112 STDERR.puts "FATAL: #{err}: #{err.message} tries: #{tries}"
114 exit 1
118 begin
119 case cmd
120 when 'cp'
121 filename = ARGV.shift or raise ArgumentError, '<filename> <key>'
122 key = ARGV.shift or raise ArgumentError, '<filename> <key>'
123 ARGV.shift and raise ArgumentError, '<filename> <key>'
124 cfg[:class] or raise ArgumentError, 'E: --class must be specified'
125 store_file_retry(mg, key, cfg[:class], filename)
126 when 'cat'
127 ARGV.empty? and raise ArgumentError, '<key1> [<key2> ...]'
128 ARGV.each do |key|
129 if (!cat[:raw] && key =~ /^_big_info:/)
130 mg.bigfile_write(key, STDOUT, {:verify => true})
131 else
132 mg.get_file_data(key) { |fp| sysrwloop(fp, STDOUT) }
135 when 'ls'
136 prefixes = ARGV.empty? ? [ nil ] : ARGV
137 prefixes.each do |prefix|
138 mg.each_key(prefix) do |key|
139 if ls_l
140 path_nr = "% 2d" % mg.get_paths(key).size
141 size = mg.size(key)
142 if ls_h && size > 1024
143 suff = ''
144 %w(K M G).each do |s|
145 size /= 1024.0
146 suff = s
147 break if size <= 1024
149 size = sprintf("%.1f%s", size, suff)
150 else
151 size = size.to_s
153 size = (' ' * (12 - size.length)) << size # right justify
154 puts [ path_nr, size, key ].pack("A4 A16 A32")
155 else
156 puts key
160 when 'rm'
161 ARGV.empty? and raise ArgumentError, '<key1> [<key2>]'
162 ARGV.each { |key| mg.delete(key) }
163 when 'mv'
164 from = ARGV.shift or raise ArgumentError, '<from> <to>'
165 to = ARGV.shift or raise ArgumentError, '<from> <to>'
166 ARGV.shift and raise ArgumentError, '<from> <to>'
167 mg.rename(from, to)
168 when 'stat' # this outputs a RFC822-like format
169 ARGV.empty? and raise ArgumentError, '<key1> [<key2>]'
170 ARGV.each_with_index do |key, i|
171 if size = mg.size(key)
172 puts "Key: #{key}"
173 puts "Size: #{size}"
174 mg.get_paths(key).each_with_index do |path,i|
175 puts "URL-#{i}: #{path}"
177 puts ""
178 else
179 STDERR.puts "No such key: #{key}"
182 when 'tee'
183 require 'tempfile'
184 key = ARGV.shift or raise ArgumentError, '<key>'
185 ARGV.shift and raise ArgumentError, '<key>'
186 cfg[:class] or raise ArgumentError, 'E: --class must be specified'
187 buf = ''
188 tmp = Tempfile.new('mog-tee') # TODO: explore Transfer-Encoding:chunked :)
189 at_exit { tmp.unlink }
191 # if stdout is pointing to /dev/null, don't bother installing the filter.
192 STDOUT.sync = true
193 tee_filter = File.stat('/dev/null') == STDOUT.stat ?
194 nil : Proc.new { |buf| STDOUT.write(buf); buf }
195 begin
196 sysrwloop(STDIN, tmp, tee_filter)
197 store_file_retry(mg, key, cfg[:class], tmp.path)
198 ensure
199 tmp.close
201 when 'test'
202 truth, ok = true, nil
203 raise ArgumentError, "-e must be specified" unless (test.size == 1)
205 truth, key = case ARGV.size
206 when 1
207 [ true, ARGV[0] ]
208 when 2
209 if ARGV[0] != "!"
210 raise ArgumentError, "#{ARGV[0]}: binary operator expected"
212 [ false, ARGV[1] ]
213 else
214 raise ArgumentError, "Too many arguments"
217 paths = mg.get_paths(key)
218 if test[:e]
219 ok = !!(paths && paths.size > 0)
220 else
221 raise ArgumentError, "Unknown flag: -#{test.keys.first}"
224 truth or ok = ! ok
225 exit ok ? 0 : 1
226 else
227 raise ArgumentError, "Unknown command: #{cmd}"
229 rescue ArgumentError => err
230 STDERR.puts "Usage: #{$0} #{cmd} #{err.message}"
231 exit 1
233 exit 0