Check service running with flag file

634 views
Skip to first unread message

Eddie Mashayev

unread,
Jun 10, 2015, 7:30:33 AM6/10/15
to puppet...@googlegroups.com
Hi All,

I want to check that my service "nails" running on all my servers only if file "/etc/NONAILS" not exists on this server.

If it exists don’t start this process, this file works like a flag.

I wrote script in puppet to check that and it works. 


Still it throes error when this file do exists:


[root@test ~]# puppet  agent -t

Info: Retrieving pluginfacts

Info: Retrieving plugin

Info: Loading facts

Info: Loading facts

Info: Caching catalog for test

Info: Applying configuration version '1433921081'

Error: Could not find command 'checkForFile'

Error: /Stage[main]/Check_service/Exec[checkForFile]/returns: change from notrun to 0 failed: Could not find command 'checkForFile'

Notice: /Stage[main]/Check_service/Service[nails]: Dependency Exec[checkForFile] has failures: true

Warning: /Stage[main]/Check_service/Service[nails]: Skipping because of failed dependencies


Question can I do it in cleaner way without throwing errors??

  

This is my script:


class check_service {

        service
{ "nails":
               
ensure => "running",
                enable
=> true,
                hasstatus  
=> false,
                hasrestart
=> true,
               
require => exec['checkForFile'],
       
}

       
exec {"checkForFile":
                path
=> "/usr/bin/",
                onlyif
=> "test -e /etc/NONAILS",
       
}
}


Eddie Mashayev

unread,
Jun 11, 2015, 2:13:13 AM6/11/15
to puppet...@googlegroups.com
Anyone?!

Denmat

unread,
Jun 11, 2015, 3:56:58 AM6/11/15
to puppet...@googlegroups.com
Hi,

May not be the best solution but you can specify the command to run when starting a service, you could use that instead.

service {blah:
  start => 'test .... && service blah start'
}

Alternatively you might want to put logic in the init file, into a facter value or some other way.

Cheers
Den
--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/6cc13de4-eeba-44d7-8bd6-15af22d775fc%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Craig Dunn

unread,
Jun 11, 2015, 5:55:19 AM6/11/15
to puppet...@googlegroups.com
You haven't provided a command to run via the command attribute, so
the provider will attempt to execute what you have in the resource
title (checkForFile) - that is to say, it is trying to execute the
command 'checkForFile', which of course, doesn't exist.

As Denmat already pointed out, this may not be the best solution, but
if you really want to stick with this approach then you need to
specify a command - try adding.... command => '/bin/true' to the exec
resource declaration to get around your current problem.

Craig
> --
> You received this message because you are subscribed to the Google Groups
> "Puppet Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to puppet-users...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/puppet-users/6cc13de4-eeba-44d7-8bd6-15af22d775fc%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.



--
Enviatics | Automation and configuration management
http://www.enviatics.com | @Enviatics
Puppet Training http://www.enviatics.com/training/

Craig Dunn

unread,
Jun 11, 2015, 6:01:55 AM6/11/15
to puppet...@googlegroups.com
On reading this again, what you are trying to do won't work - you are
requiring that the exec resource be "satisfied" before the service
starts - if the exec resource doesn't run because onlyif returns
false, this is normal behaviour and not a failure, therefore the
resource is still satisfied and your service resource will be acted
on. All you are doing is determining order.

Eddie Mashayev

unread,
Jun 11, 2015, 8:24:57 AM6/11/15
to puppet...@googlegroups.com

Thanks, do you have any other suggestion how can I do it properly?

I want “nails” process to be running only if there is no /etc/NONAILS flag in my OS.

jcbollinger

unread,
Jun 11, 2015, 9:06:53 AM6/11/15
to puppet...@googlegroups.com


On Thursday, June 11, 2015 at 7:24:57 AM UTC-5, Eddie Mashayev wrote:

Thanks, do you have any other suggestion how can I do it properly?

I want “nails” process to be running only if there is no /etc/NONAILS flag in my OS.




The canonical way to inform the catalog compiler about node state is via node facts.  It should be pretty straightforward to create a custom fact that simply reports on the presence or absence of one file; then you put the Service resource in a conditional block that depends on the value of that fact.


John

Eddie Mashayev

unread,
Jun 18, 2015, 4:31:49 AM6/18/15
to puppet...@googlegroups.com
Hi,

Found a solution:

Create ruby script in:
root@foreman]# cat /etc/puppet/environments/production/modules/customfacts/lib/facter/check_file_exsist.rb
Facter.add('check_nails_exsist') do
  setcode do
   File.exists?('/etc/NONAILS')
 end
end

We are checking if /etc/NONAILS exist, if yes return true else false.

Create puppet manifest to ensure the service running, if file exist stop the server:
[root@foreman]# cat /etc/puppet/environments/production/modules/check_service/manifests/init.pp
# Class: check_service

class check_service {
        if $check_nails_exsist  == 'true' {
                service { 'nails':
                ensure => "stopped",
                enable => false,
                hasstatus  => false,
                hasrestart => false,
                }
        }
        else {
                service { 'nails':
                ensure => "running",
                enable => true,
                hasstatus  => false,
                hasrestart => true,
                }
        }
}


Hope it helped.
Reply all
Reply to author
Forward
0 new messages