Defined Type: scap::target

Defined in:
modules/scap/manifests/target.pp

Overview

Define: scap::target

Sets up a scap3 target for a deployment repository. This will include ths scap package and ferm fules, ensure that the $deploy_user has proper sudo rules and public key installed.

Parameters

ensure

present or absent to setup or remove

deploy_user

user that will be used for deployments

key_name

The name of a keyholder ssh key used to access this deployment target. This should correspond to a key which is defined in keyholder::agent (which are defined by hiera data in common::scap::server) Warning: If key_name is left undefined, then you must define the correct ssh::userkey for your deploy_user so that scap can connect to the target from the deployment server with a corresponding key in keyholder.

service_name

service name that should be allowed to be restarted via sudo by deploy_user. Default: undef.

additional_services_names

An array of additional services to apply sudo rules to by deploy_user. Default: []

package_name

the name of the scap3 deployment package Default: $title

manage_user

Specify whether to create a User resource for the $deploy_user. This should be set to false if you have defined the user elsewhere. Default: true

sudo_rules

An array of additional sudo rules pertaining to the service to install on the target node. Default: []

Usage:

scap::target { 'eventlogging/eventlogging':
    deploy_user => 'eventlogging',
}

scap::target { 'dumps/dumps':
    deploy_user => 'datasets',
    manage_user => false,
    key_name    => 'dumps',
}

Parameters:

  • deploy_user (String)
  • key_name (String) (defaults to: $deploy_user)
  • ensure (Wmflib::Ensure) (defaults to: 'present')
  • service_name (Optional[String]) (defaults to: undef)
  • additional_services_names (Array[String]) (defaults to: [])
  • package_name (String) (defaults to: $title)
  • manage_user (Boolean) (defaults to: true)
  • sudo_rules (Array[String]) (defaults to: [])


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
# File 'modules/scap/manifests/target.pp', line 57

