Skip to content

Instantly share code, notes, and snippets.

@GuGuss
Last active May 29, 2020 14:39
Show Gist options
  • Save GuGuss/09c6fe1c3f4ff44bf2e7246ec2507d80 to your computer and use it in GitHub Desktop.
Save GuGuss/09c6fe1c3f4ff44bf2e7246ec2507d80 to your computer and use it in GitHub Desktop.
Activity Script to compare the deployed services and runtimes versions with the Platform.sh public registry.
/**
* 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: Switch to the official Platform.sh registry: https://docs.platform.sh/registry/images/registry.json
// Temporary fix thanks to @yann!
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);
@GuGuss
Copy link
Author

GuGuss commented May 29, 2020

This generates a Slack notifications like:

Augustin Delaporte redeployed environment Master
Your PHP 7.1 is deprecated, please upgrade to a newer version: 7.2,7.3,7.4.
Your Python 3.7 is not the latest, you can upgrade to a newer version: 3.8.
Your Redis 4.0 is not the latest, you can upgrade to a newer version: 5.0.
Your MariaDB 10.4 is the most recent one.
Your PostgreSQL 10 is not the latest, you can upgrade to a newer version: 12.
Congratulations for deploying on a Friday! :calendar:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment