2
votes

I have 2 defined resources created

  1. Download an application and set it up as a service

  2. Create the config file for the service

I want 1 to run before 2 as 1 creates the directory which 2 creates the config file in.

But what I also need is if the config file is updated, it should notify the service in 1 to refresh. This is where I'm having a problem as I don't seem to be able to access the service.

Here is the sample code I'm using:

class start {
    resource::run_service{'start'}
    ->
    resource::conf:service_conf{'conf'}
}

define resource::run_service {
    # Does some things
    service { 'main':
        ensure => 'running',
        enable => true
    }
}

define resource::conf::service_conf {
    file { 'file.config':
        ensure  => file,
        path    => '/some/path/',
        owner   => 'user',
        group   => 'group',
        mode    => 0600,
        content => template('templates/templatefile.erb'),
        notify  => Resource::Run_service::Service['main'], # What is the proper way to do this?
    }
}

This is the error I get from puppet:

Error: Could not retrieve catalog from remote server: Error 400 on SERVER: Invalid relationship: File[file.config] { notify => Resource::Run_service::Service['main'] }, because Resource::Run_service::Service['main'] doesn't seem to be in the catalog

I can do this:

notify  => Resource::Run_service['main'],

But it gives me a dependency cycle.

Is there a way I can refresh the service only?

1

1 Answers

3
votes

Some issues here:

  • resource::run_service{'start'} -> resource::conf:service_conf{'conf'} is specifying a dependency which is the opposite of what you specified that you desire (2 before 1). I assume this is a typo because it contradicts other parts of your question.
  • You should put defined resource types in their own manifests.
  • You get no real added value from defined resource types that contain no parameters nor use of the intrinsic $title variable, so you may want to just remove them. At the very least, you should probably reorganize them so that the directory creation for the config file is in the same defined resource type as the config file.
  • Resource::Run_service::Service['main'] is not a valid resource name, so that is why you get the 'not in catalog' error. Native resource types have no namespace, so you do not need to specify the defined resource type namespace ahead of them.
  • You need to use strings for octals with the mode attribute values. This an error in Puppet because it can lead to undefined behavior with the type conversion in Ruby.

With all that in mind, the easiest path forward for you is:

class start {

}

define resource::run_service {
  file { 'directory that contains config file':
    ensure => directory,
    before => File['file.config'],
  }

  # Does some things
  service { 'main':
    ensure => 'running',
    enable => true
  }
}

define resource::conf::service_conf {
  file { 'file.config':
    ensure  => file,
    path    => '/some/path/',
    owner   => 'user',
    group   => 'group',
    mode    => '0600',
    content => template('templates/templatefile.erb'),
    notify  => Service['main'],
  }
}

This will get you the behavior you desire with the least amount of pain.