Puppet Class: jupyterhub::server

Defined in:
modules/jupyterhub/manifests/server.pp

Overview

SPDX-License-Identifier: Apache-2.0

Class: jupyterhub::server

Sets up a JupyterHub server for WMF that spawns jupyterhub-singleuser processes in conda environemnts with Systemd. Uses the conda-analytics package as the base environment from which to spawn new user conda environments.

NOTE: This class is deprecating the jupyterhub class in init.pp

Parameters

config

A hash of string key,val pairs to provide to jupyterhub_config.py. These are better documented in jupyterhub_config.py.

Parameters:

  • config (Any) (defaults to: { 'port' => '8880' })


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
# File 'modules/jupyterhub/manifests/server.pp', line 15

class jupyterhub::server (
    $config = {
        'port' => '8880'
    }
) {
    Class['conda_analytics'] -> Class['jupyterhub::server']

    # TODO: 'jupyterhub' is defined by the deprecated jupyterhub class in init.pp.
    # Rename it to 'jupyterhub' here when we remove the other one.
    $service_name = 'jupyterhub-conda'

    $base_path           = "/srv/${service_name}"
    $data_path           = "${base_path}/data"
    $config_path         = "/etc/${service_name}"
    $notebook_config_path= '/etc/jupyter'

    file { [$base_path, $data_path, $config_path, $notebook_config_path]:
        ensure => 'directory',
    }

    # spawners.py contains our custom CondaEnvProfilesSpawner.
    file { "${config_path}/spawners.py":
        source => 'puppet:///modules/jupyterhub/config/spawners.py',
        mode   => '0444',
    }

    # This launches jupyterhub-singleuser from the specified conda env.
    file { "${config_path}/jupyterhub-singleuser-conda-env.sh":
        source => 'puppet:///modules/jupyterhub/config/jupyterhub-singleuser-conda-env.sh',
        mode   => '0555',
    }

    $default_config = {
        'conda_base_env_prefix' => $::conda_analytics::prefix,
        'cookie_secret_file'    => "${data_path}/jupyterhub_cookie_secret",
        'db_url'                => "sqlite:///${data_path}/jupyterhub.sqlite.db",
        'proxy_pid_file'        => "${data_path}/jupyterhub-proxy.pid",
    }

    # This will be rendered as key,val pairs in a dict in jupyterhub_config.py.
    $jupyterhub_config = merge(
        $default_config,
        $config
    )

    # Render the jupyterhub_config.py template.
    $jupyterhub_config_file = "${config_path}/jupyterhub_config.py"
    file { $jupyterhub_config_file:
        content => template('jupyterhub/config/jupyterhub_config.py.erb'),
        mode    => '0444',
    }

    # Generate a cookie secret.
    exec { 'jupyterhub_cookie_secret_generate':
        command     => "/usr/bin/openssl rand -hex 32 > ${data_path}/jupyterhub_cookie_secret",
        creates     => "${data_path}/jupyterhub_cookie_secret",
        environment => "RANDFILE=${data_path}/.rnd",
        umask       => '0377',
        user        => 'root',
        group       => 'root',
        require     => File[$data_path],
    }

    # jupyter_notebook_config.py configures global settings for all user Notebook Servers.
    # Currently this only configures the Notebook Terminal app to work nicely with
    # a sourced stacked conda environment.
    $jupyter_notebook_config_file = "${notebook_config_path}/jupyter_notebook_config.py"
    file { $jupyter_notebook_config_file:
        source => 'puppet:///modules/jupyterhub/config/jupyter_notebook_config.py',
        mode   => '0444',
    }

    systemd::syslog { $service_name:
        readable_by            => 'group',
        base_dir               => '/var/log',
        owner                  => 'root',
        group                  => 'root',
        force_stop             => true,
        programname_comparison => 'isequal',
    }

    systemd::service { $service_name:
        content   => systemd_template($service_name),
        restart   => true,
        subscribe => [
            File[$jupyterhub_config_file],
            File["${config_path}/spawners.py"],
            Exec['jupyterhub_cookie_secret_generate']
        ],
        require   => [
            Systemd::Syslog[$service_name],
        ]
    }
}