2017-06-23 19:24:25 +03:00
|
|
|
'use strict';
|
|
|
|
|
2017-08-21 20:03:17 +03:00
|
|
|
const Data = require('portal-api/lib/data');
|
|
|
|
const Fs = require('fs');
|
|
|
|
const Path = require('path');
|
|
|
|
const Piloted = require('piloted');
|
2017-06-29 17:38:30 +03:00
|
|
|
const Triton = require('triton');
|
|
|
|
const Url = require('url');
|
2017-06-23 19:24:25 +03:00
|
|
|
|
|
|
|
|
2017-08-21 20:03:17 +03:00
|
|
|
let timeoutId;
|
|
|
|
const loadConfig = function () {
|
|
|
|
const docker = Piloted.service('docker-compose-api');
|
|
|
|
const rethink = Piloted.service('rethinkdb');
|
|
|
|
|
2017-09-13 04:39:39 +03:00
|
|
|
const retry = () => {
|
2017-08-21 20:03:17 +03:00
|
|
|
timeoutId = setTimeout(() => {
|
|
|
|
timeoutId = null;
|
|
|
|
Piloted.refresh();
|
|
|
|
}, 1000);
|
2017-09-13 04:39:39 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
if (docker && rethink) {
|
|
|
|
bootstrap({ docker, rethink }, (err) => {
|
|
|
|
if (err) {
|
|
|
|
console.error(err);
|
|
|
|
return retry();
|
|
|
|
}
|
|
|
|
|
|
|
|
process.exit(0);
|
|
|
|
});
|
|
|
|
} else if (!timeoutId) {
|
|
|
|
retry();
|
2017-06-23 19:24:25 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-08-21 20:03:17 +03:00
|
|
|
Piloted.on('refresh', () => {
|
|
|
|
loadConfig();
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2017-09-13 04:39:39 +03:00
|
|
|
const bootstrap = function ({ docker, rethink }, cb) {
|
2017-08-21 20:03:17 +03:00
|
|
|
const settings = {
|
|
|
|
db: {
|
|
|
|
host: rethink.address
|
|
|
|
},
|
|
|
|
docker: {
|
|
|
|
protocol: 'https',
|
|
|
|
host: docker.address,
|
|
|
|
port: docker.port,
|
|
|
|
ca: process.env.DOCKER_CERT_PATH
|
|
|
|
? Fs.readFileSync(Path.join(process.env.DOCKER_CERT_PATH, 'ca.pem'))
|
|
|
|
: undefined,
|
|
|
|
cert: process.env.DOCKER_CERT_PATH
|
|
|
|
? Fs.readFileSync(Path.join(process.env.DOCKER_CERT_PATH, 'cert.pem'))
|
|
|
|
: undefined,
|
|
|
|
key: process.env.DOCKER_CERT_PATH
|
|
|
|
? Fs.readFileSync(Path.join(process.env.DOCKER_CERT_PATH, 'key.pem'))
|
|
|
|
: undefined
|
|
|
|
},
|
|
|
|
triton: {
|
|
|
|
url: process.env.SDC_URL,
|
|
|
|
account: process.env.SDC_ACCOUNT,
|
|
|
|
keyId: process.env.SDC_KEY_ID
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-06-29 17:38:30 +03:00
|
|
|
const data = new Data(settings);
|
2017-06-23 19:24:25 +03:00
|
|
|
const region = process.env.TRITON_DC || 'us-sw-1';
|
|
|
|
|
2017-09-13 04:39:39 +03:00
|
|
|
data.connect((err) => {
|
2017-08-31 00:52:33 +03:00
|
|
|
if (err) {
|
2017-09-13 04:39:39 +03:00
|
|
|
return cb(err);
|
2017-08-31 00:52:33 +03:00
|
|
|
}
|
2017-06-29 17:38:30 +03:00
|
|
|
|
2017-09-08 20:07:54 +03:00
|
|
|
data.getDatacenters((err, datacenters) => {
|
2017-08-31 00:52:33 +03:00
|
|
|
if (err) {
|
2017-09-13 04:39:39 +03:00
|
|
|
return cb(err);
|
2017-08-31 00:52:33 +03:00
|
|
|
}
|
2017-06-23 19:24:25 +03:00
|
|
|
|
2017-09-13 04:39:39 +03:00
|
|
|
// Don't continue since data is already bootstrapped
|
2017-09-08 20:07:54 +03:00
|
|
|
if (datacenters && datacenters.length) {
|
2017-09-13 04:39:39 +03:00
|
|
|
return cb();
|
2017-09-08 20:07:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
data.createDatacenter({ region, name: region }, (err, datacenter) => {
|
|
|
|
if (err) {
|
2017-09-13 04:39:39 +03:00
|
|
|
return cb(err);
|
2017-09-08 20:07:54 +03:00
|
|
|
}
|
2017-06-29 17:38:30 +03:00
|
|
|
|
2017-09-08 20:07:54 +03:00
|
|
|
Triton.createClient(
|
|
|
|
{
|
|
|
|
profile: settings.triton
|
|
|
|
},
|
|
|
|
(err, { cloudapi }) => {
|
2017-08-31 00:52:33 +03:00
|
|
|
if (err) {
|
2017-09-13 04:39:39 +03:00
|
|
|
return cb(err);
|
2017-08-31 00:52:33 +03:00
|
|
|
}
|
2017-06-29 17:38:30 +03:00
|
|
|
|
2017-09-08 20:07:54 +03:00
|
|
|
cloudapi.getAccount((err, { firstName, lastName, email, login }) => {
|
|
|
|
if (err) {
|
2017-09-13 04:39:39 +03:00
|
|
|
return cb(err);
|
2017-09-08 20:07:54 +03:00
|
|
|
}
|
2017-08-31 00:52:33 +03:00
|
|
|
|
2017-09-08 20:07:54 +03:00
|
|
|
data.createUser(
|
|
|
|
{ firstName, lastName, email, login },
|
|
|
|
(err, user) => {
|
|
|
|
if (err) {
|
2017-09-13 04:39:39 +03:00
|
|
|
return cb(err);
|
2017-07-17 17:45:14 +03:00
|
|
|
}
|
2017-09-08 20:07:54 +03:00
|
|
|
|
|
|
|
data.createPortal(
|
|
|
|
{
|
|
|
|
user,
|
|
|
|
datacenter
|
|
|
|
},
|
|
|
|
(err, portal) => {
|
|
|
|
if (err) {
|
2017-09-13 04:39:39 +03:00
|
|
|
return cb(err);
|
2017-09-08 20:07:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
console.log('data bootstrapped');
|
2017-09-13 04:39:39 +03:00
|
|
|
cb();
|
2017-09-08 20:07:54 +03:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
2017-06-23 19:24:25 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-08-21 20:03:17 +03:00
|
|
|
loadConfig();
|