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/json.rb,
lib/chef/resource/file/verification/yaml.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.

Additionally, 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

Json, SystemdUnit, Yaml

Defined Under Namespace

Classes: Json, SystemdUnit, Yaml

Instance Attribute Summary collapse

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.



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

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

Instance Attribute Details

#outputObject (readonly)

Returns the value of attribute output.



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

def output
  @output
end

Class Method Details

.lookup(name) ⇒ Object



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

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



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

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

.provides?(name) ⇒ Boolean

Returns:

  • (Boolean)


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

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

Instance Method Details

#loggerObject



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

def logger
  @parent_resource.logger
end

#to_sObject



132
133
134
135
136
137
138
139
140
# File 'lib/chef/resource/file/verification.rb', line 132

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



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

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



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

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



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

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)
  ret = interpreter.evaluate
  @output = interpreter.output
  ret
end

#verify_registered_verification(path, opts) ⇒ Object



126
127
128
129
130
# File 'lib/chef/resource/file/verification.rb', line 126

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