Puppet Class: consul::run_service

Defined in:
manifests/run_service.pp

Summary

This class is meant to be called from consul. It ensures the service is running

Overview



5
6
7
8
9
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
# File 'manifests/run_service.pp', line 5

class consul::run_service {
  assert_private()
  $service_name = $consul::init_style_real ? {
    'launchd' => 'io.consul.daemon',
    default   => 'consul',
  }

  $service_provider = $consul::init_style_real ? {
    'unmanaged' => undef,
    default     => $consul::init_style_real,
  }

  if $consul::manage_service == true and $consul::install_method != 'docker' {
    if $facts['os']['name'] == 'windows' {
      class { 'consul::windows_service':
        before => Service['consul'],
      }
    }

    service { 'consul':
      ensure   => $consul::service_ensure,
      name     => $service_name,
      enable   => $consul::service_enable,
      provider => $service_provider,
    }
  }

  if $consul::install_method == 'docker' {
    $server_mode = pick($consul::config_hash[server], false)

    if $server_mode {
      $env = ['\'CONSUL_ALLOW_PRIVILEGED_PORTS=\'']
      $docker_command = 'agent -server'
    } else {
      $env = undef
      $docker_command = 'agent'
    }

    docker::run { 'consul':
      image   => "${consul::docker_image}:${consul::version}",
      net     => 'host',
      volumes => ["${consul::config_dir}:/consul/config", "${consul::data_dir}:/consul/data"],
      env     => $env,
      command => $docker_command,
    }
  }

  case $consul::install_method {
    'docker': {
      $wan_command = "docker exec consul consul join -wan ${consul::join_wan}"
      $wan_unless = "docker exec consul consul members -wan -detailed | grep -vP \"dc=${consul::config_hash_real['datacenter']}\" | grep -P 'alive'"  #lint:ignore:140chars
    }
    default: {
      $wan_command = "consul join -wan ${consul::join_wan}"
      $wan_unless = "consul members -wan -detailed | grep -vP \"dc=${consul::config_hash_real['datacenter']}\" | grep -P 'alive'"
    }
  }

  if $consul::join_wan {
    exec { 'join consul wan':
      cwd       => $consul::config_dir,
      path      => [$consul::bin_dir,'/bin','/usr/bin'],
      command   => $wan_command,
      unless    => $wan_unless,
      subscribe => Service['consul'],
    }
  }
}