class Puppet::Settings::FileSetting

A file.

Attributes

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
59 def initialize(args)
60   @group = Unspecified.new
61   @owner = Unspecified.new
62   super(args)
63 end

Public Instance Methods

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
190 def exclusive_open(option = 'r', &block)
191   controlled_access do |mode|
192     Puppet::FileSystem.exclusive_open(file(), mode, option, &block)
193   end
194 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
95 def group
96   @group.value
97 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
67 def group=(value)
68   @group = case value
69            when "root"
70              Root.new
71            when "service"
72              # Group falls back to `nil` because we cannot assume that a "root" group exists.
73              # Some systems have root group, others have wheel, others have something else.
74              Service.new(:group, nil, @settings, :service_group_available?)
75            else
76              unknown_value(':group', value)
77            end
78 end
munge(value) click to toggle source
    # File lib/puppet/settings/file_setting.rb
111 def munge(value)
112   if value.is_a?(String) and value != ':memory:' # for sqlite3 in-memory tests
113     value = File.expand_path(value)
114   end
115   value
116 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
202 def open(option = 'r', &block)
203   controlled_access do |mode|
204     Puppet::FileSystem.open(file, mode, option, &block)
205   end
206 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
101 def owner
102   @owner.value
103 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
82 def owner=(value)
83   @owner = case value
84            when "root"
85              Root.new
86            when "service"
87              Service.new(:user, "root", @settings, :service_user_available?)
88            else
89              unknown_value(':owner', value)
90            end
91 end
set_meta(meta) click to toggle source
    # File lib/puppet/settings/file_setting.rb
105 def set_meta(meta)
106   self.owner = meta.owner if meta.owner
107   self.group = meta.group if meta.group
108   self.mode = meta.mode if meta.mode
109 end
to_resource() click to toggle source

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

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

Make sure any provided variables look up to something.

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

Private Instance Methods

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