Cleanup DO instances after test runs

We should be doing this the same way as with caas tests
where we are smart enough to only delete those where
tests were successful
This commit is contained in:
Johannes Zellner 2016-09-09 10:03:22 +02:00
parent d14342bfea
commit d6badf8a2b
2 changed files with 23 additions and 19 deletions

View File

@ -8,13 +8,14 @@
var AppStore = require('../appstore.js'),
assert = require('assert'),
AWS = require('aws-sdk'),
async = require('async'),
child_process = require('child_process'),
Cloudron = require('../cloudron.js'),
common = require('../common.js'),
mailer = require('../mailer.js'),
semver = require('semver'),
sleep = require('../shell.js').sleep,
superagent = require('superagent'),
request = require('superagent-sync'),
util = require('util');
@ -22,8 +23,8 @@ require('colors');
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
const BOX_VERSION = process.env.BOX_VERSION;
const SELFHOST_DOMAIN = process.env.SELFHOST_DOMAIN;
const SSH_KEY = 'caas';
const SELFHOST_DOMAIN = 'smartserver.io';
const SSH_KEY = 'caas_staging';
const DO_TYPE = '1gb';
const DO_REGION = 'ams2';
const DO_TOKEN = process.env.DIGITAL_OCEAN_TOKEN_STAGING;
@ -54,7 +55,7 @@ describe('Selfhost EC2 Cloudron creation', function () {
this.timeout(0);
var appStore = new AppStore('https://api.staging.cloudron.io');
var ec2 = new AWS.EC2({ accessKeyId: AWS_ACCESS_KEY, secretAccessKey: AWS_ACCESS_SECRET, region: EC2_REGION });
// var ec2 = new AWS.EC2({ accessKeyId: AWS_ACCESS_KEY, secretAccessKey: AWS_ACCESS_SECRET, region: EC2_REGION });
var owner = common.getOwner();
var cloudron, appId, backupInfo, instanceId, restoreInstanceId, migrateInstanceId;
@ -78,7 +79,7 @@ describe('Selfhost EC2 Cloudron creation', function () {
'--type ' + DO_TYPE,
'--token ' + DO_TOKEN,
'--region ' + DO_REGION,
'--awsRegion ' + EC2_REGION,
'--aws-region ' + EC2_REGION,
'--ssh-key ' + SSH_KEY,
'--access-key-id ' + AWS_ACCESS_KEY,
'--secret-access-key ' + AWS_ACCESS_SECRET,
@ -95,7 +96,7 @@ describe('Selfhost EC2 Cloudron creation', function () {
}
// Wohooo strings!
instanceId = out.stdout.split('\n').filter(function (l) { return l.indexOf('ID: ') !== -1; })[0].split(':')[1].trim();
instanceId = out.stdout.split('\n').filter(function (l) { return l.indexOf(' ID: ') !== -1; })[0].split(':')[1].trim();
console.log('New instance created with ID', instanceId);
@ -207,7 +208,7 @@ describe('Selfhost EC2 Cloudron creation', function () {
'--type ' + DO_TYPE,
'--token ' + DO_TOKEN,
'--region ' + DO_REGION,
'--awsRegion ' + EC2_REGION,
'--aws-region ' + EC2_REGION,
'--ssh-key ' + SSH_KEY,
'--access-key-id ' + AWS_ACCESS_KEY,
'--secret-access-key ' + AWS_ACCESS_SECRET,
@ -223,7 +224,7 @@ describe('Selfhost EC2 Cloudron creation', function () {
assert(false, 'Restore failed');
}
restoreInstanceId = out.stdout.split('\n').filter(function (l) { return l.indexOf('ID: ') !== -1; })[0].split(':')[1].trim();
restoreInstanceId = out.stdout.split('\n').filter(function (l) { return l.indexOf(' ID: ') !== -1; })[0].split(':')[1].trim();
console.log('New instance created with ID', restoreInstanceId);
});
@ -252,7 +253,7 @@ describe('Selfhost EC2 Cloudron creation', function () {
assert(false, 'Migrate failed');
}
migrateInstanceId = out.stdout.split('\n').filter(function (l) { return l.indexOf('ID: ') !== -1; })[0].split(':')[1].trim();
migrateInstanceId = out.stdout.split('\n').filter(function (l) { return l.indexOf(' ID: ') !== -1; })[0].split(':')[1].trim();
console.log('New instance created with ID', restoreInstanceId);
});
@ -265,16 +266,19 @@ describe('Selfhost EC2 Cloudron creation', function () {
cloudron.uninstallApp(appId);
});
xit('can delete the cloudron', function (done) {
console.log('Cleanup EC2 instances');
it('can delete the cloudron', function (done) {
console.log('Cleanup DO instances', instanceId, restoreInstanceId, migrateInstanceId);
var params = {
InstanceIds: [ instanceId, restoreInstanceId, migrateInstanceId ]
};
// we ignore errors here
function deleteDroplet(id, callback) {
superagent.del('https://api.digitalocean.com/v2/droplets/' + id).set('Authorization', 'Bearer ' + DO_TOKEN).end(function (error, result) {
if (error) console.error(error.message);
if (result.statusCode !== 204) console.error('Failed to cleanup old droplet. %s %j', result.statusCode, result.body);
ec2.terminateInstances(params, function (error) {
if (error) console.log(error);
done();
});
callback();
});
}
async.each([ instanceId, restoreInstanceId, migrateInstanceId ], deleteDroplet, done);
});
});

View File

@ -22,7 +22,7 @@ require('colors');
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
const BOX_VERSION = process.env.BOX_VERSION;
const SELFHOST_DOMAIN = process.env.SELFHOST_DOMAIN;
const SELFHOST_DOMAIN = 'cloudron.club';
const EC2_TYPE = 't2.small';
const EC2_SIZE = 30;
const EC2_REGION = 'eu-central-1';