Defined Type: consul::watch

Defined in:
manifests/watch.pp

Summary

Sets up Consul watch, to span commands when data changes.

Overview

Parameters:

  • datacenter (Optional) (defaults to: undef)

    String overriding consul’s default datacenter.

  • ensure (Enum['present', 'absent']) (defaults to: present)

    Define availability of watch. Use ‘absent’ to remove existing watches.

  • event_name (Optional) (defaults to: undef)

    Name of an event to watch for.

  • handler (Optional) (defaults to: undef)

    Full path to the script that will be excuted. This parameter is deprecated in Consul 1.0.0

  • args (Optional) (defaults to: undef)

    Arguments to be ‘exec`ed for the watch.

  • key (Optional) (defaults to: undef)

    Watch a specific key.

  • keyprefix (Optional) (defaults to: undef)

    Watch a whole keyprefix

  • passingonly (Optional[Boolean]) (defaults to: undef)

    Watch only those services that are passing healthchecks.

  • service (Optional) (defaults to: undef)

    Watch a particular service

  • service_tag (Optional) (defaults to: undef)

    This actually maps to the “tag” param for service watches. (‘tag` is a puppet builtin metaparameter)

  • state (Optional) (defaults to: undef)

    Watch a state change on a service healthcheck.

  • token (Optional) (defaults to: undef)

    String to override the default token.

  • type (Optional) (defaults to: undef)

    Type of data to watch. (Like key, service, services, nodes)

See Also:



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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'manifests/watch.pp', line 21

define consul::watch (
  Optional $args                    = undef,
  Optional $datacenter              = undef,
  Enum['present', 'absent'] $ensure = present,
  Optional $event_name              = undef,
  Optional $handler                 = undef,
  Optional $key                     = undef,
  Optional $keyprefix               = undef,
  Optional[Boolean] $passingonly    = undef,
  Optional $service                 = undef,
  Optional $service_tag             = undef,
  Optional $state                   = undef,
  Optional $token                   = undef,
  Optional $type                    = undef,
) {
  include consul
  $id = $title

  $basic_hash = {
    'type'       => $type,
    'args'       => $args,
    'handler'    => $handler,
    'datacenter' => $datacenter,
    'token'      => $token,
  }

  if (versioncmp($consul::version, '0.4.0') < 0) {
    fail('Watches are only supported in Consul 0.4.0 and above')
  }

  if (! $handler and ! $args) {
    fail('All watch conditions must have a handler or args list defined')
  }

  if ($handler and $args) {
    fail('Watch conditions cannot have both a handler and args list defined')
  }

  if (! $type ) {
    fail('All watch conditions must have a type defined')
  }

  case $type {
    'key': {
      if (! $key ) {
        fail('key is required for watch type [key]')
      }
      $type_hash = {
        key => $key,
      }
    }
    'keyprefix': {
      if (! $keyprefix ) {
        fail('keyprefix is required for watch type of [keyprefix]')
      }
      $type_hash = {
        prefix => $keyprefix,
      }
    }
    'service': {
      if (! $service ) {
        fail('service is required for watch type of [service]')
      }
      $type_hash = {
        service     => $service,
        tag         => $service_tag,
        passingonly => $passingonly,
      }
    }
    'checks': {
      $type_hash = {
        service => $service,
        state   => $state,
      }
    }
    'event': {
      $type_hash = {
        name => $event_name,
      }
    }
    /(nodes|services)/: {
      $type_hash = {}
    }
    default: {
      fail("${type} is an unrecogonized watch type that is not supported currently")
    }
  }

  $merged_hash = $basic_hash + $type_hash

  $watch_hash = {
    watches => [$merged_hash.filter |$key, $val| { $val =~ NotUndef }],
  }

  file { "${consul::config_dir}/watch_${id}.json":
    ensure  => $ensure,
    owner   => $consul::user_real,
    group   => $consul::group_real,
    mode    => $consul::config_mode,
    content => consul::sorted_json($watch_hash, $consul::pretty_config, $consul::pretty_config_indent),
    notify  => Class['consul::reload_service'],
  }
}