2015-06-14 04:33:42 +00:00
|
|
|
'use strict';
|
|
|
|
|
2015-07-27 20:21:38 +00:00
|
|
|
var common = require('./common.js'),
|
|
|
|
debug = require('debug')('e2e:appstore'),
|
2015-06-14 04:33:42 +00:00
|
|
|
request = require('superagent-sync'),
|
|
|
|
sleep = require('sleep').sleep,
|
2015-06-22 07:11:43 +00:00
|
|
|
stripe = require('stripe');
|
2015-06-14 04:33:42 +00:00
|
|
|
|
|
|
|
exports = module.exports = AppStore;
|
|
|
|
|
|
|
|
function AppStore(origin) {
|
|
|
|
this._origin = origin;
|
|
|
|
|
|
|
|
// credentials for api calls
|
|
|
|
this._credentials = {
|
|
|
|
password: null,
|
|
|
|
accessToken: null
|
|
|
|
};
|
2015-11-29 16:41:28 +00:00
|
|
|
|
|
|
|
this._adminCredentials = {
|
|
|
|
password: null,
|
|
|
|
accessToken: null
|
|
|
|
};
|
2015-06-14 04:33:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AppStore.prototype.getAccessToken = function (user) {
|
|
|
|
var res = request.get(this._origin + '/api/v1/login').auth(user.email, user.password).end();
|
2015-07-27 20:22:12 +00:00
|
|
|
common.verifyResponse(res, 'Could not login as user:' + user.email + ' password:' + user.password);
|
2015-06-14 04:33:42 +00:00
|
|
|
return res.body.accessToken;
|
|
|
|
};
|
|
|
|
|
2015-09-24 11:24:22 +00:00
|
|
|
AppStore.prototype.getProfile = function () {
|
|
|
|
var res = request.get(this._origin + '/api/v1/profile').query({ accessToken: this._credentials.accessToken }).end();
|
|
|
|
return res.body.profile;
|
|
|
|
};
|
|
|
|
|
2015-06-14 04:33:42 +00:00
|
|
|
AppStore.prototype.setCredentials = function (password, accessToken) {
|
|
|
|
this._credentials = { password: password, accessToken: accessToken };
|
|
|
|
};
|
|
|
|
|
2015-11-29 16:41:28 +00:00
|
|
|
AppStore.prototype.setAdminCredentials = function (password, accessToken) {
|
|
|
|
this._adminCredentials = { password: password, accessToken: accessToken };
|
|
|
|
};
|
|
|
|
|
2015-07-27 23:17:37 +00:00
|
|
|
AppStore.prototype.getCloudrons = function () {
|
|
|
|
var res = request.get(this._origin + '/api/v1/cloudrons').query({ accessToken: this._credentials.accessToken, page: 1, per_page: 50 }).end();
|
|
|
|
return res.body.boxes;
|
|
|
|
};
|
|
|
|
|
2015-09-30 04:23:00 +00:00
|
|
|
AppStore.prototype.getCloudron = function (boxId) {
|
|
|
|
var res = request.get(this._origin + '/api/v1/cloudrons/' + boxId).query({ accessToken: this._credentials.accessToken, page: 1, per_page: 50 }).end();
|
|
|
|
if (res.statusCode === 404) return null;
|
|
|
|
common.verifyResponse(res, 'Could not query cloudron status');
|
|
|
|
return res.body.box;
|
|
|
|
};
|
|
|
|
|
2015-12-17 21:58:49 +00:00
|
|
|
AppStore.prototype.waitForIP = function (boxId) {
|
|
|
|
process.stdout.write('Waiting for cloudron to get an IP.');
|
|
|
|
|
|
|
|
var res;
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
sleep(10);
|
|
|
|
process.stdout.write('.');
|
|
|
|
res = request.get(this._origin + '/api/v1/cloudrons/' + boxId).query({ accessToken: this._credentials.accessToken }).end();
|
|
|
|
common.verifyResponse(res, 'Could not query cloudron status');
|
|
|
|
|
|
|
|
if (res.body.box.ip) {
|
|
|
|
debug();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
debug('Box IP:%s'.green, res.body.box.ip);
|
|
|
|
|
|
|
|
return res.body.box.ip;
|
|
|
|
};
|
|
|
|
|
2015-06-16 19:00:46 +00:00
|
|
|
AppStore.prototype.waitForCloudron = function (boxId) {
|
2015-06-14 04:33:42 +00:00
|
|
|
var creationTime = new Date();
|
|
|
|
process.stdout.write('Waiting for cloudron to come up.');
|
|
|
|
|
2015-07-27 20:16:19 +00:00
|
|
|
var boxInfo = null, res;
|
|
|
|
|
2015-06-14 04:33:42 +00:00
|
|
|
while (true) {
|
|
|
|
sleep(10);
|
|
|
|
process.stdout.write('.');
|
2015-07-27 19:34:56 +00:00
|
|
|
res = request.get(this._origin + '/api/v1/cloudrons/' + boxId).query({ accessToken: this._credentials.accessToken }).end();
|
2015-07-27 20:22:12 +00:00
|
|
|
common.verifyResponse(res, 'Could not query cloudron status');
|
2015-06-14 04:33:42 +00:00
|
|
|
|
2015-07-27 20:16:19 +00:00
|
|
|
boxInfo = res.body.box;
|
|
|
|
if (boxInfo.status === 'ready') {
|
2015-06-14 04:33:42 +00:00
|
|
|
debug();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-27 19:34:56 +00:00
|
|
|
debug('Box created in %s minutes with IP:%s'.green, (new Date() - creationTime) / 60000, res.body.box.ip);
|
|
|
|
|
2015-07-27 20:16:19 +00:00
|
|
|
// even if the cloudron sent heartbeat to appstore, doesn't mean we can contact the cloudron thanks to DO networking insanity
|
|
|
|
process.stdout.write('Waiting for Cloudron to be reachable.');
|
|
|
|
while (true) {
|
|
|
|
sleep(10);
|
|
|
|
process.stdout.write('.');
|
|
|
|
res = request.get('https://' + boxInfo.ip).end();
|
|
|
|
if (!res.error) break;
|
|
|
|
debug(res.error);
|
|
|
|
}
|
|
|
|
return boxInfo;
|
2015-06-16 19:00:46 +00:00
|
|
|
};
|
2015-06-14 04:33:42 +00:00
|
|
|
|
2015-06-16 19:00:46 +00:00
|
|
|
AppStore.prototype.createCloudron = function (box) {
|
|
|
|
var accessToken = this._credentials.accessToken;
|
|
|
|
var res = request.post(this._origin + '/api/v1/cloudrons').send(box).query({ accessToken: accessToken }).end();
|
2015-09-29 20:25:09 +00:00
|
|
|
common.verifyResponse(res, 'Could not create cloudron %j', box);
|
2015-06-16 19:00:46 +00:00
|
|
|
|
2015-06-29 06:20:05 +00:00
|
|
|
debug('Cloudron %s created'.green, box.domain);
|
2015-06-14 04:33:42 +00:00
|
|
|
return res.body.box;
|
|
|
|
};
|
|
|
|
|
2015-11-29 16:41:28 +00:00
|
|
|
// Only allowed by admins
|
2015-06-14 04:33:42 +00:00
|
|
|
AppStore.prototype.deleteCloudron = function (box) {
|
|
|
|
var res = request.post(this._origin + '/api/v1/cloudrons/' + box.id)
|
2015-11-29 16:41:28 +00:00
|
|
|
.query({ accessToken: this._adminCredentials.accessToken })
|
2015-06-14 04:33:42 +00:00
|
|
|
.set('X-HTTP-Method-Override', 'DELETE')
|
2015-11-29 16:41:28 +00:00
|
|
|
.send({ password: this._adminCredentials.password })
|
2015-06-14 04:33:42 +00:00
|
|
|
.end();
|
2015-07-27 20:22:12 +00:00
|
|
|
common. verifyResponse(res, 'Could not delete cloudron');
|
2015-09-30 04:23:00 +00:00
|
|
|
|
|
|
|
process.stdout.write('Waiting for Cloudron to disappear.');
|
|
|
|
while (true) {
|
|
|
|
if (this.getCloudron(box.id) === null) break;
|
|
|
|
|
|
|
|
sleep(10);
|
|
|
|
|
|
|
|
process.stdout.write('.');
|
|
|
|
}
|
2015-06-14 04:33:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
AppStore.prototype.getManifest = function (appId, version) {
|
|
|
|
var res = request.get(this._origin + '/api/v1/apps/' + appId + '/versions/' + version).end();
|
2015-07-27 20:22:12 +00:00
|
|
|
common.verifyResponse(res, 'Could not get get app manifest');
|
2015-06-14 04:33:42 +00:00
|
|
|
return res.body.manifest;
|
|
|
|
};
|
|
|
|
|
2015-06-16 19:00:46 +00:00
|
|
|
AppStore.prototype.restore = function (boxId, backupId) {
|
|
|
|
var res = request.post(this._origin + '/api/v1/cloudrons/' + boxId + '/restore/' + backupId).query({ accessToken: this._credentials.accessToken }).end();
|
2015-07-27 20:22:12 +00:00
|
|
|
common.verifyResponse(res, 'Could not restore cloudron');
|
2015-06-16 19:00:46 +00:00
|
|
|
};
|
|
|
|
|
2015-09-24 11:24:22 +00:00
|
|
|
AppStore.prototype.setupBilling = function (user, callback) {
|
2015-07-27 20:21:38 +00:00
|
|
|
var stripeApi = stripe(common.stripeSecret());
|
2015-06-22 07:11:43 +00:00
|
|
|
|
|
|
|
var that = this;
|
|
|
|
|
|
|
|
stripeApi.tokens.create({
|
|
|
|
card: {
|
|
|
|
number: '4242424242424242',
|
|
|
|
exp_month: 12,
|
|
|
|
exp_year: 2016,
|
|
|
|
cvc: '123'
|
|
|
|
}
|
|
|
|
}, function (error, token) {
|
|
|
|
if (error) return callback(error);
|
|
|
|
|
|
|
|
debug('Got stripe token', token.id);
|
|
|
|
|
|
|
|
var data = {
|
2015-09-24 13:48:56 +00:00
|
|
|
firstName: 'Max',
|
|
|
|
lastName: 'Mustermann',
|
|
|
|
company: 'Muster Inc',
|
|
|
|
street: '200 Muster Blvd.',
|
|
|
|
city: 'Muenster',
|
|
|
|
zip: '12312',
|
|
|
|
state: 'Munster',
|
|
|
|
country: 'DE',
|
2015-06-22 07:11:43 +00:00
|
|
|
billingToken: token.id
|
|
|
|
};
|
|
|
|
|
2015-09-24 11:24:22 +00:00
|
|
|
var res = request.put(that._origin + '/api/v1/users/' + user.id).send(data).query({ accessToken: that._credentials.accessToken }).end();
|
2015-07-27 20:22:12 +00:00
|
|
|
common.verifyResponse(res, 'Could not setup billing');
|
2015-06-22 07:11:43 +00:00
|
|
|
|
|
|
|
callback(null);
|
|
|
|
});
|
|
|
|
};
|