|
1 # |
|
2 # CDDL HEADER START |
|
3 # |
|
4 # The contents of this file are subject to the terms of the |
|
5 # Common Development and Distribution License (the "License"). |
|
6 # You may not use this file except in compliance with the License. |
|
7 # |
|
8 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE |
|
9 # or http://www.opensolaris.org/os/licensing. |
|
10 # See the License for the specific language governing permissions |
|
11 # and limitations under the License. |
|
12 # |
|
13 # When distributing Covered Code, include this CDDL HEADER in each |
|
14 # file and include the License file at usr/src/OPENSOLARIS.LICENSE. |
|
15 # If applicable, add the following below this CDDL HEADER, with the |
|
16 # fields enclosed by brackets "[]" replaced with your own identifying |
|
17 # information: Portions Copyright [yyyy] [name of copyright owner] |
|
18 # |
|
19 # CDDL HEADER END |
|
20 # |
|
21 |
|
22 # |
|
23 # Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved. |
|
24 # |
|
25 |
|
26 Puppet::Type.type(:boot_environment).provide(:boot_environment) do |
|
27 desc "Provider for Oracle Solaris Boot Environments (BEs)" |
|
28 confine :operatingsystem => [:solaris] |
|
29 defaultfor :osfamily => :solaris, :kernelrelease => ['5.11', '5.12'] |
|
30 commands :beadm => '/usr/sbin/beadm', :zpool => '/usr/sbin/zpool' |
|
31 |
|
32 def self.instances |
|
33 beadm(:list, "-H").split("\n").collect do |line| |
|
34 data = line.split(";") |
|
35 name = data[0] |
|
36 if data[2].include? "R" |
|
37 activate = :true |
|
38 else |
|
39 activate = :false |
|
40 end |
|
41 |
|
42 new(:name => name, |
|
43 :ensure => :present, |
|
44 :activate => activate) |
|
45 end |
|
46 end |
|
47 |
|
48 def self.prefetch(resources) |
|
49 # pull the instances on the system |
|
50 bes = instances |
|
51 |
|
52 # set the provider for the resource to set the property_hash |
|
53 resources.keys.each do |name| |
|
54 if provider = bes.find{ |be| be.name == name} |
|
55 resources[name].provider = provider |
|
56 end |
|
57 end |
|
58 end |
|
59 |
|
60 def activate |
|
61 @property_hash[:activate] |
|
62 end |
|
63 |
|
64 def activate=(value) |
|
65 if value == :true |
|
66 beadm("activate", @resource[:name]) |
|
67 end |
|
68 end |
|
69 |
|
70 def exists? |
|
71 @property_hash[:ensure] == :present |
|
72 end |
|
73 |
|
74 def build_flags |
|
75 flags = [] |
|
76 |
|
77 if description = @resource[:description] |
|
78 flags << "-d" << "'#{description}'" |
|
79 end |
|
80 |
|
81 if clone_be = @resource[:clone_be] |
|
82 if clone_be.include? "@" |
|
83 if beadm(:list, "-H", "-s").split("\n").detect \ |
|
84 { |line| line.split(";")[1] == clone_be } |
|
85 flags << "-e" << clone_be |
|
86 else |
|
87 Puppet.warning "BE #{clone_be} not found. Skipping -e |
|
88 argument." |
|
89 end |
|
90 else |
|
91 if beadm(:list, "-H").split("\n").detect \ |
|
92 { |line| line.split(";")[0] == clone_be } |
|
93 flags << "-e" << clone_be |
|
94 else |
|
95 Puppet.warning "BE #{clone_be} not found. Skipping -e |
|
96 argument." |
|
97 end |
|
98 end |
|
99 end |
|
100 |
|
101 if options = @resource[:options] |
|
102 options.each { |key, value| flags << "-o" << "#{key}=#{value}" } |
|
103 end |
|
104 |
|
105 if zp = @resource[:zpool] |
|
106 found = false |
|
107 for line in zpool(:list, "-o", "name", "-H").each_line do |
|
108 if zp == line.strip |
|
109 found = true |
|
110 flags << "-p" << zp |
|
111 end |
|
112 end |
|
113 if not found |
|
114 raise Puppet::Error, \ |
|
115 "Unable to create BE in zpool #{zp} -- #{zp} does not exist" |
|
116 end |
|
117 end |
|
118 flags |
|
119 end |
|
120 |
|
121 def create |
|
122 beadm(:create, build_flags, @resource[:name]) |
|
123 if @resource[:activate] == :true |
|
124 beadm(:activate, @resource[:name]) |
|
125 end |
|
126 end |
|
127 |
|
128 def destroy |
|
129 if beadm(:list, "-H", @resource[:name]).split(";")[2] =~ /N/ |
|
130 Puppet.warning "Unable to destroy #{@resource[:name]} as it is |
|
131 the active BE." |
|
132 else |
|
133 beadm(:destroy, "-f", "-F", @resource[:name]) |
|
134 end |
|
135 end |
|
136 end |