Skip to content

Instantly share code, notes, and snippets.

@mattapperson
Last active October 14, 2015 19:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattapperson/57cab7520bec1d1ab190 to your computer and use it in GitHub Desktop.
Save mattapperson/57cab7520bec1d1ab190 to your computer and use it in GitHub Desktop.
rancher-deploy - A quick script I wrote to deploy services to rancher in one command... will finish it up and deploy via NPM soon...
#!/usr/bin/env node
process.env.RANCHER_URL = '<your project URL here>';
process.env.RANCHER_ACCESS_KEY = '<your access key here>';
process.env.RANCHER_SECRET_KEY = '<your secret key here>';
var spawn = require('child_process').spawn;
var exec = require('child_process').exec;
var path = require('path');
var fs = require('fs')
var readYaml = require('read-yaml');
var semver = require('semver');
var writeYaml = require('write-yaml');
var request = require('request');
var generate = require('project-name-generator');
var service = process.argv[2];
var version = process.argv[3];
var latestBuildName;
request.get(`${process.env.RANCHER_URL}/services`, {
'auth': {
'user': process.env.RANCHER_ACCESS_KEY,
'pass': process.env.RANCHER_SECRET_KEY,
'sendImmediately': true
}
}, function(err, response, body) {
var services = JSON.parse(body).data;
var thisAppsActiveServiceName = services.filter(function(loadedService) {
return loadedService.name.startsWith(service);
})[0];
thisAppsActiveServiceName = thisAppsActiveServiceName ? thisAppsActiveServiceName.name : undefined;
var thisAppsVersionNumber;
if(thisAppsActiveServiceName) {
thisAppsVersionNumber = thisAppsActiveServiceName.match(/([0-9].[0-9].[0-9])(-?[^-]+)?/);
}
readYaml('docker-compose.yml', function(err, data) {
if (err) throw err;
if(!data[service]) throw "invalid docker-compose.yml, no service named " + service + " was found";
if(!version) {
version = '00' + generate({ words: 2 }).raw.join('');
}
latestBuildName = `${service}${version}`;
// if the latest tag is not deployed yet
if((thisAppsActiveServiceName && !thisAppsVersionNumber) ||
thisAppsActiveServiceName && thisAppsVersionNumber && semver.satisfies(version, `>${thisAppsVersionNumber[0]}`)) {
console.log('Upgrading app...')
data[latestBuildName] = data[service];
data[thisAppsActiveServiceName] = {
old: 'true'
};
writeYaml('upgrade.yml', data, function(err) {
if (err) console.log(err);
exec('rancher-compose pull --cached', function (error, stdout, stderr) {
var child = spawn('rancher-compose', [
'--file', 'upgrade.yml',
'upgrade', thisAppsActiveServiceName, latestBuildName,
'--pull', '--cleanup'
]);
child.stdout.pipe(process.stdout);
});
});
} else if(!thisAppsActiveServiceName) {
console.log('Creating app on rancher');
console.log('Pulling updated images if we need to...');
exec('rancher-compose pull --cached', function (error, stdout, stderr) {
var child = spawn('rancher-compose', [
'up', '-d'
]);
child.stdout.pipe(process.stdout);
});
} else {
console.log('Nothing to do...')
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment