Puppet Plan: aptly::cleanup_snapshots

Defined in:
plans/cleanup_snapshots.pp

Summary

Cleanup unreferenced aptly snapshots

Overview

NOTE: This plan should be run against a server, that has aptly installed and executable.

Parameters:

  • targets (TargetSpec)

    Target server to run ‘aptly` command on

  • filter (Optional[String[1]]) (defaults to: undef)

    Only delete unreferenced snapshots matching this regexp pattern

  • noop (Boolean) (defaults to: false)

    Whether to really delete snapshots or just report them

Returns:

  • (ResultSet)

    Set of run_task Results



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'plans/cleanup_snapshots.pp', line 13

plan aptly::cleanup_snapshots(
  TargetSpec $targets,
  Boolean $noop = false,
  Optional[String[1]] $filter = undef,
) {
  $tt = $targets.get_targets
  if $tt.length > 1 {
    fail_plan('This plan cannot deal with more than one target', 'aptly::cleanup_snapshots/too-many-targets', count => $tt.length)
  }

  # Collect published snapshots
  $publish_res = run_task('aptly::publish_list', $targets)
  $published_snapshots = $publish_res.first.value['publish_list'].reduce([]) |$memo, $x| {
    if $x['SourceKind'] == 'snapshot' {
      $memo + $x.get('Sources', []).map |$s| {
        $name = $s['Name']
      }
    } else {
      $memo
    }
  }

  # Collect snapshots, matching the filter (if given)
  $snapshot_res = run_task('aptly::snapshot_list', $targets)
  $snapshots = $snapshot_res.first.value['snapshot_list'].reduce([]) |$memo, $x| {
    $name = $x['Name']
    if $filter {
      if $name =~ $filter {
        $memo + $name
      } else {
        $memo
      }
    } else { # No filter given
      $memo + $name
    }
  }

  $disposed_snapshots = $snapshots - $published_snapshots
  $result = $disposed_snapshots.map |$snapshot| {
    if $noop {
      out::verbose("Deleting snapshot ${snapshot}...")
      Result($tt[0], { snapshot_drop => true, noop => true })
    } else {
      run_task('aptly::snapshot_drop', $targets, name => $snapshot).first
    }
  }

  return ResultSet($result)
}