Defined Type: service::uwsgi

Defined in:
modules/service/manifests/uwsgi.pp

Summary

service::uwsgi provides a common wrapper for setting up python services based on uwsgi on.

Overview

Examples:

service::uwsgi { 'myservice':
    port   => 8520,
    config => {
       'wsgi-file' => "/path/to/wsgi_file"
       chdir       => "path_to_dir",
       venv        => "path_to_virtualenv",
       param1 => 'val1',
       param2 => $myvar
    },
}

Parameters:

  • port (Stdlib::Port)

    Port on which to run the service

  • config (Hash) (defaults to: {})

    The individual service's config to use. It must be a hash of key => value pairs, or a YAML-formatted string. Note that the complete configuration will be assembled using the bits given here and common service configuration directives.

  • systemd_user (String[1]) (defaults to: 'www-data')

    the user to use in the systemd unit

  • systemd_group (String[1]) (defaults to: 'www-data')

    the group to use in the systemd unit

  • no_workers (Integer) (defaults to: $facts['processors']['count'])

    Number of workers to start.

  • healthcheck_url (String) (defaults to: '/_info')

    The url to monitor the service at. 200 OK is the expected answer. If has_spec it true, this is supposed to be the base url for the spec request

  • has_spec (Boolean) (defaults to: false)

    If the service specifies a swagger spec, use it to thoroughly monitor it

  • repo (String) (defaults to: "${title}/deploy")

    The name of the repo to use for deployment. Default: $title/deploy

  • firejail (Boolean) (defaults to: true)

    run in firejail

  • local_logging (Boolean) (defaults to: true)

    Whether to store log entries on the target node as well.

  • icinga_check (Boolean) (defaults to: true)

    Whether to include an Icinga check for monitoring the service.

  • deployment (String) (defaults to: 'scap3')

    What deployment system to use for deploying this service. Note: this parameter will be removed onces ores.wmflabs.org stops using service::uwsgi

  • deployment_user (String) (defaults to: 'deploy-service')

    The user that will own the service code. Only applicable when $deployment ='scap3'.

  • deployment_manage_user (Boolean) (defaults to: true)

    Boolean. Whether or not scap::target manages user. Only applicable when $deployment ='scap3'.

  • sudo_rules (Array[String]) (defaults to: [])

    An array of string representing sudo rules in the sudoers format that you want the service to have.

  • contact_groups (String) (defaults to: lookup('contactgroups', {'default_value' => 'admins'}))

    Contact groups for alerting.

  • add_logging_config (Boolean) (defaults to: true)

    Inject logging configuration into the generated uwsgi config file that will route logs to the Logstash ingest host defined in service::configuration::logstash_host and optionally local files if $local_logging is true. Default: true

  • core_limit (String) (defaults to: '0')

    This setting adds the LimitCore functionality of the uwsgi's systemd unit. Useful when segfaults are happening regularly and a more detailed report is needed. Values: 'unlimited', 'nG' (n is a number of Gigabytes), or '0' for no core.

  • nrpe_check_http (Optional[Nrpe::Check_http]) (defaults to: undef)

    a list of arguments to pass to check_http



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
# File 'modules/service/manifests/uwsgi.pp', line 54

