Module: Pipette::Util

Included in:
Pipette
Defined in:
lib/pipette/util.rb

Instance Method Summary collapse

Instance Method Details

#disk_parts(dev) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/pipette/util.rb', line 46

def disk_parts(dev)
  if dev && match = dev.match(%r{/dev/sd([d-p])([0-1][0-9]|[0-9])})
    disk, part = match[1], match[2].to_i
  else
    nil
  end
end

#next_avail_diskObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/pipette/util.rb', line 15

def next_avail_disk
  @last_disk ||= Dir["/dev/sd[d-p]{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}"].sort { |a,b|
    disk_parts(a) <=> disk_parts(b)
  }.last

  disk, part = disk_parts(@last_disk)

  if disk
    if part.to_i >= 15
      disk = disk.succ
      part = 1
    else
      part += 1
    end
  else
    disk, part = "d", "1"
  end

  @last_disk = "/dev/sd#{disk}#{part}"
end

#next_mdObject



36
37
38
39
40
41
42
43
44
# File 'lib/pipette/util.rb', line 36

def next_md
  md = Dir["/dev/md[0-9]*"].sort.last
  if md && match = md.match(%r{/dev/md([\d+])})
    i = match[1].to_i + 1
  else
    i = 0
  end
  "/dev/md#{i}"
end

#run_command(cmd, *args) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/pipette/util.rb', line 5

def run_command(cmd, *args)
  args.unshift(cmd)

  command = args.flatten.join(' ')

  say_with_time command do
    `#{command}`
  end
end

#say_with_time(msg = "", &block) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/pipette/util.rb', line 54

def say_with_time(msg = "", &block)
  @depth ||= 0
  @depth += 1
  indent = '  ' * @depth
  puts "#{indent}#{msg}"
  t = Time.now
  res = yield
  puts "#{indent}-> %0.4f" % (Time.now - t)
  @depth -= 1
  res
end

#wait_for(*paths) ⇒ Object



66
67
68
69
70
71
72
73
# File 'lib/pipette/util.rb', line 66

def wait_for(*paths)
  paths.flatten.each do |path|
    puts "Waiting for #{path} to appear..."
    until File.exist?(path)
      sleep 0.1
    end
  end
end