Puppet Class: puppetmaster::gitclone

Defined in:
modules/puppetmaster/manifests/gitclone.pp

Summary

This class handles the repositories from which the puppetmasters pull

Overview

Parameters:

  • secure_private (Boolean) (defaults to: true)

    If false, /etc/puppet/private will be labs/private.git. Otherwise, some magic is done to have local repositories and sync between puppetmasters.

  • is_git_master (Boolean) (defaults to: false)

    If True, the git private repository here will be considered a master.

  • prevent_cherrypicks (Boolean) (defaults to: true)

    If true, setup git hooks to prevent manual modification of the git repos.

  • use_r10k (Boolean) (defaults to: false)

    If true, use r10k

  • enable_netbox

    If true, enable netbox repos

  • user (String[1]) (defaults to: 'gitpuppet')

    The user which should own the git repositories

  • group (String[1]) (defaults to: 'gitpuppet')

    The group which should own the git repositories

  • netbox_hiera_enable (Boolean) (defaults to: false)

    add the netbox-hiera repo

  • netbox_hiera_source (Stdlib::HTTPUrl) (defaults to: 'https://netbox-exports.wikimedia.org/netbox-hiera')

    The git source of the nebox hiera repo

  • netbox_hiera_path (Stdlib::Unixpath) (defaults to: '/srv/netbox-hiera')

    The repo path pf the nebox hiera repo

  • servers (Hash[String, Puppetmaster::Backends]) (defaults to: {})

    list of puppetmaster backend servers

  • r10k_sources (Hash[String, Puppetmaster::R10k::Source]) (defaults to: {})

    r10k_sources configuration



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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'modules/puppetmaster/manifests/gitclone.pp', line 16

