2016-09-08 11:13:38 +00:00
|
|
|
/*
|
2016-09-13 08:19:18 +00:00
|
|
|
*
|
|
|
|
* This tests a flow for cloudron owner creating a selfhosted cloudron on digitalocean
|
2016-09-08 11:13:38 +00:00
|
|
|
* Owner creates a cloudron, activates it, installs and app and deletes the cloudron eventually
|
2016-09-13 08:19:18 +00:00
|
|
|
* We use a us-east-1 (US standard) backup bucket here
|
2016-09-08 11:13:38 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var AppStore = require('../appstore.js'),
|
|
|
|
assert = require('assert'),
|
2016-09-09 08:03:22 +00:00
|
|
|
async = require('async'),
|
2016-09-08 11:13:38 +00:00
|
|
|
child_process = require('child_process'),
|
|
|
|
Cloudron = require('../cloudron.js'),
|
|
|
|
common = require('../common.js'),
|
2017-02-02 16:08:51 +00:00
|
|
|
digitalocean = require('../digitalocean.js'),
|
2016-09-08 11:13:38 +00:00
|
|
|
mailer = require('../mailer.js'),
|
2016-10-13 14:43:21 +00:00
|
|
|
rimraf = require('rimraf'),
|
2016-09-08 11:13:38 +00:00
|
|
|
semver = require('semver'),
|
|
|
|
sleep = require('../shell.js').sleep,
|
2016-09-09 08:03:22 +00:00
|
|
|
superagent = require('superagent'),
|
2016-09-08 11:13:38 +00:00
|
|
|
request = require('superagent-sync'),
|
|
|
|
util = require('util');
|
|
|
|
|
|
|
|
require('colors');
|
|
|
|
|
|
|
|
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
|
|
|
|
const BOX_VERSION = process.env.BOX_VERSION;
|
2016-09-13 08:19:18 +00:00
|
|
|
const DO_SELFHOST_DOMAIN = process.env.DO_SELFHOST_DOMAIN;
|
2017-02-02 16:08:51 +00:00
|
|
|
const DO_SSH_KEY = 'e2e_selfhost';
|
2016-09-08 11:13:38 +00:00
|
|
|
const DO_TYPE = '1gb';
|
2016-09-13 08:19:18 +00:00
|
|
|
const DO_REGION = 'nyc3';
|
2017-02-02 16:08:51 +00:00
|
|
|
const DO_IMAGE_SLUG = 'ubuntu-16-04-x64';
|
2016-09-08 11:13:38 +00:00
|
|
|
const BACKUP_KEY = 'somesecret';
|
2016-10-13 14:41:59 +00:00
|
|
|
const BACKUP_FOLDER = '/tmp/' + process.env.DO_SELFHOST_DOMAIN;
|
2016-09-08 11:13:38 +00:00
|
|
|
|
|
|
|
function machine(args, options) {
|
|
|
|
// https://github.com/nodejs/node-v0.x-archive/issues/9265
|
|
|
|
options = options || { };
|
|
|
|
args = util.isArray(args) ? args : args.match(/[^\s"]+|"([^"]+)"/g);
|
|
|
|
args = args.map(function (e) { return e[0] === '"' ? e.slice(1, -1) : e; }); // remove the quotes
|
|
|
|
|
|
|
|
console.log('cloudron machine ' + args.join(' '));
|
|
|
|
|
|
|
|
try {
|
|
|
|
var cp = child_process.spawnSync('cloudron', ['machine'].concat(args), { stdio: [ options.stdin || 'pipe', options.stdout || 'pipe', 'pipe' ], encoding: options.encoding || 'utf8' });
|
|
|
|
return cp;
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-02 16:08:51 +00:00
|
|
|
function cloudronCommand(args, options) {
|
|
|
|
// https://github.com/nodejs/node-v0.x-archive/issues/9265
|
|
|
|
options = options || { };
|
|
|
|
args = util.isArray(args) ? args : args.match(/[^\s"]+|"([^"]+)"/g);
|
|
|
|
args = args.map(function (e) { return e[0] === '"' ? e.slice(1, -1) : e; }); // remove the quotes
|
|
|
|
|
|
|
|
console.log('cloudron ' + args.join(' '));
|
|
|
|
|
|
|
|
try {
|
|
|
|
var cp = child_process.spawnSync('cloudron', args, { stdio: [ options.stdin || 'pipe', options.stdout || 'pipe', 'pipe' ], encoding: options.encoding || 'utf8' });
|
|
|
|
return cp;
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-17 12:29:15 +00:00
|
|
|
describe('Selfhost DigitalOcean with filesystem backend', function () {
|
2016-09-08 11:13:38 +00:00
|
|
|
this.timeout(0);
|
|
|
|
|
2016-09-13 08:19:18 +00:00
|
|
|
var appStore = new AppStore();
|
2016-09-08 11:13:38 +00:00
|
|
|
|
|
|
|
var owner = common.getOwner();
|
|
|
|
var cloudron, appId, backupInfo, instanceId, restoreInstanceId, migrateInstanceId;
|
|
|
|
var fromVersion, toVersion, nextVersion;
|
|
|
|
|
2017-02-02 16:08:51 +00:00
|
|
|
digitalocean.setCredentials(process.env.DIGITAL_OCEAN_TOKEN_STAGING);
|
|
|
|
|
2016-09-08 11:13:38 +00:00
|
|
|
it('can query versions', function () {
|
2016-09-13 07:59:13 +00:00
|
|
|
var res = request.get(process.env.BOX_VERSIONS_URL).end();
|
2016-09-08 11:13:38 +00:00
|
|
|
common.verifyResponse2xx(res);
|
|
|
|
var boxVersions = Object.keys(common.stripUnreachable(res.body)).sort(semver.rcompare);
|
|
|
|
fromVersion = boxVersions[2]; // we released a new version in before.js
|
|
|
|
toVersion = boxVersions[1];
|
|
|
|
assert.strictEqual(toVersion, BOX_VERSION);
|
|
|
|
nextVersion = boxVersions[0];
|
|
|
|
|
|
|
|
console.log('Will test update from %s to %s and then %s', fromVersion.yellow, toVersion.yellow, nextVersion.yellow);
|
|
|
|
});
|
|
|
|
|
2017-02-02 16:08:51 +00:00
|
|
|
it('can create a cloudron', function (done) {
|
|
|
|
digitalocean.create('tselfhost-filesystem-test', DO_IMAGE_SLUG, DO_SSH_KEY, DO_REGION, DO_TYPE, '', function (error, result) {
|
|
|
|
if (error) return done(error);
|
|
|
|
|
2017-02-07 11:58:30 +00:00
|
|
|
instanceId = result.id;
|
|
|
|
console.log('New instance created with id', instanceId);
|
2017-02-02 16:08:51 +00:00
|
|
|
|
|
|
|
var ip = null;
|
|
|
|
|
|
|
|
function waitForSSH() {
|
|
|
|
var params = [
|
|
|
|
'--cloudron ' + cloudron._box.ip,
|
|
|
|
'--ssh-key ' + DO_SSH_KEY,
|
|
|
|
'--ssh-port 22'
|
|
|
|
];
|
|
|
|
|
|
|
|
var out = machine('ssh ' + params.concat(['echo "hello"']).join(' '));
|
|
|
|
if (out.status === 0) return done();
|
|
|
|
|
|
|
|
setTimeout(waitForSSH, 2000);
|
|
|
|
}
|
|
|
|
|
|
|
|
function waitForDroplet() {
|
|
|
|
digitalocean.get(result.id, function (error, result) {
|
|
|
|
if (error) return done(error);
|
|
|
|
|
|
|
|
console.log('status', result.status);
|
|
|
|
|
|
|
|
ip = result.ip;
|
|
|
|
|
|
|
|
if (result.status === 'active') {
|
|
|
|
cloudron = new Cloudron({
|
|
|
|
domain: DO_SELFHOST_DOMAIN,
|
|
|
|
setupToken: null,
|
|
|
|
version: toVersion,
|
|
|
|
ip: result.ip
|
|
|
|
});
|
|
|
|
|
|
|
|
return waitForSSH();
|
|
|
|
}
|
|
|
|
|
|
|
|
setTimeout(waitForDroplet, 2000);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
waitForDroplet();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can install the Cloudron', function () {
|
2016-09-08 11:13:38 +00:00
|
|
|
var params = [
|
2017-02-02 16:08:51 +00:00
|
|
|
'--cloudron ' + cloudron._box.ip,
|
|
|
|
'--ssh-key ' + DO_SSH_KEY,
|
|
|
|
'--ssh-port 22'
|
2016-09-08 11:13:38 +00:00
|
|
|
];
|
|
|
|
|
2017-02-02 16:08:51 +00:00
|
|
|
console.log('wget cloudron-setup');
|
|
|
|
var out = machine('ssh ' + params.concat(['wget https://cloudron.io/cloudron-setup']).join(' '));
|
2016-09-08 11:13:38 +00:00
|
|
|
console.log(out.stdout, out.stderr);
|
2017-02-02 16:08:51 +00:00
|
|
|
assert.strictEqual(out.status, 0);
|
2016-09-08 11:13:38 +00:00
|
|
|
|
2017-02-02 16:08:51 +00:00
|
|
|
console.log('chmod +x cloudron-setup');
|
|
|
|
out = machine('ssh ' + params.concat(['chmod +x cloudron-setup']).join(' '));
|
|
|
|
console.log(out.stdout, out.stderr);
|
|
|
|
assert.strictEqual(out.status, 0);
|
2016-09-08 11:13:38 +00:00
|
|
|
|
2017-02-02 16:08:51 +00:00
|
|
|
console.log('run cloudron-setup');
|
2017-02-07 14:26:58 +00:00
|
|
|
out = machine('ssh ' + params.concat(['"./cloudron-setup --provider digitalocean --prerelease --version ' + toVersion + ' --env staging --skip-reboot"']).join(' '));
|
2017-02-02 16:08:51 +00:00
|
|
|
console.log(out.stdout, out.stderr);
|
2017-02-07 14:26:58 +00:00
|
|
|
assert.strictEqual(out.status, 0);
|
2016-09-08 11:13:38 +00:00
|
|
|
|
2017-02-07 14:26:58 +00:00
|
|
|
console.log('reboot the server');
|
|
|
|
out = machine('ssh ' + params.concat(['systemctl reboot']).join(' '));
|
|
|
|
console.log(out.stdout, out.stderr);
|
2017-02-02 16:08:51 +00:00
|
|
|
});
|
2016-09-08 11:13:38 +00:00
|
|
|
|
2017-02-02 16:08:51 +00:00
|
|
|
it('can setup dns', function () {
|
2016-09-08 11:13:38 +00:00
|
|
|
cloudron = new Cloudron({
|
2016-09-13 08:19:18 +00:00
|
|
|
domain: DO_SELFHOST_DOMAIN,
|
2016-09-08 11:13:38 +00:00
|
|
|
setupToken: null,
|
|
|
|
version: toVersion,
|
2017-02-02 16:08:51 +00:00
|
|
|
ip: cloudron._box.ip
|
2016-09-08 11:13:38 +00:00
|
|
|
});
|
2017-02-02 16:08:51 +00:00
|
|
|
|
|
|
|
var dnsConfig = {
|
|
|
|
domain: process.env.DO_SELFHOST_DOMAIN,
|
|
|
|
provider: 'digitalocean',
|
|
|
|
token: process.env.DIGITAL_OCEAN_TOKEN_STAGING
|
|
|
|
};
|
|
|
|
|
|
|
|
cloudron.waitForBox(true);
|
|
|
|
cloudron.setupDns(dnsConfig);
|
2016-09-08 11:13:38 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('can activate the box', function () {
|
|
|
|
cloudron.activate(owner);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can login to the box', function () {
|
|
|
|
var token = cloudron.getOauthToken(owner);
|
|
|
|
cloudron.setCredentials(owner.password, token);
|
2017-02-02 16:08:51 +00:00
|
|
|
|
|
|
|
var params = [
|
|
|
|
process.env.DO_SELFHOST_DOMAIN,
|
|
|
|
'--username ' + owner.username,
|
|
|
|
'--password ' + owner.password
|
|
|
|
];
|
|
|
|
|
|
|
|
var out = cloudronCommand('login ' + params.join(' '));
|
|
|
|
console.log(out.stdout, out.stderr);
|
|
|
|
|
|
|
|
assert.strictEqual(out.status, 0);
|
2016-09-08 11:13:38 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('can take a breather', function () {
|
|
|
|
// do can be really slow to come up. the addon containers take their own sweet time (they are "async" with the box startup)
|
|
|
|
// we end up sending email even before the mail container is ready
|
2016-09-10 08:02:30 +00:00
|
|
|
sleep(30);
|
2016-09-08 11:13:38 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('can enable email', function () {
|
|
|
|
cloudron.setEmailEnabled(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('send mail to cloudron user', function (done) {
|
2016-09-13 08:19:18 +00:00
|
|
|
mailer.sendMailToCloudronUser(owner.username + '@' + DO_SELFHOST_DOMAIN, done);
|
2016-09-08 11:13:38 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('did receive mail', function (done) {
|
|
|
|
cloudron.checkMail(owner, done);
|
|
|
|
});
|
|
|
|
|
|
|
|
var location = 'test' + (Math.random() * 10000).toFixed();
|
|
|
|
it('can install app', function () {
|
|
|
|
var manifest = appStore.getManifest(common.TESTAPP_ID, common.TESTAPP_VERSION);
|
|
|
|
appId = cloudron.installApp(location, manifest);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can populate the addons', function () {
|
|
|
|
cloudron.populateAddons(cloudron.appFqdn(location));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can check the addons', function () {
|
|
|
|
cloudron.checkAddons(cloudron.appFqdn(location), owner);
|
|
|
|
});
|
|
|
|
|
2017-04-15 15:45:07 +00:00
|
|
|
it('can set appstore config', function () {
|
2017-04-15 15:00:57 +00:00
|
|
|
var token = appStore.getAccessToken(owner);
|
|
|
|
appStore.setCredentials(owner.password, token);
|
|
|
|
var profile = appStore.getProfile();
|
|
|
|
cloudron.setAppstoreConfig(profile.id, token);
|
2017-04-15 14:32:45 +00:00
|
|
|
});
|
|
|
|
|
2016-09-08 11:13:38 +00:00
|
|
|
it('can update the box', function () {
|
|
|
|
cloudron.checkForUpdates();
|
2017-04-24 18:10:47 +00:00
|
|
|
sleep(60); // give it a minute to get update info
|
2016-09-08 11:13:38 +00:00
|
|
|
|
|
|
|
var params = [
|
2017-02-02 16:08:51 +00:00
|
|
|
'--cloudron ' + process.env.DO_SELFHOST_DOMAIN,
|
|
|
|
'--ssh-key ' + DO_SSH_KEY,
|
|
|
|
'--ssh-port 22'
|
2016-09-08 11:13:38 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
var out = machine('update ' + params.join(' '));
|
|
|
|
console.log(out.stdout, out.stderr);
|
|
|
|
|
2017-02-02 16:08:51 +00:00
|
|
|
if (out.stdout.indexOf('Update successful') === -1) {
|
2016-09-08 11:13:38 +00:00
|
|
|
assert(false, 'Update failed');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it('wait for app to be ready', function () {
|
|
|
|
cloudron.waitForApp(appId);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can configure app', function () {
|
|
|
|
location = location + 'x';
|
|
|
|
cloudron.configureApp(appId, location);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can check the addons', function () {
|
|
|
|
cloudron.checkAddons(cloudron.appFqdn(location), owner);
|
|
|
|
});
|
|
|
|
|
2017-01-13 03:18:12 +00:00
|
|
|
it('can check dnsbl', function () {
|
|
|
|
cloudron.checkDnsbl(cloudron.appFqdn(location));
|
|
|
|
});
|
|
|
|
|
2016-09-08 11:13:38 +00:00
|
|
|
it('can reboot the cloudron', function () {
|
|
|
|
cloudron.reboot();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('runs the app', function () {
|
|
|
|
cloudron.waitForApp(appId);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can check the addons', function () {
|
|
|
|
cloudron.checkAddons(cloudron.appFqdn(location), owner);
|
|
|
|
});
|
|
|
|
|
2017-01-13 03:18:12 +00:00
|
|
|
it('can check dnsbl', function () {
|
|
|
|
cloudron.checkDnsbl(cloudron.appFqdn(location));
|
|
|
|
});
|
|
|
|
|
2016-09-08 11:13:38 +00:00
|
|
|
it('can check mail', function (done) {
|
|
|
|
cloudron.checkMail(owner, done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can backup the box', function () {
|
|
|
|
backupInfo = cloudron.backup();
|
|
|
|
assert.strictEqual(backupInfo.dependsOn.length, 1);
|
|
|
|
});
|
|
|
|
|
2016-10-13 14:41:59 +00:00
|
|
|
it('can list backups', function () {
|
2017-02-02 16:08:51 +00:00
|
|
|
var out = machine('backup list --cloudron ' + DO_SELFHOST_DOMAIN);
|
2016-10-13 14:41:59 +00:00
|
|
|
console.log(out.stdout, out.stderr);
|
|
|
|
|
2017-02-02 16:08:51 +00:00
|
|
|
var knownBackupIds= out.stdout.split('\n').filter(function (l) { return l.indexOf('box_') >= 0; }).map(function (l) { return l.split(' ')[0].trim(); });
|
2016-10-13 14:41:59 +00:00
|
|
|
|
2017-02-02 16:08:51 +00:00
|
|
|
console.log('Last backup ids:', knownBackupIds);
|
2016-10-13 14:41:59 +00:00
|
|
|
|
2017-02-02 16:08:51 +00:00
|
|
|
assert(knownBackupIds.some(function (id) { return id === backupInfo.id; }));
|
2016-10-13 14:41:59 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('can download backups', function () {
|
2017-04-15 16:39:26 +00:00
|
|
|
var out = machine('backup download --cloudron ' + DO_SELFHOST_DOMAIN + ' ' + backupInfo.id + ' ' + BACKUP_FOLDER);
|
2016-10-13 14:41:59 +00:00
|
|
|
console.log(out.stdout, out.stderr);
|
|
|
|
|
|
|
|
if (out.stdout.indexOf(backupInfo.id) === -1) assert(false, 'Download failed');
|
|
|
|
|
|
|
|
// TODO verify the md5sum
|
|
|
|
});
|
|
|
|
|
2017-02-02 16:08:51 +00:00
|
|
|
// it('can restore the box', function () {
|
|
|
|
// var params = [
|
|
|
|
// '--fqdn ' + DO_SELFHOST_DOMAIN,
|
|
|
|
// // THOSE SHOULD BE STASHED IN THE CONFIG
|
|
|
|
// // '--type ' + DO_TYPE,
|
|
|
|
// // '--token ' + DO_TOKEN,
|
|
|
|
// // '--region ' + DO_REGION,
|
|
|
|
// // '--ssh-key ' + SSH_KEY,
|
|
|
|
// // '--backup-key ' + BACKUP_KEY,
|
|
|
|
// '--backup ' + backupInfo.id,
|
|
|
|
// '--backup-folder ' + BACKUP_FOLDER
|
|
|
|
// ];
|
2016-09-08 11:13:38 +00:00
|
|
|
|
2017-02-02 16:08:51 +00:00
|
|
|
// var out = machine('restore digitalocean ' + params.join(' '));
|
|
|
|
// console.log(out.stdout, out.stderr);
|
2016-09-08 11:13:38 +00:00
|
|
|
|
2017-02-02 16:08:51 +00:00
|
|
|
// if (out.stdout.indexOf('You can now use your Cloudron at') === -1) {
|
|
|
|
// assert(false, 'Restore failed');
|
|
|
|
// }
|
2016-09-08 11:13:38 +00:00
|
|
|
|
2017-02-02 16:08:51 +00:00
|
|
|
// restoreInstanceId = out.stdout.split('\n').filter(function (l) { return l.indexOf(' ID: ') !== -1; })[0].split(':')[1].trim();
|
2016-09-08 11:13:38 +00:00
|
|
|
|
2017-02-02 16:08:51 +00:00
|
|
|
// console.log('New instance created with ID', restoreInstanceId);
|
2016-09-08 11:13:38 +00:00
|
|
|
|
2017-02-02 16:08:51 +00:00
|
|
|
// rimraf.sync(BACKUP_FOLDER);
|
|
|
|
// });
|
2016-09-08 11:13:38 +00:00
|
|
|
|
|
|
|
it('runs the app', function () {
|
|
|
|
cloudron.waitForApp(appId);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can uninstall app', function () {
|
|
|
|
cloudron.uninstallApp(appId);
|
|
|
|
});
|
|
|
|
|
2016-09-09 08:03:22 +00:00
|
|
|
it('can delete the cloudron', function (done) {
|
|
|
|
console.log('Cleanup DO instances', instanceId, restoreInstanceId, migrateInstanceId);
|
2016-09-08 11:13:38 +00:00
|
|
|
|
2016-09-09 08:03:22 +00:00
|
|
|
// we ignore errors here
|
|
|
|
function deleteDroplet(id, callback) {
|
2016-09-09 09:54:40 +00:00
|
|
|
if (!id) return callback();
|
|
|
|
|
2017-02-02 16:08:51 +00:00
|
|
|
digitalocean.destroy(id, function (error) {
|
2016-09-09 08:03:22 +00:00
|
|
|
if (error) console.error(error.message);
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async.each([ instanceId, restoreInstanceId, migrateInstanceId ], deleteDroplet, done);
|
2016-09-08 11:13:38 +00:00
|
|
|
});
|
|
|
|
});
|