Defined Type: service::node

Defined in:
puppet/modules/service/manifests/node.pp

Overview

Define: service::node

service::node provides a common wrapper for setting up Node.js services based on service-runner and/or service-template-node. Note that most of the facts listed as parameters to this class are set correctly via the service class and/or Hiera. The only required parameter is the port. Alternatively, config may be given as well if needed.

Parameters

port

Port on which to run the service

config

The individual service's config to use. It can be eaither 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 node service configuration directives. Default: {}

module

The service's start-up module. Services based on service-template-node do not need to change this. Default: ./app.js

entrypoint

If the service's starter module exports a specific function that should be used on start-up, set this parameter to the function's name. Default: ''

script

The script used for starting the service. Default: server.js

git_remote

The git clone URL. Needs to be set only if the service is to be cloned from a source different than Gerrit. Default: undef

log_level

The service's log level. Default: $::service::log_level

environment

Extra environment variables to set in the systemd config template, in a key-value format.

npm_environment

Extra environment variables to set when running npm install as array of KEY=value strings. Default: []

node_version

The Node version to use.

build

Whether to run npm build. Default: false

Examples

To set up a service named myservice on port 8520 and with a templated configuration, use:

service::node { 'myservice':
    port   => 8520,
    config => template('myservice/config.yaml.erb'),
}

Likewise, you can supply the configuration directly as a hash:

service::node { 'myservice':
    port   => 8520,
    config => {
        param1 => 'val1',
        param2 => $myvar
    },
}

Parameters:

  • port (Any)
  • node_version (Any)
  • config (Any) (defaults to: {})
  • module (Any) (defaults to: './app.js')
  • entrypoint (Any) (defaults to: '')
  • script (Any) (defaults to: 'server.js')
  • git_remote (Any) (defaults to: undef)
  • log_level (Any) (defaults to: undef)
  • environment (Any) (defaults to: {})
  • npm_environment (Any) (defaults to: [])
  • build (Any) (defaults to: false)


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
# File 'puppet/modules/service/manifests/node.pp', line 72

define service::node(
    $port,
    $node_version,
    $config          = {},
    $module          = './app.js',
    $entrypoint      = '',
    $script          = 'server.js',
    $git_remote      = undef,
    $log_level       = undef,
    $environment     = {},
    $npm_environment = [],
    $build           = false,
) {

    require ::service

    require ::npm
    require ::npm::globals

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

    # sanity check since a default port cannot be assigned
    unless $port and $port =~ Integer {
        fail('Service port must be specified and must be a number!')
    }

    # set up the remote URL
    $remote = $git_remote ? {
        undef   => sprintf($::git::urlformat, "mediawiki/services/${title}"),
        default => $git_remote
    }

    # the service's location
    $dir = "${::service::root_dir}/${title}"
    # the local log file name
    $log_file = "${::service::log_dir}/${title}.log"
    # set the log level
    $loglev = $log_level ? {
        undef   => $::service::log_level,
        default => $log_level
    }
    # ensure the RB port is defined for the config
    $restbase_port = defined(Class['restbase']) ? {
        true    => $::restbase::port,
        default => 7231,
    }

    # the repo
    git::clone { $title:
        directory => $dir,
        remote    => $remote,
    }

    nvm::install { "${title}_node_${node_version}":
        version => $node_version,
        before  => Npm::Install[$dir],
    }

    # install the dependencies
    npm::install { $dir:
        directory    => $dir,
        environment  => $npm_environment,
        node_version => $node_version,
        require      => Git::Clone[$title],
    }

    # the service's configuration file
    file { "${title}_config_yaml":
        ensure  => present,
        owner   => $::share_owner,
        group   => $::share_group,
        mode    => '0664',
        path    => "${dir}/config.vagrant.yaml",
        content => merge_config(
            template('service/node/config.yaml.erb'),
            $config
        ),
        require => Git::Clone[$title],
    }

    # set the log file's permissions
    file { $log_file:
        ensure => present,
        mode   => '0666',
        owner  => $::share_owner,
        group  => $::share_group,
    }

    # log rotation
    file { "/etc/logrotate.d/${title}":
        content => template('service/logrotate.erb'),
        owner   => 'root',
        group   => 'root',
        mode    => '0444',
    }

    # schedule the service for git-updates via vagrant git-update
    service::gitupdate { $title:
        type    => 'nodejs',
        update  => true,
        restart => true,
    }

    # the service definition
    systemd::service { $title:
        template_name      => 'node',
        service_params     => {
            subscribe => [
                File["${title}_config_yaml"],
                Npm::Install[$dir],
            ],
        },
        epp_template       => true,
        template_variables => {
            title        => $title,
            uptitle      => inline_template('<%= @title.gsub(/[^a-zA-Z0-9_]/, "_").upcase %>'),
            dir          => $dir,
            port         => $port,
            script       => $script,
            environment  => $environment,
            node_version => $node_version,
            build        => $build,
        },
        require            => Git::Clone[$title],
    }
}