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

Inherits:
Object
  • Object
show all
Extended by:
Mixin::DescendantsTracker
Defined in:
lib/chef/resource/file/verification.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

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
116
117
118
# File 'lib/chef/resource/file/verification.rb', line 108

def verify_command(path, opts)
  # First implementation interpolated `file`; docs & RFC claim `path`
  # is interpolated. Until `file` can be deprecated, interpolate both.
  Chef.log_deprecation(
    "%{file} is deprecated in verify command and will not be "\
    "supported in Chef 13. Please use %{path} instead."
  ) if @command.include?("%{file}")
  command = @command % { :file => path, :path => path }
  interpreter = Chef::GuardInterpreter.for_resource(@parent_resource, command, @command_opts)
  interpreter.evaluate
end

#verify_registered_verification(path, opts) ⇒ Object



120
121
122
123
124
# File 'lib/chef/resource/file/verification.rb', line 120

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