Sha256: f4833576665a1aba58739c319dc698f3eaf663d291522438ade8428684df94ef

Contents?: true

Size: 1.46 KB

Versions: 2

Compression:

Stored size: 1.46 KB

Contents

class Pipette
  module Util

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

      command = args.flatten.join(' ')

      say_with_time command do
        `#{command}`
      end
    end

    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

    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

    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

    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

    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

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
pipette-0.1.1 lib/pipette/util.rb
pipette-0.1.0 lib/pipette/util.rb