create a file only if directory exists

6,184 views
Skip to first unread message

Hang Chan

unread,
Nov 20, 2008, 3:05:26 PM11/20/08
to Puppet Users
I'm trying to create a virtual host file only if the document root
directory exists. It doesn't look like puppet is seeing the require
metaparameter. Currently, the file is being created whether or not
the document root directory exists. What is the best way to currently
do this?

I'm trying to create a file called site1.conf in /apps/apache/conf.d
only if /var/www/html/site1 directory exists.

file { "/apps/apache/conf.d/site1.conf":
ensure => present,
content => template("apache/virtual_host.erb"),
notify => Service["jboss_apache"],
require => File["/var/www/html/site1"]
}


joe

unread,
Nov 20, 2008, 3:36:42 PM11/20/08
to Puppet Users
Your require is actually creating the site1 directory if it doesn't
exist (unless your file def. for that directory has ensure => absent).
I think you want to go the other way and ensure that the site1.conf is
created by the definition for the site1 directory. In other words,
put a require for the site1.conf in the file definition for the site1
directory.

Andrew Shafer

unread,
Nov 20, 2008, 3:46:01 PM11/20/08
to puppet...@googlegroups.com
Hang,

The 'require' is going to look for a resource definition and make sure that the current resource is applied later. 'require' establishes explicit order, not conditional logic.

Also... Please don't take what I'm about to write the wrong way.

Are you familiar with the concept of 'code smells' or 'anti-patterns'?  When I see people trying to set up logic like that I personally think it is a Puppet smell.

Why?

Because  at the end of the day, once you get it all working, you don't have a way to know where site1 exists without sshing to all the boxes and looking for that directory.  What if you have some site1 specific logic, do you just keep adding if logic on a directory existing?

I personally recommend you reframe your strategy to declare site1 specific resources and then declare the hosts that serve site1.  Whenever possible declare, I think it will pay off in the long term.

Regards,
Andrew

Hang Chan

unread,
Nov 20, 2008, 4:34:43 PM11/20/08
to Puppet Users
I'm just hardcoding it to debug it, actual configuation isn't like
this.

I tried it the other way too and it's still creating the file.

file { "/apps/apache/conf.d/site1.conf":
ensure => present,
content => template("apache/virtual_host.erb"),
notify => Service["jboss_apache"],
# require => File["/var/www/html/site1"]
}

file { "/var/www/html/site1":
require => File["/apps/apache/conf.d/site1.conf"]
}

Is the way to approach this to use the exec type and onlyif resource?

Andrew Shafer

unread,
Nov 20, 2008, 5:22:01 PM11/20/08
to puppet...@googlegroups.com
hardcoding what?  what are you trying to debug?

Read what 'require' does. Require is not a conditional branch, it makes ordering explicit.

Exec with onlyif is one way you could get the behavior, or an if condition with a custom fact would.  There might be another way too.

I believe all of those idioms are going to be clumsy and hard to manage.

What is your end goal?

Hang Chan

unread,
Nov 21, 2008, 8:47:53 AM11/21/08
to Puppet Users
I'm trying to create a virtual host file only if the document root
directory exists. I could care less what method I use as long as it
works. Please let me know the best way to do this. I realize now
that require won't accomplish what I want.

I don't need puppet to create the /var/www/html/site1 directory as
that is created manually. Want puppet to check If the /var/www/html/
site1 directory exists and create the /apps/apache/conf.d/site1.conf
file if it does. Also need puppet to delete the /apps/apache/conf.d/
site1.conf file if the /var/www/html/site1 directory no longer exists.

On Nov 20, 5:22 pm, "Andrew Shafer" <and...@reductivelabs.com> wrote:
> hardcoding what?  what are you trying to debug?
>
> Read what 'require' does. Require is not a conditional branch, it makes
> ordering explicit.
>
> Exec with onlyif is one way you could get the behavior, or an if condition
> with a custom fact would.  There might be another way too.
>
> I believe all of those idioms are going to be clumsy and hard to manage.
>
> What is your end goal?
>

Mike Renfro

unread,
Nov 21, 2008, 9:46:52 AM11/21/08
to puppet...@googlegroups.com
Hang Chan wrote:

> I don't need puppet to create the /var/www/html/site1 directory as
> that is created manually. Want puppet to check If the /var/www/html/
> site1 directory exists and create the /apps/apache/conf.d/site1.conf
> file if it does. Also need puppet to delete the /apps/apache/conf.d/
> site1.conf file if the /var/www/html/site1 directory no longer exists.

You might consider making a puppet definition that would both create the
site1 directory, and then copy the site1.conf afterwards. If you want
something already written, then David Schmitt's Apache module at
http://git.black.co.at/?p=module-apache;a=tree;h=refs/heads/production;hb=refs/heads/production
should handle all of what you're asking for. Either use it as-is, or
look through his manifests for how his definitions to enable and disable
a site programmatically. Some of his methods are Debian-specific, so
they might not translate entirely into your environment.

--
Mike Renfro / R&D Engineer, Center for Manufacturing Research,
931 372-3601 / Tennessee Technological University -- ren...@tntech.edu

Hang Chan

unread,
Nov 21, 2008, 11:18:34 AM11/21/08
to Puppet Users
I looked through his definitions and it only checks if the proper
packages exists before setting up his configuration files. I need to
check if the directory exists as a condition. I don't want to create
the directory if it doesn't, as it is created manually right now and
needs to stay that way for awhile. Eventually it will all be
automated in puppet, but haven't gone that far yet.

I just want to do something like this (in bash):

if [ -d "/var/www/html/site1" ]; then
touch /apps/apache/conf.d/site1.conf
exit 0
else
echo "directory does not exist"
exit 1
fi

This seems like a simple thing to do, but for the life of me can't
figure out how to do this in puppet.

On Nov 21, 9:46 am, Mike Renfro <ren...@tntech.edu> wrote:
> Hang Chan wrote:
> > I don't need puppet to create the /var/www/html/site1 directory as
> > that is created manually.  Want puppet to check If the /var/www/html/
> > site1 directory exists and create the /apps/apache/conf.d/site1.conf
> > file if it does.  Also need puppet to delete the /apps/apache/conf.d/
> > site1.conf file if the /var/www/html/site1 directory no longer exists.
>
> You might consider making a puppet definition that would both create the
>   site1 directory, and then copy the site1.conf afterwards. If you want
> something already written, then David Schmitt's Apache module athttp://git.black.co.at/?p=module-apache;a=tree;h=refs/heads/productio...

Mike Renfro

unread,
Nov 21, 2008, 11:30:41 AM11/21/08
to puppet...@googlegroups.com
On 11/21/2008 10:18 AM, Hang Chan wrote:
> I looked through his definitions and it only checks if the proper
> packages exists before setting up his configuration files.

site.pp also includes an apache::site definition that can create or
remove a configuration file depending on if you call the definition with
ensure => 'present' (the default), or ensure => 'absent'. That
definition could be easily extended to also automatically create or
remove the folder you're worried about.

> I need to check if the directory exists as a condition. I don't want
> to create the directory if it doesn't, as it is created manually
> right now and needs to stay that way for awhile.

Then you'll probably have to copy down the configuration file into a
separate location, and use an exec to cp it into the proper destination
with an onlyif test.

Hang Chan

unread,
Nov 21, 2008, 11:38:22 AM11/21/08
to Puppet Users
> Then you'll probably have to copy down the configuration file into a
> separate location, and use an exec to cp it into the proper destination
> with an onlyif test.

That sounds like a good suggestion. Have to stop being so
straightforward with my thinking.
Reply all
Reply to author
Forward
0 new messages