Class: Puppet::Settings::FileSetting

Inherits:
StringSetting show all
Defined in:
lib/puppet/settings/file_setting.rb

Overview

A file.

Defined Under Namespace

Classes: Root, Service, SettingError, Unspecified

Constant Summary

Constants inherited from BaseSetting

BaseSetting::HOOK_TYPES

Instance Attribute Summary collapse

Attributes inherited from BaseSetting

#call_hook, #default, #deprecated, #desc, #name, #section, #short

Instance Method Summary collapse

Methods inherited from StringSetting

#validate

Methods inherited from BaseSetting

#allowed_on_commandline?, available_call_hook_values, #call_hook_on_define?, #call_hook_on_initialize?, #completely_deprecated?, #deprecated?, #getopt_args, #has_hook?, #hook=, #inspect, #iscreated, #iscreated?, #optparse_args, #print, #to_config, #value

Constructor Details

#initialize(args) ⇒ FileSetting

Returns a new instance of FileSetting.



61
62
63
64
65
# File 'lib/puppet/settings/file_setting.rb', line 61

def initialize(args)
  @group = Unspecified.new
  @owner = Unspecified.new
  super(args)
end

Instance Attribute Details

#modeObject

Returns the value of attribute mode.



59
60
61
# File 'lib/puppet/settings/file_setting.rb', line 59

def mode
  @mode
end

Instance Method Details

#exclusive_open(option = 'r', &block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • option (String) (defaults to: 'r')

    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



180
181
182
183
184
# File 'lib/puppet/settings/file_setting.rb', line 180

def exclusive_open(option = 'r', &block)
  controlled_access do |mode|
    Puppet::FileSystem.exclusive_open(file(), mode, option, &block)
  end
end

#groupString?

Returns the name of the group to use for the file or nil if the group should not be managed.

Returns:

  • (String, nil)

    the name of the group to use for the file or nil if the group should not be managed



97
98
99
# File 'lib/puppet/settings/file_setting.rb', line 97

def group
  @group.value
end

#group=(value) ⇒ Object

Parameters:

  • value (String)

    the group to use on the created file (can only be “root” or “service”)



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/puppet/settings/file_setting.rb', line 69

def group=(value)
  @group = case value
           when "root"
             Root.new
           when "service"
             # Group falls back to `nil` because we cannot assume that a "root" group exists.
             # Some systems have root group, others have wheel, others have something else.
             Service.new(:group, nil, @settings, :service_group_available?)
           else
             unknown_value(':group', value)
           end
end

#munge(value) ⇒ Object



113
114
115
116
117
118
# File 'lib/puppet/settings/file_setting.rb', line 113

def munge(value)
  if value.is_a?(String) and value != ':memory:' # for sqlite3 in-memory tests
    value = File.expand_path(value)
  end
  value
end

#open(option = 'r', &block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • option (String) (defaults to: 'r')

    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



192
193
194
195
196
# File 'lib/puppet/settings/file_setting.rb', line 192

def open(option = 'r', &block)
  controlled_access do |mode|
    Puppet::FileSystem.open(file, mode, option, &block)
  end
end

#ownerString?

Returns the name of the user to use for the file or nil if the user should not be managed.

Returns:

  • (String, nil)

    the name of the user to use for the file or nil if the user should not be managed



103
104
105
# File 'lib/puppet/settings/file_setting.rb', line 103

def owner
  @owner.value
end

#owner=(value) ⇒ Object

Parameters:

  • value (String)

    the owner to use on the created file (can only be “root” or “service”)



84
85
86
87
88
89
90
91
92
93
# File 'lib/puppet/settings/file_setting.rb', line 84

def owner=(value)
  @owner = case value
           when "root"
             Root.new
           when "service"
             Service.new(:user, "root", @settings, :service_user_available?)
           else
             unknown_value(':owner', value)
           end
end

#set_meta(meta) ⇒ Object



107
108
109
110
111
# File 'lib/puppet/settings/file_setting.rb', line 107

def set_meta(meta)
  self.owner = meta.owner if meta.owner
  self.group = meta.group if meta.group
  self.mode = meta.mode if meta.mode
end

#to_resourceObject

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



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/puppet/settings/file_setting.rb', line 125

def to_resource
  type = self.type
  return nil unless type

  path = value

  return nil unless path.is_a?(String)

  # Make sure the paths are fully qualified.
  path = File.expand_path(path)

  return nil unless type == :directory || Puppet::FileSystem.exist?(path)
  return nil if path =~ %r{^/dev} || path =~ %r{^[A-Z]:/dev}i

  resource = Puppet::Resource.new(:file, path)

  if Puppet[:manage_internal_file_permissions]
    if mode
      # This ends up mimicking the munge method of the mode
      # parameter to make sure that we're always passing the string
      # version of the octal number.  If we were setting the
      # 'should' value for mode rather than the 'is', then the munge
      # method would be called for us automatically.  Normally, one
      # wouldn't need to call the munge method manually, since
      # 'should' gets set by the provider and it should be able to
      # provide the data in the appropriate format.
      mode = self.mode
      mode = mode.to_i(8) if mode.is_a?(String)
      mode = mode.to_s(8)
      resource[:mode] = mode
    end

    # REMIND fails on Windows because chown/chgrp functionality not supported yet
    if Puppet.features.root? and !Puppet::Util::Platform.windows?
      resource[:owner] = owner if owner
      resource[:group] = group if group
    end
  end

  resource[:ensure] = type
  resource[:loglevel] = :debug
  resource[:links] = :follow
  resource[:backup] = false

  resource.tag(section, name, "settings")

  resource
end

#typeObject



120
121
122
# File 'lib/puppet/settings/file_setting.rb', line 120

def type
  :file
end