define service::uwsgi(
    Stdlib::Port               $port,
    Hash                       $config                 = {},
    String[1]                  $systemd_user           = 'www-data',
    String[1]                  $systemd_group          = 'www-data',
    Integer                    $no_workers             = $facts['processors']['count'],
    String                     $healthcheck_url        = '/_info',
    Boolean                    $has_spec               = false,
    String                     $repo                   = "${title}/deploy",
    Boolean                    $firejail               = true,
    Boolean                    $icinga_check           = true,
    Boolean                    $local_logging          = true,
    String                     $deployment_user        = 'deploy-service',
    Boolean                    $deployment_manage_user = true,
    String                     $deployment             = 'scap3',
    Array[String]              $sudo_rules             = [],
    Boolean                    $add_logging_config     = true,
    String                     $core_limit             = '0',
    Optional[Nrpe::Check_http] $nrpe_check_http        = undef,
    # lint:ignore:wmf_styleguide
    String        $contact_groups         = lookup('contactgroups', {'default_value' => 'admins'}),
    # lint:endignore
) {
    if $deployment == 'scap3' {
        scap::target { $repo:
            service_name => $title,
            deploy_user  => $deployment_user,
            before       => Uwsgi::App[$title],
            manage_user  => $deployment_manage_user,
            sudo_rules   => $sudo_rules,
        }
    }

    # Import all common configuration
    include service::configuration

    # we do not allow empty names
    unless $title and size($title) > 0 {
        fail('No name for this resource given!')
    }

    # the local log file name
    $local_logdir = "${service::configuration::log_dir}/${title}"
    $local_logfile = "${local_logdir}/main.log"

    if $add_logging_config {
      if $local_logging {
          ensure_resource('file', '/srv/log', {'ensure' => 'directory' })
          file { $local_logdir:
              ensure => directory,
              owner  => 'www-data',
              group  => 'root',
              mode   => '0755',
              before => Uwsgi::App[$title],
          }
          logrotate::conf { $title:
              ensure  => present,
              content => template('service/logrotate.uwsgi.erb'),
          }
          $log_config_local = {
              log-route => ['local .*', 'logstash .*'],
              logger    => [
                  "local file:${local_logfile}",
                  "logstash socket:${service::configuration::logstash_host}:11514",
              ],
          }
      } else {
          $log_config_local = {
              log-route => ['logstash .*'],
              logger    => [
                  "logstash socket:${service::configuration::logstash_host}:11514",
              ],
          }
      }
      $log_config_shared = {
          log-encoder => [
              # lint:ignore:single_quote_string_with_variables
              # Add a timestamps to local log messages
              'format:local [${strftime:%%Y-%%m-%%dT%%H:%%M:%%S}] ${msgnl}',

              # Encode messages to the logstash logger as json datagrams.
              # msgpack would be nicer
              join([
                  'json:logstash {"@timestamp":"${strftime:%%Y-%%m-%%dT%%H:%%M:%%S}","type":"',
                  $title,
                  '","logger_name":"uwsgi","host":"%h","level":"INFO","message":"${msg}"}'], '')
              #lint:endignore
          ],
      }
      $logging_config = deep_merge($log_config_shared, $log_config_local)
    } else {
      # Use the default log routing of uwsgi which will emit events to
      # stdout/stderr with no special formatting. journald will add
      # timestamps.
      $logging_config = {}
    }

    if !defined(File["/etc/${title}"]) {
        file { "/etc/${title}":
            ensure => directory,
            owner  => 'root',
            group  => 'root',
            mode   => '0755',
        }
    }

    $plugins = debian::codename::ge('bullseye') ? {
        true    => 'python3, logfile, logsocket',
        default => 'python, python3, logfile, logsocket',
    }
    $base_config = {
        plugins     => $plugins,
        master      => true,
        http-socket => "0.0.0.0:${port}",
        processes   => $no_workers,
        die-on-term => true,
    }
    $complete_config = deep_merge($base_config, $logging_config, $config)

    uwsgi::app { $title:
        core_limit    => $core_limit,
        systemd_user  => $systemd_user,
        systemd_group => $systemd_group,
        monitoring    => bool2str($icinga_check, 'present','absent'),
        settings      => {
            uwsgi => $complete_config,
        },
    }

    if $icinga_check {
        if $has_spec {
            # Advanced monitoring
            include service::monitoring

            $fact_ip = $facts['networking']['ip']
            $monitor_url = "http://${fact_ip}:${port}${healthcheck_url}"
            nrpe::monitor_service{ "endpoints_${title}":
                description   => "${title} endpoints health",
                nrpe_command  => "/usr/bin/service-checker-swagger -t 5 ${fact_ip} ${monitor_url}",
                contact_group => $contact_groups,
            }
            # we also support smart-releases
            # TODO: Enable has_autorestart
            service::deployment_script { $name:
                monitor_url     => $monitor_url,
            }
        } elsif $nrpe_check_http {
            nrpe::monitor_service {"uwsgi-nrpe-${title}":
                description   => "uWSGI ${title} (http via nrpe)",
                nrpe_command  => wmflib::argparse($nrpe_check_http, '/usr/lib/nagios/plugins/check_http'),
                contact_group => $contact_groups,
                notes_url     => "https://wikitech.wikimedia.org/wiki/Services/Monitoring/${title}",
            }

        } else {
            # Basic monitoring
            monitoring::service { $title:
                description   => $title,
                check_command => "check_http_port_url!${port}!${healthcheck_url}",
                contact_group => $contact_groups,
                notes_url     => "https://wikitech.wikimedia.org/wiki/Services/Monitoring/${title}",
            }
        }
    }
}