define scap::target(
    String $deploy_user,
    String $key_name                            = $deploy_user,
    Wmflib::Ensure   $ensure                    = 'present',
    Optional[String] $service_name              = undef,
    Array[String]    $additional_services_names = [],
    String           $package_name              = $title,
    Boolean          $manage_user               = true,
    Array[String]    $sudo_rules                = [],
) {
    # Include scap3 package and ssh ferm rules.
    include scap
    include scap::ferm

    if !$service_name and !empty($additional_services_names) {
        fail('service_name must be set if additional_services_names is set')
    }

    if $manage_user
    {
        if !defined(Group[$deploy_user]) {
            group { $deploy_user:
                ensure => $ensure,
                system => true,
                before => User[$deploy_user],
            }
        }
        if !defined(User[$deploy_user]) {
            user { $deploy_user:
                ensure => $ensure,
                shell  => '/bin/bash',
                home   => "/var/lib/${deploy_user}",
                system => true,
            }
            file { "/var/lib/${deploy_user}":
                ensure => ensure_directory($ensure),
                owner  => $deploy_user,
                group  => $deploy_user,
                mode   => '0755',
            }
        } else {
            notice("manage_user=true but user ${deploy_user} already defined")
        }
        if !defined(Ssh::Userkey[$deploy_user]) {
            $key_name_safe = regsubst($key_name, '\W', '_', 'G')

            ssh::userkey { $deploy_user:
                ensure  => $ensure,
                content => secret("keyholder/${key_name_safe}.pub"),
            }
        }
    }

    if $::realm == 'labs' {
        if !defined(Security::Access::Config["scap-allow-${deploy_user}"]) {
            # Allow $deploy_user login from scap deployment host.
            # adds an exception in /etc/security/access.conf
            # to work around labs-specific restrictions
            $deployment_host = lookup('scap::deployment_server')
            $deployment_ip = ipresolve($deployment_host, 4, $::nameservers[0])
            security::access::config { "scap-allow-${deploy_user}":
                ensure   => $ensure,
                content  => "+ : ${deploy_user} : ${deployment_ip}\n",
                priority => 60,
            }
        }
    }

    # Allow deploy user user to sudo -u $user, and to sudo /usr/sbin/service
    # if $service_name is defined.
    #
    # Two sets of privileges are defined: one for scap to able to sudo -u $user,
    # which should be defined only once per node, and another for restarting
    # whichever services are being deployed.
    #
    # NOTE: sudo -u $user is currently needed by scap3.
    # TODO: Remove this when it is no longer needed.

    if !defined(Sudo::User["scap_${deploy_user}"]) {

        sudo::user { "scap_${deploy_user}":
            ensure     => $ensure,
            user       => $deploy_user,
            privileges => ["ALL=(${deploy_user}) NOPASSWD: ALL"],
        }
    }

    if $service_name {
        $all_services_names = array_concat($additional_services_names, $service_name)
        $all_services_names.each |String $svc_name| {
            $service_privileges = [
                "ALL=(root) NOPASSWD: /usr/sbin/service ${svc_name} start",
                "ALL=(root) NOPASSWD: /usr/sbin/service ${svc_name} stop",
                "ALL=(root) NOPASSWD: /usr/sbin/service ${svc_name} restart",
                "ALL=(root) NOPASSWD: /usr/sbin/service ${svc_name} reload",
                "ALL=(root) NOPASSWD: /usr/sbin/service ${svc_name} status",
                "ALL=(root) NOPASSWD: /usr/sbin/service ${svc_name} try-restart",
                "ALL=(root) NOPASSWD: /usr/sbin/service ${svc_name} force-reload",
                "ALL=(root) NOPASSWD: /usr/sbin/service ${svc_name} graceful-stop"
            ]

            if !defined(Sudo::User["scap_${deploy_user}_${svc_name}"]) {
                sudo::user { "scap_${deploy_user}_${svc_name}":
                    ensure     => $ensure,
                    user       => $deploy_user,
                    privileges => $service_privileges,
                }
            }
        }
    }

    if !empty($sudo_rules) {
        $sudo_rule_name = regsubst("scap_sudo_rules_${deploy_user}_${title}", '/', '_', 'G')

        if !defined(Sudo::User[$sudo_rule_name]) {
                sudo::user { $sudo_rule_name:
                    ensure     => $ensure,
                    user       => $deploy_user,
                    privileges => $sudo_rules,
                }
        }
    }

    if $::realm == 'labs' {
        $require_package = $manage_user ? {
            true    => [
                Package['scap'],
                User[$deploy_user],
            ],
            default => Package['scap'],
        }
    } else {
        $require_package = [ Package['scap'], User[$deploy_user] ]
    }

    # Have scap actually deploy the source, restart the service if needed, etc
    # Assume $deploy_user already has sudo permissions because of the block above.
    package { $package_name:
        ensure          => $ensure,
        install_options => [{
            owner => $deploy_user
        }],
        provider        => 'scap3',
        require         => $require_package,
    }

    # XXX: Temporary work-around for switching services from Trebuchet to Scap3
    # The Scap3 provider doesn't touch the target dir if it's already a git repo
    # which means that even after switching the provider we end up with the
    # wrong user (root) owning it. Therefore, as a temporary measure we chown
    # the target dir's parent so that the subsequent invocation of deploy-local
    # is able to create the needed dirs and symlinks
    $chown_user = "${deploy_user}:${deploy_user}"
    $name_array = split($package_name, '/')
    $pkg_root = inline_template(
        '<%= @name_array[0,@name_array.size - 1].join("/") %>'
    )
    $chown_target = "/srv/deployment/${pkg_root}"
    $exec_name = "chown ${chown_target} for ${deploy_user}"
    if !defined(Exec[$exec_name]) {
        if $::realm == 'labs' {
            $require_exec = $manage_user ? {
                true    => [
                    User[$deploy_user],
                    Group[$deploy_user]
                ],
                default => undef,
            }
        } else {
            $require_exec = [User[$deploy_user], Group[$deploy_user]]
        }

        exec { $exec_name:
            command => "/bin/chown -R ${chown_user} ${chown_target}",
            # perform the chown only if root is the effective owner
            onlyif  => "/usr/bin/test -O /srv/deployment/${package_name}",
            require => $require_exec,
        }
    }
}