Class: BatCave::Store
- Inherits:
-
Object
- Object
- BatCave::Store
- Includes:
- BatCave::Support::EnvPath, BatCave::Support::Git
- Defined in:
- lib/batcave/store.rb
Instance Method Summary collapse
-
#each(environment, &block) ⇒ Object
def store.
-
#load(manifest_path) ⇒ Object
def each.
- #lock(path, &block) ⇒ Object
-
#store(dsl) ⇒ Object
def lock.
Methods included from BatCave::Support::EnvPath
Methods included from BatCave::Support::Git
Instance Method Details
#each(environment, &block) ⇒ Object
def store
51 52 53 54 55 56 57 58 |
# File 'lib/batcave/store.rb', line 51 def each(environment, &block) basedir = path(environment) manifest_path = File.join(basedir, ".batcave", "manifest") manifest = load(manifest_path) manifest["things"].each do |thing| yield thing end end |
#load(manifest_path) ⇒ Object
def each
60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/batcave/store.rb', line 60 def load(manifest_path) manifest = {} if File.exists?(manifest_path) fd = File.new(manifest_path, "a+") # Load the current manifest so we can modify it manifest = YAML.load(fd.read) manifest = {} if !manifest fd.close end # Handle empty manifest. (YAML.load returns false for empty files) manifest["things"] ||= {} return manifest end |
#lock(path, &block) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/batcave/store.rb', line 11 def lock(path, &block) lockfile = "#{path}.lock" File.open(lockfile, "a+") do |lockfd| locked = lockfd.flock(File::LOCK_EX | File::LOCK_NB) if !locked info = lockfd.read raise "Store is currently locked, cannot write to it. (info: #{info})" end lockfd.rewind lockfd.write("pid:$$") lockfd.flush block.call end # This step is not required for flock(2) to work, but it should help to # keep users from being confused. File.delete(lockfile) end |
#store(dsl) ⇒ Object
def lock
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/batcave/store.rb', line 31 def store(dsl) basedir = path(dsl.environment) manifest_path = File.join(basedir, ".batcave", "manifest") FileUtils.mkdir_p(File.join(basedir, ".batcave")) lock(manifest_path) do manifest = load(manifest_path) # Store this thing. manifest["things"][dsl.thing] = dsl.to_hash # Write the manifest to a tmpfile and rename it. tmpfile = manifest_path + ".tmp" File.open(tmpfile, "w") do |tmp| tmp.write(manifest.to_yaml) tmp.flush end File.rename(tmpfile, manifest_path) end end |