Last active
November 11, 2015 14:11
-
-
Save smbambling/29bac41c434274afbfa1 to your computer and use it in GitHub Desktop.
Puppet Class to auto create PE Classifier Environment groups based on PuppetServer environments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## This class requires env_utils, node_manager and stdlib | |
# Array of PE Environment Groups excluding the default 'Agent-specified environment' group | |
$pe_environment_groups = delete(node_groups().filter |$key, $val| { $val["environment_trumps"] == true }.map |$key, $val| { $val["name"] }, 'Agent-specified environment') | |
# Obtain PuppetServer environments and munge to look like PE Classifier environment Groups | |
$puppet_environments = regsubst(capitalize(environments()), '$', ' environment', 'G') | |
# Array of PE Environment Groups that should be removed based | |
# the PuppetServer environment no longer exists | |
$remove_pe_environment_groups = difference($pe_environment_groups, $puppet_environments) | |
# Remove the PE Environment Groups | |
$remove_pe_environment_groups.each | $group | { | |
node_group { $group: | |
ensure => 'absent', | |
} | |
} | |
# Array for PuppetServer environments | |
$environments = environments() | |
# Create PE Environment Groups in the Classifier | |
$environments.each | $env | { | |
# Capitalize the Environment Name | |
$cap_env = capitalize($env) | |
$qa_subdomain = regsubst($env, 'si_', '') | |
# Set classifier parent group | |
$parent_group = $env ? { | |
'production' => 'All Nodes', | |
default => 'Production environment', | |
} | |
# Set classifier rule for static branches | |
$group_rule = $env ? { | |
'production' => ['and', ['~', ['fact','name'], '.*']], | |
/^si_.*$/ => ['and', ['~', ['fact','domain'], "${qa_subdomain}.example.com"]], | |
default => ['and', ['=', ['fact','name'], 'this_is_bogus']], | |
} | |
# Create PE Environment Group | |
node_group { "$cap_env environment": | |
ensure => 'present', | |
environment => $env, | |
override_environment => true, | |
parent => "$parent_group", | |
rule => $group_rule, | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment