Puppet Class: profile::wmcs::metricsinfra::prometheus_manager

Defined in:
modules/profile/manifests/wmcs/metricsinfra/prometheus_manager.pp

Overview

Parameters:

  • db_hostname (String) (defaults to: lookup('profile::wmcs::metricsinfra::prometheus_manager::db_hostname', {default_value => 'wu5emp5wblz.svc.trove.eqiad1.wikimedia.cloud'}))
  • db_database (String) (defaults to: lookup('profile::wmcs::metricsinfra::prometheus_manager::db_database', {default_value => 'prometheusconfig'}))
  • db_user_username (String) (defaults to: lookup('profile::wmcs::metricsinfra::prometheus_manager::db_user_username', {default_value => 'configuser'}))
  • db_user_password (String) (defaults to: lookup('profile::wmcs::metricsinfra::prometheus_manager::db_user_password'))
  • db_admin_username (String) (defaults to: lookup('profile::wmcs::metricsinfra::prometheus_manager::db_admin_username', {default_value => 'configadmin'}))
  • db_admin_password (String) (defaults to: lookup('profile::wmcs::metricsinfra::prometheus_manager::db_admin_password'))


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
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
# File 'modules/profile/manifests/wmcs/metricsinfra/prometheus_manager.pp', line 1

class profile::wmcs::metricsinfra::prometheus_manager (
    String $db_hostname       = lookup('profile::wmcs::metricsinfra::prometheus_manager::db_hostname', {default_value => 'wu5emp5wblz.svc.trove.eqiad1.wikimedia.cloud'}),
    String $db_database       = lookup('profile::wmcs::metricsinfra::prometheus_manager::db_database', {default_value => 'prometheusconfig'}),
    String $db_user_username  = lookup('profile::wmcs::metricsinfra::prometheus_manager::db_user_username', {default_value => 'configuser'}),
    String $db_user_password  = lookup('profile::wmcs::metricsinfra::prometheus_manager::db_user_password'),
    String $db_admin_username = lookup('profile::wmcs::metricsinfra::prometheus_manager::db_admin_username', {default_value => 'configadmin'}),
    String $db_admin_password = lookup('profile::wmcs::metricsinfra::prometheus_manager::db_admin_password'),
) {
    $gitdir = '/var/lib/git'
    $clone_dir = "${gitdir}/cloud/metricsinfra/prometheus-manager"
    $venv_dir = "${clone_dir}/venv"
    $config_dir = '/etc/prometheus-manager'
    $config_file = "${config_dir}/config.yaml"
    $metrics_dir = '/run/prometheus-manager-metrics'

    wmflib::dir::mkdir_p("${gitdir}/cloud/metricsinfra")

    file { $config_dir:
        ensure => directory,
    }

    $config = {
        'DATABASE' => {
            'HOST'     => $db_hostname,
            'DATABASE' => $db_database,
            'USER'     => {
                'USERNAME' => $db_user_username,
                'PASSWORD' => $db_user_password,
            },
            'ADMIN'    => {
                'USERNAME' => $db_admin_username,
                'PASSWORD' => $db_admin_password,
            },
        },
        'SQLALCHEMY_ENGINE_OPTIONS' => {
            # trove tends to kill db sessions at 120s of age,
            # unlike default mariadb at 300s
            'pool_recycle' => 90,
        },
    }

    file { $config_file:
        ensure  => file,
        content => to_yaml($config),
        notify  => Uwsgi::App['prometheus-manager'],
    }

    # Needed for prometheus exporter to share metrics between uwsgi processes
    file { $metrics_dir:
        ensure => directory,
        owner  => 'www-data',
        group  => 'www-data',
    }
    systemd::tmpfile { 'prometheus-manager-shared-metrics':
        content => "d ${metrics_dir} 0755 www-data www-data",
    }

    # TODO: better deployment model (scap, debian, so on) - T288052
    git::clone { 'cloud/metricsinfra/prometheus-manager':
        ensure    => latest,
        directory => $clone_dir,
        owner     => 'www-data',
        group     => 'www-data',
        notify    => [
            Uwsgi::App['prometheus-manager'],
            Exec['prometheus-manager-venv-requirements'],
            Exec['prometheus-manager-migrate'],
        ],
    }

    # the software uses some packages not in debian repos,
    # so we need to do some ugly venv trickery (based on Quarry's manifests).
    # the relevant pypi-only packages are as of writing:
    #  * flask-alembic
    #  * prometheus-flask-exporter
    #  * sqlalchemy-json
    ensure_packages(['python3-venv'])
    exec { 'prometheus-manager-venv':
        user    => 'www-data',
        command => "/usr/bin/python3 -m venv ${venv_dir}",
        creates => $venv_dir,
        require => Git::Clone['cloud/metricsinfra/prometheus-manager'],
        notify  => Exec['prometheus-manager-venv-update-pip-wheel'],
    }
    exec { 'prometheus-manager-venv-update-pip-wheel':
        user        => 'www-data',
        command     => "${venv_dir}/bin/pip install -U pip wheel",
        notify      => Exec['prometheus-manager-venv-requirements'],
        refreshonly => true,
    }
    exec { 'prometheus-manager-venv-requirements':
        user        => 'www-data',
        command     => "${venv_dir}/bin/pip install -r ${clone_dir}/requirements.txt",
        notify      => Uwsgi::App['prometheus-manager'],
        refreshonly => true,
    }

    $env = [
        # fix prometheus exporter for multiple uwsgi processes/workers
        "PROMETHEUS_MULTIPROC_DIR=${metrics_dir}",

        # location of our config file
        "PROMETHEUS_MANAGER_CONFIG_PATH=${config_file}",
    ]

    uwsgi::app { 'prometheus-manager':
        settings => {
            uwsgi => {
                'plugins'   => 'python3',
                'socket'    => '/run/uwsgi/prometheus_manager.sock',
                'wsgi-file' => "${clone_dir}/wsgi.py",
                'chdir'     => $clone_dir,
                'callable'  => 'app',
                'master'    => true,
                'processes' => 4,
                'venv'      => $venv_dir,
                'env'       => $env,
            },
        },
    }

    # again, a better tool would be nice for deployment
    # automatically run database migrations after git updates
    $migrate_env = $env + [
        # TODO: why is this needed, I have no clue
        "PYTHONPATH=${clone_dir}"
    ]
    exec { 'prometheus-manager-migrate':
        command     => "${venv_dir}/bin/python3 scripts/pm-migrate",
        cwd         => $clone_dir,
        environment => $migrate_env,
        user        => 'www-data',
        require     => [
            File[$config_file],
            Git::Clone['cloud/metricsinfra/prometheus-manager'],
        ],
        refreshonly => true,
    }

    nginx::site { 'prometheus-manager-web-nginx':
        require => Uwsgi::App['prometheus-manager'],
        content => template('profile/wmcs/metricsinfra/configserver/prometheus-manager.nginx.erb'),
    }
}