Blank, Jessica
2007-Nov-08 18:21 UTC
Downloading a file into a directory that does not exist
Hello again, all: When downloading a file from the puppetmaster''s fileserver, like so: ==========SNIP=========define install_file ($mode = 0644, $owner = ''root'', $group = ''root'') { file { "${name}": source => [ "puppet://${puppetserver}/dist/files/${name}_${operatingsystem}", "puppet://${puppetserver}/dist/files/${name}" ], mode => $mode, owner => $owner, group => $group } } ==========SNIP========= If the file to be downloaded is, say, /etc/foo/bar/baz.txt, and the directory /etc/foo (and thus /etc/foo/bar) does not exist, the operation fails. The error message looks like this: err: //site_1515/install_file[/etc/foo/bar/baz.txt]/File[/etc/foo/bar/baz.txt ]/ensure: change from absent to file failed: Could not set file on ensure: No such file or directory - /etc/foo/bar/baz.puppettmp at /etc/puppet/manifests/functions.pp:48 It looks like this issue has been addressed at least twice before, at: http://reductivelabs.com/trac/puppet/ticket/86 (" Directory creation fails if parent directory does not exist ") http://reductivelabs.com/trac/puppet/ticket/668 (" Using File with ensure => directory fails if there''s no parent directory ") I''ve tried to solve the problem with mkdir -p (I evidently had the same idea as the person from ticket #668), but I can''t figure out how to do it. Puppet scripts often seem to execute out of order, due to the highly declarative nature of the language, so I tried doing it with dependencies... However, although I know how to make an exec depend upon a file EXISTING, I do not know how to make it depend upon a file NOT existing. Something like this: ==========SNIP========= exec { "mkdir -p \"${name}\"; rmdir \"${name}\";": path => "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", require => ! File["${name}"] } ==========SNIP========= The above would, of course, be a bit of a kluge ("create the target filepath as a directory, then remove the directory, leaving only the parent directory, before downloading the file into the newly created directory..."), but it would, in theory work. Of course, "! File" doesn''t exist. Can anyone think of a way to solve this problem in an elegant and reliable manner? If I''m trying to download /etc/foo/bar/baz.txt, and /etc/foo does not exist, I really need /etc/foo and /etc/foo/bar created so that the download will not fail. :) --Jessica _______________________________________________ Puppet-users mailing list Puppet-users@madstop.com https://mail.madstop.com/mailman/listinfo/puppet-users
David Schmitt
2007-Nov-08 18:57 UTC
Re: Downloading a file into a directory that does not exist
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Thursday 08 November 2007, Blank, Jessica wrote:> Hello again, all: > > When downloading a file from the puppetmaster''s fileserver, like so: > > ==========SNIP=========> define install_file ($mode = 0644, $owner = ''root'', $group = ''root'') { > file { "${name}": > source => [ > > "puppet://${puppetserver}/dist/files/${name}_${operatingsystem}", > "puppet://${puppetserver}/dist/files/${name}" > ], > mode => $mode, owner => $owner, group => $group > } > } > ==========SNIP=========> > If the file to be downloaded is, say, /etc/foo/bar/baz.txt, and the > directory /etc/foo (and thus /etc/foo/bar) does not exist, the operation > fails. > > The error message looks like this: > > err: > //site_1515/install_file[/etc/foo/bar/baz.txt]/File[/etc/foo/bar/baz.txt > ]/ensure: change from absent to file failed: Could not set file on > ensure: No such file or directory - /etc/foo/bar/baz.puppettmp at > /etc/puppet/manifests/functions.pp:48 > > It looks like this issue has been addressed at least twice before, at: > http://reductivelabs.com/trac/puppet/ticket/86 (" Directory creation > fails if parent directory does not exist ") > http://reductivelabs.com/trac/puppet/ticket/668 (" Using File with > ensure => directory fails if there''s no parent directory ") > > I''ve tried to solve the problem with mkdir -p (I evidently had the same > idea as the person from ticket #668), but I can''t figure out how to do > it. Puppet scripts often seem to execute out of order, due to the highly > declarative nature of the language, so I tried doing it with > dependencies... However, although I know how to make an exec depend upon > a file EXISTING, I do not know how to make it depend upon a file NOT > existing. > > Something like this: > > ==========SNIP=========> exec { "mkdir -p \"${name}\"; rmdir \"${name}\";": > path => > "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", > require => ! File["${name}"] > } > ==========SNIP=========> > The above would, of course, be a bit of a kluge ("create the target > filepath as a directory, then remove the directory, leaving only the > parent directory, before downloading the file into the newly created > directory..."), but it would, in theory work. > > Of course, "! File" doesn''t exist. > > Can anyone think of a way to solve this problem in an elegant and > reliable manner? > > If I''m trying to download /etc/foo/bar/baz.txt, and /etc/foo does not > exist, I really need /etc/foo and /etc/foo/bar created so that the > download will not fail. :)$dir = "/..." $file = "${dir}/..." exec { "create_${dir}": cmd => "mkdir -p ${dir}", creates => $dir, } file { $file: ensure => "...", require => Exec["create_${dir}"] } Note the usage of a descriptive identifier as title of the exec. Regards, David - -- The primary freedom of open source is not the freedom from cost, but the free- dom to shape software to do what you want. This freedom is /never/ exercised without cost, but is available /at all/ only by accepting the very different costs associated with open source, costs not in money, but in time and effort. - -- http://www.schierer.org/~luke/log/20070710-1129/on-forks-and-forking -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.6 (GNU/Linux) iD8DBQFHM1wj/Pp1N6Uzh0URAkynAKCjI6OWbYDLCVZfc4tH0TswQijLCQCcDd9y ttzwn3VUyPJbsD6eOOl+IRE=VQN/ -----END PGP SIGNATURE-----
Blank, Jessica
2007-Nov-08 19:11 UTC
Re: Downloading a file into a directory that doesnot exist
This is a great idea, and I would likely have done something very much like it. However, all that ''install_file'' is passed is a complete path to a file (e.g. ''/etc/foo/bar/baz.txt''), NOT a directory name and a filename (e.g. ''/etc/foo/bar'' and ''baz.txt'', respectively). Without rewriting install_file to require two arguments instead of one, (and I''m not entirely sure how I would do that; so far, I''ve only written functions that use $name as their sole argument) how could I separate out the directory name from the filename? Can puppet do regexp transformations on strings to lop off ''the last slash and anything after it''? --Jessica -----Original Message----- From: puppet-users-bounces@madstop.com [mailto:puppet-users-bounces@madstop.com] On Behalf Of David Schmitt Sent: Thursday, November 08, 2007 1:58 PM To: Puppet User Discussion Subject: Re: [Puppet-users] Downloading a file into a directory that doesnot exist -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Thursday 08 November 2007, Blank, Jessica wrote:> Hello again, all: > > When downloading a file from the puppetmaster''s fileserver, like so: > > ==========SNIP=========> define install_file ($mode = 0644, $owner = ''root'', $group = ''root'') { > file { "${name}": > source => [ > > "puppet://${puppetserver}/dist/files/${name}_${operatingsystem}", > "puppet://${puppetserver}/dist/files/${name}" > ], > mode => $mode, owner => $owner, group => $group > } > } > ==========SNIP=========> > If the file to be downloaded is, say, /etc/foo/bar/baz.txt, and the > directory /etc/foo (and thus /etc/foo/bar) does not exist, the > operation fails. > > The error message looks like this: > > err: > //site_1515/install_file[/etc/foo/bar/baz.txt]/File[/etc/foo/bar/baz.t > xt > ]/ensure: change from absent to file failed: Could not set file on > ensure: No such file or directory - /etc/foo/bar/baz.puppettmp at > /etc/puppet/manifests/functions.pp:48 > > It looks like this issue has been addressed at least twice before, at: > http://reductivelabs.com/trac/puppet/ticket/86 (" Directory creation > fails if parent directory does not exist ") > http://reductivelabs.com/trac/puppet/ticket/668 (" Using File with > ensure => directory fails if there''s no parent directory ") > > I''ve tried to solve the problem with mkdir -p (I evidently had the > same idea as the person from ticket #668), but I can''t figure out how > to do it. Puppet scripts often seem to execute out of order, due to > the highly declarative nature of the language, so I tried doing it > with dependencies... However, although I know how to make an exec > depend upon a file EXISTING, I do not know how to make it depend upon > a file NOT existing. > > Something like this: > > ==========SNIP=========> exec { "mkdir -p \"${name}\"; rmdir \"${name}\";": > path => > "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", > require => ! File["${name}"] > } > ==========SNIP=========> > The above would, of course, be a bit of a kluge ("create the target > filepath as a directory, then remove the directory, leaving only the > parent directory, before downloading the file into the newly created > directory..."), but it would, in theory work. > > Of course, "! File" doesn''t exist. > > Can anyone think of a way to solve this problem in an elegant and > reliable manner? > > If I''m trying to download /etc/foo/bar/baz.txt, and /etc/foo does not > exist, I really need /etc/foo and /etc/foo/bar created so that the > download will not fail. :)$dir = "/..." $file = "${dir}/..." exec { "create_${dir}": cmd => "mkdir -p ${dir}", creates => $dir, } file { $file: ensure => "...", require => Exec["create_${dir}"] } Note the usage of a descriptive identifier as title of the exec. Regards, David - -- The primary freedom of open source is not the freedom from cost, but the free- dom to shape software to do what you want. This freedom is /never/ exercised without cost, but is available /at all/ only by accepting the very different costs associated with open source, costs not in money, but in time and effort. - -- http://www.schierer.org/~luke/log/20070710-1129/on-forks-and-forking -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.6 (GNU/Linux) iD8DBQFHM1wj/Pp1N6Uzh0URAkynAKCjI6OWbYDLCVZfc4tH0TswQijLCQCcDd9y ttzwn3VUyPJbsD6eOOl+IRE=VQN/ -----END PGP SIGNATURE----- _______________________________________________ Puppet-users mailing list Puppet-users@madstop.com https://mail.madstop.com/mailman/listinfo/puppet-users
David Schmitt
2007-Nov-08 20:16 UTC
Re: Downloading a file into a directory that doesnot exist
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Thursday 08 November 2007, Blank, Jessica wrote:> This is a great idea, and I would likely have done something very much > like it. However, all that ''install_file'' is passed is a complete path > to a file (e.g. ''/etc/foo/bar/baz.txt''), NOT a directory name and a > filename (e.g. ''/etc/foo/bar'' and ''baz.txt'', respectively). > > Without rewriting install_file to require two arguments instead of one, > (and I''m not entirely sure how I would do that; so far, I''ve only > written functions that use $name as their sole argument)See http://reductivelabs.com/trac/puppet/wiki/LanguageTutorial#definitions : define something($second_var, $third=''defaultvalue'') { }> how could I > separate out the directory name from the filename? Can puppet do regexp > transformations on strings to lop off ''the last slash and anything after > it''?No, but you can create your own functions. See http://reductivelabs.com/trac/puppet/wiki/WritingYourOwnFunctions Coincidentally, you could use my basename and dirname functions from http://git.black.co.at/?p=manifests.git;a=tree;f=modules/common/plugins/puppet/parser/functions;hb=HEAD Regards, David - -- The primary freedom of open source is not the freedom from cost, but the free- dom to shape software to do what you want. This freedom is /never/ exercised without cost, but is available /at all/ only by accepting the very different costs associated with open source, costs not in money, but in time and effort. - -- http://www.schierer.org/~luke/log/20070710-1129/on-forks-and-forking -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.6 (GNU/Linux) iD8DBQFHM26t/Pp1N6Uzh0URAp7jAJ0Qp/2M77LyD8fcDm3QICJxO8XMTgCdHc7i /RJG/DODiktW1SGIfmptiwk=DeoO -----END PGP SIGNATURE-----