summaryrefslogtreecommitdiffstats
path: root/lib/puppet/type/augeas.rb
blob: da9cff369e03ffa28478e783549320af38150459 (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
#--
#  Copyright (C) 2008 Red Hat Inc.
#
#  This library is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public
#  License as published by the Free Software Foundation; either
#  version 2 of the License, or (at your option) any later version.
#
#  This library is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#  General Public License for more details.
#
#  You should have received a copy of the GNU General Public
#  License along with this library; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
#
# Author: Bryan Kearney <bkearney@redhat.com>

Puppet::Type.newtype(:augeas) do
    include Puppet::Util

    feature :parse_commands, "Parse the command string"
    feature :need_to_run?, "If the command should run"
    feature :execute_changes, "Actually make the changes"

    @doc = "Apply the changes (single or array of changes) to the filesystem
        via the augeas tool.

         Requires:
           - augeas to be installed (http://www.augeas.net)
           - ruby-augeas bindings

         Sample usage with a string::

            augeas{\"test1\" :
                   context => \"/files/etc/sysconfig/firstboot\",
                   changes => \"set RUN_FIRSTBOOT YES\",
                   onlyif  => \"match other_value size > 0\",
             }

         Sample usage with an array and custom lenses::

            augeas{\"jboss_conf\":
                context => \"/files\",
                changes => [
                    \"set /etc/jbossas/jbossas.conf/JBOSS_IP $ipaddress\",
                    \"set /etc/jbossas/jbossas.conf/JAVA_HOME /usr\"
                ],
                load_path => \"$/usr/share/jbossas/lenses\",
            }

         "

    newparam (:name) do
        desc "The name of this task. Used for uniqueness"
        isnamevar
    end

    newparam (:context) do
        desc "Optional context path. This value is pre-pended to the paths of all changes"
        defaultto ""
    end

    newparam (:onlyif) do
        desc "Optional augeas command and comparisons to control the execution of this type.
             Supported onlyif syntax::

               get [AUGEAS_PATH] [COMPARATOR] [STRING]
               match [MATCH_PATH] size [COMPARATOR] [INT]
               match [MATCH_PATH] include [STRING]
               match [MATCH_PATH] == [AN_ARRAY]

             where::

               AUGEAS_PATH is a valid path scoped by the context
               MATCH_PATH is a valid match synatx scoped by the context
               COMPARATOR is in the set [> >= != == <= <]
               STRING is a string
               INT is a number
               AN_ARRAY is in the form ['a string', 'another']"
        defaultto ""
    end


    newparam(:changes) do
        desc "The changes which should be applied to the filesystem. This
        can be either a string which contains a command or an array of commands.
        Commands supported are::

          set [PATH] [VALUE]     Sets the value VALUE at loction PATH
          rm [PATH]              Removes the node at location PATH
          remove [PATH]          Synonym for rm
          clear [PATH]           Keeps the node at PATH, but removes the value.
          ins [LABEL] [WHERE] [PATH]
                                 Inserts an empty node LABEL either [WHERE={before|after}] PATH.
          insert [LABEL] [WHERE] [PATH]
                                 Synonym for ins
        If the parameter 'context' is set that value is prepended to PATH"

        munge do |value|
            provider.parse_commands(value)
        end
    end


    newparam(:root) do
        desc "A file system path; all files loaded by Augeas are loaded underneath ROOT"
        defaultto "/"
    end

    newparam(:load_path) do
        desc "Optional colon separated list of directories; these directories are searched for schema definitions"
        defaultto ""
    end

    newparam(:force) do
        desc "Optional command to force the augeas type to execute even if it thinks changes
        will not be made. This does not overide the only setting. If onlyif is set, then the
        foce setting will not override that result"

        defaultto false
    end

    newparam(:type_check) do
        desc "Set to true if augeas should perform typechecking. Optional, defaults to false"
        newvalues(:true, :false)

        defaultto :false
    end

    # This is the acutal meat of the code. It forces
    # augeas to be run and fails or not based on the augeas return
    # code.
    newproperty(:returns) do |property|
        include Puppet::Util
        desc "The expected return code from the augeas command. Should not be set"

        defaultto 0

        # Make output a bit prettier
        def change_to_s(currentvalue, newvalue)
            return "executed successfully"
        end

        # if the onlyif resource is provided, then the value is parsed.
        # a return value of 0 will stop exection becuase it matches the
        # default value.
        def retrieve
            if @resource.provider.need_to_run?()
                :need_to_run
            else
                0
            end
        end

        # Actually execute the command.
        def sync
            @resource.provider.execute_changes()
        end
    end

end