Puppet Plan: peadm::status

Defined in:
plans/status.pp

Summary

Aggregates the status of one or more stacks into a table format or json encoded string and dumps to stdout.

Overview

Examples:

peadm::status($targets, 'table', true, true)

Parameters:

  • targets (TargetSpec)

    These are a list of the primary puppetservers from one or multiple puppet stacks

  • format (Enum[json,table]) (defaults to: 'table')

    The output format to dump to stdout (json or table)

  • summarize (Boolean) (defaults to: true)

    Controls the type of json output to render, defaults to true

  • verbose (Boolean) (defaults to: false)

    Toggles the output to show all the operationally services, can be loads more data

  • colors (Boolean) (defaults to: $format ? { json => false, default => true)

    Toggles the usage of colors, you may want to disable if the format is json



10
11
12
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'plans/status.pp', line 10

plan peadm::status(
  TargetSpec $targets,
  Enum[json,table] $format = 'table',
  Boolean $verbose = false,
  Boolean $summarize = true,
  Boolean $colors = $format ? { json => false, default => true }
) {
  peadm::assert_supported_bolt_version()

  $results = run_task('peadm::infrastatus', $targets, { format => 'json'})
  # returns the data in a hash 
  $stack_status = $results.reduce({}) | $res, $item | {
    $data = $item.value[output]
    $stack_name = $item.target.peadm::certname()
    $status = peadm::determine_status($data, $colors).merge(stack_name => $stack_name )
    $res.merge({ $stack_name => $status })
  }

  $overall_degraded_stacks = $stack_status.filter | $item | { $item[1][status] =~ /degraded/ }
  $overall_failed_stacks = $stack_status.filter | $item | { $item[1][status] =~ /failed/ }
  $overall_passed_stacks = $stack_status.filter | $item | { $item[1][status] =~ /operational/ }

  # determine if the overall status is failed, operationally, or degraded
  if $overall_degraded_stacks.count > 0 {
    $overall_status = peadm::convert_status('degraded', undef, $colors)
  } else {
    $overall_status = peadm::convert_status($overall_failed_stacks.count, $stack_status.count, $colors)
  }

  # produce titles and headers for tables
  $table_title = "Overall Status: ${overall_status}"
  $table_head = ['Stack', 'Status']
  $service_table_head = ['Stack', 'Service', 'Url', 'Status']

  $stack_table_rows = $stack_status.map | $item | { [$item[0], $item[1][status]] }
  $passed_table_rows = $overall_passed_stacks.map | $item | { [$item[0], $item[1][status]] }

  # produce array of degraded or failed services
  $bad_svc_rows = $overall_failed_stacks.merge($overall_degraded_stacks).map | $item | {
    $item[1][failed].map | $svc | {
      [$item[0], *$svc[0].split('/'), peadm::convert_status($svc[1], undef, $colors)]
    }
  }

  # produce array of working services
  $good_svc_rows = $stack_status.map | $item | {
    $item[1][passed].map | $svc | {
      [$item[0], *$svc[0].split('/'), peadm::convert_status($svc[1], undef, $colors)]
    }
  }

  # print to table via out::message or return json output
  if $format == 'table' {
    $summary_table = format::table({title => $table_title,
                  head => $table_head,
                  rows => $stack_table_rows,
                  style => {width => 80}
                  })
    out::message($summary_table)
    unless $bad_svc_rows.empty {
      $failed_table = format::table({title => 'Failed Service Status',
                  head => $service_table_head,
                  rows => $bad_svc_rows[0]
                  })
      out::message($failed_table)
    }

    if $verbose and ! $good_svc_rows.empty {
      $passed_table = format::table({title => 'Operational Service Status',
                  head => $service_table_head,
                  rows => $good_svc_rows[0]
                  })
      out::message($passed_table)
    }
  } else {
    if $summarize {
      $failed = $bad_svc_rows.empty ? { true => [], default => peadm::convert_hash($service_table_head, $bad_svc_rows[0]) }
      $passed = $good_svc_rows.empty ? { true => [], default => peadm::convert_hash($service_table_head, $good_svc_rows[0]) }
      $summary_json = {
        'summary' => {
          'status' => $overall_status,
          'stacks' => $stack_table_rows.hash
        },
        'failed' => $failed,
        'operational' => $passed
      }
      return $summary_json
    } else {
      return $stack_status
    }
  }
}