Skip to content

Commit 223e15b

Browse files
committed
feat: add webmock for easier api mocking in rspec tests
1 parent a4f6bc5 commit 223e15b

9 files changed

Lines changed: 463 additions & 10 deletions

File tree

REFERENCE.md

Lines changed: 209 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ the `os_patching` fact.
2121
[puppet health check](https://forge.puppet.com/albatrossflavour/puppet_health_check)
2222
module to perform a pre-check on the nodes you're planning to patch. If the nodes pass the
2323
check, they get patched
24+
* [`os_patching::patch_batch`](#os_patching--patch_batch): Patch nodes in a batch, is called from patch_group plan or patch_pql plan
25+
* [`os_patching::patch_group`](#os_patching--patch_group): Patch nodes collected by a fact group
26+
* [`os_patching::patch_pql`](#os_patching--patch_pql): Patch nodes collected by a PQL query
2427

2528
## Classes
2629

@@ -43,6 +46,7 @@ class { 'os_patching':
4346
'end' => '2019-01-15T23:59:59+10:00',
4447
},
4548
},
49+
group => 'patching01',
4650
}
4751
```
4852

@@ -65,6 +69,7 @@ class profiles::soe::patching (
6569
patch_window => $patch_window,
6670
reboot_override => $reboot_override,
6771
blackout_windows => $full_blackout_windows,
72+
group => 'patching01',
6873
}
6974
}
7075
```
@@ -119,6 +124,7 @@ The following parameters are available in the `os_patching` class:
119124
* [`windows_update_interval_mins`](#-os_patching--windows_update_interval_mins)
120125
* [`fact_mode`](#-os_patching--fact_mode)
121126
* [`ensure`](#-os_patching--ensure)
127+
* [`group`](#-os_patching--group)
122128

123129
##### <a name="-os_patching--puppet_binary"></a>`puppet_binary`
124130

@@ -212,7 +218,7 @@ This overrides the setting in the task
212218

213219
##### <a name="-os_patching--patch_window"></a>`patch_window`
214220

215-
Data type: `Optional[String]`
221+
Data type: `Optional[Pattern[/^[A-Za-z0-9\-_ ]+$/]]`
216222

217223
A freeform text entry used to allocate a node to a specific patch window (Optional)
218224

@@ -296,6 +302,14 @@ Data type: `Enum['present', 'absent']`
296302

297303
`present` to install scripts, cronjobs, files, etc, `absent` to cleanup a system that previously hosted us
298304

305+
##### <a name="-os_patching--group"></a>`group`
306+
307+
Data type: `Optional[Pattern[/^[A-Za-z0-9\-_ ]+$/]]`
308+
309+
The group to assign the node for patching purposes.
310+
311+
Default value: `undef`
312+
299313
## Tasks
300314

301315
### <a name="clean_cache"></a>`clean_cache`
@@ -399,3 +413,197 @@ Data type: `Optional[Integer]`
399413

400414
Default value: `1800`
401415

416+
### <a name="os_patching--patch_batch"></a>`os_patching::patch_batch`
417+
418+
Patch nodes in a batch, is called from patch_group plan or patch_pql plan
419+
420+
#### Parameters
421+
422+
The following parameters are available in the `os_patching::patch_batch` plan:
423+
424+
* [`batch`](#-os_patching--patch_batch--batch)
425+
* [`catch_errors`](#-os_patching--patch_batch--catch_errors)
426+
* [`noop_state`](#-os_patching--patch_batch--noop_state)
427+
* [`run_health_check`](#-os_patching--patch_batch--run_health_check)
428+
* [`service_enabled`](#-os_patching--patch_batch--service_enabled)
429+
* [`service_running`](#-os_patching--patch_batch--service_running)
430+
* [`runinterval`](#-os_patching--patch_batch--runinterval)
431+
* [`debug`](#-os_patching--patch_batch--debug)
432+
433+
##### <a name="-os_patching--patch_batch--batch"></a>`batch`
434+
435+
Data type: `TargetSpec`
436+
437+
The batch of nodes to patch
438+
439+
##### <a name="-os_patching--patch_batch--catch_errors"></a>`catch_errors`
440+
441+
Data type: `Boolean`
442+
443+
Whether to catch errors during task execution
444+
445+
Default value: `true`
446+
447+
##### <a name="-os_patching--patch_batch--noop_state"></a>`noop_state`
448+
449+
Data type: `Boolean`
450+
451+
Whether to consider noop state during health check
452+
453+
Default value: `false`
454+
455+
##### <a name="-os_patching--patch_batch--run_health_check"></a>`run_health_check`
456+
457+
Data type: `Boolean`
458+
459+
Whether to run a health check before patching
460+
461+
Default value: `false`
462+
463+
##### <a name="-os_patching--patch_batch--service_enabled"></a>`service_enabled`
464+
465+
Data type: `Boolean`
466+
467+
Whether the puppet service should be enabled during health check
468+
469+
Default value: `true`
470+
471+
##### <a name="-os_patching--patch_batch--service_running"></a>`service_running`
472+
473+
Data type: `Boolean`
474+
475+
Whether the puppet service should be running during health check
476+
477+
Default value: `true`
478+
479+
##### <a name="-os_patching--patch_batch--runinterval"></a>`runinterval`
480+
481+
Data type: `Integer[0]`
482+
483+
The runinterval to use during health check
484+
485+
Default value: `1800`
486+
487+
##### <a name="-os_patching--patch_batch--debug"></a>`debug`
488+
489+
Data type: `Boolean`
490+
491+
Whether to enable debug output
492+
493+
Default value: `false`
494+
495+
### <a name="os_patching--patch_group"></a>`os_patching::patch_group`
496+
497+
Patch nodes collected by a fact group
498+
499+
#### Parameters
500+
501+
The following parameters are available in the `os_patching::patch_group` plan:
502+
503+
* [`group`](#-os_patching--patch_group--group)
504+
* [`patch_in_batches`](#-os_patching--patch_group--patch_in_batches)
505+
* [`batch_size`](#-os_patching--patch_group--batch_size)
506+
* [`run_health_check`](#-os_patching--patch_group--run_health_check)
507+
* [`debug`](#-os_patching--patch_group--debug)
508+
* [`pql_query`](#-os_patching--patch_group--pql_query)
509+
510+
##### <a name="-os_patching--patch_group--group"></a>`group`
511+
512+
Data type: `String[1]`
513+
514+
The fact group name to patch
515+
516+
##### <a name="-os_patching--patch_group--patch_in_batches"></a>`patch_in_batches`
517+
518+
Data type: `Boolean`
519+
520+
Whether to patch nodes in batches
521+
522+
Default value: `true`
523+
524+
##### <a name="-os_patching--patch_group--batch_size"></a>`batch_size`
525+
526+
Data type: `Integer[0]`
527+
528+
The size of each batch if patching in batches
529+
530+
Default value: `15`
531+
532+
##### <a name="-os_patching--patch_group--run_health_check"></a>`run_health_check`
533+
534+
Data type: `Boolean`
535+
536+
Whether to run a health check after patching
537+
538+
Default value: `true`
539+
540+
##### <a name="-os_patching--patch_group--debug"></a>`debug`
541+
542+
Data type: `Boolean`
543+
544+
Whether to enable debug output
545+
546+
Default value: `false`
547+
548+
##### <a name="-os_patching--patch_group--pql_query"></a>`pql_query`
549+
550+
Data type: `String[1]`
551+
552+
The PQL query to retrieve nodes in the group
553+
554+
Default value: `"inventory[certname] { facts.os_patching.group = '${group}'}"`
555+
556+
### <a name="os_patching--patch_pql"></a>`os_patching::patch_pql`
557+
558+
Patch nodes collected by a PQL query
559+
560+
#### Parameters
561+
562+
The following parameters are available in the `os_patching::patch_pql` plan:
563+
564+
* [`pql_query`](#-os_patching--patch_pql--pql_query)
565+
* [`patch_in_batches`](#-os_patching--patch_pql--patch_in_batches)
566+
* [`batch_size`](#-os_patching--patch_pql--batch_size)
567+
* [`run_health_check`](#-os_patching--patch_pql--run_health_check)
568+
* [`debug`](#-os_patching--patch_pql--debug)
569+
570+
##### <a name="-os_patching--patch_pql--pql_query"></a>`pql_query`
571+
572+
Data type: `String[1]`
573+
574+
The PQL query to retrieve nodes to patch
575+
576+
Default value: `'inventory[certname] { facts.os.family = "redhat" }'`
577+
578+
##### <a name="-os_patching--patch_pql--patch_in_batches"></a>`patch_in_batches`
579+
580+
Data type: `Boolean`
581+
582+
Whether to patch nodes in batches
583+
584+
Default value: `true`
585+
586+
##### <a name="-os_patching--patch_pql--batch_size"></a>`batch_size`
587+
588+
Data type: `Integer[0]`
589+
590+
The size of each batch if patching in batches
591+
592+
Default value: `15`
593+
594+
##### <a name="-os_patching--patch_pql--run_health_check"></a>`run_health_check`
595+
596+
Data type: `Boolean`
597+
598+
Whether to run a health check after patching
599+
600+
Default value: `true`
601+
602+
##### <a name="-os_patching--patch_pql--debug"></a>`debug`
603+
604+
Data type: `Boolean`
605+
606+
Whether to enable debug output
607+
608+
Default value: `false`
609+

lib/facter/os_patching.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,5 +302,21 @@
302302
data['blocked_reasons'] = blocked_reasons
303303
data
304304
end
305+
306+
chunk(:group) do
307+
data = {}
308+
groupfile = os_patching_dir + '/group'
309+
if File.file?(groupfile)
310+
group = File.open(groupfile, 'r').to_a
311+
line = group.last.chomp
312+
matchdata = line.match(/^(.*)$/)
313+
if matchdata[0]
314+
data['group'] = matchdata[0]
315+
end
316+
else
317+
data['group'] = 'default'
318+
end
319+
data
320+
end
305321
end
306322
end

manifests/init.pp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@
9696
# @param ensure
9797
# `present` to install scripts, cronjobs, files, etc, `absent` to cleanup a system that previously hosted us
9898
#
99+
# @param group
100+
# The group to assign the node for patching purposes.
101+
#
99102
# @example assign node to 'Week3' patching window, force a reboot and create a blackout window for the end of the year
100103
# class { 'os_patching':
101104
# patch_window => 'Week3',
@@ -106,6 +109,7 @@
106109
# 'end' => '2019-01-15T23:59:59+10:00',
107110
# },
108111
# },
112+
# group => 'patching01',
109113
# }
110114
#
111115
# @example An example profile to setup patching, sourcing blackout windows from hiera
@@ -125,6 +129,7 @@
125129
# patch_window => $patch_window,
126130
# reboot_override => $reboot_override,
127131
# blackout_windows => $full_blackout_windows,
132+
# group => 'patching01',
128133
# }
129134
# }
130135
#
@@ -163,8 +168,9 @@
163168
Variant[Enum['absent'], Integer[1,31]] $patch_cron_monthday,
164169
Variant[Enum['absent'], Integer[0,7]] $patch_cron_weekday,
165170
Integer[0,59] $patch_cron_min = fqdn_rand(59),
166-
Optional[String] $patch_window = undef,
171+
Optional[Pattern[/^[A-Za-z0-9\-_ ]+$/]] $patch_window = undef,
167172
Optional[Hash] $blackout_windows = undef,
173+
Optional[Pattern[/^[A-Za-z0-9\-_ ]+$/]] $group = undef,
168174
) {
169175
# None tunable
170176
$cache_dir = lookup('os_patching::cache_dir',Stdlib::Absolutepath,first,undef)
@@ -207,10 +213,6 @@
207213
default => 'absent',
208214
}
209215

210-
if ($patch_window and $patch_window !~ /[A-Za-z0-9\-_ ]+/ ) {
211-
fail('The patch window can only contain alphanumerics, space, underscore and dash')
212-
}
213-
214216
file { $cache_dir:
215217
ensure => $ensure_dir,
216218
force => true,
@@ -243,6 +245,11 @@
243245
default => 'absent'
244246
}
245247

248+
$group_ensure = ($ensure == 'present' and $group) ? {
249+
true => 'file',
250+
default => 'absent',
251+
}
252+
246253
file { "${cache_dir}/patch_window":
247254
ensure => $patch_window_ensure,
248255
content => $patch_window,
@@ -258,6 +265,11 @@
258265
notify => Exec[$fact_exec],
259266
}
260267

268+
file { "${cache_dir}/group":
269+
ensure => $group_ensure,
270+
content => $group,
271+
}
272+
261273
$reboot_override_ensure = ($ensure == 'present' and $reboot_override) ? {
262274
true => 'file',
263275
default => 'absent',
@@ -314,6 +326,7 @@
314326
"${cache_dir}/patch_window",
315327
"${cache_dir}/reboot_override",
316328
"${cache_dir}/blackout_windows",
329+
"${cache_dir}/group",
317330
],
318331
}
319332
}

metadata.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
{
2424
"name": "puppetlabs-cron_core",
2525
"version_requirement": ">= 1.3.0 < 2.0.0"
26+
},
27+
{
28+
"name": "puppet-puppet_health_check",
29+
"version_requirement": ">= 0.9.0 < 1.0.0"
2630
}
2731
],
2832
"operatingsystem_support": [

plans/patch_after_healthcheck.pp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,12 @@
99
) {
1010
# Run an initial health check to make sure the target nodes are ready
1111

12-
$health_checks = run_task('puppet_health_check::agent_health',
13-
$nodes,
12+
$health_checks = run_task('puppet_health_check::agent_health', $nodes,
1413
target_noop_state => $noop_state,
1514
target_service_enabled => true,
1615
target_service_running => true,
1716
target_runinterval => $runinterval,
18-
'_catch_errors' => true,
17+
_catch_errors => true,
1918
)
2019

2120
$nodes_to_patch = $health_checks.filter | $items | { $items.value['state'] == 'clean' }

0 commit comments

Comments
 (0)