Class: ChefLocker

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

Overview

See Also:

Instance Method Summary collapse

Constructor Details

#initialize(output: $stdout) ⇒ ChefLocker

Returns a new instance of ChefLocker.

Parameters:

  • output (#puts) (defaults to: $stdout)

    IO-like object to output info to



6
7
8
9
# File 'lib/chef_locker.rb', line 6

def initialize(output: $stdout)
  @output = output
  @runlock = Chef::RunLock.new(Chef::Config.lockfile)
end

Instance Method Details

#lock(message) ⇒ Object

Locks Chef runs (prevents Chef clients from converging)

Parameters:

  • message (#to_s)

    message to include in lock file

Raises:

  • (ArgumentError)


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/chef_locker.rb', line 14

def lock(message)
  message = message.to_s
  raise ArgumentError, "Message can't be blank" if message.strip.empty?

  @runlock.acquire
  begin
    @output.puts 'Lock acquired'
    @runlock.save_pid
    @runlock.runlock.write(' ' + message)
    @runlock.runlock.fsync

    # Now we block until interrupted (there might be a better way to do this)
    loop { $stdin.read }
  ensure
    @runlock.release
  end
end