summaryrefslogtreecommitdiffstats
path: root/lib/puppet/util/settings/file_setting.rb
blob: 0fa65d8465f0f54a4ddd622e1767b28998d9c248 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
require 'puppet/util/settings/setting'

# A file.
class Puppet::Util::Settings::FileSetting < Puppet::Util::Settings::Setting
  AllowedOwners = %w{root service}
  AllowedGroups = %w{root service}

  class SettingError < StandardError; end

  attr_accessor :mode, :create

  # Should we create files, rather than just directories?
  def create_files?
    create
  end

  def group=(value)
    unless AllowedGroups.include?(value)
      identifying_fields = [desc,name,default].compact.join(': ')
      raise SettingError, "Internal error: The :group setting for #{identifying_fields} must be 'service', not '#{value}'"
    end
    @group = value
  end

  def group
    return unless @group
    @settings[:group]
  end

  def owner=(value)
    unless AllowedOwners.include?(value)
      identifying_fields = [desc,name,default].compact.join(': ')
      raise SettingError, "Internal error: The :owner setting for #{identifying_fields} must be either 'root' or 'service', not '#{value}'"
    end
    @owner = value
  end

  def owner
    return unless @owner
    return "root" if @owner == "root" or ! use_service_user?
    @settings[:user]
  end

  def use_service_user?
    @settings[:mkusers] or @settings.service_user_available?
  end

  # Set the type appropriately.  Yep, a hack.  This supports either naming
  # the variable 'dir', or adding a slash at the end.
  def munge(value)
    # If it's not a fully qualified path...
    if value.is_a?(String) and value !~ /^\$/ and value != 'false'
      # Make it one
      value = File.expand_path(value)
    end
    if value.to_s =~ /\/$/
      @type = :directory
      return value.sub(/\/$/, '')
    end
    value
  end

  # Return the appropriate type.
  def type
    value = @settings.value(self.name)
    if @name.to_s =~ /dir/
      return :directory
    elsif value.to_s =~ /\/$/
      return :directory
    elsif value.is_a? String
      return :file
    else
      return nil
    end
  end

  # Turn our setting thing into a Puppet::Resource instance.
  def to_resource
    return nil unless type = self.type

    path = self.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 or create_files? or File.exist?(path)
    return nil if path =~ /^\/dev/ or path =~ /^[A-Z]:\/dev/i

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

    if Puppet[:manage_internal_file_permissions]
      resource[:mode] = self.mode if self.mode

      if Puppet.features.root?
        resource[:owner] = self.owner if self.owner
        resource[:group] = self.group if self.group
      end
    end

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

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

    resource
  end

  # Make sure any provided variables look up to something.
  def validate(value)
    return true unless value.is_a? String
    value.scan(/\$(\w+)/) { |name|
      name = $1
      unless @settings.include?(name)
        raise ArgumentError,
          "Settings parameter '#{name}' is undefined"
      end
    }
  end
end