2015-07-20 00:09:47 -07:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
exports = module.exports = {
|
|
|
|
|
setupAddons: setupAddons,
|
|
|
|
|
teardownAddons: teardownAddons,
|
|
|
|
|
backupAddons: backupAddons,
|
|
|
|
|
restoreAddons: restoreAddons,
|
|
|
|
|
|
|
|
|
|
getEnvironment: getEnvironment,
|
|
|
|
|
getLinksSync: getLinksSync,
|
|
|
|
|
getBindsSync: getBindsSync,
|
|
|
|
|
|
|
|
|
|
// exported for testing
|
2015-10-07 15:55:57 -07:00
|
|
|
_setupOauth: setupOauth,
|
|
|
|
|
_teardownOauth: teardownOauth
|
2015-07-20 00:09:47 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var appdb = require('./appdb.js'),
|
|
|
|
|
assert = require('assert'),
|
|
|
|
|
async = require('async'),
|
|
|
|
|
child_process = require('child_process'),
|
|
|
|
|
clientdb = require('./clientdb.js'),
|
|
|
|
|
config = require('./config.js'),
|
|
|
|
|
DatabaseError = require('./databaseerror.js'),
|
|
|
|
|
debug = require('debug')('box:addons'),
|
2015-10-19 11:24:21 -07:00
|
|
|
docker = require('./docker.js').connection,
|
2015-07-20 00:09:47 -07:00
|
|
|
fs = require('fs'),
|
|
|
|
|
generatePassword = require('password-generator'),
|
|
|
|
|
hat = require('hat'),
|
|
|
|
|
MemoryStream = require('memorystream'),
|
|
|
|
|
once = require('once'),
|
|
|
|
|
path = require('path'),
|
|
|
|
|
paths = require('./paths.js'),
|
|
|
|
|
safe = require('safetydance'),
|
|
|
|
|
shell = require('./shell.js'),
|
|
|
|
|
spawn = child_process.spawn,
|
|
|
|
|
util = require('util'),
|
2015-10-19 10:54:36 -07:00
|
|
|
uuid = require('node-uuid');
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2015-10-07 16:10:08 -07:00
|
|
|
var NOOP = function (app, options, callback) { return callback(); };
|
2015-07-20 00:09:47 -07:00
|
|
|
|
|
|
|
|
// setup can be called multiple times for the same app (configure crash restart) and existing data must not be lost
|
|
|
|
|
// teardown is destructive. app data stored with the addon is lost
|
|
|
|
|
var KNOWN_ADDONS = {
|
|
|
|
|
ldap: {
|
|
|
|
|
setup: setupLdap,
|
|
|
|
|
teardown: teardownLdap,
|
|
|
|
|
backup: NOOP,
|
|
|
|
|
restore: setupLdap
|
|
|
|
|
},
|
2015-10-18 09:52:37 -07:00
|
|
|
localstorage: {
|
|
|
|
|
setup: NOOP, // docker creates the directory for us
|
|
|
|
|
teardown: NOOP,
|
|
|
|
|
backup: NOOP, // no backup because it's already inside app data
|
|
|
|
|
restore: NOOP
|
|
|
|
|
},
|
|
|
|
|
mongodb: {
|
|
|
|
|
setup: setupMongoDb,
|
|
|
|
|
teardown: teardownMongoDb,
|
|
|
|
|
backup: backupMongoDb,
|
|
|
|
|
restore: restoreMongoDb
|
2015-07-20 00:09:47 -07:00
|
|
|
},
|
|
|
|
|
mysql: {
|
|
|
|
|
setup: setupMySql,
|
|
|
|
|
teardown: teardownMySql,
|
|
|
|
|
backup: backupMySql,
|
|
|
|
|
restore: restoreMySql,
|
|
|
|
|
},
|
2015-10-18 09:52:37 -07:00
|
|
|
oauth: {
|
|
|
|
|
setup: setupOauth,
|
|
|
|
|
teardown: teardownOauth,
|
|
|
|
|
backup: NOOP,
|
|
|
|
|
restore: setupOauth
|
|
|
|
|
},
|
2015-07-20 00:09:47 -07:00
|
|
|
postgresql: {
|
|
|
|
|
setup: setupPostgreSql,
|
|
|
|
|
teardown: teardownPostgreSql,
|
|
|
|
|
backup: backupPostgreSql,
|
|
|
|
|
restore: restorePostgreSql
|
|
|
|
|
},
|
|
|
|
|
redis: {
|
|
|
|
|
setup: setupRedis,
|
|
|
|
|
teardown: teardownRedis,
|
2015-10-12 13:29:27 -07:00
|
|
|
backup: backupRedis,
|
2015-07-20 00:09:47 -07:00
|
|
|
restore: setupRedis // same thing
|
|
|
|
|
},
|
2015-10-18 09:52:37 -07:00
|
|
|
sendmail: {
|
|
|
|
|
setup: setupSendMail,
|
|
|
|
|
teardown: teardownSendMail,
|
|
|
|
|
backup: NOOP,
|
|
|
|
|
restore: setupSendMail
|
|
|
|
|
},
|
2015-10-18 08:40:24 -07:00
|
|
|
scheduler: {
|
|
|
|
|
setup: NOOP,
|
|
|
|
|
teardown: NOOP,
|
|
|
|
|
backup: NOOP,
|
|
|
|
|
restore: NOOP
|
|
|
|
|
},
|
2015-10-18 09:52:37 -07:00
|
|
|
simpleauth: {
|
|
|
|
|
setup: setupSimpleAuth,
|
|
|
|
|
teardown: teardownSimpleAuth,
|
|
|
|
|
backup: NOOP,
|
|
|
|
|
restore: setupSimpleAuth
|
2015-07-20 00:09:47 -07:00
|
|
|
},
|
|
|
|
|
_docker: {
|
|
|
|
|
setup: NOOP,
|
|
|
|
|
teardown: NOOP,
|
|
|
|
|
backup: NOOP,
|
|
|
|
|
restore: NOOP
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var RMAPPDIR_CMD = path.join(__dirname, 'scripts/rmappdir.sh');
|
|
|
|
|
|
|
|
|
|
function debugApp(app, args) {
|
|
|
|
|
assert(!app || typeof app === 'object');
|
|
|
|
|
|
|
|
|
|
var prefix = app ? (app.location || 'naked_domain') : '(no app)';
|
|
|
|
|
debug(prefix + ' ' + util.format.apply(util, Array.prototype.slice.call(arguments, 1)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function setupAddons(app, addons, callback) {
|
|
|
|
|
assert.strictEqual(typeof app, 'object');
|
|
|
|
|
assert(!addons || typeof addons === 'object');
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
|
|
|
|
if (!addons) return callback(null);
|
|
|
|
|
|
2015-07-20 11:03:11 -07:00
|
|
|
debugApp(app, 'setupAddons: Settings up %j', Object.keys(addons));
|
|
|
|
|
|
2015-07-20 00:09:47 -07:00
|
|
|
async.eachSeries(Object.keys(addons), function iterator(addon, iteratorCallback) {
|
|
|
|
|
if (!(addon in KNOWN_ADDONS)) return iteratorCallback(new Error('No such addon:' + addon));
|
|
|
|
|
|
2015-10-07 16:10:08 -07:00
|
|
|
debugApp(app, 'Setting up addon %s with options %j', addon, addons[addon]);
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2015-10-07 16:10:08 -07:00
|
|
|
KNOWN_ADDONS[addon].setup(app, addons[addon], iteratorCallback);
|
2015-07-20 00:09:47 -07:00
|
|
|
}, callback);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function teardownAddons(app, addons, callback) {
|
|
|
|
|
assert.strictEqual(typeof app, 'object');
|
|
|
|
|
assert(!addons || typeof addons === 'object');
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
|
|
|
|
if (!addons) return callback(null);
|
|
|
|
|
|
2015-07-20 11:03:11 -07:00
|
|
|
debugApp(app, 'teardownAddons: Tearing down %j', Object.keys(addons));
|
|
|
|
|
|
2015-07-20 00:09:47 -07:00
|
|
|
async.eachSeries(Object.keys(addons), function iterator(addon, iteratorCallback) {
|
|
|
|
|
if (!(addon in KNOWN_ADDONS)) return iteratorCallback(new Error('No such addon:' + addon));
|
|
|
|
|
|
2015-10-07 16:10:08 -07:00
|
|
|
debugApp(app, 'Tearing down addon %s with options %j', addon, addons[addon]);
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2015-10-07 16:10:08 -07:00
|
|
|
KNOWN_ADDONS[addon].teardown(app, addons[addon], iteratorCallback);
|
2015-07-20 00:09:47 -07:00
|
|
|
}, callback);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function backupAddons(app, addons, callback) {
|
|
|
|
|
assert.strictEqual(typeof app, 'object');
|
|
|
|
|
assert(!addons || typeof addons === 'object');
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
|
|
|
|
debugApp(app, 'backupAddons');
|
|
|
|
|
|
|
|
|
|
if (!addons) return callback(null);
|
|
|
|
|
|
2015-07-20 11:03:11 -07:00
|
|
|
debugApp(app, 'backupAddons: Backing up %j', Object.keys(addons));
|
|
|
|
|
|
2015-07-20 00:09:47 -07:00
|
|
|
async.eachSeries(Object.keys(addons), function iterator (addon, iteratorCallback) {
|
|
|
|
|
if (!(addon in KNOWN_ADDONS)) return iteratorCallback(new Error('No such addon:' + addon));
|
|
|
|
|
|
2015-10-07 16:10:08 -07:00
|
|
|
KNOWN_ADDONS[addon].backup(app, addons[addon], iteratorCallback);
|
2015-07-20 00:09:47 -07:00
|
|
|
}, callback);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function restoreAddons(app, addons, callback) {
|
|
|
|
|
assert.strictEqual(typeof app, 'object');
|
|
|
|
|
assert(!addons || typeof addons === 'object');
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
|
|
|
|
debugApp(app, 'restoreAddons');
|
|
|
|
|
|
|
|
|
|
if (!addons) return callback(null);
|
|
|
|
|
|
2015-07-20 11:03:11 -07:00
|
|
|
debugApp(app, 'restoreAddons: restoring %j', Object.keys(addons));
|
|
|
|
|
|
2015-07-20 00:09:47 -07:00
|
|
|
async.eachSeries(Object.keys(addons), function iterator (addon, iteratorCallback) {
|
|
|
|
|
if (!(addon in KNOWN_ADDONS)) return iteratorCallback(new Error('No such addon:' + addon));
|
|
|
|
|
|
2015-10-07 16:10:08 -07:00
|
|
|
KNOWN_ADDONS[addon].restore(app, addons[addon], iteratorCallback);
|
2015-07-20 00:09:47 -07:00
|
|
|
}, callback);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getEnvironment(app, callback) {
|
|
|
|
|
assert.strictEqual(typeof app, 'object');
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
|
|
|
|
appdb.getAddonConfigByAppId(app.id, callback);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getLinksSync(app, addons) {
|
|
|
|
|
assert.strictEqual(typeof app, 'object');
|
|
|
|
|
assert(!addons || typeof addons === 'object');
|
|
|
|
|
|
|
|
|
|
var links = [ ];
|
|
|
|
|
|
|
|
|
|
if (!addons) return links;
|
|
|
|
|
|
|
|
|
|
for (var addon in addons) {
|
|
|
|
|
switch (addon) {
|
|
|
|
|
case 'mysql': links.push('mysql:mysql'); break;
|
|
|
|
|
case 'postgresql': links.push('postgresql:postgresql'); break;
|
|
|
|
|
case 'sendmail': links.push('mail:mail'); break;
|
|
|
|
|
case 'redis': links.push('redis-' + app.id + ':redis-' + app.id); break;
|
|
|
|
|
case 'mongodb': links.push('mongodb:mongodb'); break;
|
|
|
|
|
default: break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return links;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getBindsSync(app, addons) {
|
|
|
|
|
assert.strictEqual(typeof app, 'object');
|
|
|
|
|
assert(!addons || typeof addons === 'object');
|
|
|
|
|
|
|
|
|
|
var binds = [ ];
|
|
|
|
|
|
|
|
|
|
if (!addons) return binds;
|
|
|
|
|
|
|
|
|
|
for (var addon in addons) {
|
|
|
|
|
switch (addon) {
|
|
|
|
|
case '_docker': binds.push('/var/run/docker.sock:/var/run/docker.sock:rw'); break;
|
|
|
|
|
case 'localstorage': binds.push(path.join(paths.DATA_DIR, app.id, 'data') + ':/app/data:rw'); break;
|
|
|
|
|
default: break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return binds;
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-07 16:10:08 -07:00
|
|
|
function setupOauth(app, options, callback) {
|
2015-07-20 00:09:47 -07:00
|
|
|
assert.strictEqual(typeof app, 'object');
|
2015-10-07 16:10:08 -07:00
|
|
|
assert.strictEqual(typeof options, 'object');
|
2015-07-20 00:09:47 -07:00
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
|
|
|
|
var appId = app.id;
|
2015-10-15 16:31:45 -07:00
|
|
|
var id = 'cid-' + uuid.v4();
|
2015-07-20 00:09:47 -07:00
|
|
|
var clientSecret = hat(256);
|
|
|
|
|
var redirectURI = 'https://' + config.appFqdn(app.location);
|
2015-10-15 12:50:48 +02:00
|
|
|
var scope = 'profile';
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2015-10-07 15:55:57 -07:00
|
|
|
debugApp(app, 'setupOauth: id:%s clientSecret:%s', id, clientSecret);
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2015-10-15 16:31:45 -07:00
|
|
|
clientdb.delByAppIdAndType(appId, clientdb.TYPE_OAUTH, function (error) { // remove existing creds
|
2015-07-20 00:09:47 -07:00
|
|
|
if (error && error.reason !== DatabaseError.NOT_FOUND) return callback(error);
|
|
|
|
|
|
2015-10-15 16:31:45 -07:00
|
|
|
clientdb.add(id, appId, clientdb.TYPE_OAUTH, clientSecret, redirectURI, scope, function (error) {
|
2015-07-20 00:09:47 -07:00
|
|
|
if (error) return callback(error);
|
|
|
|
|
|
|
|
|
|
var env = [
|
|
|
|
|
'OAUTH_CLIENT_ID=' + id,
|
|
|
|
|
'OAUTH_CLIENT_SECRET=' + clientSecret,
|
|
|
|
|
'OAUTH_ORIGIN=' + config.adminOrigin()
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
debugApp(app, 'Setting oauth addon config to %j', env);
|
|
|
|
|
|
|
|
|
|
appdb.setAddonConfig(appId, 'oauth', env, callback);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-07 16:10:08 -07:00
|
|
|
function teardownOauth(app, options, callback) {
|
2015-07-20 00:09:47 -07:00
|
|
|
assert.strictEqual(typeof app, 'object');
|
2015-10-07 16:10:08 -07:00
|
|
|
assert.strictEqual(typeof options, 'object');
|
2015-07-20 00:09:47 -07:00
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
2015-10-07 15:55:57 -07:00
|
|
|
debugApp(app, 'teardownOauth');
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2015-10-15 16:31:45 -07:00
|
|
|
clientdb.delByAppIdAndType(app.id, clientdb.TYPE_OAUTH, function (error) {
|
2015-07-20 00:09:47 -07:00
|
|
|
if (error && error.reason !== DatabaseError.NOT_FOUND) console.error(error);
|
|
|
|
|
|
|
|
|
|
appdb.unsetAddonConfig(app.id, 'oauth', callback);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-09 11:44:32 +02:00
|
|
|
function setupSimpleAuth(app, options, callback) {
|
|
|
|
|
assert.strictEqual(typeof app, 'object');
|
|
|
|
|
assert.strictEqual(typeof options, 'object');
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
|
|
|
|
var appId = app.id;
|
2015-10-15 16:31:45 -07:00
|
|
|
var id = 'cid-' + uuid.v4();
|
2015-10-15 12:50:48 +02:00
|
|
|
var scope = 'profile';
|
2015-10-09 11:44:32 +02:00
|
|
|
|
|
|
|
|
debugApp(app, 'setupSimpleAuth: id:%s', id);
|
|
|
|
|
|
2015-10-15 16:31:45 -07:00
|
|
|
clientdb.delByAppIdAndType(app.id, clientdb.TYPE_SIMPLE_AUTH, function (error) { // remove existing creds
|
2015-10-09 11:44:32 +02:00
|
|
|
if (error && error.reason !== DatabaseError.NOT_FOUND) return callback(error);
|
|
|
|
|
|
2015-10-15 16:31:45 -07:00
|
|
|
clientdb.add(id, appId, clientdb.TYPE_SIMPLE_AUTH, '', '', scope, function (error) {
|
2015-10-09 11:44:32 +02:00
|
|
|
if (error) return callback(error);
|
|
|
|
|
|
|
|
|
|
var env = [
|
2015-10-11 13:36:55 +02:00
|
|
|
'SIMPLE_AUTH_SERVER=172.17.42.1',
|
|
|
|
|
'SIMPLE_AUTH_PORT=' + config.get('simpleAuthPort'),
|
2015-10-13 08:39:13 -07:00
|
|
|
'SIMPLE_AUTH_URL=http://172.17.42.1:' + config.get('simpleAuthPort'), // obsolete, remove
|
|
|
|
|
'SIMPLE_AUTH_ORIGIN=http://172.17.42.1:' + config.get('simpleAuthPort'),
|
2015-10-11 13:36:55 +02:00
|
|
|
'SIMPLE_AUTH_CLIENT_ID=' + id
|
2015-10-09 11:44:32 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
debugApp(app, 'Setting simple auth addon config to %j', env);
|
|
|
|
|
|
|
|
|
|
appdb.setAddonConfig(appId, 'simpleauth', env, callback);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function teardownSimpleAuth(app, options, callback) {
|
|
|
|
|
assert.strictEqual(typeof app, 'object');
|
|
|
|
|
assert.strictEqual(typeof options, 'object');
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
|
|
|
|
debugApp(app, 'teardownSimpleAuth');
|
|
|
|
|
|
2015-10-15 16:31:45 -07:00
|
|
|
clientdb.delByAppIdAndType(app.id, clientdb.TYPE_SIMPLE_AUTH, function (error) {
|
2015-10-09 11:44:32 +02:00
|
|
|
if (error && error.reason !== DatabaseError.NOT_FOUND) console.error(error);
|
|
|
|
|
|
|
|
|
|
appdb.unsetAddonConfig(app.id, 'simpleauth', callback);
|
2015-07-20 00:09:47 -07:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-07 16:10:08 -07:00
|
|
|
function setupLdap(app, options, callback) {
|
2015-07-20 00:09:47 -07:00
|
|
|
assert.strictEqual(typeof app, 'object');
|
2015-10-07 16:10:08 -07:00
|
|
|
assert.strictEqual(typeof options, 'object');
|
2015-07-20 00:09:47 -07:00
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
|
|
|
|
var env = [
|
|
|
|
|
'LDAP_SERVER=172.17.42.1',
|
2015-10-11 13:37:53 +02:00
|
|
|
'LDAP_PORT=' + config.get('ldapPort'),
|
|
|
|
|
'LDAP_URL=ldap://172.17.42.1:' + config.get('ldapPort'),
|
2015-07-20 00:09:47 -07:00
|
|
|
'LDAP_USERS_BASE_DN=ou=users,dc=cloudron',
|
2015-09-25 21:17:48 -07:00
|
|
|
'LDAP_GROUPS_BASE_DN=ou=groups,dc=cloudron',
|
|
|
|
|
'LDAP_BIND_DN=cn='+ app.id + ',ou=apps,dc=cloudron',
|
|
|
|
|
'LDAP_BIND_PASSWORD=' + hat(256) // this is ignored
|
2015-07-20 00:09:47 -07:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
debugApp(app, 'Setting up LDAP');
|
|
|
|
|
|
|
|
|
|
appdb.setAddonConfig(app.id, 'ldap', env, callback);
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-07 16:10:08 -07:00
|
|
|
function teardownLdap(app, options, callback) {
|
2015-07-20 00:09:47 -07:00
|
|
|
assert.strictEqual(typeof app, 'object');
|
2015-10-07 16:10:08 -07:00
|
|
|
assert.strictEqual(typeof options, 'object');
|
2015-07-20 00:09:47 -07:00
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
|
|
|
|
debugApp(app, 'Tearing down LDAP');
|
|
|
|
|
|
|
|
|
|
appdb.unsetAddonConfig(app.id, 'ldap', callback);
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-07 16:10:08 -07:00
|
|
|
function setupSendMail(app, options, callback) {
|
2015-07-20 00:09:47 -07:00
|
|
|
assert.strictEqual(typeof app, 'object');
|
2015-10-07 16:10:08 -07:00
|
|
|
assert.strictEqual(typeof options, 'object');
|
2015-07-20 00:09:47 -07:00
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
|
|
|
|
var env = [
|
|
|
|
|
'MAIL_SMTP_SERVER=mail',
|
2015-10-09 09:13:50 -07:00
|
|
|
'MAIL_SMTP_PORT=2500', // if you change this, change the mail container
|
2015-07-20 00:09:47 -07:00
|
|
|
'MAIL_SMTP_USERNAME=' + (app.location || app.id), // use app.id for bare domains
|
|
|
|
|
'MAIL_DOMAIN=' + config.fqdn()
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
debugApp(app, 'Setting up sendmail');
|
|
|
|
|
|
|
|
|
|
appdb.setAddonConfig(app.id, 'sendmail', env, callback);
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-07 16:10:08 -07:00
|
|
|
function teardownSendMail(app, options, callback) {
|
2015-07-20 00:09:47 -07:00
|
|
|
assert.strictEqual(typeof app, 'object');
|
2015-10-07 16:10:08 -07:00
|
|
|
assert.strictEqual(typeof options, 'object');
|
2015-07-20 00:09:47 -07:00
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
|
|
|
|
debugApp(app, 'Tearing down sendmail');
|
|
|
|
|
|
|
|
|
|
appdb.unsetAddonConfig(app.id, 'sendmail', callback);
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-07 16:10:08 -07:00
|
|
|
function setupMySql(app, options, callback) {
|
2015-07-20 00:09:47 -07:00
|
|
|
assert.strictEqual(typeof app, 'object');
|
2015-10-07 16:10:08 -07:00
|
|
|
assert.strictEqual(typeof options, 'object');
|
2015-07-20 00:09:47 -07:00
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
|
|
|
|
debugApp(app, 'Setting up mysql');
|
|
|
|
|
|
|
|
|
|
var container = docker.getContainer('mysql');
|
|
|
|
|
var cmd = [ '/addons/mysql/service.sh', 'add', app.id ];
|
|
|
|
|
|
|
|
|
|
container.exec({ Cmd: cmd, AttachStdout: true, AttachStderr: true }, function (error, execContainer) {
|
|
|
|
|
if (error) return callback(error);
|
|
|
|
|
|
|
|
|
|
execContainer.start(function (error, stream) {
|
|
|
|
|
if (error) return callback(error);
|
|
|
|
|
|
|
|
|
|
var stdout = new MemoryStream();
|
|
|
|
|
var stderr = new MemoryStream();
|
|
|
|
|
|
|
|
|
|
execContainer.modem.demuxStream(stream, stdout, stderr);
|
|
|
|
|
stderr.on('data', function (data) { debugApp(app, data.toString('utf8')); }); // set -e output
|
|
|
|
|
|
|
|
|
|
var chunks = [ ];
|
|
|
|
|
stdout.on('data', function (chunk) { chunks.push(chunk); });
|
|
|
|
|
|
|
|
|
|
stream.on('error', callback);
|
|
|
|
|
stream.on('end', function () {
|
|
|
|
|
var env = Buffer.concat(chunks).toString('utf8').split('\n').slice(0, -1); // remove trailing newline
|
|
|
|
|
debugApp(app, 'Setting mysql addon config to %j', env);
|
|
|
|
|
appdb.setAddonConfig(app.id, 'mysql', env, callback);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-07 16:10:08 -07:00
|
|
|
function teardownMySql(app, options, callback) {
|
|
|
|
|
assert.strictEqual(typeof app, 'object');
|
|
|
|
|
assert.strictEqual(typeof options, 'object');
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
2015-07-20 00:09:47 -07:00
|
|
|
var container = docker.getContainer('mysql');
|
|
|
|
|
var cmd = [ '/addons/mysql/service.sh', 'remove', app.id ];
|
|
|
|
|
|
|
|
|
|
debugApp(app, 'Tearing down mysql');
|
|
|
|
|
|
|
|
|
|
container.exec({ Cmd: cmd, AttachStdout: true, AttachStderr: true }, function (error, execContainer) {
|
|
|
|
|
if (error) return callback(error);
|
|
|
|
|
|
|
|
|
|
execContainer.start(function (error, stream) {
|
|
|
|
|
if (error) return callback(error);
|
|
|
|
|
|
|
|
|
|
var data = '';
|
|
|
|
|
stream.on('error', callback);
|
|
|
|
|
stream.on('data', function (d) { data += d.toString('utf8'); });
|
|
|
|
|
stream.on('end', function () {
|
|
|
|
|
appdb.unsetAddonConfig(app.id, 'mysql', callback);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-07 16:10:08 -07:00
|
|
|
function backupMySql(app, options, callback) {
|
2015-07-20 00:09:47 -07:00
|
|
|
debugApp(app, 'Backing up mysql');
|
|
|
|
|
|
|
|
|
|
callback = once(callback); // ChildProcess exit may or may not be called after error
|
|
|
|
|
|
|
|
|
|
var output = fs.createWriteStream(path.join(paths.DATA_DIR, app.id, 'mysqldump'));
|
|
|
|
|
output.on('error', callback);
|
|
|
|
|
|
|
|
|
|
var cp = spawn('/usr/bin/docker', [ 'exec', 'mysql', '/addons/mysql/service.sh', 'backup', app.id ]);
|
|
|
|
|
cp.on('error', callback);
|
|
|
|
|
cp.on('exit', function (code, signal) {
|
|
|
|
|
debugApp(app, 'backupMySql: done. code:%s signal:%s', code, signal);
|
|
|
|
|
if (!callback.called) callback(code ? 'backupMySql failed with status ' + code : null);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
cp.stdout.pipe(output);
|
|
|
|
|
cp.stderr.pipe(process.stderr);
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-07 16:10:08 -07:00
|
|
|
function restoreMySql(app, options, callback) {
|
2015-07-20 00:09:47 -07:00
|
|
|
callback = once(callback); // ChildProcess exit may or may not be called after error
|
|
|
|
|
|
2015-10-08 02:08:27 -07:00
|
|
|
setupMySql(app, options, function (error) {
|
2015-07-20 00:09:47 -07:00
|
|
|
if (error) return callback(error);
|
|
|
|
|
|
|
|
|
|
debugApp(app, 'restoreMySql');
|
|
|
|
|
|
|
|
|
|
var input = fs.createReadStream(path.join(paths.DATA_DIR, app.id, 'mysqldump'));
|
|
|
|
|
input.on('error', callback);
|
|
|
|
|
|
|
|
|
|
// cannot get this to work through docker.exec
|
|
|
|
|
var cp = spawn('/usr/bin/docker', [ 'exec', '-i', 'mysql', '/addons/mysql/service.sh', 'restore', app.id ]);
|
|
|
|
|
cp.on('error', callback);
|
|
|
|
|
cp.on('exit', function (code, signal) {
|
|
|
|
|
debugApp(app, 'restoreMySql: done %s %s', code, signal);
|
|
|
|
|
if (!callback.called) callback(code ? 'restoreMySql failed with status ' + code : null);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
cp.stdout.pipe(process.stdout);
|
|
|
|
|
cp.stderr.pipe(process.stderr);
|
|
|
|
|
input.pipe(cp.stdin).on('error', callback);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-07 16:10:08 -07:00
|
|
|
function setupPostgreSql(app, options, callback) {
|
2015-07-20 00:09:47 -07:00
|
|
|
assert.strictEqual(typeof app, 'object');
|
2015-10-07 16:10:08 -07:00
|
|
|
assert.strictEqual(typeof options, 'object');
|
2015-07-20 00:09:47 -07:00
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
|
|
|
|
debugApp(app, 'Setting up postgresql');
|
|
|
|
|
|
|
|
|
|
var container = docker.getContainer('postgresql');
|
|
|
|
|
var cmd = [ '/addons/postgresql/service.sh', 'add', app.id ];
|
|
|
|
|
|
|
|
|
|
container.exec({ Cmd: cmd, AttachStdout: true, AttachStderr: true }, function (error, execContainer) {
|
|
|
|
|
if (error) return callback(error);
|
|
|
|
|
|
|
|
|
|
execContainer.start(function (error, stream) {
|
|
|
|
|
if (error) return callback(error);
|
|
|
|
|
|
|
|
|
|
var stdout = new MemoryStream();
|
|
|
|
|
var stderr = new MemoryStream();
|
|
|
|
|
|
|
|
|
|
execContainer.modem.demuxStream(stream, stdout, stderr);
|
|
|
|
|
stderr.on('data', function (data) { debugApp(app, data.toString('utf8')); }); // set -e output
|
|
|
|
|
|
|
|
|
|
var chunks = [ ];
|
|
|
|
|
stdout.on('data', function (chunk) { chunks.push(chunk); });
|
|
|
|
|
|
|
|
|
|
stream.on('error', callback);
|
|
|
|
|
stream.on('end', function () {
|
|
|
|
|
var env = Buffer.concat(chunks).toString('utf8').split('\n').slice(0, -1); // remove trailing newline
|
|
|
|
|
debugApp(app, 'Setting postgresql addon config to %j', env);
|
|
|
|
|
appdb.setAddonConfig(app.id, 'postgresql', env, callback);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-07 16:10:08 -07:00
|
|
|
function teardownPostgreSql(app, options, callback) {
|
|
|
|
|
assert.strictEqual(typeof app, 'object');
|
|
|
|
|
assert.strictEqual(typeof options, 'object');
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
2015-07-20 00:09:47 -07:00
|
|
|
var container = docker.getContainer('postgresql');
|
|
|
|
|
var cmd = [ '/addons/postgresql/service.sh', 'remove', app.id ];
|
|
|
|
|
|
|
|
|
|
debugApp(app, 'Tearing down postgresql');
|
|
|
|
|
|
|
|
|
|
container.exec({ Cmd: cmd, AttachStdout: true, AttachStderr: true }, function (error, execContainer) {
|
|
|
|
|
if (error) return callback(error);
|
|
|
|
|
|
|
|
|
|
execContainer.start(function (error, stream) {
|
|
|
|
|
if (error) return callback(error);
|
|
|
|
|
|
|
|
|
|
var data = '';
|
|
|
|
|
stream.on('error', callback);
|
|
|
|
|
stream.on('data', function (d) { data += d.toString('utf8'); });
|
|
|
|
|
stream.on('end', function () {
|
|
|
|
|
appdb.unsetAddonConfig(app.id, 'postgresql', callback);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-07 16:10:08 -07:00
|
|
|
function backupPostgreSql(app, options, callback) {
|
2015-07-20 00:09:47 -07:00
|
|
|
debugApp(app, 'Backing up postgresql');
|
|
|
|
|
|
|
|
|
|
callback = once(callback); // ChildProcess exit may or may not be called after error
|
|
|
|
|
|
|
|
|
|
var output = fs.createWriteStream(path.join(paths.DATA_DIR, app.id, 'postgresqldump'));
|
|
|
|
|
output.on('error', callback);
|
|
|
|
|
|
|
|
|
|
var cp = spawn('/usr/bin/docker', [ 'exec', 'postgresql', '/addons/postgresql/service.sh', 'backup', app.id ]);
|
|
|
|
|
cp.on('error', callback);
|
|
|
|
|
cp.on('exit', function (code, signal) {
|
|
|
|
|
debugApp(app, 'backupPostgreSql: done %s %s', code, signal);
|
|
|
|
|
if (!callback.called) callback(code ? 'backupPostgreSql failed with status ' + code : null);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
cp.stdout.pipe(output);
|
|
|
|
|
cp.stderr.pipe(process.stderr);
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-07 16:10:08 -07:00
|
|
|
function restorePostgreSql(app, options, callback) {
|
2015-07-20 00:09:47 -07:00
|
|
|
callback = once(callback); // ChildProcess exit may or may not be called after error
|
|
|
|
|
|
2015-10-08 02:08:27 -07:00
|
|
|
setupPostgreSql(app, options, function (error) {
|
2015-07-20 00:09:47 -07:00
|
|
|
if (error) return callback(error);
|
|
|
|
|
|
|
|
|
|
debugApp(app, 'restorePostgreSql');
|
|
|
|
|
|
|
|
|
|
var input = fs.createReadStream(path.join(paths.DATA_DIR, app.id, 'postgresqldump'));
|
|
|
|
|
input.on('error', callback);
|
|
|
|
|
|
|
|
|
|
// cannot get this to work through docker.exec
|
|
|
|
|
var cp = spawn('/usr/bin/docker', [ 'exec', '-i', 'postgresql', '/addons/postgresql/service.sh', 'restore', app.id ]);
|
|
|
|
|
cp.on('error', callback);
|
|
|
|
|
cp.on('exit', function (code, signal) {
|
|
|
|
|
debugApp(app, 'restorePostgreSql: done %s %s', code, signal);
|
|
|
|
|
if (!callback.called) callback(code ? 'restorePostgreSql failed with status ' + code : null);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
cp.stdout.pipe(process.stdout);
|
|
|
|
|
cp.stderr.pipe(process.stderr);
|
|
|
|
|
input.pipe(cp.stdin).on('error', callback);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-07 16:10:08 -07:00
|
|
|
function setupMongoDb(app, options, callback) {
|
2015-07-20 00:09:47 -07:00
|
|
|
assert.strictEqual(typeof app, 'object');
|
2015-10-07 16:10:08 -07:00
|
|
|
assert.strictEqual(typeof options, 'object');
|
2015-07-20 00:09:47 -07:00
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
|
|
|
|
debugApp(app, 'Setting up mongodb');
|
|
|
|
|
|
|
|
|
|
var container = docker.getContainer('mongodb');
|
|
|
|
|
var cmd = [ '/addons/mongodb/service.sh', 'add', app.id ];
|
|
|
|
|
|
|
|
|
|
container.exec({ Cmd: cmd, AttachStdout: true, AttachStderr: true }, function (error, execContainer) {
|
|
|
|
|
if (error) return callback(error);
|
|
|
|
|
|
|
|
|
|
execContainer.start(function (error, stream) {
|
|
|
|
|
if (error) return callback(error);
|
|
|
|
|
|
|
|
|
|
var stdout = new MemoryStream();
|
|
|
|
|
var stderr = new MemoryStream();
|
|
|
|
|
|
|
|
|
|
execContainer.modem.demuxStream(stream, stdout, stderr);
|
|
|
|
|
stderr.on('data', function (data) { debugApp(app, data.toString('utf8')); }); // set -e output
|
|
|
|
|
|
|
|
|
|
var chunks = [ ];
|
|
|
|
|
stdout.on('data', function (chunk) { chunks.push(chunk); });
|
|
|
|
|
|
|
|
|
|
stream.on('error', callback);
|
|
|
|
|
stream.on('end', function () {
|
|
|
|
|
var env = Buffer.concat(chunks).toString('utf8').split('\n').slice(0, -1); // remove trailing newline
|
|
|
|
|
debugApp(app, 'Setting mongodb addon config to %j', env);
|
|
|
|
|
appdb.setAddonConfig(app.id, 'mongodb', env, callback);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-07 16:10:08 -07:00
|
|
|
function teardownMongoDb(app, options, callback) {
|
|
|
|
|
assert.strictEqual(typeof app, 'object');
|
|
|
|
|
assert.strictEqual(typeof options, 'object');
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
2015-07-20 00:09:47 -07:00
|
|
|
var container = docker.getContainer('mongodb');
|
|
|
|
|
var cmd = [ '/addons/mongodb/service.sh', 'remove', app.id ];
|
|
|
|
|
|
|
|
|
|
debugApp(app, 'Tearing down mongodb');
|
|
|
|
|
|
|
|
|
|
container.exec({ Cmd: cmd, AttachStdout: true, AttachStderr: true }, function (error, execContainer) {
|
|
|
|
|
if (error) return callback(error);
|
|
|
|
|
|
|
|
|
|
execContainer.start(function (error, stream) {
|
|
|
|
|
if (error) return callback(error);
|
|
|
|
|
|
|
|
|
|
var data = '';
|
|
|
|
|
stream.on('error', callback);
|
|
|
|
|
stream.on('data', function (d) { data += d.toString('utf8'); });
|
|
|
|
|
stream.on('end', function () {
|
|
|
|
|
appdb.unsetAddonConfig(app.id, 'mongodb', callback);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-07 16:10:08 -07:00
|
|
|
function backupMongoDb(app, options, callback) {
|
2015-07-20 00:09:47 -07:00
|
|
|
debugApp(app, 'Backing up mongodb');
|
|
|
|
|
|
|
|
|
|
callback = once(callback); // ChildProcess exit may or may not be called after error
|
|
|
|
|
|
|
|
|
|
var output = fs.createWriteStream(path.join(paths.DATA_DIR, app.id, 'mongodbdump'));
|
|
|
|
|
output.on('error', callback);
|
|
|
|
|
|
|
|
|
|
var cp = spawn('/usr/bin/docker', [ 'exec', 'mongodb', '/addons/mongodb/service.sh', 'backup', app.id ]);
|
|
|
|
|
cp.on('error', callback);
|
|
|
|
|
cp.on('exit', function (code, signal) {
|
|
|
|
|
debugApp(app, 'backupMongoDb: done %s %s', code, signal);
|
|
|
|
|
if (!callback.called) callback(code ? 'backupMongoDb failed with status ' + code : null);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
cp.stdout.pipe(output);
|
|
|
|
|
cp.stderr.pipe(process.stderr);
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-07 16:10:08 -07:00
|
|
|
function restoreMongoDb(app, options, callback) {
|
2015-07-20 00:09:47 -07:00
|
|
|
callback = once(callback); // ChildProcess exit may or may not be called after error
|
|
|
|
|
|
2015-10-08 02:08:27 -07:00
|
|
|
setupMongoDb(app, options, function (error) {
|
2015-07-20 00:09:47 -07:00
|
|
|
if (error) return callback(error);
|
|
|
|
|
|
|
|
|
|
debugApp(app, 'restoreMongoDb');
|
|
|
|
|
|
|
|
|
|
var input = fs.createReadStream(path.join(paths.DATA_DIR, app.id, 'mongodbdump'));
|
|
|
|
|
input.on('error', callback);
|
|
|
|
|
|
|
|
|
|
// cannot get this to work through docker.exec
|
|
|
|
|
var cp = spawn('/usr/bin/docker', [ 'exec', '-i', 'mongodb', '/addons/mongodb/service.sh', 'restore', app.id ]);
|
|
|
|
|
cp.on('error', callback);
|
|
|
|
|
cp.on('exit', function (code, signal) {
|
|
|
|
|
debugApp(app, 'restoreMongoDb: done %s %s', code, signal);
|
|
|
|
|
if (!callback.called) callback(code ? 'restoreMongoDb failed with status ' + code : null);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
cp.stdout.pipe(process.stdout);
|
|
|
|
|
cp.stderr.pipe(process.stderr);
|
|
|
|
|
input.pipe(cp.stdin).on('error', callback);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function forwardRedisPort(appId, callback) {
|
|
|
|
|
assert.strictEqual(typeof appId, 'string');
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
|
|
|
|
docker.getContainer('redis-' + appId).inspect(function (error, data) {
|
|
|
|
|
if (error) return callback(new Error('Unable to inspect container:' + error));
|
|
|
|
|
|
|
|
|
|
var redisPort = parseInt(safe.query(data, 'NetworkSettings.Ports.6379/tcp[0].HostPort'), 10);
|
|
|
|
|
if (!Number.isInteger(redisPort)) return callback(new Error('Unable to get container port mapping'));
|
|
|
|
|
|
|
|
|
|
return callback(null);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-11 12:08:32 -07:00
|
|
|
function stopAndRemoveRedis(container, callback) {
|
|
|
|
|
function ignoreError(func) {
|
|
|
|
|
return function (callback) {
|
|
|
|
|
func(function (error) {
|
|
|
|
|
if (error) debug('stopAndRemoveRedis: Ignored error:', error);
|
|
|
|
|
callback();
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// stopping redis with SIGTERM makes it commit the database to disk
|
|
|
|
|
async.series([
|
|
|
|
|
ignoreError(container.stop.bind(container, { t: 10 })),
|
|
|
|
|
ignoreError(container.wait.bind(container)),
|
|
|
|
|
ignoreError(container.remove.bind(container, { force: true, v: true }))
|
|
|
|
|
], callback);
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-20 00:09:47 -07:00
|
|
|
// Ensures that app's addon redis container is running. Can be called when named container already exists/running
|
2015-10-07 16:10:08 -07:00
|
|
|
function setupRedis(app, options, callback) {
|
|
|
|
|
assert.strictEqual(typeof app, 'object');
|
|
|
|
|
assert.strictEqual(typeof options, 'object');
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
2015-07-20 00:09:47 -07:00
|
|
|
var redisPassword = generatePassword(64, false /* memorable */);
|
|
|
|
|
var redisVarsFile = path.join(paths.ADDON_CONFIG_DIR, 'redis-' + app.id + '_vars.sh');
|
|
|
|
|
var redisDataDir = path.join(paths.DATA_DIR, app.id + '/redis');
|
|
|
|
|
|
|
|
|
|
if (!safe.fs.writeFileSync(redisVarsFile, 'REDIS_PASSWORD=' + redisPassword)) {
|
|
|
|
|
return callback(new Error('Error writing redis config'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!safe.fs.mkdirSync(redisDataDir) && safe.error.code !== 'EEXIST') return callback(new Error('Error creating redis data dir:' + safe.error));
|
|
|
|
|
|
|
|
|
|
var createOptions = {
|
|
|
|
|
name: 'redis-' + app.id,
|
|
|
|
|
Hostname: config.appFqdn(app.location),
|
|
|
|
|
Tty: true,
|
2015-10-15 11:00:39 -07:00
|
|
|
Image: 'cloudron/redis:0.7.0', // if you change this, fix setup/INFRA_VERSION as well
|
2015-07-20 00:09:47 -07:00
|
|
|
Cmd: null,
|
2015-10-08 08:46:19 -07:00
|
|
|
Volumes: {
|
|
|
|
|
'/tmp': {},
|
2015-10-15 11:00:39 -07:00
|
|
|
'/run': {}
|
2015-10-08 08:46:19 -07:00
|
|
|
},
|
2015-10-19 21:35:02 -07:00
|
|
|
VolumesFrom: [],
|
|
|
|
|
HostConfig: {
|
|
|
|
|
Binds: [
|
|
|
|
|
redisVarsFile + ':/etc/redis/redis_vars.sh:ro',
|
|
|
|
|
redisDataDir + ':/var/lib/redis:rw'
|
|
|
|
|
],
|
|
|
|
|
Memory: 1024 * 1024 * 75, // 100mb
|
|
|
|
|
MemorySwap: 1024 * 1024 * 75 * 2, // 150mb
|
|
|
|
|
PortBindings: {
|
|
|
|
|
'6379/tcp': [{ HostPort: '0', HostIp: '127.0.0.1' }]
|
|
|
|
|
},
|
|
|
|
|
ReadonlyRootfs: true,
|
|
|
|
|
RestartPolicy: {
|
|
|
|
|
'Name': 'always',
|
|
|
|
|
'MaximumRetryCount': 0
|
|
|
|
|
}
|
2015-07-20 00:09:47 -07:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var env = [
|
|
|
|
|
'REDIS_URL=redis://redisuser:' + redisPassword + '@redis-' + app.id,
|
|
|
|
|
'REDIS_PASSWORD=' + redisPassword,
|
|
|
|
|
'REDIS_HOST=redis-' + app.id,
|
|
|
|
|
'REDIS_PORT=6379'
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
var redisContainer = docker.getContainer(createOptions.name);
|
2015-10-11 12:08:32 -07:00
|
|
|
stopAndRemoveRedis(redisContainer, function () {
|
2015-07-20 00:09:47 -07:00
|
|
|
docker.createContainer(createOptions, function (error) {
|
|
|
|
|
if (error && error.statusCode !== 409) return callback(error); // if not already created
|
|
|
|
|
|
2015-10-19 21:35:02 -07:00
|
|
|
redisContainer.start(function (error) {
|
2015-07-20 00:09:47 -07:00
|
|
|
if (error && error.statusCode !== 304) return callback(error); // if not already running
|
|
|
|
|
|
|
|
|
|
appdb.setAddonConfig(app.id, 'redis', env, function (error) {
|
|
|
|
|
if (error) return callback(error);
|
|
|
|
|
|
|
|
|
|
forwardRedisPort(app.id, callback);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-07 16:10:08 -07:00
|
|
|
function teardownRedis(app, options, callback) {
|
|
|
|
|
assert.strictEqual(typeof app, 'object');
|
|
|
|
|
assert.strictEqual(typeof options, 'object');
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
2015-07-20 00:09:47 -07:00
|
|
|
var container = docker.getContainer('redis-' + app.id);
|
|
|
|
|
|
|
|
|
|
var removeOptions = {
|
|
|
|
|
force: true, // kill container if it's running
|
2015-10-11 11:37:05 -07:00
|
|
|
v: true // removes volumes associated with the container
|
2015-07-20 00:09:47 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
container.remove(removeOptions, function (error) {
|
|
|
|
|
if (error && error.statusCode !== 404) return callback(new Error('Error removing container:' + error));
|
|
|
|
|
|
|
|
|
|
safe.fs.unlinkSync(paths.ADDON_CONFIG_DIR, 'redis-' + app.id + '_vars.sh');
|
|
|
|
|
|
|
|
|
|
shell.sudo('teardownRedis', [ RMAPPDIR_CMD, app.id + '/redis' ], function (error, stdout, stderr) {
|
|
|
|
|
if (error) return callback(new Error('Error removing redis data:' + error));
|
|
|
|
|
|
|
|
|
|
appdb.unsetAddonConfig(app.id, 'redis', callback);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
2015-10-12 13:29:27 -07:00
|
|
|
|
|
|
|
|
function backupRedis(app, options, callback) {
|
|
|
|
|
debugApp(app, 'Backing up redis');
|
|
|
|
|
|
|
|
|
|
callback = once(callback); // ChildProcess exit may or may not be called after error
|
|
|
|
|
|
|
|
|
|
var cp = spawn('/usr/bin/docker', [ 'exec', 'redis-' + app.id, '/addons/redis/service.sh', 'backup' ]);
|
|
|
|
|
cp.on('error', callback);
|
|
|
|
|
cp.on('exit', function (code, signal) {
|
|
|
|
|
debugApp(app, 'backupRedis: done. code:%s signal:%s', code, signal);
|
|
|
|
|
if (!callback.called) callback(code ? 'backupRedis failed with status ' + code : null);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
cp.stdout.pipe(process.stdout);
|
|
|
|
|
cp.stderr.pipe(process.stderr);
|
|
|
|
|
}
|