Ask Your Question
0

access default values from other modules

asked 2015-11-10 07:31:09 -0500

teleivo gravatar image

I have a java application for which I wrote a component module (see code below) that deploys a war file on tomcat using the forge module puppetlabs/tomcat

# Class: openmrs: See README.md for documentation.
class openmrs (
  $tomcat_catalina_base,
  $tomcat_user,
  $openmrs_application_data_directory,
  $db_host = 'localhost',
  $db_name = 'openmrs',
  $db_owner = 'openmrs',
  $db_owner_password = 'openmrs',
) {
  validate_absolute_path($tomcat_catalina_base)
  validate_string($tomcat_user)
  validate_absolute_path($openmrs_application_data_directory)
  validate_string($db_host)
  validate_string($db_name)
  validate_string($db_owner)
  validate_string($db_owner_password)

  include '::tomcat'

  file { $openmrs_application_data_directory:
    ensure => 'directory',
    owner  => $tomcat_user,
    group  => $tomcat_user,
    mode   => '0755',
  }

  mysql::db { $db_name:
    user     => $db_owner,
    password => $db_owner_password,
    host     => $db_host,
    grant    => ['ALL'],
  }

  tomcat::war { 'openmrs.war':
    catalina_base => $tomcat_catalina_base,
    war_source    =>
    'http://sourceforge.net/projects/openmrs/files/releases/OpenMRS_Platform_1.11.4/openmrs.war',
    require       => [
                        File[$openmrs_application_data_directory],
                        Mysql::Db[$db_name],
    ]
  }
}

I install tomcat in a separate tomcat profile. In the component module I need to set the attribute 'catalinabase' of tomcat::war to point to the existing tomcat instance and I also need access to the tomcatuser to create a file that is readable by the tomcat user. I therefore pass these 2 variables as required parameters into my module.

I just wonder about the best practice here:

  • Is it ok to have a component module that has required parameters, since there are no sane defaults for some parameters (like in my case for tomcat user/base folder)?
  • I assume it is bad practice to access variables directly from another module like '::tomcat' in a component module to get default values from there?
  • When would you write a component module? In my case I could also just move my module code up to a profile which installs/configures tomcat with puppetlabs/tomcat and deploys the war file.
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2015-11-12 01:09:03 -0500

Cristian Falcas gravatar image

This is how we do it also.

Since at profile level we don't want to do any logic and want to declare only classes, all other resources necessary and ifs/cases are declared in classes named "conf_something".

We mostly push configuration from hiera. So classes that use the same value for some parameters are configured from hiera with a lot of interpolation. For example:

{
     --- variables---
  "foreman_proxy_port": 8444,
  "puppet_master_port": 8140,
      .....
  "abrt::libreport::ureport::url": "https://%{::puppetmaster}:%{hiera('foreman_proxy_port')}/abrt",
  "foreman_proxy::puppet_url": "https://%{::fqdn}:%{hiera('puppet_master_port')}",
  ....
}

But, in case of defines and other resources where we need the same value as some parameter from another module, we do access the variable directly. For this we need to be certain that that specific class was declared before our resource.

This keeps our code cleaner.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2015-11-10 07:31:09 -0500

Seen: 69 times

Last updated: Nov 12 '15