Class: RamDev
- Inherits:
-
Object
- Object
- RamDev
- Defined in:
- lib/ramdev.rb
Instance Attribute Summary collapse
-
#diskname ⇒ Object
readonly
Returns the value of attribute diskname.
-
#mountpoint ⇒ Object
readonly
Returns the value of attribute mountpoint.
-
#ramdisk ⇒ Object
readonly
Returns the value of attribute ramdisk.
Instance Method Summary collapse
- #build(rcpath, size = nil) ⇒ Object
- #copy_folders ⇒ Object
- #fix(rcpath) ⇒ Object
-
#initialize(sufix = "_ramdev") ⇒ RamDev
constructor
path to rc file, and 10 mb default size.
- #load_runcom(rcfile) ⇒ Object
- #memory ⇒ Object
- #restore_folders ⇒ Object
- #unbuild(rcpath, force = false) ⇒ Object
- #valid_paths? ⇒ Boolean
Constructor Details
#initialize(sufix = "_ramdev") ⇒ RamDev
path to rc file, and 10 mb default size.
11 12 13 14 |
# File 'lib/ramdev.rb', line 11 def initialize(sufix = "_ramdev") @backupSuffix = sufix @store = PStore.new("/tmp/ramdev.pstore") end |
Instance Attribute Details
#diskname ⇒ Object (readonly)
Returns the value of attribute diskname.
8 9 10 |
# File 'lib/ramdev.rb', line 8 def diskname @diskname end |
#mountpoint ⇒ Object (readonly)
Returns the value of attribute mountpoint.
8 9 10 |
# File 'lib/ramdev.rb', line 8 def mountpoint @mountpoint end |
#ramdisk ⇒ Object (readonly)
Returns the value of attribute ramdisk.
8 9 10 |
# File 'lib/ramdev.rb', line 8 def ramdisk @ramdisk end |
Instance Method Details
#build(rcpath, size = nil) ⇒ Object
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 |
# File 'lib/ramdev.rb', line 85 def build(rcpath, size = nil) @size = size @size ||= memory / 2 load_runcom(rcpath) ramdisk = Ramdisk.new(mountpoint) human_num = "#{@size / 1048576}".reverse.gsub(/...(?=.)/,'\&,').reverse puts "Allocating ramdisk size: #{human_num} MB " if( !ramdisk.allocate(@size) ) puts "Allocation failed. Size: #{human_num} MB" exit end if( !ramdisk.format(diskname, :hfs) ) puts "Failed to format disk." exit end if( !ramdisk.mount ) puts "Failed to mount at #{mountpoint}" ramdisk.deallocate exit end if !valid_paths? puts "paths are not valid!".color(:red) raise(:hell) end copy_folders puts "RAM disk mounted at #{mountpoint}" #FIX: Not compatible with Windows: fork do puts "ramdev_sync pid #{Process.pid}" File.open("/tmp/ramdev_sync.pid", "w") {|f| f.write("#{Process.pid}\n")} @store.transaction do |s| s["pid"] = Process.pid end trap("QUIT") do puts "Interrupted by signal, ramdev_sync will stop now." exit end watcher = RamDevSync.new(File.open(rcpath)) watcher.listen puts "ramdev_sync listening..." sleep end end |
#copy_folders ⇒ Object
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 |
# File 'lib/ramdev.rb', line 190 def copy_folders @paths.each do |p| src = p["source"].gsub(/\/+$/,"") next if src.nil? || src.length < 1 des = p["destination"].gsub(/\/+$/,"").gsub(/^\/+/,"") name = src.match(/([^\/\\]+)$/)[1] if des.length > 0 des = mountpoint+"/#{des}" des = des.gsub(/\/{2,}/,"/") else des = mountpoint end des = File.absolute_path(des) print "Copying #{src}...\n" FileUtils.mkdir_p(des) unless File.exist?(des) IO.popen("rsync --progress -ra #{src} #{des}") do |rsync_io| until rsync_io.eof? line = rsync_io.readline line = line.gsub("\n","") next unless line =~ /to-check/ m = line.match(/to-check=([0-9]+)\/([0-9]+)/) scale = (m[1].to_f / m[2].to_f) prog = "#{[9613].pack('U*')}" * ( (1.0 - scale) * 30.0).round prog += " " * (scale * 30.0).round print "#{prog}| #{des}/#{name} " print "\r" end end print "\n" FileUtils.move(src, src+@backupSuffix) puts "Linking: #{name}" File.symlink("#{des}/#{name}", src) end end |
#fix(rcpath) ⇒ Object
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 |
# File 'lib/ramdev.rb', line 16 def fix(rcpath) load_runcom(rcpath) watcher = RamDevSync.new(File.open(rcpath)) if watcher.running? puts `ps #{watcher.pid}` puts "It appears ramdev is still running at pid: #{watcher.pid}.".color(:yellow) puts "Try 'ramdev down' first to fix any file linking problem." puts "Otherwise kill the process first before running 'ramdev fix'." return end @paths.each do |p| src = p["source"].gsub(/\/+$/,"") if( File.exists?(src+@backupSuffix) ) if(!File.exists?(src) || File.symlink?(src)) FileUtils.safe_unlink(src) if File.symlink?(src) puts "Moving backup: ".color(:green) + "#{src+@backupSuffix} to #{src}" FileUtils.move(src+@backupSuffix, src) else puts "skipping file: ".color(:yellow) + ("#{src}") end end end end |
#load_runcom(rcfile) ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/ramdev.rb', line 141 def load_runcom(rcfile) return if @loaded == true rc = YAML.load(File.open(rcfile)) if rc.nil? @mountpoint = "/ramdev" @diskname = "ramdev" @paths ||= [] @paths.push({ source: Dir.pwd, destination: "" }) else @mountpoint = rc["ramdisk"]["mountpoint"] @diskname = rc["ramdisk"]["name"] # TODO: Get size of paths and create default ramdisk size based it (2x) @paths = rc["ramdisk"]["paths"] @size ||= rc["ramdisk"]["size"] if rc["ramdisk"]["size"] @loaded = true end end |
#memory ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/ramdev.rb', line 72 def memory if @memory.nil? r = `/usr/sbin/system_profiler SPHardwareDataType`.split("\n").collect { |s| s == "" ? nil : s.strip }.compact r = r.collect { |s| s.split(":").collect { |ss| ss.strip }} memstring = "" r.each do |i| memstring = i[1] if i[0] == "Memory" end @memory = memstring.match(/([0-9]+) GB/)[1].to_i * 1073741824 end @memory end |
#restore_folders ⇒ Object
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/ramdev.rb', line 172 def restore_folders @paths.each do |p| src = p["source"].gsub(/\/+$/,"") if !File.exists? src+@backupSuffix || !File.symlink?(src) puts "Referenced files don't appear to be linked properly. " return false end end @paths.each do |p| src = p["source"].gsub(/\/+$/,"") FileUtils.safe_unlink src FileUtils.move(src+@backupSuffix, src) end true end |
#unbuild(rcpath, force = false) ⇒ Object
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 |
# File 'lib/ramdev.rb', line 45 def unbuild(rcpath, force = false) #TODO force sync load_runcom(rcpath) if !force && !restore_folders puts "Ramdisk shutdown was halted because there was a problem restoring folders." puts "Eject the ramdisk manually once you've sorted out any issues." return end ramdisk = Ramdisk.new(mountpoint) pid = @store.transaction do |s| s["pid"] end Process.kill("QUIT", pid) if pid @store.transaction do |s| s["pid"] = nil end ramdisk.unmount ramdisk.deallocate "RAM disk removed at #{mountpoint}" end |
#valid_paths? ⇒ Boolean
161 162 163 164 165 166 167 168 169 170 |
# File 'lib/ramdev.rb', line 161 def valid_paths? @paths.each do |p| src = p["source"].gsub(/\/+$/,"") if File.exist?("#{src}#{@backupSuffix}") puts "A backup already exists for: '#{src}' at: '#{src}#{@backupSuffix}'" return false end end return true end |