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. The repository containing the code is assumed to be already deploy by other means.

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

  • 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.

  • 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

  • ensure (Wmflib::Ensure) (defaults to: present) —

    whether the service should be present or absent (default=present)



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

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,
    Boolean                    $firejail               = true,
    Boolean                    $icinga_check           = true,
    Boolean                    $local_logging          = true,
    Array[String]              $sudo_rules             = [],
    Boolean                    $add_logging_config     = true,
    String                     $core_limit             = '0',
    Wmflib::Ensure             $ensure                 = present,
    Optional[Nrpe::Check_http] $nrpe_check_http        = undef,
    # lint:ignore:wmf_styleguide
    String        $contact_groups         = lookup('contactgroups', {'default_value' => 'admins'}),
    # lint:endignore
) {
    # 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 => $ensure == present ? {true => directory, default => absent},
              owner  => 'www-data',
              group  => 'root',
              mode   => '0755',
              before => Uwsgi::App[$title],
          }
          logrotate::conf { $title:
              ensure  => $ensure,
              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 = {}
    }

    # This can't be set to ensure absent as names can conflict with other usages
    # For example /etc/netbox is used for netbox related config/scripts
    if !defined(File["/etc/${title}"]) {
        file { "/etc/${title}":
            ensure => directory,
            owner  => 'root',
            group  => 'root',
            mode   => '0755',
        }
    }

    $base_config = {
        plugins     => 'python3, logfile, logsocket',
        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:
        ensure        => $ensure,
        core_limit    => $core_limit,
        systemd_user  => $systemd_user,
        systemd_group => $systemd_group,
        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}":
                ensure        => $ensure,
                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:
                ensure      => $ensure,
                monitor_url => $monitor_url,
            }
        } elsif $nrpe_check_http {
            nrpe::monitor_service {"uwsgi-nrpe-${title}":
                ensure        => $ensure,
                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:
                ensure        => $ensure,
                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}",
            }
        }
    }
}