Script to check for deprecated or newer available runtimes and services versions

Overview

Every time you redeploy an environment, the script will:

  • check the versions of your project’s services and runtimes.
  • compare those versions with the Platform.sh registry.
  • send a Slack notification about the ones which you need to upgrade.

Colors

  • Red: 1 or more service is deprecated.
  • Orange: 1 or more service is not at the latest version.
  • Green: All services are at the latest version.

Script

/**
 * Returns a key/value object containing all variables relevant for the activity.
 *
 * That includes project level variables, plus any variables visible for
 * the relevant environment for the activity, if any.
 *
 * Note that JSON-encoded values will show up as a string, and need to be
 * decoded with JSON.parse().
 */
function variables() {
    var vars = {};
    activity.payload.deployment.variables.forEach(function(variable) {
        vars[variable.name] = variable.value;
    });
    return vars;
}

/**
 * Sends a color-coded formatted message to Slack.
 *
 * You must first configure a Platform.sh variable named "SLACK_URL".
 * That is the group and channel to which the message will be sent.
 *
 * To control what events it will run on, use the --events switch in
 * the Platform.sh CLI.
 *
 * @param {string} title
 *   The title of the message block to send.
 * @param {string} message
 *   The message body to send.
 */
function sendSlackMessage(title, message) {
    if ((new Date).getDay() === 5) {
        message += '\r\nCongratulations for deploying on a Friday! :calendar:';
    }
    var body = {
        'attachments': [{
            'title': title,
            'text': message,
            'color': color,
        }],
    };
    var url = variables()['SLACK_URL'];
    if (!url) {
        throw new Error('You must define a SLACK_URL project variable.');
    }
    var resp = fetch(url,{
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
        },
        body: JSON.stringify(body),
    });
    if (!resp.ok) {
        console.log('[LOG] Sending slack message failed: ' + resp.body.text());
    }
}

/**
 * Compare the services and runtimes versions with the Platform.sh public registry.
 *
 * @param {json} services
 *   The services/runtimes that your project is using within the activity.payload.deployment payload.
 * @param {json} registry
 *   The Platform.sh registry available at: https://docs.platform.sh/registry/images/registry.json
 */
function compareVersions(services, registry) {
    var results = Object.keys(services).map(function(serviceName) {
        var service = services[serviceName];
        var s = service.type.split(':');
        var type = s[0];
        var version = s[1];
        var registryService = registry[type];
        var name = registryService.name;
        if(!registryService) {
            return 'Unsupported '+ type;
        }
        var versions = registryService.versions;
        
        // Check for supported versions
        var indexOfSupported = versions.supported.indexOf(version);
        if(indexOfSupported !== -1) {
            var resp = 'Your ' + name + ' ' + version + ' is the most recent one.\n';
            if(indexOfSupported < (versions.supported.length - 1)) {
                if (color == 'good') { color = 'warning' };
                resp = 'Your ' + name + ' ' + version + ' is not the latest, you can upgrade to a newer version: ' + versions.supported[versions.supported.length - 1] + '.\n';
            }
            return resp;
        }

        // Check for deprecated versions
        var indexOfDeprecated = versions.deprecated.indexOf(version);
        if(indexOfDeprecated !== -1) {
            color = 'danger';
            return 'Your ' + name + ' ' + version + ' is deprecated, please upgrade to a newer version: ' + versions.supported + '.\n';
        }
        return  'Your ' + name + ' ' + version + ' is not supported.';
    });
    return results;
}

// @TODO: Remove the temporary fix when the official registry can be fetched entirely.
var res = fetch('https://master-7rqtwti-ms5ebigtfuhc6.eu-3.platformsh.site', {headers: {'content-type': 'application/json'}});
if (!res.ok) {
    console.log('[LOG] Failed to load the Platform.sh registry.');
}

var color = 'good';
var registryPayload = res.json();
var message = compareVersions(activity.payload.deployment.webapps, registryPayload).join('');
message += compareVersions(activity.payload.deployment.services, registryPayload).join('');
console.log(message);
var title = activity.text;
sendSlackMessage(title, message);

Caveat

This script is using a temporary registry (https://master-7rqtwti-ms5ebigtfuhc6.eu-3.platformsh.site) until a fix is deployed to our Glue server. I’ll update it here when the fix is released.