summaryrefslogtreecommitdiff
path: root/sitemodules/profiles/manifests/puppet_server.pp
blob: f555193df38d9f2f1f2cc1e62d27ae6e16c4a1f9 (plain)
1
2
3
4
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
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
# Class: profiles::puppet_server
# ==============================
#
# This class takes care of resources on the puppet server
#
# Parameters
# ----------
#
# @param git_pull_ssh_passphrase passphrase to use for the ssh key to pull
#                                new code from the control repository
# @param git_pull_branches       array of branches to be pulled by the
#                                puppet-deploy webhook
# @param git_pull_directories    branch to directory mapping where the puppet
#                                control repository for a branch is checked
#                                out
# @param git_pull_token          token that is valid to trigger the
#                                puppet-deploy webhook
#
# Examples
# --------
#
# @example
#   class roles::myhost {
#     include profiles::puppet_server
#   }
#
# Authors
# -------
#
# Jan Dittberner <jandd@cacert.org>
#
# Copyright
# ---------
#
# Copyright 2018-2020 Jan Dittberner
class profiles::puppet_server (
  String $git_pull_ssh_passphrase,
  String $git_pull_token,
  Array[String] $git_pull_branches           = ["master"],
  Hash[String, String] $git_pull_directories = {
    'master' => '/etc/puppetlabs/code/environments/production'
  },
) {
  package { ['git', 'r10k', 'sshpass', 'webhook']:
    ensure => installed,
  }

  file { '/usr/local/sbin/git-pull-hook':
    ensure => absent,
  }
  file { '/usr/local/sbin/puppet-deploy':
    owner   => 'root',
    group   => 'root',
    mode    => '0750',
    source  => 'puppet:///modules/profiles/puppet_server/puppet-deploy',
    require => [Package['sshpass'], Package['git'], Package['r10k'], Package['webhook']],
  }

  service { 'git-pull-hook':
    ensure => stopped,
    enable => false,
  } ->
  file { '/etc/init.d/git-pull-hook':
    ensure => absent,
  }
  file { '/etc/git-pull-hook.ini':
    ensure => absent,
  }

  file { '/etc/puppet-deploy.ini':
    ensure  => file,
    owner   => 'root',
    group   => 'root',
    mode    => '0400',
    content => epp(
      'profiles/puppet_server/puppet-deploy.ini.epp',
      {
        'ssh_passphrase'  => $git_pull_ssh_passphrase,
        'git_branches'    => $git_pull_branches,
        'git_directories' => $git_pull_directories,
      }
    ),
  }
  file { '/etc/systemd/system/webhook.service':
    ensure => file,
    owner  => 'root',
    group  => 'root',
    mode   => '0644',
    source => 'puppet:///modules/profiles/puppet_server/webhook.service',
  } ~>
  exec { '/usr/bin/systemctl daemon reload':
    refreshonly => true,
  }
  file { '/etc/webhook.conf':
    ensure  => file,
    owner   => 'root',
    group   => 'root',
    mode    => '0400',
    content => epp(
      'profiles/puppet_server/webhook.conf.epp', {
        'token'    => $git_pull_token,
        'branches' => $git_pull_branches,
      }
    ),
  }

  service { 'webhook':
    ensure    => running,
    enable    => true,
    subscribe => [File['/etc/webhook.conf'], File['/usr/local/sbin/puppet-deploy']],
    require   => [
      File['/etc/webhook.conf'], File['/usr/local/sbin/puppet-deploy'],
      File['/etc/puppet-deploy.ini'], File['/etc/systemd/system/webhook.service'],
    ],
  }
}