Class: Chef::Resource::File::Verification

Inherits:
Object
  • Object
show all
Extended by:
Mixin::DescendantsTracker
Defined in:
lib/chef/resource/file/verification.rb,
lib/chef/resource/file/verification/systemd_unit.rb

Overview

See RFC 027 for a full specification

File verifications allow user-supplied commands a means of preventing file resource content deploys. Their intended use is to verify the contents of a temporary file before it is deployed onto the system.

Similar to not_if and only_if, file verifications can take a ruby block, which will be called, or a string, which will be executed as a Shell command.

Additonally, Chef or third-party verifications can ship "registered verifications" that the user can use by specifying a :symbol as the command name.

To create a registered verification, create a class that inherits from Chef::Resource::File::Verification and use the provides class method to give it name. Registered verifications are expected to supply a verify instance method that takes 2 arguments.

Example: class Chef class Resource class File::Verification::Foo < Chef::Resource::File::Verification provides :noop def verify(path, opts) #yolo true end end end end

Direct Known Subclasses

SystemdUnit

Defined Under Namespace

Classes: SystemdUnit

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Mixin::DescendantsTracker

descendants, descendants, direct_descendants, direct_descendants, find_descendants_by_name, find_descendants_by_name, inherited, store_inherited

Constructor Details

#initialize(parent_resource, command, opts, &block) ⇒ Verification

Returns a new instance of Verification


83
84
85
86
87
# File 'lib/chef/resource/file/verification.rb', line 83

def initialize(parent_resource, command, opts, &block)
  @command, @command_opts = command, opts
  @block = block
  @parent_resource = parent_resource
end

Class Method Details

.lookup(name) ⇒ Object


75
76
77
78
79
80
81
# File 'lib/chef/resource/file/verification.rb', line 75

def self.lookup(name)
  c = descendants.find { |d| d.provides?(name) }
  if c.nil?
    raise Chef::Exceptions::VerificationNotFound.new "No file verification for #{name} found."
  end
  c
end

.provides(name) ⇒ Object


67
68
69
# File 'lib/chef/resource/file/verification.rb', line 67

def self.provides(name)
  @provides = name
end

.provides?(name) ⇒ Boolean

Returns:

  • (Boolean)

71
72
73
# File 'lib/chef/resource/file/verification.rb', line 71

def self.provides?(name)
  @provides == name
end

Instance Method Details

#verify(path, opts = {}) ⇒ Object


89
90
91
92
93
94
95
96
97
98
# File 'lib/chef/resource/file/verification.rb', line 89

def verify(path, opts = {})
  Chef::Log.debug("Running verification[#{self}] on #{path}")
  if @block
    verify_block(path, opts)
  elsif @command.is_a?(Symbol)
    verify_registered_verification(path, opts)
  elsif @command.is_a?(String)
    verify_command(path, opts)
  end
end

#verify_block(path, opts) ⇒ Object

opts is currently unused, but included in the API to support future extensions


102
103
104
# File 'lib/chef/resource/file/verification.rb', line 102

def verify_block(path, opts)
  @block.call(path)
end

#verify_command(path, opts) ⇒ Object

We reuse Chef::GuardInterpreter in order to support the same set of options that the not_if/only_if blocks do


108
109
110
111
112
113
114
115
# File 'lib/chef/resource/file/verification.rb', line 108

def verify_command(path, opts)
  if @command.include?("%{file}")
    raise ArgumentError, "The %{file} expansion for verify commands has been removed. Please use %{path} instead."
  end
  command = @command % { :path => path }
  interpreter = Chef::GuardInterpreter.for_resource(@parent_resource, command, @command_opts)
  interpreter.evaluate
end

#verify_registered_verification(path, opts) ⇒ Object


117
118
119
120
121
# File 'lib/chef/resource/file/verification.rb', line 117

def verify_registered_verification(path, opts)
  verification_class = Chef::Resource::File::Verification.lookup(@command)
  v = verification_class.new(@parent_resource, @command, @command_opts, &@block)
  v.verify(path, opts)
end