Class: Moku::FileInstanceRepo

Inherits:
Object
  • Object
show all
Defined in:
lib/moku/file_instance_repo.rb

Overview

Repository for persisting instances to files

Instance Method Summary collapse

Constructor Details

#initialize(instances_path: Moku.instance_root, releases_path: Moku.releases_root, branches_path: Moku.branches_root, locks_path: Moku.locks_root, git_runner: Moku.git_runner) ⇒ FileInstanceRepo

Returns a new instance of FileInstanceRepo.


15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/moku/file_instance_repo.rb', line 15

def initialize(
  instances_path: Moku.instance_root,
  releases_path: Moku.releases_root,
  branches_path: Moku.branches_root,
  locks_path: Moku.locks_root,
  git_runner: Moku.git_runner
)
  @instances_path = Pathname.new(instances_path)
  @releases_path = Pathname.new(releases_path)
  @branches_path = Pathname.new(branches_path)
  @locks_path = Pathname.new(locks_path)
  @git_runner = git_runner
end

Instance Method Details

#find(name) ⇒ Object

Raises:

  • (ArgumentError)

29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/moku/file_instance_repo.rb', line 29

def find(name)
  raise ArgumentError unless name
  lock!(name) if Moku.enable_locking
  contents = instance_content(name)
  releases = releases_content(name)
  Instance.new(
    name: name,
    source: instance_from_hash(name, contents),
    deploy: ArchiveReference.from_hash(contents["deploy"], git_runner),
    infrastructure: ArchiveReference.from_hash(
      [contents["infrastructure"]].flatten.first,
      git_runner
    ),
    dev: ArchiveReference.from_hash([contents["dev"]].flatten.first, git_runner),
    releases: releases.fetch("releases", []).map {|r| LoggedRelease.from_hash(r) }
  )
end

#lock!(name) ⇒ Object

Obtain an exclusive lock on the lockfile, using the OS's flock feature. The OS will release the lock automatically when the program exits. We intentionally do not remove the lockfile under any circumstances. Idempotent

Raises:


59
60
61
62
63
64
65
66
67
# File 'lib/moku/file_instance_repo.rb', line 59

def lock!(name)
  return if active_locks.include?(name)

  locks_path.mkpath
  lockfile = File.open(locks_path/name, File::RDWR|File::CREAT, 0o644)
  raise InstanceBusyError unless lockfile.flock(File::LOCK_EX|File::LOCK_NB)

  active_locks << name
end

#save_instance(instance) ⇒ Object


51
52
53
# File 'lib/moku/file_instance_repo.rb', line 51

def save_instance(instance)
  write_branch(instance.name, instance.default_branch)
end

#save_releases(instance) ⇒ Object


47
48
49
# File 'lib/moku/file_instance_repo.rb', line 47

def save_releases(instance)
  write_releases(instance.name, instance.releases)
end