Hi Edd, On 14.12.2011, at 13:27, Edd Grant wrote:
> Hi Martin, > > I have swapped all the subscribes out for requires as per your > suggestion but copy_archive_$name still runs every time. Any ideas? The exec check_unpacked_archive_exists_$name is parsed and validated on every run. I see two possibilities: 1. make the exec fail if the directory exists. e.g. command => "/bin/test ! -d $targetDir && mkdir $targetDir" But: This will put an error in every puppet report. 2. do not remove the archive or touch a file with the same name. e.g. command => "/bin/rm -f $targetDir/$archiveName && /usr/bin/touch $targetDir/$archiveName", this will work, because the file resource has replace => false. > > Code below: > > define archive::unpack($archiveName, > $appName, > $archiveDir, > $targetDir, > $pathFolder, > $owner = "root", > $group = "root", > $mode = "644") { > > #Set the extraction command appropriately based on the archive type. > $command = $archiveName ? { > /(^.*\.tar\.gz$)|(^.*\.tgz$)/ => "/bin/tar zxf $targetDir/ > $archiveName", > /(^.*\.tar$)/ => "/bin/tar xf $targetDir/$archiveName", > /^.*\.zip$/ => "/usr/bin/unzip $targetDir/$archiveName", > default => "Error: Could not detect archive type from archive name > ($archiveName), cannot unpack. Supported types: .tar.gzip, .zip", > } > > exec { "check_unpacked_archive_exists_$name": > command => "/bin/echo '$targetDir/$appName does not exist, it will > be created.'", > cwd => $targetDir, > creates => "$targetDir/$appName", > onlyif => "/usr/bin/test ! -d $targetDir/$appName", > logoutput => true, > } > > # copy file from puppet master to local system > file { "copy_archive_$name": > path => "$targetDir/$archiveName", > source => "$archiveDir/$archiveName", > replace => false, > require => Exec["check_unpacked_archive_exists_$name"], > } > > # extract local file > exec { "unpack_archive_$name": > command => $command, > cwd => $targetDir, > creates => "$targetDir/$appName", > logoutput => true, > require => File["copy_archive_$name"], > } > > # delete copied archive > exec { "delete_copied_archive_$name": > command => "/bin/rm -f $targetDir/$archiveName", > cwd => "$targetDir", > logoutput => true, > require => Exec["unpack_archive_$name"], > } > } > > > > > On Dec 14, 11:20 am, Martin Alfke <tux...@gmail.com> wrote: >> Edd, >> >> On 14.12.2011, at 11:54, Edd Grant wrote: >> >>> Martin, >> >>> r.e. your comment: "The command will only get executed in case that >>> $targeDir/$appName does not exists. The command will always return >>> 0 !!" can I ask what you were referring to here? was it the exec >>> "check_unpacked_archive_exists_$name"? I tested the statement in the >>> onlyif and that definitely returns 0 when the directory is absent and >>> 1 when the directory is present. I'm not quite sure what to do here to >>> correct this? >> >> I was referring to exec "check_unpacked_archive_exists_$name" >> I only wanted to make clear that the command always returns exitcode 0. >> Nothing wrong here. >> >> >> >> >> >> >> >> >> >>> Cheers, >> >>> Edd >> >>> On Dec 14, 10:18 am, Edd Grant <e...@eddgrant.com> wrote: >>>> Wow - thanks for the super quick reply Martin. Will try out your >>>> suggestions. >> >>>> Cheers, >> >>>> Edd >> >>>> On Dec 14, 10:16 am, Martin Alfke <tux...@gmail.com> wrote: >> >>>>> Hi, >> >>>>> my answer is inline.. >> >>>>> On 14.12.2011, at 11:09, Edd Grant wrote: >> >>>>>> Hi All, >> >>>>>> I'm trying to write a module which unpacks an archive to a specified >>>>>> location, the idea is as follows: >> >>>>>> Let's say I'm trying to deploy an archive of grails-1.3.7 >>>>>> Check that a directory exists at $targetDir/grails-1.3.7 >>>>>> If it does, do nothing >>>>>> If it doesn't then do the following... >>>>>> Copy grails-1.3.7.zip from the module source to $targetDir >>>>>> Unpack to $targetDir/grails-1.3.7 >>>>>> Delete the archive so we don't end up with mess in our directories >>>>>> I have this mostly working, i.e. the code below performs all of the >>>>>> steps above successfully, but for some reason I cannot stop steps 4 and >>>>>> onwards from happening every time Puppet applies the manifests, >>>>>> irrespective of whether the $targetDir/grails-1.3.7 directory already >>>>>> exists. >> >>>>>> Here's the code >>>>>> Module code: modules/archive/unpack.pp >> >>>>>> define archive::unpack($archiveName, >>>>>> $appName, >>>>>> $archiveDir, >>>>>> $targetDir, >>>>>> $pathFolder, >>>>>> $owner = "root", >>>>>> $group = "root", >>>>>> $mode = "644") { >> >>>>>> #Set the extraction command appropriately based on the archive type. >>>>>> $command = $archiveName ? { >>>>>> /(^.*\.tar\.gz$)|(^.*\.tgz$)/ => "/bin/tar zxf >>>>>> $targetDir/$archiveName", >>>>>> /(^.*\.tar$)/ => "/bin/tar xf $targetDir/$archiveName", >>>>>> /^.*\.zip$/ => "/usr/bin/unzip $targetDir/$archiveName", >>>>>> default => "Error: Could not detect archive type from archive name >>>>>> ($archiveName), cannot unpack. Supported types: .tar.gzip, .zip", >>>>>> } >> >>>>>> # Check if the unpacked archive directory exists >>>>>> # the idea here was to have all subsequent actions >>>>>> # subscribe to the outcome of this check so that the archive >>>>>> # would only be copied, unpacked, chowned and chmodded if >>>>>> # the directory specified by this exec did not exist. This doesn't seem >>>>>> # to work though since "copy_archive_$name" always seems to be invoked >>>>>> # irrespective of the outcome of the onlyif condition in this exec. >>>>>> exec { "check_unpacked_archive_exists_$name": >>>>>> command => "/bin/echo '$targetDir/$appName does not exist, it will >>>>>> be created.'", >>>>>> cwd => $targetDir, >>>>>> creates => "$targetDir/$appName", >>>>>> onlyif => "/usr/bin/test ! -d $targetDir/$appName", >>>>>> logoutput => true, >>>>>> } >> >>>>> The command will only get executed in case that $targeDir/$appName does >>>>> not exists. >>>>> The command will always return 0 !! >> >>>>>> # copy file from puppet master to local system >>>>>> file { "copy_archive_$name": >>>>>> path => "$targetDir/$archiveName", >>>>>> source => "$archiveDir/$archiveName", >>>>>> replace => false, >>>>>> subscribe => Exec["check_unpacked_archive_exists_$name"], >>>>>> } >> >>>>> Here you subscribe to the exec resource. >>>>> Exec resource will get parsed but the command will not run >>>>> What you want is >>>>> require => Exec[...] >> >>>>> Using require instead of subscribe will make sure that the file resource >>>>> will only be done if the exec resource command is executed. >> >>>>>> # extract local file >>>>>> exec { "unpack_archive_$name": >>>>>> command => $command, >>>>>> cwd => $targetDir, >>>>>> creates => "$targetDir/$appName", >>>>>> refreshonly => true, >>>>>> logoutput => true, >>>>>> subscribe => File["copy_archive_$name"], >>>>>> } >> >>>>> Same here: use require instead of subscribe. >> >>>>>> # delete copied archive >>>>>> exec { "delete_copied_archive_$name": >>>>>> command => "/bin/rm -f $targetDir/$archiveName", >>>>>> cwd => "$targetDir", >>>>>> subscribe => Exec["unpack_archive_$name"], >>>>>> logoutput => true, >>>>>> } >>>>>> } >> >>>>> Same here. >> >>>>> Kind regards, >> >>>>> Martin >> >>>>>> Invocation code: >> >>>>>> class grails { >> >>>>>> $appName = "grails-2.0.0.M1" >>>>>> $archiveName = "$appName.zip" >>>>>> $archiveDir = "puppet:///modules/grails" >>>>>> $targetDir = "/usr/local/java" >>>>>> $pathFolder = "bin" >>>>>> $owner = root >>>>>> $group = dev >>>>>> $mode = 6775 >> >>>>>> archive::unpack { "install_$appName": >>>>>> archiveName => $archiveName, >>>>>> appName => $appName, >>>>>> archiveDir => $archiveDir, >>>>>> targetDir => $targetDir, >>>>>> pathFolder => $pathFolder, >>>>>> owner => $owner, >>>>>> group => $group, >>>>>> mode => $mode, >>>>>> } >>>>>> } >> >>>>>> The frustrating thing is that the exec called >>>>>> "check_unpacked_archive_exists_$name" is definitely only firing when it >>>>>> finds the sought folder to be missing, which is what I want it to do. >>>>>> What I don't understand is why the file called "copy_archive_$name" >>>>>> which subscribes to that exec gets fired everytime. I can't help but >>>>>> think that this would be made much much easier if only the file resource >>>>>> supported 'onlyif'... but while it doesn't does anyone have any insight >>>>>> as to why I'm seeing this issue? >> >>>>>> Cheers, >> >>>>>> Edd >> >>>>>> -- >>>>>> Web:http://www.eddgrant.com >>>>>> Email: e...@eddgrant.com >>>>>> Mobile: +44 (0) 7861 394 543 >> >>>>>> -- >>>>>> You received this message because you are subscribed to the Google >>>>>> Groups "Puppet Users" group. >>>>>> To post to this group, send email to puppet-users@googlegroups.com. >>>>>> To unsubscribe from this group, send email to >>>>>> puppet-users+unsubscr...@googlegroups.com. >>>>>> For more options, visit this group >>>>>> athttp://groups.google.com/group/puppet-users?hl=en. >> >>> -- >>> You received this message because you are subscribed to the Google Groups >>> "Puppet Users" group. >>> To post to this group, send email to puppet-users@googlegroups.com. >>> To unsubscribe from this group, send email to >>> puppet-users+unsubscr...@googlegroups.com. >>> For more options, visit this group >>> athttp://groups.google.com/group/puppet-users?hl=en. > > -- > You received this message because you are subscribed to the Google Groups > "Puppet Users" group. > To post to this group, send email to puppet-users@googlegroups.com. > To unsubscribe from this group, send email to > puppet-users+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/puppet-users?hl=en. > -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.