The Puppet Labs Issue Tracker has Moved: https://tickets.puppetlabs.com

This issue tracker is now in read-only archive mode and automatic ticket export has been disabled. Redmine users will need to create a new JIRA account to file tickets using https://tickets.puppetlabs.com. See the following page for information on filing tickets with JIRA:

yumrepo_refactor.patch

Patch that adds provider for yumrepo - Luke Kanies, 08/03/2011 10:05 am

Download (11.1 KB)

View differences:

lib/puppet/provider/yumrepo/plain.rb
1
require 'puppet/provider'
2

  
3
Puppet::Type.type(:yumrepo).provide :plain do
4
    self.filetype = Puppet::Util::FileType.filetype(:flat)
5

  
6
    class << self
7
        attr_accessor :filetype
8
        # The writer is only used for testing, there should be no need
9
        # to change yumconf or inifile in any other context
10
        attr_accessor :yumconf
11
        attr_writer :inifile
12
    end
13

  
14
    @inifile = nil
15

  
16
    @yumconf = "/etc/yum.conf"
17

  
18
    # Where to put files for brand new sections
19
    @defaultrepodir = nil
20

  
21
    def self.instances
22
        l = []
23
        check = validproperties
24
        clear
25
        inifile.each_section do |s|
26
            next if s.name == "main"
27
            obj = create(:name => s.name, :check => check)
28
            current_values = obj.retrieve
29
            obj.eachproperty do |property|
30
                if current_values[property].nil?
31
                    obj.delete(property.name)
32
                else
33
                    property.should = current_values[property]
34
                end
35
            end
36
            obj.delete(:check)
37
            l << obj
38
        end
39
        l
40
    end
41

  
42
    # Return the Puppet::Util::IniConfig::File for the whole yum config
43
    def self.inifile
44
        if @inifile.nil?
45
            @inifile = read()
46
            main = @inifile['main']
47
            if main.nil?
48
                raise Puppet::Error, "File #{yumconf} does not contain a main section"
49
            end
50
            reposdir = main['reposdir']
51
            reposdir ||= "/etc/yum.repos.d, /etc/yum/repos.d"
52
            reposdir.gsub!(/[\n,]/, " ")
53
            reposdir.split.each do |dir|
54
                Dir::glob("#{dir}/*.repo").each do |file|
55
                    if File.file?(file)
56
                        @inifile.read(file)
57
                    end
58
                end
59
            end
60
            reposdir.split.each do |dir|
61
                if File::directory?(dir) && File::writable?(dir)
62
                    @defaultrepodir = dir
63
                    break
64
                end
65
            end
66
        end
67
        return @inifile
68
    end
69

  
70
    # Parse the yum config files. Only exposed for the tests
71
    # Non-test code should use self.inifile to get at the
72
    # underlying file
73
    def self.read
74
        result = Puppet::Util::IniConfig::File.new()
75
        result.read(yumconf)
76
        main = result['main']
77
        if main.nil?
78
            raise Puppet::Error, "File #{yumconf} does not contain a main section"
79
        end
80
        reposdir = main['reposdir']
81
        reposdir ||= "/etc/yum.repos.d, /etc/yum/repos.d"
82
        reposdir.gsub!(/[\n,]/, " ")
83
        reposdir.split.each do |dir|
84
            Dir::glob("#{dir}/*.repo").each do |file|
85
                if File.file?(file)
86
                    result.read(file)
87
                end
88
            end
89
        end
90
        if @defaultrepodir.nil?
91
            reposdir.split.each do |dir|
92
                if File::directory?(dir) && File::writable?(dir)
93
                    @defaultrepodir = dir
94
                    break
95
                end
96
            end
97
        end
98
        return result
99
    end
100

  
101
    # Return the Puppet::Util::IniConfig::Section with name NAME
102
    # from the yum config
103
    def self.section(name)
104
        result = inifile[name]
105
        if result.nil?
106
            # Brand new section
107
            path = yumconf
108
            unless @defaultrepodir.nil?
109
                path = File::join(@defaultrepodir, "#{name}.repo")
110
            end
111
            Puppet::info "create new repo #{name} in file #{path}"
112
            result = inifile.add_section(name, path)
113
        end
114
        return result
115
    end
116

  
117
    # Store all modifications back to disk
118
    def self.store
119
        inifile.store
120
        unless Puppet[:noop]
121
            target_mode = 0644 # FIXME: should be configurable
122
            inifile.each_file do |file|
123
                current_mode = File.stat(file).mode & 0777
124
                unless current_mode == target_mode
125
                    Puppet::info "changing mode of #{file} from %03o to %03o" % [current_mode, target_mode]
126
                    File.chmod(target_mode, file)
127
                end
128
            end
129
        end
130
    end
131

  
132
    # This is only used during testing.
133
    def self.clear
134
        @inifile = nil
135
        @yumconf = "/etc/yum.conf"
136
        @defaultrepodir = nil
137
    end
138

  
139
    # Return the Puppet::Util::IniConfig::Section for this yumrepo resource
140
    def section
141
        self.class.section(self[:name])
142
    end
143

  
144
    # Store modifications to this yumrepo resource back to disk
145
    def store
146
        self.class.store
147
    end
148
end
lib/puppet/type/yumrepo.rb
14 14
        end
15 15

  
16 16
        def sync
