~ubuntu-branches/ubuntu/oneiric/puppet/oneiric-security

« back to all changes in this revision

Viewing changes to lib/puppet/provider/zone/solaris.rb

  • Committer: Bazaar Package Importer
  • Author(s): Micah Anderson
  • Date: 2008-07-26 15:43:45 UTC
  • mto: (3.1.1 lenny) (1.3.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 16.
  • Revision ID: james.westby@ubuntu.com-20080726154345-1fmgo76b4l72ulvc
ImportĀ upstreamĀ versionĀ 0.24.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
Puppet::Type.type(:zone).provide(:solaris) do
 
2
    desc "Provider for Solaris Zones."
 
3
 
 
4
    commands :adm => "/usr/sbin/zoneadm", :cfg => "/usr/sbin/zonecfg"
 
5
    defaultfor :operatingsystem => :solaris
 
6
 
 
7
    mk_resource_methods
 
8
 
 
9
    # Convert the output of a list into a hash
 
10
    def self.line2hash(line)
 
11
        fields = [:id, :name, :ensure, :path]
 
12
 
 
13
        properties = {}
 
14
        line.split(":").each_with_index { |value, index|
 
15
            properties[fields[index]] = value
 
16
        }
 
17
 
 
18
        # Configured but not installed zones do not have IDs
 
19
        if properties[:id] == "-"
 
20
            properties.delete(:id)
 
21
        end
 
22
 
 
23
        properties[:ensure] = symbolize(properties[:ensure])
 
24
 
 
25
        return properties
 
26
    end
 
27
 
 
28
    def self.instances
 
29
        # LAK:NOTE See http://snurl.com/21zf8  [groups_google_com] 
 
30
        x = adm(:list, "-cp").split("\n").collect do |line|
 
31
            new(line2hash(line))
 
32
        end
 
33
    end
 
34
 
 
35
    # Perform all of our configuration steps.
 
36
    def configure
 
37
        # If the thing is entirely absent, then we need to create the config.
 
38
        str = %{create -b
 
39
set zonepath=%s
 
40
} % @resource[:path]
 
41
 
 
42
        # Then perform all of our configuration steps.  It's annoying
 
43
        # that we need this much internal info on the resource.
 
44
        @resource.send(:properties).each do |property|
 
45
            if property.is_a? ZoneConfigProperty and ! property.insync?(properties[property.name])
 
46
                str += property.configtext + "\n"
 
47
            end
 
48
        end
 
49
 
 
50
        str += "commit\n"
 
51
        setconfig(str)
 
52
    end
 
53
 
 
54
    def destroy
 
55
        zonecfg :delete, "-F"
 
56
    end
 
57
 
 
58
    def exists?
 
59
        properties[:ensure] != :absent
 
60
    end
 
61
 
 
62
    # Clear out the cached values.
 
63
    def flush
 
64
        @property_hash.clear
 
65
    end
 
66
 
 
67
    def install
 
68
        zoneadm :install
 
69
    end
 
70
 
 
71
    # Look up the current status.
 
72
    def properties
 
73
        if @property_hash.empty?
 
74
            @property_hash = status || {}
 
75
            if @property_hash.empty?
 
76
                @property_hash[:ensure] = :absent
 
77
            else
 
78
                @resource.class.validproperties.each do |name|
 
79
                    @property_hash[name] ||= :absent
 
80
                end
 
81
            end
 
82
 
 
83
        end
 
84
        @property_hash.dup
 
85
    end
 
86
 
 
87
    # We need a way to test whether a zone is in process.  Our 'ensure'
 
88
    # property models the static states, but we need to handle the temporary ones.
 
89
    def processing?
 
90
        if hash = status()
 
91
            case hash[:ensure]
 
92
            when "incomplete", "ready", "shutting_down"
 
93
                true
 
94
            else
 
95
                false
 
96
            end
 
97
        else
 
98
            false
 
99
        end
 
100
    end
 
101
 
 
102
    # Collect the configuration of the zone.
 
103
    def getconfig
 
104
        output = zonecfg :info
 
105
 
 
106
        name = nil
 
107
        current = nil
 
108
        hash = {}
 
109
        output.split("\n").each do |line|
 
110
            case line
 
111
            when /^(\S+):\s*$/:
 
112
                name = $1
 
113
                current = nil # reset it
 
114
            when /^(\S+):\s*(.+)$/:
 
115
                hash[$1.intern] = $2
 
116
            when /^\s+(\S+):\s*(.+)$/:
 
117
                if name
 
118
                    unless hash.include? name
 
119
                        hash[name] = []
 
120
                    end
 
121
 
 
122
                    unless current
 
123
                        current = {}
 
124
                        hash[name] << current
 
125
                    end
 
126
                    current[$1.intern] = $2
 
127
                else
 
128
                    err "Ignoring '%s'" % line
 
129
                end
 
130
            else
 
131
                debug "Ignoring zone output '%s'" % line
 
132
            end
 
133
        end
 
134
 
 
135
        return hash
 
136
    end
 
137
 
 
138
    # Execute a configuration string.  Can't be private because it's called
 
139
    # by the properties.
 
140
    def setconfig(str)
 
141
        command = "#{command(:cfg)} -z %s -f -" % @resource[:name]
 
142
        debug "Executing '%s' in zone %s with '%s'" % [command, @resource[:name], str]
 
143
        IO.popen(command, "w") do |pipe|
 
144
            pipe.puts str
 
145
        end
 
146
 
 
147
        unless $? == 0
 
148
            raise ArgumentError, "Failed to apply configuration"
 
149
        end
 
150
    end
 
151
 
 
152
    def start
 
153
        # Check the sysidcfg stuff
 
154
        if cfg = @resource[:sysidcfg]
 
155
            path = File.join(@resource[:path], "root", "etc", "sysidcfg")
 
156
 
 
157
            unless File.exists?(path)
 
158
                begin
 
159
                    File.open(path, "w", 0600) do |f|
 
160
                        f.puts cfg
 
161
                    end
 
162
                rescue => detail
 
163
                    if Puppet[:debug]
 
164
                        puts detail.stacktrace
 
165
                    end
 
166
                    raise Puppet::Error, "Could not create sysidcfg: %s" % detail
 
167
                end
 
168
            end
 
169
        end
 
170
 
 
171
        zoneadm :boot
 
172
    end
 
173
 
 
174
    # Return a hash of the current status of this zone.
 
175
    def status
 
176
        begin
 
177
            output = adm "-z", @resource[:name], :list, "-p"
 
178
        rescue Puppet::ExecutionFailure
 
179
            return nil
 
180
        end
 
181
 
 
182
        main = self.class.line2hash(output.chomp)
 
183
 
 
184
        # Now add in the configuration information
 
185
        config_status.each do |name, value|
 
186
            main[name] = value
 
187
        end
 
188
 
 
189
        main
 
190
    end
 
191
 
 
192
    def stop
 
193
        zoneadm :halt
 
194
    end
 
195
 
 
196
    def unconfigure
 
197
        zonecfg :delete, "-F"
 
198
    end
 
199
 
 
200
    def uninstall
 
201
        zoneadm :uninstall, "-F"
 
202
    end
 
203
 
 
204
    private
 
205
 
 
206
    # Turn the results of getconfig into status information.
 
207
    def config_status
 
208
        config = getconfig()
 
209
        result = {}
 
210
 
 
211
        result[:autoboot] = config[:autoboot] ? config[:autoboot].intern : :absent
 
212
        result[:pool] = config[:pool]
 
213
        result[:shares] = config[:shares]
 
214
        if dir = config["inherit-pkg-dir"]
 
215
            result[:inherit] = dir.collect { |dirs| dirs[:dir] }
 
216
        end
 
217
        if net = config["net"]
 
218
            result[:ip] = net.collect { |params| "%s:%s" % [params[:physical], params[:address]] }
 
219
        end
 
220
 
 
221
        result
 
222
    end
 
223
 
 
224
    def zoneadm(*cmd)
 
225
        begin
 
226
            adm("-z", @resource[:name], *cmd)
 
227
        rescue Puppet::ExecutionFailure => detail
 
228
            self.fail "Could not %s zone: %s" % [cmd[0], detail]
 
229
        end
 
230
    end
 
231
 
 
232
    def zonecfg(*cmd)
 
233
        # You apparently can't get the configuration of the global zone
 
234
        return "" if self.name == "global"
 
235
 
 
236
        begin
 
237
            cfg("-z", self.name, *cmd)
 
238
        rescue Puppet::ExecutionFailure => detail
 
239
            self.fail "Could not %s zone: %s" % [cmd[0], detail]
 
240
        end
 
241
    end
 
242
end
 
243