Puppet Plan: patching
- Defined in:
- plans/init.pp
Summary
Our generic and semi-opinionated workflow.Overview
It serves as a showcase of how all of the building blocks in this module can be tied together to create a full blown patching workflow. This is a great initial workflow to patch servers. We fully expect others to take this workflow as a build-block and customize it to meet their needs.
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 |
# File 'plans/init.pp', line 114
plan patching (
TargetSpec $targets,
Boolean $filter_offline_targets = false,
Optional[Boolean] $monitoring_enabled = undef,
Optional[String] $monitoring_plan = undef,
Optional[String] $pre_update_plan = undef,
Optional[String] $update_provider = undef,
Optional[String] $post_update_plan = undef,
Optional[Enum['only_required', 'never', 'always']] $reboot_strategy = undef,
Optional[String] $reboot_message = undef,
Optional[Integer] $reboot_wait = undef,
Optional[Integer] $disconnect_wait = undef,
Optional[String] $snapshot_plan = undef,
Optional[Boolean] $snapshot_create = undef,
Optional[Boolean] $snapshot_delete = undef,
Optional[Enum['none', 'pretty', 'csv']] $report_format = undef,
Optional[String] $report_file = undef,
Boolean $noop = false,
) {
## Filter offline targets
$check_puppet_result = run_plan('patching::check_puppet', $targets,
filter_offline_targets => $filter_offline_targets)
# use all targets, both with and without puppet
$_targets = $check_puppet_result['all']
# read variables for plan-level settings
$plan_vars = $_targets[0].vars
$reboot_wait_plan = pick($reboot_wait,
$plan_vars['patching_reboot_wait'],
300)
$report_format_plan = pick($report_format,
$plan_vars['patching_report_format'],
'pretty')
$report_file_plan = pick($report_file,
$plan_vars['patching_report_file'],
'patching_report.csv')
## Group all of the targets based on their 'patching_order' var
$ordered_groups = run_plan('patching::ordered_groups', $_targets)
# we can now use the $ordered_keys array above to index into our $ordered_hash
# pretty cool huh?
$ordered_groups.each |$group_hash| {
$ordered_targets = $group_hash['targets']
if $ordered_targets.empty {
fail_plan("Targets not assigned the var: 'patching_order'")
}
# override configurable parameters on a per-group basis
# if there is no customization for this group, it defaults to the global setting
# set at the plan level above
$group_vars = $ordered_targets[0].vars
# Prescedence: CLI > Config > Default
$monitoring_plan_group = pick($monitoring_plan,
$group_vars['patching_monitoring_plan'],
'patching::monitoring_solarwinds')
$monitoring_enabled_group = pick($monitoring_enabled,
$group_vars['patching_monitoring_enabled'],
true)
$reboot_strategy_group = pick($reboot_strategy,
$group_vars['patching_reboot_strategy'],
'only_required')
$reboot_message_group = pick($reboot_message,
$group_vars['patching_reboot_message'],
'NOTICE: This system is currently being updated.')
$update_provider_group = pick_default($update_provider,
$group_vars['patching_update_provider'],
undef)
$reboot_wait_group = pick($reboot_wait,
$group_vars['patching_reboot_wait'],
300)
$pre_update_plan_group = pick($pre_update_plan,
$group_vars['patching_pre_update_plan'],
'patching::pre_update')
$post_update_plan_group = pick($post_update_plan,
$group_vars['patching_post_update_plan'],
'patching::post_update')
$snapshot_plan_group = pick($snapshot_plan,
$group_vars['patching_snapshot_plan'],
'patching::snapshot_vmware')
$snapshot_create_group = pick($snapshot_create,
$group_vars['patching_snapshot_create'],
true)
$snapshot_delete_group = pick($snapshot_delete,
$group_vars['patching_snapshot_delete'],
true)
$disconnect_wait_group = pick($disconnect_wait,
$group_vars['patching_disconnect_wait'],
10)
# do normal patching
## Update patching cache (yum update, apt-get update, etc)
run_task('patching::cache_update', $ordered_targets,
_noop => $noop)
## Check for updates on hosts
$available_results = run_plan('patching::available_updates', $ordered_targets,
provider => $update_provider_group,
format => 'pretty',
noop => $noop)
$update_targets = $available_results['has_updates']
if $update_targets.empty {
next()
}
## Disable monitoring
if $monitoring_enabled_group and $monitoring_plan_group and $monitoring_plan_group != 'disabled' {
run_plan($monitoring_plan_group, $update_targets,
action => 'disable',
noop => $noop)
}
## Create VM snapshots
if $snapshot_create_group and $snapshot_plan_group and $snapshot_plan_group != 'disabled'{
run_plan($snapshot_plan_group, $update_targets,
action => 'create',
noop => $noop)
}
## Run pre-patching script.
run_plan($pre_update_plan_group, $update_targets,
noop => $noop)
## Run package update.
$update_result = run_task('patching::update', $update_targets,
provider => $update_provider_group,
_catch_errors => true,
_noop => $noop)
## Collect list of successful updates
$update_ok_targets = $update_result.ok_set.targets
## Collect list of failed updates
$update_errors = $update_result.error_set
## Check if any hosts with failed updates.
if $update_errors.empty {
$status = 'OK: No errors detected.'
} else {
# TODO print out the full error message for each of these
alert('The following hosts failed during update:')
alert($update_errors)
$status = 'WARNING: Errors detected during update.'
}
if !$update_ok_targets.empty {
## Run post-patching script.
run_plan($post_update_plan_group, $update_ok_targets,
noop => $noop)
## Check if reboot required and reboot if true.
run_plan('patching::reboot_required', $update_ok_targets,
strategy => $reboot_strategy_group,
message => $reboot_message_group,
wait => $reboot_wait_group,
disconnect_wait => $disconnect_wait_group,
noop => $noop)
## Remove VM snapshots
if $snapshot_delete_group and $snapshot_plan_group and $snapshot_plan_group != 'disabled' {
run_plan($snapshot_plan_group, $update_ok_targets,
action => 'delete',
noop => $noop)
}
}
# else {
# # TODO should we break here?
# }
## enable monitoring
if $monitoring_enabled_group and $monitoring_plan_group and $monitoring_plan_group != 'disabled' {
run_plan($monitoring_plan_group, $update_targets,
action => 'enable',
noop => $noop)
}
}
## Re-establish all targets availability after reboot
# This is necessary in case one of the groups affects the availability of a previous group.
# Two use cases here:
# 1. A later group is a hypervisor. In this instance the hypervisor will reboot causing the
# VMs to go offline and we need to wait for those child VMs to come back up before
# collecting history metrics.
# 2. A later group is a linux router. In this instance maybe the patching of the linux router
# affects the reachability of previous hosts.
wait_until_available($_targets, wait_time => $reboot_wait_plan)
## Collect summary report
run_plan('patching::update_history', $_targets,
format => $report_format_plan,
report_file => $report_file_plan)
## Display final status
return()
}
|