class puppetmaster::gitclone(
    Boolean                                  $secure_private      = true,
    Boolean                                  $is_git_master       = false,
    Boolean                                  $prevent_cherrypicks = true,
    Boolean                                  $use_r10k            = false,
    Boolean                                  $netbox_hiera_enable = false,
    Stdlib::HTTPUrl                          $netbox_hiera_source = 'https://netbox-exports.wikimedia.org/netbox-hiera',
    Stdlib::Unixpath                         $netbox_hiera_path   = '/srv/netbox-hiera',
    String[1]                                $user                = 'gitpuppet',
    String[1]                                $group               = 'gitpuppet',
    Hash[String, Puppetmaster::Backends]     $servers             = {},
    Hash[String, Puppetmaster::R10k::Source] $r10k_sources        = {}
){

    include puppetmaster
    $is_master = $servers.has_key($facts['networking']['fqdn'])

    class  { 'puppetmaster::base_repo':
        gitdir   => $puppetmaster::gitdir,
        gitowner => $user,
    }

    file {"${puppetmaster::gitdir}/operations/puppet/.git/hooks/post-merge":
        ensure  => absent,
    }
    file {
        default:
            ensure  => stdlib::ensure($prevent_cherrypicks, 'file'),
            owner   => $user,
            group   => $group,
            mode    => '0550',
            require => Git::Clone['operations/puppet'];
        "${puppetmaster::gitdir}/operations/puppet/.git/hooks/pre-commit":
            source  => 'puppet:///modules/puppetmaster/git/pre-commit';
        "${puppetmaster::gitdir}/operations/puppet/.git/hooks/pre-merge":
            source  => 'puppet:///modules/puppetmaster/git/pre-merge';
        "${puppetmaster::gitdir}/operations/puppet/.git/hooks/pre-rebase":
            source  => 'puppet:///modules/puppetmaster/git/pre-rebase';
        "${puppetmaster::gitdir}/operations/software/.git/hooks/pre-commit":
            source  => 'puppet:///modules/puppetmaster/git/pre-commit',
            require => Git::Clone['operations/software'];
    }
    file {$puppetmaster::volatiledir:
        ensure => directory,
        owner  => 'root',
        group  => 'puppet',
        mode   => '0750';
    }

    if $secure_private {
        $private_repo_dir = '/srv/private'
        # Set up private repo.
        # Note that puppet does not actually clone the repo -- puppetizing that
        # turns out to be a big, insecure mess.
        #
        # However, it is enough to push from the private repo master
        # in order to create the repo.

        # on any master for private data,
        # /srv/private contains the actual repository.
        # On the non-masters, it is a bare git repo used only to receive
        # the push from upstream

        if $is_git_master {
            file { $private_repo_dir:
                ensure  => directory,
                owner   => $user,
                group   => $group,
                mode    => '0640', # Will become 0755 for dir automatically
                recurse => true,
            }

            # On a private master, /srv/private is a real repository
            exec { "${private_repo_dir} init":
                command => '/usr/bin/git init',
                user    => 'root',
                group   => 'root',
                cwd     => $private_repo_dir,
                creates => "${private_repo_dir}/.git",
                require => File[$private_repo_dir],
            }
            # Ssh wrapper to use the gitpuppet private key
            file { "${private_repo_dir}/.git/ssh_wrapper.sh":
                ensure  => file,
                source  => 'puppet:///modules/puppetmaster/git/private/ssh_wrapper.sh',
                owner   => 'root',
                group   => 'root',
                mode    => '0555',
                require => Exec['/srv/private init'],
            }

            # add config and pre-commit hook to perform yamllint on the hieradata dir
            ensure_packages(['yamllint'])
            $yamllint_conf_file = '/etc/puppet/yamllint.yaml'
            file {'/etc/puppet/yamllint.yaml':
                ensure => file,
                source => 'puppet:///modules/puppetmaster/git/yamllint.yaml',
            }
            file { "${private_repo_dir}/.git/hooks/pre-commit":
                ensure  => file,
                owner   => $user,
                group   => $group,
                mode    => '0550',
                source  => 'puppet:///modules/puppetmaster/private-repo-pre-commit.sh',
                require => Exec['/srv/private init'],
            }

            file { "${private_repo_dir}/.git/hooks/commit-msg":
                ensure  => file,
                source  => 'puppet:///modules/puppetmaster/git/private/commit-msg-master',
                owner   => $user,
                group   => $group,
                mode    => '0550',
                require => Exec['/srv/private init'],
            }

            # Syncing hooks
            # This hook updates /var/lib and pushes changes to the backend workers
            $puppet_servers = wmflib::role::hosts('puppetserver')
            file { "${private_repo_dir}/.git/hooks/post-commit":
                ensure  => file,
                content => template('puppetmaster/git-master-postcommit.erb'),
                owner   => $user,
                group   => $group,
                mode    => '0550',
                require => Exec['/srv/private init'],
            }

            # Post receive script in case the push is from another master
            # This will reset to head, and transmit data to /var/lib
            file { "${private_repo_dir}/.git/hooks/post-receive":
                source  => 'puppet:///modules/puppetmaster/git/private/post-receive-master',
                owner   => $user,
                group   => $group,
                mode    => '0550',
                require => File['/srv/private'],
            }
            file { "${private_repo_dir}/.git/config":
                source  => 'puppet:///modules/puppetmaster/git/private/config',
                owner   => $user,
                group   => $group,
                mode    => '0550',
                require => File['/srv/private'],
            }
        } else {
            puppetmaster::gitprivate { $private_repo_dir:
                bare     => true,
                dir_mode => '0700',
                owner    => $user,
                group    => $group,
            }

            # This will transmit data to /var/lib...
            file { "${private_repo_dir}/hooks/post-receive":
                source  => 'puppet:///modules/puppetmaster/git/private/post-receive',
                owner   => $user,
                group   => $group,
                mode    => '0550',
                require => Puppetmaster::Gitprivate[$private_repo_dir],
            }
        }

        # What is in the private repo must also be in operations/private in
        # /var/lib/git...

        $private_dir = "${puppetmaster::gitdir}/operations/private"

        puppetmaster::gitprivate { $private_dir:
            origin   => $private_repo_dir,
            owner    => $user,
            group    => 'puppet',
            dir_mode => '0750',
        }

        if $is_git_master {
            Exec["${private_repo_dir} init"] -> Puppetmaster::Gitprivate[$private_dir]
        } else {
            Puppetmaster::Gitprivate[$private_repo_dir] -> Puppetmaster::Gitprivate[$private_dir]
        }

        # ...and linked to /etc/puppet
        file { '/etc/puppet/private':
                ensure => link,
                target => "${puppetmaster::gitdir}/operations/private",
                force  => true;
        }
    } else {
        file { '/etc/puppet/private':
            ensure => link,
            target => "${puppetmaster::gitdir}/labs/private",
            force  => true,
        }
    }

    # The labs/private repo isn't used by production
    #  puppet, but it is maintained by puppet-merge
    #  so we need a check-out on the frontends.
    if $is_master or $::realm == 'labs' {
        file { '/var/lib/git/labs':
            ensure => directory,
            owner  => $user,
            group  => $group,
            mode   => '0755',
        }
        git::clone { 'labs/private':
            require   => File["${puppetmaster::gitdir}/labs"],
            owner     => $user,
            group     => $group,
            directory => "${puppetmaster::gitdir}/labs/private",
        }
    } else {
        file { '/var/lib/git/labs':
            ensure => absent,
            force  => true,
        }
    }

    git::clone {'operations/software':
        require   => File["${puppetmaster::gitdir}/operations"],
        owner     => $user,
        group     => $group,
        directory => "${puppetmaster::gitdir}/operations/software",
        origin    => 'https://gerrit.wikimedia.org/r/operations/software';
    }

    $link_sub_dirs = [ 'templates', 'files', 'manifests', 'modules', 'vendor_modules', 'hieradata', 'environments']
    if $use_r10k {
        $link_sub_dirs.each |$sub_dir| {
            file { "/etc/puppet/${sub_dir}":
                ensure => absent,
            }
        }
        class { 'puppetmaster::r10k':
            sources => $r10k_sources,
        }
    } else {
        # These symlinks will allow us to use /etc/puppet for the puppetmaster to
        # run out of.
        $link_sub_dirs.each |$sub_dir| {
            file { "/etc/puppet/${sub_dir}":
                ensure => link,
                target => "${puppetmaster::gitdir}/operations/puppet/${sub_dir}",
                force  => true,
            }
        }
    }
    if $netbox_hiera_enable {
        git::clone {'netbox-hiera':
            owner     => $user,
            group     => $group,
            directory => $netbox_hiera_path,
            origin    => $netbox_hiera_source,
        }
        # TODO: we should template the hiera yaml file to avoid this
        file { '/etc/puppet/netbox':
            ensure => link,
            target => $netbox_hiera_path,
        }
    }
}