summaryrefslogtreecommitdiffstats
path: root/lib/puppet/provider/zone/solaris.rb
blob: d92a774c14f445904ae32375e04c998a9b81cbba (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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
Puppet::Type.type(:zone).provide(:solaris) do
    desc "Provider for Solaris Zones."

    commands :adm => "/usr/sbin/zoneadm", :cfg => "/usr/sbin/zonecfg"
    defaultfor :operatingsystem => :solaris

    # Convert the output of a list into a hash
    def self.line2hash(line)
        fields = [:id, :name, :ensure, :path]

        hash = {}
        line.split(":").each_with_index { |value, index|
            hash[fields[index]] = value
        }

        # Configured but not installed zones do not have IDs
        if hash[:id] == "-"
            hash.delete(:id)
        end

        return hash
    end

    def self.list
        adm(:list, "-cp").split("\n").collect do |line|
            hash = line2hash(line)

            obj = nil
            unless obj = @model[hash[:name]]
                obj = @model.create(:name => hash[:name])
            end

            obj.setstatus(hash)

            obj
        end
    end

    # Perform all of our configuration steps.
    def configure
        # If the thing is entirely absent, then we need to create the config.
        str = %{create -b
set zonepath=%s
} % @model[:path]

        # Then perform all of our configuration steps.  It's annoying
        # that we need this much internal info on the model.
        @model.send(:properties).each do |property|
            if property.is_a? ZoneConfigProperty and ! property.insync?
                str += property.configtext + "\n"
            end
        end

        str += "commit\n"
        setconfig(str)
    end

    def destroy
        zonecfg :delete, "-F"
    end

    def install
        zoneadm :install
    end

    # We need a way to test whether a zone is in process.  Our 'ensure'
    # property models the static states, but we need to handle the temporary ones.
    def processing?
        if hash = statushash()
            case hash[:ensure]
            when "incomplete", "ready", "shutting_down"
                true
            else
                false
            end
        else
            false
        end
    end

    # Collect the configuration of the zone.
    def getconfig
        output = zonecfg :info

        name = nil
        current = nil
        hash = {}
        output.split("\n").each do |line|
            case line
            when /^(\S+):\s*$/:
                name = $1
                current = nil # reset it
            when /^(\S+):\s*(.+)$/:
                hash[$1.intern] = $2
            when /^\s+(\S+):\s*(.+)$/:
                if name
                    unless hash.include? name
                        hash[name] = []
                    end

                    unless current
                        current = {}
                        hash[name] << current
                    end
                    current[$1.intern] = $2
                else
                    err "Ignoring '%s'" % line
                end
            else
                debug "Ignoring zone output '%s'" % line
            end
        end
        return hash
    end

    def retrieve
        if hash = statushash()
            setstatus(hash)

            # Now retrieve the configuration itself and set appropriately.
            getconfig()
        end
    end

    # Execute a configuration string.  Can't be private because it's called
    # by the properties.
    def setconfig(str)
        command = "#{command(:cfg)} -z %s -f -" % @model[:name]
        debug "Executing '%s' in zone %s with '%s'" % [command, @model[:name], str]
        IO.popen(command, "w") do |pipe|
            pipe.puts str
        end

        unless $? == 0
            raise ArgumentError, "Failed to apply configuration"
        end
    end

    def start
        # Check the sysidcfg stuff
        if cfg = @model[:sysidcfg]
            path = File.join(@model[:path], "root", "etc", "sysidcfg")

            unless File.exists?(path)
                begin
                    File.open(path, "w", 0600) do |f|
                        f.puts cfg
                    end
                rescue => detail
                    if Puppet[:debug]
                        puts detail.stacktrace
                    end
                    raise Puppet::Error, "Could not create sysidcfg: %s" % detail
                end
            end
        end

        zoneadm :boot
    end

    # Return a hash of the current status of this zone.
    def statushash
        begin
            output = adm "-z", @model[:name], :list, "-p"
        rescue Puppet::ExecutionFailure
            return nil
        end

        return self.class.line2hash(output.chomp)
    end

    def stop
        zoneadm :halt
    end

    def unconfigure
        zonecfg :delete, "-F"
    end

    def uninstall
        zoneadm :uninstall, "-F"
    end

    private

    def zoneadm(*cmd)
        begin
            adm("-z", @model[:name], *cmd)
        rescue Puppet::ExecutionFailure => detail
            self.fail "Could not %s zone: %s" % [cmd[0], detail]
        end
    end

    def zonecfg(*cmd)
        begin
            cfg("-z", @model[:name], *cmd)
        rescue Puppet::ExecutionFailure => detail
            self.fail "Could not %s zone: %s" % [cmd[0], detail]
        end
    end
end

# $Id$