Class: Puppet::Provider::PropertyList

Inherits:
Puppet::Provider
  • Object
show all
Defined in:
lib/puppet/provider/propertylist.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value = {}) ⇒ PropertyList

Returns a new instance of PropertyList.



92
93
94
95
# File 'lib/puppet/provider/propertylist.rb', line 92

def initialize(value={})
  super(value)
  @property_flush = {}
end

Class Method Details

.get_format(path) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/puppet/provider/propertylist.rb', line 78

def get_format(path)
  bytes = IO.read(path, 8)
  if bytes.eql?('bplist00')
    return :binary
  elsif bytes =~ /\A\<\?xml\sve/
    return :xml
  else
    warn("Error: #{path} is not a Property List.")
    false
  end
end

.get_propertylist_properties(path) ⇒ Object



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
# File 'lib/puppet/provider/propertylist.rb', line 28

def get_propertylist_properties(path)
  absent = { :name => path, :ensure => :absent }

  unless File.exists?(path)
    return absent
  end

  unless File.file?(path)
    raise Puppet::Error, "Error: #{path} is not a file, [#{File.ftype(path)}]."
  end

  return absent unless format = get_format(path)

  stat    = File.stat path
  content = read_plist path
  {
    :name     => path,
    :format   => format,
    :ensure   => :present,
    :owner    => Etc.getpwuid(stat.uid).name,
    :group    => Etc.getgrgid(stat.gid).name,
    :mode     => stat.mode.to_s(8)[2..-1],
    :content  => content,
  }
end

.instancesObject



11
12
13
14
15
16
17
18
19
20
# File 'lib/puppet/provider/propertylist.rb', line 11

def instances
  args = Puppet::Util::CommandLine.new.args
  resource_type, resource_name = args.each { |x| x }
  unless resource_name
    err = ['Listing propertylist instances is not supported.',
    'Please specify a file or directory, e.g. puppet resource file /etc'].join(' ')
    raise Puppet::Error, err
  end
  [ new(get_propertylist_properties(resource_name)) ]
end

.prefetch(resources) ⇒ Object



22
23
24
25
26
# File 'lib/puppet/provider/propertylist.rb', line 22

def prefetch(resources)
  resources.each do |k,v|
    v.provider = new(get_propertylist_properties(k))
  end
end

.read_plist(path) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/puppet/provider/propertylist.rb', line 54

def read_plist(path)
  begin
    plist = CFPropertyList::List.new(:file => path)
  rescue Exception
    warn("Warning: #{path} is not a Property List.")
  end
  return {} unless plist
  CFPropertyList.native_types(plist.value)
end

.write_plist(path, content, format) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/puppet/provider/propertylist.rb', line 64

def write_plist(path, content, format)
  f = case format
  when :xml
    CFPropertyList::List::FORMAT_XML
  when :binary
    CFPropertyList::List::FORMAT_BINARY
  else
    raise Puppet::Error, "Bad Format: #{format}"
  end
  plist = CFPropertyList::List.new
  plist.value = CFPropertyList.guess(content)
  plist.save(path, f, {:formatted => true})
end

Instance Method Details

#createObject



97
98
99
# File 'lib/puppet/provider/propertylist.rb', line 97

def create
  @property_flush[:ensure] = :present
end

#destroyObject



105
106
107
# File 'lib/puppet/provider/propertylist.rb', line 105

def destroy
  @property_flush[:ensure] = :absent
end

#exists?Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/puppet/provider/propertylist.rb', line 101

def exists?
  @property_hash[:ensure] == :present
end

#flushObject



144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/puppet/provider/propertylist.rb', line 144

def flush
  if @property_flush[:ensure] == :absent
    FileUtils.rm resource[:path]
  else
    set_content
    set_owner
    set_group
    set_mode
  end
  # Collect the resources again once they've been changed (that way `puppet
  # resource` will show the correct values after changes have been made).
  @property_hash = self.class.get_propertylist_properties(resource[:path])
end

#set_contentObject



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/puppet/provider/propertylist.rb', line 121

def set_content
  path    = resource[:path]
  content = if resource[:content].size != 1
    resource[:content]
  else
    resource[:content].first
  end
  if resource[:method] == :insert
    if File.exists?(path) and File.file?(path)
      original = self.class.read_plist path
      case original
      when Hash
        content = original.merge(content)
      when Array
        content = original.zip(content).collect { |x| x.compact.last }
      else
        content = content
      end
    end
  end
  self.class.write_plist(path, content, resource[:format])
end

#set_groupObject



113
114
115
# File 'lib/puppet/provider/propertylist.rb', line 113

def set_group
  FileUtils.chown nil, resource[:group], resource[:path]
end

#set_modeObject



117
118
119
# File 'lib/puppet/provider/propertylist.rb', line 117

def set_mode
  FileUtils.chmod resource[:mode].to_i(8), resource[:path]
end

#set_ownerObject



109
110
111
# File 'lib/puppet/provider/propertylist.rb', line 109

def set_owner
  FileUtils.chown resource[:owner], nil, resource[:path]
end