Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run the same class multiple times?

Tags:

ruby

puppet

I have a puppet module which deploys a JAR file and writes some properties files (by using ERB templates). Recently we added a "mode" feature to the application, meaning the application can run in different modes depending on the values entered in the manifest. My hierarchy is as follows:

setup

*config

**files

*install

Meaning setup calls the config class and the install class. The install class deploys the relevant RPM file according to the mode(s)

The config class checks the modes and for each mode calls the files class with the specific mode and directory parameters, the reason for this structure is that the value of the properties depends on the actual mode.

The technical problem is that if I have multiple modes in the manifest (which is my goal) I need to call the files class twice:

if grep($modesArray, $online_str) == [$online_str] {
    class { 'topology::files' :
      dir   => $code_dir,
      mode  => $online_str
    }
  }

  $offline_str = "offline"
  $offline_suffix = "_$offline_str"
  if grep($modesArray, $offline_str) == [$offline_str] {
    $dir = "$code_dir$offline_suffix"
    class { 'topology::files' :
      dir   => $dir,
      mode  => $offline_str
    }

However, in puppet you cannot declare the same class twice.

I am trying to figure out how I can call a class twice or perhaps a method which I can access its parameters from my ERB files, but I can't figure this out

The documentation says it's possible but doesn't say how (I checked here https://docs.puppetlabs.com/puppet/latest/reference/lang_classes.html#declaring-classes).

So to summarize is there a way to either:

  1. Call the same class more then once with different parameters
  2. (Some other way to) Create multiple files based on the same ERB file (with different parameters each time)
like image 993
Sigal Shaharabani Avatar asked Aug 19 '14 10:08

Sigal Shaharabani


1 Answers

You can simply declare your class as a define:

define topology::files($dir,$mode){
  file{"${dir}/filename":
    content=> template("topology/${mode}.erb"), 
  }
}

That will apply a different template for each mode

And then, instantiate it as many times as you want:

if grep($modesArray, $online_str) == [$online_str] {
    topology::files{ "topology_files_${online_str}" :
      dir   => $code_dir,
      mode  => $online_str
    }
  }

  $offline_str = "offline"
  $offline_suffix = "_$offline_str"
  if grep($modesArray, $offline_str) == [$offline_str] {
    $dir = "$code_dir$offline_suffix"
    topology::files{ "topology_files_${online_str}" :
      dir   => $dir,
      mode  => $offline_str
    }
like image 61
Raul Andres Avatar answered Oct 31 '22 03:10

Raul Andres