You are browsing a read-only backup copy of Wikitech. The live site can be found at wikitech.wikimedia.org

Puppet style guide

From Wikitech-static
Jump to navigation Jump to search

Please read the upstream style-guide. And install puppet-lint.

format

Many existing manifests use tabs or two-spaces (as suggested in the style guide) instead of our 4 space indent standard; when working on existing code always follow the existing whitespace style of the file or module you are editing. Please do not mix cleanup changes with functional changes in a single patch.

Spacing, Indentation, & Whitespace

  • Must use four-space soft tabs.
  • Must not use literal tab characters.
  • Must not contain trailing white space
  • Must align fat comma arrows (=>) within blocks of attributes.

Quoting

  • Must use single quotes unless interpolating variables.
  • All variables should be enclosed in in braces ({}) when being interpolated in a string. like this
  • Variables standing by themselves should not be quoted.like this
  • Must not quote booleans:
true

but not

'true' 

and also no

"true"

Resources

  • Must single quote all resource names and their attribute (unless they contain a variable, of course).
  • Ensure must always be the first attribute.
  • Put a trailing comma after the final resource parameter.
  • Again: Must align fat comma arrows (=>) within blocks of attributes.
  • Don't group resources of the same type (a.k.a compression) :

do

       file { '/etc/default/exim4':
           require => Package['exim4-config'],
           owner   => 'root',
           group   => 'root',
           mode    => '0444',
           content => template('exim/exim4.default.erb'),
       }
       file { '/etc/exim4/aliases/':
           ensure  => 'directory',
           require => Package['exim4-config'],
           mode    => '0755',
           owner   => 'root',
           group   => 'root',
       } 

don't do

       file { '/etc/default/exim4':
           require => Package['exim4-config'],
           owner   => 'root',
           group   => 'root',
           mode    => '0444',
           content => template('exim/exim4.default.erb');
      '/etc/exim4/aliases/':
           ensure  => 'directory',
           require => Package['exim4-config'],
           mode    => '0755',
           owner   => 'root',
           group   => 'root',
       } 
  • keep the resource name and the resource type on the same line. No need for extra indentation.

Conditionals

  • Don't use selectors inside resources:

good:

   $file_mode = $::operatingsystem ? {
     debian => '0007',
     redhat => '0776',
     fedora => '0007',
   }
   file { '/tmp/readme.txt':
      content => "Hello World\n",
      mode    => $file_mode,
   }

bad:

    file { '/tmp/readme.txt':
     mode => $::operatingsystem ? {
       debian => '0777',
       redhat => '0776',
       fedora => '0007',
     }
   }
  • Case statements should have default cases. like this.

Classes

All classes and resource type definitions must be in separate files in the manifests directory of their module.

  • Do not nest classes.
  • Ignore Inheritance as possible, puppet is not good at that.
  • Try not to use top-space variables, but if you do use them, scope them correctly:
$::operatingsystem

but not

$operatingsystem
  • Do not use dashes in class names, preferably use alpha-betaic names only.
  • In parameterized class and defined resource type declarations, parameters that are required should be listed before optional parameters. like this.

Including

  • One include per line.
  • One class per include.
  • Include only the class you need, not the entire scope.