mogilefs-client.git  about / heads / tags
MogileFS client library for Ruby
blob aa20f0849c48202cacae18d4655ac1b3b4c2b358 7084 bytes (raw)
$ git show pipeline:bin/mog	# shows this blob on the CLI

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
 
#!/usr/bin/env ruby
require 'mogilefs'
require 'optparse'
[ STDIN, STDOUT, STDERR].each { |io| io.binmode }

trap('INT') { exit 130 }
trap('PIPE') { exit 0 }

# this is to be compatible with config files used by the Perl tools
def parse_config_file!(path, overwrite = false)
  dest = {}
  File.open(path).each_line do |line|
    line.strip!
    if /^(domain|class)\s*=\s*(\S+)/.match(line)
      dest[$1.to_sym] = $2
    elsif m = /^(?:trackers|hosts)\s*=\s*(.*)/.match(line)
      dest[:hosts] = $1.split(/\s*,\s*/)
    elsif m = /^timeout\s*=\s*(.*)/.match(line)
      dest[:timeout] = m[1].to_f
    else
      STDERR.puts "Ignored configuration line: #{line}" unless /^#/.match(line)
    end
  end
  dest
end

# parse the default config file if one exists
def_file = File.expand_path("~/.mogilefs-client.conf")
def_cfg = File.exist?(def_file) ? parse_config_file!(def_file) : {}

# parse the command-line first, these options take precedence over all else
cli_cfg = {}
config_file = nil
ls_l = false
ls_h = false
test = {}
cat = { :raw => false }

ARGV.options do |x|
  x.banner = "Usage: #{$0} [options] <command> [<arguments>]"
  x.separator ''

  x.on('-c', '--config=/path/to/config',
       'config file to load') { |file| config_file = file }

  x.on('-t', '--trackers=host1[,host2]', '--hosts=host1[,host2]', Array,
       'hostnames/IP addresses of trackers') do |trackers|
    cli_cfg[:hosts] = trackers
  end

  x.on('-e', 'True if key exists') { test[:e] = true }
  x.on('-r', '--raw', 'show raw big_info file information') { cat[:raw] = true }

  x.on('-C', '--class=s', 'class') { |klass| cli_cfg[:class] = klass }
  x.on('-d', '--domain=s', 'domain') { |domain| cli_cfg[:domain] = domain }
  x.on('-l', "long listing format (`ls' command)") { ls_l = true }
  x.on('-h', '--human-readable',
       "print sizes in human-readable format (`ls' command)") { ls_h = true }

  x.separator ''
  x.on('--help', 'Show this help message.') { puts x; exit }
  x.parse!
end

# parse the config file specified at the command-line
file_cfg = config_file ? parse_config_file!(config_file, true) : {}

# read environment variables, too.  This Ruby API favors the term
# "hosts", however upstream MogileFS teminology favors "trackers" instead.
# Favor the term more consistent with what the MogileFS inventors used.
env_cfg = {}
if ENV["MOG_TRACKERS"]
  env_cfg[:hosts] = ENV["MOG_TRACKERS"].split(/\s*,\s*/)
end
if ENV["MOG_HOSTS"] && (env_cfg[:hosts] || []).empty?
  env_cfg[:hosts] = ENV["MOG_HOSTS"].split(/\s*,\s*/)
end
env_cfg[:domain] = ENV["MOG_DOMAIN"] if ENV["MOG_DOMAIN"]
env_cfg[:class] = ENV["MOG_CLASS"] if ENV["MOG_CLASS"]

# merge the configs, favoring them in order specified:
cfg = {}.merge(def_cfg).merge(env_cfg).merge(file_cfg).merge(cli_cfg)

# error-checking
err = []
err << "trackers must be specified" if cfg[:hosts].nil? || cfg[:hosts].empty?
err << "domain must be specified" unless cfg[:domain]
if err.any?
  STDERR.puts "Errors:\n  #{err.join("\n  ")}"
  STDERR.puts ARGV.options
  exit 1
end

unless cmd = ARGV.shift
  STDERR.puts ARGV.options
  exit 1
end

cfg[:timeout] ||= 30 # longer timeout for interactive use
mg = MogileFS::MogileFS.new(cfg)

def store_file_retry(mg, key, storage_class, filepath)
  tries = 0
  begin
    mg.store_file(key, storage_class, filepath)
  rescue MogileFS::UnreadableSocketError,
         MogileFS::Backend::NoDevicesError => err
    if ((tries += 1) < 10)
      STDERR.puts "Retrying on error: #{err}: #{err.message} tries: #{tries}"
      retry
    else
      STDERR.puts "FATAL: #{err}: #{err.message} tries: #{tries}"
    end
    exit 1
  end
