class Puppet::Settings::FileSetting

A file.

Attributes

create[RW]
mode[RW]

Public Class Methods

new(args) click to toggle source
Calls superclass method Puppet::Settings::BaseSetting::new
   # File lib/puppet/settings/file_setting.rb
58 def initialize(args)
59   @group = Unspecified.new
60   @owner = Unspecified.new
61   super(args)
62 end

Public Instance Methods

create_files?() click to toggle source

Should we create files, rather than just directories?

   # File lib/puppet/settings/file_setting.rb
65 def create_files?
66   create
67 end
exclusive_open(option = 'r', &block) click to toggle source

@api private @param option [String] Extra file operation mode information to use

(defaults to read-only mode 'r')
This is the standard mechanism Ruby uses in the IO class, and therefore
encoding may be explicitly like fmode : encoding or fmode : "BOM|UTF-*"
for example, a:ASCII or w+:UTF-8
    # File lib/puppet/settings/file_setting.rb
194 def exclusive_open(option = 'r', &block)
195   controlled_access do |mode|
196     Puppet::FileSystem.exclusive_open(file(), mode, option, &block)
197   end
198 end
group() click to toggle source

@return [String, nil] the name of the group to use for the file or nil if the group should not be managed @api public

    # File lib/puppet/settings/file_setting.rb
 99 def group
100   @group.value
101 end
group=(value) click to toggle source

@param value [String] the group to use on the created file (can only be “root” or “service”) @api public

   # File lib/puppet/settings/file_setting.rb
71 def group=(value)
72   @group = case value
73            when "root"
74              Root.new
75            when "service"
76              # Group falls back to `nil` because we cannot assume that a "root" group exists.
77              # Some systems have root group, others have wheel, others have something else.
78              Service.new(:group, nil, @settings, :service_group_available?)
79            else
80              unknown_value(':group', value)
81            end
82 end
munge(value) click to toggle source
    # File lib/puppet/settings/file_setting.rb
115 def munge(value)
116   if value.is_a?(String) and value != ':memory:' # for sqlite3 in-memory tests
117     value = File.expand_path(value)
118   end
119   value
120 end
open(option = 'r', &block) click to toggle source

@api private @param option [String] Extra file operation mode information to use

(defaults to read-only mode 'r')
This is the standard mechanism Ruby uses in the IO class, and therefore
encoding may be explicitly like fmode : encoding or fmode : "BOM|UTF-*"
for example, a:ASCII or w+:UTF-8
    # File lib/puppet/settings/file_setting.rb
206 def open(option = 'r', &block)
207   controlled_access do |mode|
208     Puppet::FileSystem.open(file, mode, option, &block)
209   end
210 end
owner() click to toggle source

@return [String, nil] the name of the user to use for the file or nil if the user should not be managed @api public

    # File lib/puppet/settings/file_setting.rb
105 def owner
106   @owner.value
107 end
owner=(value) click to toggle source

@param value [String] the owner to use on the created file (can only be “root” or “service”) @api public

   # File lib/puppet/settings/file_setting.rb
86 def owner=(value)
87   @owner = case value
88            when "root"
89              Root.new
90            when "service"
91              Service.new(:user, "root", @settings, :service_user_available?)
92            else
93              unknown_value(':owner', value)
94            end
95 end
set_meta(meta) click to toggle source
    # File lib/puppet/settings/file_setting.rb
109 def set_meta(meta)
110   self.owner = meta.owner if meta.owner
111   self.group = meta.group if meta.group
112   self.mode = meta.mode if meta.mode
113 end
to_resource() click to toggle source

Turn our setting thing into a Puppet::Resource instance.

    # File lib/puppet/settings/file_setting.rb
127 def to_resource
128   type = self.type
129   return nil unless type
130 
131   path = self.value
132 
133   return nil unless path.is_a?(String)
134 
135   # Make sure the paths are fully qualified.
136   path = File.expand_path(path)
137 
138   return nil unless type == :directory or create_files? or Puppet::FileSystem.exist?(path)
139   return nil if path =~ /^\/dev/ or path =~ /^[A-Z]:\/dev/i
140 
141   resource = Puppet::Resource.new(:file, path)
142 
143   if Puppet[:manage_internal_file_permissions]
144     if self.mode
145       # This ends up mimicking the munge method of the mode
146       # parameter to make sure that we're always passing the string
147       # version of the octal number.  If we were setting the
148       # 'should' value for mode rather than the 'is', then the munge
149       # method would be called for us automatically.  Normally, one
150       # wouldn't need to call the munge method manually, since
151       # 'should' gets set by the provider and it should be able to
152       # provide the data in the appropriate format.
153       mode = self.mode
154       mode = mode.to_i(8) if mode.is_a?(String)
155       mode = mode.to_s(8)
156       resource[:mode] = mode
157     end
158 
159     # REMIND fails on Windows because chown/chgrp functionality not supported yet
160     if Puppet.features.root? and !Puppet::Util::Platform.windows?
161       resource[:owner] = self.owner if self.owner
162       resource[:group] = self.group if self.group
163     end
164   end
165 
166   resource[:ensure] = type
167   resource[:loglevel] = :debug
168   resource[:links] = :follow
169   resource[:backup] = false
170 
171   resource.tag(self.section, self.name, "settings")
172 
173   resource
174 end
type() click to toggle source
    # File lib/puppet/settings/file_setting.rb
122 def type
123   :file
124 end
validate(value) click to toggle source

Make sure any provided variables look up to something.

    # File lib/puppet/settings/file_setting.rb
177 def validate(value)
178   return true unless value.is_a? String
179   value.scan(/\$(\w+)/) { |name|
180     name = $1
181     unless @settings.include?(name)
182       raise ArgumentError,
183         _("Settings parameter '%{name}' is undefined") % { name: name }
184     end
185   }
186 end

Private Instance Methods

controlled_access() { |yielded_value| ... } click to toggle source
    # File lib/puppet/settings/file_setting.rb
222 def controlled_access(&block)
223   chown = nil
224   if Puppet.features.root?
225     chown = [owner, group]
226   else
227     chown = [nil, nil]
228   end
229 
230   Puppet::Util::SUIDManager.asuser(*chown) do
231     # Update the umask to make non-executable files
232     Puppet::Util.withumask(File.umask ^ 0111) do
233       yielded_value = case self.mode
234                       when String
235                         self.mode.to_i(8)
236                       when NilClass
237                         0640
238                       else
239                         self.mode
240                       end
241 
242       yield yielded_value
243     end
244   end
245 end
file() click to toggle source
    # File lib/puppet/settings/file_setting.rb
214 def file
215   Puppet::FileSystem.pathname(value)
216 end
unknown_value(parameter, value) click to toggle source
    # File lib/puppet/settings/file_setting.rb
218 def unknown_value(parameter, value)
219   raise SettingError, _("The %{parameter} parameter for the setting '%{name}' must be either 'root' or 'service', not '%{value}'") % { parameter: parameter, name: name, value: value }
220 end