diff --git a/cloudron.js b/cloudron.js index f928c17..fba8fc6 100644 --- a/cloudron.js +++ b/cloudron.js @@ -103,6 +103,21 @@ Cloudron.prototype.getOauthToken = function (user) { return accessToken; }; +// setup dns for selfhosters +Cloudron.prototype.setupDns = function (dnsConfig) { + var res = request.post('https://' + this._box.ip + '/api/v1/cloudron/dns_setup').send(dnsConfig).end(); + common.verifyResponse2xx(res, 'Could not setup Cloudron dns'); + + for (var i = 0; i < 60; ++i) { + sleep(5); + + res = request.get(this._origin + '/api/v1/cloudron/status').end(); + common.verifyResponse2xx(res, 'Could not get Cloudron status'); + + if (res.body.adminFqdn) return; + } +}; + // activate the box Cloudron.prototype.activate = function (user) { var setupToken = this._box.setupToken; @@ -160,12 +175,12 @@ Cloudron.prototype.waitForApp = function (appId, version) { return res.body; }; -Cloudron.prototype.waitForBox = function () { +Cloudron.prototype.waitForBox = function (byIp) { process.stdout.write('Waiting for box.'); var res; for (var i = 0; i < 60; i++) { sleep(20); - res = request.get(this._origin + '/api/v1/cloudron/status').end(); + res = request.get((byIp ? ('https://' + this._box.ip) : this._origin) + '/api/v1/cloudron/status').end(); if (res.statusCode === 200) { console.log(); return; diff --git a/package.json b/package.json index a72fd7e..60018c1 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "End to end testing", "main": "test.js", "scripts": { - "test": "DEBUG=superagent-sync,e2e:* DEBUG_COLORS=true ./node_modules/.bin/mocha --bail test/*", + "test": "DEBUG=superagent-sync,e2e:* DEBUG_COLORS=true ./node_modules/.bin/mocha --bail test/selfhost-digitalocean-filesystem-test", "parallel_test": "./parallel_test.sh" }, "repository": { diff --git a/test/selfhost-digitalocean-filesystem-test.js b/test/selfhost-digitalocean-filesystem-test.js index cc839c1..4ca88e0 100644 --- a/test/selfhost-digitalocean-filesystem-test.js +++ b/test/selfhost-digitalocean-filesystem-test.js @@ -14,6 +14,7 @@ var AppStore = require('../appstore.js'), child_process = require('child_process'), Cloudron = require('../cloudron.js'), common = require('../common.js'), + digitalocean = require('../digitalocean.js'), mailer = require('../mailer.js'), rimraf = require('rimraf'), semver = require('semver'), @@ -27,10 +28,10 @@ require('colors'); process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'; const BOX_VERSION = process.env.BOX_VERSION; const DO_SELFHOST_DOMAIN = process.env.DO_SELFHOST_DOMAIN; -const SSH_KEY = 'id_rsa_e2e_selfhost'; +const DO_SSH_KEY = 'e2e_selfhost'; const DO_TYPE = '1gb'; const DO_REGION = 'nyc3'; -const DO_TOKEN = process.env.DIGITAL_OCEAN_TOKEN_STAGING; +const DO_IMAGE_SLUG = 'ubuntu-16-04-x64'; const BACKUP_KEY = 'somesecret'; const BACKUP_FOLDER = '/tmp/' + process.env.DO_SELFHOST_DOMAIN; @@ -51,6 +52,23 @@ function machine(args, options) { } } +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; + } +} + describe('Selfhost DigitalOcean with filesystem backend', function () { this.timeout(0); @@ -60,6 +78,8 @@ describe('Selfhost DigitalOcean with filesystem backend', function () { var cloudron, appId, backupInfo, instanceId, restoreInstanceId, migrateInstanceId; var fromVersion, toVersion, nextVersion; + digitalocean.setCredentials(process.env.DIGITAL_OCEAN_TOKEN_STAGING); + it('can query versions', function () { var res = request.get(process.env.BOX_VERSIONS_URL).end(); common.verifyResponse2xx(res); @@ -72,35 +92,95 @@ describe('Selfhost DigitalOcean with filesystem backend', function () { console.log('Will test update from %s to %s and then %s', fromVersion.yellow, toVersion.yellow, nextVersion.yellow); }); - it('can create a cloudron', function () { + 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); + + console.log('New instance created with id', result.id); + + 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 () { var params = [ - '--fqdn ' + DO_SELFHOST_DOMAIN, - '--type ' + DO_TYPE, - '--token ' + DO_TOKEN, - '--region ' + DO_REGION, - '--ssh-key ' + SSH_KEY, - '--backup-key ' + BACKUP_KEY, - '--release ' + toVersion + '--cloudron ' + cloudron._box.ip, + '--ssh-key ' + DO_SSH_KEY, + '--ssh-port 22' ]; - var out = machine('create digitalocean ' + params.join(' ')); + console.log('wget cloudron-setup'); + var out = machine('ssh ' + params.concat(['wget https://cloudron.io/cloudron-setup']).join(' ')); + console.log(out.stdout, out.stderr); + assert.strictEqual(out.status, 0); + + 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); + + console.log('run cloudron-setup'); + out = machine('ssh ' + params.concat(['"./cloudron-setup --provider digitalocean --prerelease --version ' + toVersion + ' --env staging"']).join(' '));//, { stdout: process.stdout, stderr: process.stderr }); console.log(out.stdout, out.stderr); - if (out.stdout.indexOf('You can now setup your Cloudron at') === -1) { - assert(false, 'Creation failed'); - } - - // Wohooo strings! - 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); + // due to the reboot, we might get a non 0 status code + assert(out.status === 0 || out.stdout.indexOf('Rebooting this server now to let bootloader changes take effect.') !== -1); + }); + it('can setup dns', function () { cloudron = new Cloudron({ domain: DO_SELFHOST_DOMAIN, setupToken: null, version: toVersion, - ip: null + ip: cloudron._box.ip }); + + var dnsConfig = { + domain: process.env.DO_SELFHOST_DOMAIN, + provider: 'digitalocean', + token: process.env.DIGITAL_OCEAN_TOKEN_STAGING + }; + + cloudron.waitForBox(true); + cloudron.setupDns(dnsConfig); }); it('can activate the box', function () { @@ -110,6 +190,17 @@ describe('Selfhost DigitalOcean with filesystem backend', function () { it('can login to the box', function () { var token = cloudron.getOauthToken(owner); cloudron.setCredentials(owner.password, token); + + 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); }); it('can take a breather', function () { @@ -148,17 +239,15 @@ describe('Selfhost DigitalOcean with filesystem backend', function () { cloudron.checkForUpdates(); var params = [ - DO_SELFHOST_DOMAIN, - '--yes', - '--ssh-key ' + SSH_KEY, - '--username ' + owner.username, - '--password ' + owner.password + '--cloudron ' + process.env.DO_SELFHOST_DOMAIN, + '--ssh-key ' + DO_SSH_KEY, + '--ssh-port 22' ]; var out = machine('update ' + params.join(' ')); console.log(out.stdout, out.stderr); - if (out.stdout.indexOf('You can now use your Cloudron at') === -1) { + if (out.stdout.indexOf('Update successful') === -1) { assert(false, 'Update failed'); } }); @@ -206,14 +295,14 @@ describe('Selfhost DigitalOcean with filesystem backend', function () { }); it('can list backups', function () { - var out = machine('backup list ' + DO_SELFHOST_DOMAIN); + var out = machine('backup list --cloudron ' + DO_SELFHOST_DOMAIN); console.log(out.stdout, out.stderr); - var lastBackupId = out.stdout.split('\n').filter(function (l) { return l.indexOf('backup_') === 0; }).map(function (l) { return l.split(' ')[0].trim(); }).pop(); + var knownBackupIds= out.stdout.split('\n').filter(function (l) { return l.indexOf('box_') >= 0; }).map(function (l) { return l.split(' ')[0].trim(); }); - console.log('Last backup id:', lastBackupId); + console.log('Last backup ids:', knownBackupIds); - assert.equal(lastBackupId, backupInfo.id); + assert(knownBackupIds.some(function (id) { return id === backupInfo.id; })); }); it('can download backups', function () { @@ -225,61 +314,32 @@ describe('Selfhost DigitalOcean with filesystem backend', function () { // TODO verify the md5sum }); - 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 - ]; + // 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 + // ]; - var out = machine('restore digitalocean ' + params.join(' ')); - console.log(out.stdout, out.stderr); + // var out = machine('restore digitalocean ' + params.join(' ')); + // console.log(out.stdout, out.stderr); - if (out.stdout.indexOf('You can now use your Cloudron at') === -1) { - assert(false, 'Restore failed'); - } + // if (out.stdout.indexOf('You can now use your Cloudron at') === -1) { + // 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); + // console.log('New instance created with ID', restoreInstanceId); - rimraf.sync(BACKUP_FOLDER); - }); - - it('runs the app', function () { - cloudron.waitForApp(appId); - }); - - // Only works so far with upgrades, as the app needs to be restarted for the test to suceed - xit('can check the addons', function () { - cloudron.checkAddons(cloudron.appFqdn(location), owner); - }); - - it('can migrate cloudron', function () { - var params = [ - '--fqdn ' + DO_SELFHOST_DOMAIN, - // THOSE SHOULD BE STASHED IN THE CONFIG - // '--token ' + DO_TOKEN, - // '--ssh-key ' + SSH_KEY - ]; - - var out = machine('migrate digitalocean ' + params.join(' ')); - console.log(out.stdout, out.stderr); - - if (out.stdout.indexOf('You can now use your Cloudron at') === -1) { - assert(false, 'Migrate failed'); - } - - 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); - }); + // rimraf.sync(BACKUP_FOLDER); + // }); it('runs the app', function () { cloudron.waitForApp(appId); @@ -296,10 +356,8 @@ describe('Selfhost DigitalOcean with filesystem backend', function () { function deleteDroplet(id, callback) { if (!id) return callback(); - superagent.del('https://api.digitalocean.com/v2/droplets/' + id).set('Authorization', 'Bearer ' + DO_TOKEN).end(function (error, result) { + digitalocean.destroy(id, function (error) { if (error) console.error(error.message); - if (result.statusCode !== 204) console.error('Failed to cleanup old droplet. %s %j', result.statusCode, result.body); - callback(); }); }