Ask Your Question
0

Having difficulties with my defined type and making it robust

asked 2016-06-29 19:11:28 -0500

felipe1982 gravatar image

updated 2016-06-29 19:16:45 -0500

GOAL : I want to avoid duplicating my code/lines in this hieradata. How can I rethink my approach to this?

The component module freshserver::html creates an HTML file with links to launch an application. The link names are based on the company id (e.g. HEH, or BEAT). One company may have many subcompanies, and so multiple links. Each subcompany needs its own html file too, so if HEH has subcompanies WEB, BEAT, HEHB, UDAS we need 5 html files which are all alike, each html file having 5 sets of links.

Hiera is useful for this, because every host/node has DIFFERENT company names on it

profiles::html

class profiles::html (
  $clients = hiera('profiles::html::clients'),
){
  Freshserver::Html {
    directory => '/u2/prod1/conf/GUI/web',
  }
  create_resources(freshserver::html, $clients)

}

hieradata

profiles::html::clients:
  kelloggs:
    ensure: file
    hide_links: false
    notice: 'this is an example of a so-called normal 1:1 company:html file'
   web:
    hide_links: true
    notice:     'this is an example of a many-to-many, with 1 html file per company, and each html files having all company links'
    companies:
    - heh
    - hehb
    - web
    - udas
    - beat
  udas:
    hide_links: true
    warn:       'Not Yet Live'
    notice:     'notice'
    companies:
    - heh
    - hehb
    - web
    - udas
    - beat
  hehb:
    hide_links: true
    warn:       'Not Yet Live'
    notice:    'notice'
    companies:
    - heh
    - hehb
    - web
    - udas
    - beat
  heh:
    hide_links: true
    warn:       'Not Yet Live'
    notice:    'notice'
    companies:
    - heh
    - hehb
    - web
    - udas        
    - beat
  beat:
    hide_links: true
    warn:       'Not Yet Live'
    notice:    'notice'
    companies:
    - heh
    - hehb
    - web
    - udas
    - beat

define freshserver::html

# configures <client>.html files in $GW on fresh server
define freshserver::html (
  $directory,
  $companies    = $title,
  $ensure       = file,
  $hide_links   = false,
  $notice       = undef,
  $symlink_path = undef,
  $warn         = undef,
){

  validate_absolute_path($directory)

  $companies_real = any2array($companies)

  $full_path = "${directory}/${title}.html"

  file { $title:
    ensure  => $ensure,
    owner   => 'root',
    group   => 'root',
    mode    => '0644',
    path    => $full_path,
    content => template( 'freshserver/html/client.html.erb'),
  }

  if $symlink_path != undef {
    exec { "${title}-symlink":
      user    => 'root',
      path    => '/usr/bin:/bin',
      command => "echo test",
      require => File[$title],
    }
  }

}

client.html.erb

<!-- begin links -->
<% if @hide_links != true -%>
<table border=0>
    <tr>
      <td align="center">
       <strong>In Office (Internal)</strong>
      </td>
    </tr>
    <% @companies_real.each do |company| -%>
  <tr>
    <td width=250 align=center><a href="http://in.<%= company %>.example.com.au/cgi/launch_lettus.py?config=<%= company %>"><%= company.upcase %></a></td><td>Fresh<td>
  </tr>
  <tr>
    <td width=250 align=center><a href="http://in.<%= company %>.example.com.au/cgi/launch_lettus.py?config=<%= company %>&windowsize=large"><%= company.upcase %></a></td><td>Large<td>
  </tr>
  <tr>
    <td width=250 align=center><a href="http://in.<%= company %>.example.com.au/cgi/launch_lettus.py?config=<%= company %>&windowsize=xlarge"><%= company.upcase %></a></td><td>XLarge<td>
  </tr>
  <% end -%>
  <tr>
  <td colspan="2">&nbsp;</td>
    <tr>
      <td align="center">
       <strong>At Home (External)</strong>
      </td>
    </tr>
  </tr>
    <% @companies_real.each do |company| -%>
  <tr>
    <td width=250 align=center><a href="http://<%= company %>.example.com.au/cgi/launch_lettus.py?config=<%= company %>"><%= company.upcase %></a></td><td>Fresh<td>
  </tr>
  <tr>
    <td width=250 align=center><a href="http://<%= company %>.example.com.au/cgi/launch_lettus.py?config=<%= company %>&windowsize=large"><%= company.upcase ...
(more)
edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
0

answered 2016-07-01 08:29:15 -0500

updated 2016-07-01 08:30:19 -0500

How about:

$companies = keys($clients)

Call your define instead of create_resources, passing in the $clients hash. $title is the key for the current company.

freshserver::html { $companies:
clients => $clients, }

No need to define the companies array in hiera at all.

edit flag offensive delete link more
0

answered 2016-06-30 03:54:05 -0500

lupin gravatar image

updated 2016-06-30 03:55:20 -0500

If sub-companies values are common you can make it into seperate key:value pair and reference it,

sub-companies:
   - heh
   - hehb
   ....

then on

beat:
    companies: "%{hiera('sub-companies')}" 
udas:
    companies: "%{hiera('sub-companies')}"
edit flag offensive delete link more

Comments

I am looking for a way to make identical HTML files, 1 for each subcompany, each of which contains links for every (sub)company. heh.html contains links for heh, hehb, udas, beat, and web udas.html, beat.html, web.html, hehb.html are COPIES of the above. Subcompanies are always different.

felipe1982 gravatar imagefelipe1982 ( 2016-06-30 06:51:11 -0500 )edit

How about Calling your define instead of create_resources, passing in the $clients hash. $title is the key for the current company. freshserver::html { $companies: clients => $clients, } No need to define the companies array in hiera at all. Thanks http://qwikfix.co.uk/sky-customer-services/

googleclone gravatar imagegoogleclone ( 2016-08-13 07:05:16 -0500 )edit

Your Answer

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

Add Answer

Question Tools

Stats

Asked: 2016-06-29 19:11:28 -0500

Seen: 33 times

Last updated: Jul 01 '16