1
0
mirror of https://github.com/yldio/copilot.git synced 2024-11-15 07:40:07 +02:00
copilot/docker/api/bootstrap-data.js

98 lines
2.1 KiB
JavaScript
Raw Normal View History

2017-06-23 19:24:25 +03:00
'use strict';
2017-06-29 17:38:30 +03:00
const Triton = require('triton');
const Url = require('url');
const Path = require('path');
const Fs = require('fs');
2017-06-23 19:24:25 +03:00
2017-07-17 17:45:14 +03:00
const Data = require('./lib/data');
2017-06-23 19:24:25 +03:00
2017-06-29 17:38:30 +03:00
const {
DOCKER_HOST,
DOCKER_CERT_PATH,
SDC_URL,
SDC_ACCOUNT,
SDC_KEY_ID
} = process.env;
2017-07-17 17:45:14 +03:00
const DOCKER_HOST_URL = DOCKER_HOST ? Url.parse(DOCKER_HOST) : {};
2017-06-29 17:38:30 +03:00
const settings = {
db: {
host: process.env.RETHINK_HOST || 'localhost'
},
docker: {
protocol: 'https',
host: DOCKER_HOST_URL.hostname,
port: DOCKER_HOST_URL.port,
2017-07-17 17:45:14 +03:00
ca: DOCKER_CERT_PATH
? Fs.readFileSync(Path.join(DOCKER_CERT_PATH, 'ca.pem'))
: undefined,
cert: DOCKER_CERT_PATH
? Fs.readFileSync(Path.join(DOCKER_CERT_PATH, 'cert.pem'))
: undefined,
key: DOCKER_CERT_PATH
? Fs.readFileSync(Path.join(DOCKER_CERT_PATH, 'key.pem'))
: undefined
2017-06-29 17:38:30 +03:00
},
triton: {
url: SDC_URL,
account: SDC_ACCOUNT,
keyId: SDC_KEY_ID
}
};
2017-07-17 17:45:14 +03:00
const ifError = function(err) {
2017-06-23 19:24:25 +03:00
if (err) {
console.error(err);
process.exit(1);
}
};
2017-07-17 17:45:14 +03:00
const bootstrap = function() {
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-07-17 17:45:14 +03:00
data.connect(err => {
2017-06-29 17:38:30 +03:00
ifError(err);
2017-06-23 19:24:25 +03:00
data.createDatacenter({ region, name: region }, (err, datacenter) => {
ifError(err);
2017-07-17 17:45:14 +03:00
Triton.createClient(
{
profile: settings.triton
},
(err, { cloudapi }) => {
2017-06-23 19:24:25 +03:00
ifError(err);
2017-06-29 17:38:30 +03:00
2017-07-17 17:45:14 +03:00
cloudapi.getAccount((err, { firstName, lastName, email, login }) => {
2017-06-29 17:38:30 +03:00
ifError(err);
2017-07-17 17:45:14 +03:00
data.createUser(
{ firstName, lastName, email, login },
(err, user) => {
ifError(err);
data.createPortal(
{
user,
datacenter
},
(err, portal) => {
ifError(err);
console.log('data bootstrapped');
process.exit(0);
}
);
}
);
2017-06-29 17:38:30 +03:00
});
2017-07-17 17:45:14 +03:00
}
);
2017-06-23 19:24:25 +03:00
});
});
};
bootstrap();