]> git.donarmstrong.com Git - dsa-puppet.git/blob - modules/concat/manifests/init.pp
set a tlsaport for gobby's web service
[dsa-puppet.git] / modules / concat / manifests / init.pp
1 # A system to construct files using fragments from other files or templates.
2 #
3 # This requires at least puppet 0.25 to work correctly as we use some
4 # enhancements in recursive directory management and regular expressions
5 # to do the work here.
6 #
7 # USAGE:
8 # The basic use case is as below:
9 #
10 # concat{"/etc/named.conf":
11 #    notify => Service["named"]
12 # }
13 #
14 # concat::fragment{"foo.com_config":
15 #    target  => "/etc/named.conf",
16 #    order   => 10,
17 #    content => template("named_conf_zone.erb")
18 # }
19 #
20 # # add a fragment not managed by puppet so local users
21 # # can add content to managed file
22 # concat::fragment{"foo.com_user_config":
23 #    target  => "/etc/named.conf",
24 #    order   => 12,
25 #    ensure  => "/etc/named.conf.local"
26 # }
27 #
28 # This will use the template named_conf_zone.erb to build a single
29 # bit of config up and put it into the fragments dir.  The file
30 # will have an number prefix of 10, you can use the order option
31 # to control that and thus control the order the final file gets built in.
32 #
33 # You can also specify a path and use a different name for your resources:
34 #
35 # # You can make this something dynamic, based on whatever parameters your
36 # # module/class for example.
37 # $vhost_file = '/etc/httpd/vhosts/01-my-vhost.conf'
38 #
39 # concat{'apache-vhost-myvhost':
40 #   path => $vhost_file,
41 # }
42 #
43 # # We don't care where the file is located, just what to put in it.
44 # concat::fragment {'apache-vhost-myvhost-main':
45 #   target  => 'apache-vhost-myvhost',
46 #   content => '<virtualhost *:80>',
47 #   order   => 01,
48 # }
49 #
50 # concat::fragment {'apache-vhost-myvhost-close':
51 #   target  => 'apache-vhost-myvhost',
52 #   content => '</virtualhost>',
53 #   order   => 99,
54 # }
55 #
56 #
57 # SETUP:
58 # The class concat::setup uses the fact concat_basedir to define the variable
59 # $concatdir, where all the temporary files and fragments will be
60 # durably stored. The fact concat_basedir will be set up on the client to
61 # <Puppet[:vardir]>/concat, so you will be able to run different setup/flavours
62 # of puppet clients.
63 # However, since this requires the file lib/facter/concat_basedir.rb to be
64 # deployed on the clients, so you will have to set "pluginsync = true" on
65 # both the master and client, at least for the first run.
66 #
67 # There's some regular expression magic to figure out the puppet version but
68 # if you're on an older 0.24 version just set $puppetversion = 24
69 #
70 # DETAIL:
71 # We use a helper shell script called concatfragments.sh that gets placed
72 # in <Puppet[:vardir]>/concat/bin to do the concatenation.  While this might
73 # seem more complex than some of the one-liner alternatives you might find on
74 # the net we do a lot of error checking and safety checks in the script to avoid
75 # problems that might be caused by complex escaping errors etc.
76 #
77 # LICENSE:
78 # Apache Version 2
79 #
80 # LATEST:
81 # http://github.com/ripienaar/puppet-concat/
82 #
83 # CONTACT:
84 # R.I.Pienaar <rip@devco.net>
85 # Volcane on freenode
86 # @ripienaar on twitter
87 # www.devco.net
88
89
90 # Sets up so that you can use fragments to build a final config file,
91 #
92 # OPTIONS:
93 #  - path       The path to the final file. Use this in case you want to
94 #               differentiate between the name of a resource and the file path.
95 #               Note: Use the name you provided in the target of your
96 #               fragments.
97 #  - mode       The mode of the final file
98 #  - owner      Who will own the file
99 #  - group      Who will own the file
100 #  - force      Enables creating empty files if no fragments are present
101 #  - warn       Adds a normal shell style comment top of the file indicating
102 #               that it is built by puppet
103 #  - backup     Controls the filebucketing behavior of the final file and
104 #               see File type reference for its use.  Defaults to 'puppet'
105 #  - replace    Whether to replace a file that already exists on the local
106 #               system
107 #
108 # ACTIONS:
109 #  - Creates fragment directories if it didn't exist already
110 #  - Executes the concatfragments.sh script to build the final file, this
111 #    script will create directory/fragments.concat.   Execution happens only
112 #    when:
113 #    * The directory changes
114 #    * fragments.concat != final destination, this means rebuilds will happen
115 #      whenever someone changes or deletes the final file.  Checking is done
116 #      using /usr/bin/cmp.
117 #    * The Exec gets notified by something else - like the concat::fragment
118 #      define
119 #  - Copies the file over to the final destination using a file resource
120 #
121 # ALIASES:
122 #  - The exec can notified using Exec["concat_/path/to/file"] or
123 #    Exec["concat_/path/to/directory"]
124 #  - The final file can be referened as File["/path/to/file"] or
125 #    File["concat_/path/to/file"]
126 define concat(
127   $path = $name,
128   $owner = $::id,
129   $group = $concat::setup::root_group,
130   $mode = '0644',
131   $warn = false,
132   $force = false,
133   $backup = 'puppet',
134   $replace = true,
135   $gnu = undef,
136   $order='alpha'
137 ) {
138   include concat::setup
139
140   $safe_name   = regsubst($name, '/', '_', 'G')
141   $concatdir   = $concat::setup::concatdir
142   $version     = $concat::setup::majorversion
143   $fragdir     = "${concatdir}/${safe_name}"
144   $concat_name = 'fragments.concat.out'
145   $default_warn_message = '# This file is managed by Puppet. DO NOT EDIT.'
146
147   case $warn {
148     'true', true, yes, on: {
149       $warnmsg = $default_warn_message
150     }
151     'false', false, no, off: {
152       $warnmsg = ''
153     }
154     default: {
155       $warnmsg = $warn
156     }
157   }
158
159   $warnmsg_escaped = regsubst($warnmsg, "'", "'\\\\''", 'G')
160   $warnflag = $warnmsg_escaped ? {
161     ''      => '',
162     default => "-w '${warnmsg_escaped}'"
163   }
164
165   case $force {
166     'true', true, yes, on: {
167       $forceflag = '-f'
168     }
169     'false', false, no, off: {
170       $forceflag = ''
171     }
172     default: {
173       fail("Improper 'force' value given to concat: ${force}")
174     }
175   }
176
177   case $order {
178     numeric: {
179       $orderflag = '-n'
180     }
181     alpha: {
182       $orderflag = ''
183     }
184     default: {
185       fail("Improper 'order' value given to concat: ${order}")
186     }
187   }
188
189   File {
190     owner   => $::id,
191     group   => $group,
192     mode    => $mode,
193     backup  => $backup,
194     replace => $replace
195   }
196
197   file { $fragdir:
198     ensure => directory,
199   }
200
201   $source_real = $version ? {
202     24      => 'puppet:///concat/null',
203     default => undef,
204   }
205
206   file { "${fragdir}/fragments":
207     ensure   => directory,
208     force    => true,
209     ignore   => ['.svn', '.git', '.gitignore'],
210     notify   => Exec["concat_${name}"],
211     purge    => true,
212     recurse  => true,
213     source   => $source_real,
214   }
215
216   file { "${fragdir}/fragments.concat":
217     ensure   => present,
218   }
219
220   file { "${fragdir}/${concat_name}":
221     ensure   => present,
222   }
223
224   file { $name:
225     ensure   => present,
226     path     => $path,
227     alias    => "concat_${name}",
228     group    => $group,
229     mode     => $mode,
230     owner    => $owner,
231     source   => "${fragdir}/${concat_name}",
232   }
233
234   exec { "concat_${name}":
235     alias       => "concat_${fragdir}",
236     command     => "${concat::setup::concatdir}/bin/concatfragments.sh -o ${fragdir}/${concat_name} -d ${fragdir} ${warnflag} ${forceflag} ${orderflag}",
237     notify      => File[$name],
238     require     => [
239       File[$fragdir],
240       File["${fragdir}/fragments"],
241       File["${fragdir}/fragments.concat"],
242     ],
243     subscribe   => File[$fragdir],
244     unless      => "${concat::setup::concatdir}/bin/concatfragments.sh -o ${fragdir}/${concat_name} -d ${fragdir} -t ${warnflag} ${forceflag} ${orderflag}",
245   }
246
247   if $::id == 'root' {
248     Exec["concat_${name}"] {
249       user  => root,
250       group => $group,
251     }
252   }
253 }
254
255 # vim:sw=2:ts=2:expandtab:textwidth=79