end

def human_size(size)
  suff = ''
  %w(K M G).each do |s|
    size /= 1024.0
    if size <= 1024
      suff = s
      break
    end
  end
  sprintf("%.1f%s", size, suff)
end

begin
  case cmd
  when 'cp'
    filename = ARGV.shift or raise ArgumentError, '<filename> <key>'
    key = ARGV.shift or raise ArgumentError, '<filename> <key>'
    ARGV.shift and raise ArgumentError, '<filename> <key>'
    store_file_retry(mg, key, cfg[:class], filename)
  when 'cat'
    ARGV.empty? and raise ArgumentError, '<key1> [<key2> ...]'
    ARGV.each do |key|
      if (!cat[:raw] && key =~ /^_big_info:/)
        mg.bigfile_write(key, STDOUT, {:verify => true})
      else
        mg.get_file_data(key, STDOUT)
      end
    end
  when 'ls'
    prefixes = ARGV.empty? ? [ nil ] : ARGV
    if ls_l
      each_key = lambda do |key, size, devcount|
        size = ls_h && size > 1024 ? human_size(size) : size.to_s
        size = (' ' * (12 - size.length)) << size # right justify
        puts [ sprintf("% 2d", devcount), size, key ].pack("A4 A16 A*")
      end
    else
      each_key = lambda { |key| puts key }
    end
    prefixes.each { |prefix| mg.each_key(prefix, &each_key) }
  when 'rm'
    ARGV.empty? and raise ArgumentError, '<key1> [<key2>]'
    ARGV.each { |key| mg.delete(key) }
  when 'mv'
    from = ARGV.shift or raise ArgumentError, '<from> <to>'
    to = ARGV.shift or raise ArgumentError, '<from> <to>'
    ARGV.shift and raise ArgumentError, '<from> <to>'
    mg.rename(from, to)
  when 'stat' # this outputs a RFC822-like format
    ARGV.empty? and raise ArgumentError, '<key1> [<key2>]'
    ARGV.each_with_index do |key, i|
      if size = mg.size(key)
        puts "Key: #{key}"
        puts "Size: #{size}"
        mg.get_paths(key).each_with_index do |path,i|
          puts "URL-#{i}: #{path}"
        end
        puts ""
      else
        STDERR.puts "No such key: #{key}"
      end
    end
  when 'tee'
    require 'tempfile'
    key = ARGV.shift or raise ArgumentError, '<key>'
    ARGV.shift and raise ArgumentError, '<key>'
    cfg[:class] or raise ArgumentError, 'E: --class must be specified'
    buf = ''
    tmp = Tempfile.new('mog-tee') # TODO: explore Transfer-Encoding:chunked :)

    # if stdout is pointing to /dev/null, don't bother installing the filter.
    STDOUT.sync = true
    tee_obj = tmp
    if File.stat('/dev/null') != STDOUT.stat
      tee_obj = lambda do |buf|
        rv = nil
        [ STDOUT, tmp ].each { |io| rv = io.write(buf) }
        rv
      end
      def tee_obj.write(buf)
        self[buf]
      end
    end
    begin
      MogileFS::X.copy_stream(STDIN, tee_obj)
      store_file_retry(mg, key, cfg[:class], tmp.path)
    ensure
      tmp.close!
    end
  when 'test'
    truth, ok = true, nil
    raise ArgumentError, "-e must be specified" unless (test.size == 1)

    truth, key = case ARGV.size
    when 1
      [ true, ARGV[0] ]
    when 2
      if ARGV[0] != "!"
        raise ArgumentError, "#{ARGV[0]}: binary operator expected"
      end
      [ false, ARGV[1] ]
    else
      raise ArgumentError, "Too many arguments"
    end

    paths = mg.get_paths(key)
    if test[:e]
      ok = !!(paths && paths.size > 0)
    else
      raise ArgumentError, "Unknown flag: -#{test.keys.first}"
    end

    truth or ok = ! ok
    exit ok ? 0 : 1
  else
    raise ArgumentError, "Unknown command: #{cmd}"
  end
rescue ArgumentError => err
  STDERR.puts "Usage: #{$0} #{cmd} #{err.message}"
  exit 1
end
exit 0

git clone https://yhbt.net/mogilefs-client.git