Sha256: acc55772e9cf1ed8c72c7d5724629942d0887ff6401b35f5fb7836d675e007df

Contents?: true

Size: 1.62 KB

Versions: 1

Compression:

Stored size: 1.62 KB

Contents

module Ramdo
  module Ramdisk
    class LinuxWrapper
      def initialize
        line = Cocaine::CommandLine.new("cat", "/proc/mounts")
        line.run.each_line do |line|
          @shm_path = Regexp.last_match[1] if line =~ /(\/shm)[\s]tmpfs[\s]rw/
        end
        raise GeneralRamdiskException.new("shm path not found") unless @shm_path
      end

      def list
        disks = []
        Dir.glob(@shm_path + '/*').each do |dir|
          if dir =~ Instance::NAME_PATTERN
            disks << Instance.new(path: dir, device: @shm_path, size: Filesize.from("1 GB"))
          end
        end

        disks
      end

      def create(size)
        size = Filesize.from(size) if size.is_a? String
        raise NotEnoughFreeRamException.new unless enough_ram? size

        # Create new directory as dedicated space
        path = [@shm_path, Instance.generate_name].join('/')
        Dir.mkdir(path)

        # Receive all disk and select just created one
        list().select { |disk| disk.path == path }.first
      end

      def destroy(instance)
        return false unless File.exist? instance.path

        Dir.glob(instance.path + "/*").each { |file| File.delete(file) if File.file? file }
        Dir.rmdir(instance.path)
      end

      private
      def enough_ram?(size)
        size = Filesize.from(size) if size.is_a? String
        free_mem = ""

        line = Cocaine::CommandLine.new("cat", "/proc/meminfo")
        line.run.each_line do |line|
          if line =~ /^MemFree:[\s]+([0-9]+ kB)/
            free_mem = Regexp.last_match[1]
          end
        end

        Filesize.from(free_mem) > size
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ramdo-0.1.4 lib/ramdo/ramdisk/linux_wrapper.rb