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.



88
89
90
91
92
# File 'lib/chef/resource/file/verification.rb', line 88

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
82
# 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

#loggerObject



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

def logger
  @parent_resource.logger
end

#to_sObject



129
130
131
132
133
134
135
136
137
# File 'lib/chef/resource/file/verification.rb', line 129

def to_s
  if @block
    "<Proc>"
  elsif @command.is_a?(Symbol)
    "#{@command.inspect} (#{Chef::Resource::File::Verification.lookup(@command).name})"
  elsif @command.is_a?(String)
    @command
  end
end

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



94
95
96
97
98
99
100
101
102
103
# File 'lib/chef/resource/file/verification.rb', line 94

def verify(path, opts = {})
  logger.trace("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



107
108
109
# File 'lib/chef/resource/file/verification.rb', line 107

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



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

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



123
124
125
126
127
# File 'lib/chef/resource/file/verification.rb', line 123

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