17
            if insync?(retrieve)
18
                result = nil
17
            result = set(self.should)
18
            if should == :absent
19
                provider.section[inikey] = nil
19 20
            else
20
                result = set(self.should)
21
                if should == :absent
22
                    resource.section[inikey] = nil
23
                else
24
                    resource.section[inikey] = should
25
                end
21
                provider.section[inikey] = should
26 22
            end
27
            return result
23
            result
28 24
        end
29 25

  
30 26
        def retrieve
31
            return resource.section[inikey]
27
            return provider.section[inikey]
32 28
        end
33 29

  
34 30
        def inikey
......
63 59
                baseurl are not supported. No attempt is made to access
64 60
                files included with the **include** directive"
65 61

  
66
        class << self
67
            attr_accessor :filetype
68
            # The writer is only used for testing, there should be no need
69
            # to change yumconf or inifile in any other context
70
            attr_accessor :yumconf
71
            attr_writer :inifile
72
        end
73

  
74
        self.filetype = Puppet::Util::FileType.filetype(:flat)
75

  
76
        @inifile = nil
77

  
78
        @yumconf = "/etc/yum.conf"
79

  
80
        # Where to put files for brand new sections
81
        @defaultrepodir = nil
82

  
83
        def self.instances
84
            l = []
85
            check = validproperties
86
            clear
87
            inifile.each_section do |s|
88
                next if s.name == "main"
89
                obj = create(:name => s.name, :check => check)
90
                current_values = obj.retrieve
91
                obj.eachproperty do |property|
92
                    if current_values[property].nil?
93
                        obj.delete(property.name)
94
                    else
95
                        property.should = current_values[property]
96
                    end
97
                end
98
                obj.delete(:check)
99
                l << obj
100
            end
101
            l
102
        end
103

  
104
        # Return the Puppet::Util::IniConfig::File for the whole yum config
105
        def self.inifile
106
            if @inifile.nil?
107
                @inifile = read()
108
                main = @inifile['main']
109
                if main.nil?
110
                    raise Puppet::Error, "File #{yumconf} does not contain a main section"
111
                end
112
                reposdir = main['reposdir']
113
                reposdir ||= "/etc/yum.repos.d, /etc/yum/repos.d"
114
                reposdir.gsub!(/[\n,]/, " ")
115
                reposdir.split.each do |dir|
116
                    Dir::glob("#{dir}/*.repo").each do |file|
117
                        if File.file?(file)
118
                            @inifile.read(file)
119
                        end
120
                    end
121
                end
122
                reposdir.split.each do |dir|
123
                    if File::directory?(dir) && File::writable?(dir)
124
                        @defaultrepodir = dir
125
                        break
126
                    end
127
                end
128
            end
129
            return @inifile
130
        end
131

  
132
        # Parse the yum config files. Only exposed for the tests
133
        # Non-test code should use self.inifile to get at the
134
        # underlying file
135
        def self.read
136
            result = Puppet::Util::IniConfig::File.new()
137
            result.read(yumconf)
138
            main = result['main']
139
            if main.nil?
140
                raise Puppet::Error, "File #{yumconf} does not contain a main section"
141
            end
142
            reposdir = main['reposdir']
143
            reposdir ||= "/etc/yum.repos.d, /etc/yum/repos.d"
144
            reposdir.gsub!(/[\n,]/, " ")
145
            reposdir.split.each do |dir|
146
                Dir::glob("#{dir}/*.repo").each do |file|
147
                    if File.file?(file)
148
                        result.read(file)
149
                    end
150
                end
151
            end
152
            if @defaultrepodir.nil?
153
                reposdir.split.each do |dir|
154
                    if File::directory?(dir) && File::writable?(dir)
155
                        @defaultrepodir = dir
156
                        break
157
                    end
158
                end
159
            end
160
            return result
161
        end
162

  
163
        # Return the Puppet::Util::IniConfig::Section with name NAME
164
        # from the yum config
165
        def self.section(name)
166
            result = inifile[name]
167
            if result.nil?
168
                # Brand new section
169
                path = yumconf
170
                unless @defaultrepodir.nil?
171
                    path = File::join(@defaultrepodir, "#{name}.repo")
172
                end
173
                Puppet::info "create new repo #{name} in file #{path}"
174
                result = inifile.add_section(name, path)
175
            end
176
            return result
177
        end
178

  
179
        # Store all modifications back to disk
180
        def self.store
181
            inifile.store
182
            unless Puppet[:noop]
183
                target_mode = 0644 # FIXME: should be configurable
184
                inifile.each_file do |file|
185
                    current_mode = File.stat(file).mode & 0777
186
                    unless current_mode == target_mode
187
                        Puppet::info "changing mode of #{file} from %03o to %03o" % [current_mode, target_mode]
188
                        File.chmod(target_mode, file)
189
                    end
190
                end
191
            end
192
        end
193

  
194
        # This is only used during testing.
195
        def self.clear
196
            @inifile = nil
197
            @yumconf = "/etc/yum.conf"
198
            @defaultrepodir = nil
199
        end
200

  
201
        # Return the Puppet::Util::IniConfig::Section for this yumrepo resource
202
        def section
203
            self.class.section(self[:name])
204
        end
205

  
206 62
        def evaluate
207 63
            changes = super
208 64
            # FIXME: Dirty, dirty hack