diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/puppet/type/zone.rb | 182 |
1 files changed, 182 insertions, 0 deletions
diff --git a/lib/puppet/type/zone.rb b/lib/puppet/type/zone.rb new file mode 100644 index 000000000..86b84272a --- /dev/null +++ b/lib/puppet/type/zone.rb @@ -0,0 +1,182 @@ +Puppet::Type.newtype(:zone) do + @doc = "Solaris zones." + + newparam(:name) do + desc "The name of the zone." + + isnamevar + end + + newparam(:id) do + desc "The numerical ID of the zone. This number is autogenerated + and cannot be changed." + end + + newparam(:ip) do + desc "The IP address of the zone." + + # We need a generic IP address validation mechanism, and this library + # needs to use it. + end + + newstate(:inherited) do + desc "The list of directories that the zone inherits from the global + zone. All directories must be fully qualified." + + + validate do |value| + unless value =~ /^\// + raise ArgumentError, "The zone base must be fully qualified" + end + end + + def insync? + end + + def sync + end + end + + newstate(:status) do + desc "The running state of the zone." + + newvalue(:running) do + begin + execute("/usr/sbin/zoneadm #{@parent[:name]} boot") + rescue ExecutionFailure + self.fail "Could not stop zone: %s" % output + end + + return :zone_booted + end + + newvalue(:stopped) do + begin + execute("/usr/sbin/zoneadm #{@parent[:name]} shutdown") + rescue ExecutionFailure + self.fail "Could not stop zone: %s" % output + end + + return :zone_stopped + end + end + + newparam(:base) do + desc "The root of the zone's filesystem. Must be a fully qualified + file name. If you include '%s' in the path, then it will be + replaced with the zone's name. At this point, you cannot use + Puppet to move a zone." + + validate do |value| + unless value =~ /^\// + raise ArgumentError, "The zone base must be fully qualified" + end + end + + munge do |value| + if value =~ /%s/ + value % @parent[:name] + else + value + end + end + end + + # If Puppet is also managing the base dir or its parent dir, list them + # both as prerequisites. + autorequire(:file) do + if @parameters.include? :base + return [@parameters[:base], File.dirname(@parameters[:base])] + else + nil + end + end + + # Convert the output of a list into a hash + def self.line2hash(line) + fields = [:id, :name, :status, :root] + + hash = {} + line.split(":").each_with_index { |value, index| + hash[fields[index]] = value + } + + return hash + end + + def self.list + %x{/usr/sbin/zoneadm list -p}.split("\n").each do |line| + hash = line2hash(line) + + obj = nil + unless obj = @objects[hash[:name]] + obj = create(:name => hash[:name]) + end + + obj.setstatus(hash) + end + end + + def create + end + + def destroy + if @states[:status].is == :running + @states[:status].should = :stopped + @states[:status].sync + end + + begin + execute("/usr/sbin/zoneadm #{@parent[:name]} uninstall") + rescue ExecutionFailure + self.fail "Could not stop zone: %s" % output + end + end + + # Collect the configuration of the zone. + def info + output = execute("/usr/sbin/zonecfg -z %s info" % self[:name]) + + name = nil + hash = {} + output.split("\n").each do |line| + case line + when /^(\S+):\s*$/: name = $1 + when /^(\S+):\s*(.+)$/: hash[$1] = $2 + when /^\s+(\S+):\s*(.+)$/: + if name + unless hash[name].is_a? Hash + hash[name] = {} + end + hash[name][$1] = $2 + else + err "Ignoring '%s'" % line + end + else + debug "Ignoring zone output '%s'" % line + end + end + end + + def retrieve + output = execute("/usr/sbin/zoneadm -z #{self[:name]} list -p") + + hash = self.class.line2hash(output.chomp) + setstatus(hash) + end + + # Take the results of a listing and set everything appropriately. + def setstatus(hash) + hash.each do |param, value| + next if param == :name + case self.class.attrtype(param) + when :state: + self.is = [param, value] + else + self[param] = value + end + end + end +end + +# $Id$ |