-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathmain.js
executable file
·149 lines (124 loc) · 4.21 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*jshint esversion: 6 */
var conman = require('./container-manager.js');
var Config = require('./config.json');
var fs = require('fs');
var httpsHelper = require('./include/containter-manger-https-helper');
var DATABOX_DEV = process.env.DATABOX_DEV
var request = require('request');
var containerMangerUIServer = null;
httpsHelper.init()
.then(cert => {
//Put the CA pubic key into this processes env var so libs that work in containers also work in the CM
process.env['CM_HTTPS_CA_ROOT_CERT'] = httpsHelper.getRootCert();
conman.setHttpsHelper(httpsHelper);
return conman.connect();
})
.then(data => {
return conman.killAll(data);
})
.then(() => {
return conman.initNetworks();
})
.then(() => {
if(DATABOX_DEV) {
const devSeedScript = './updateLocalRegistry.sh';
console.log('['+Config.localRegistryName+'] updating ' + devSeedScript);
var script = "";
for(img of Config.localRegistrySeedImages) {
script += "docker pull toshdatabox/"+img+" && docker tag toshdatabox/"+img+" "+ Config.registryUrl +"/"+img+" && docker push "+ Config.registryUrl +"/"+img+"\n";
}
fs.writeFileSync(devSeedScript, script);
console.log('['+Config.localRegistryName+'] Launching');
//launch in-order to preserve IPs
return conman.launchLocalRegistry();
}
})
.then(() => {
console.log('[databox-arbiter] Launching');
return conman.launchArbiter(httpsHelper);
})
.then(info => {
//set env vars in this process so libs that work in containers also work in the CM
process.env['DATABOX_ARBITER_ENDPOINT'] = 'https://' + info.name + ':' + info.port;
process.env['ARBITER_TOKEN'] = info.CM_KEY;
process.env['CM_KEY'] = info.CM_KEY;
//require here so env vars are set!
containerMangerUIServer = require('./server.js');
//set up the arbiter proxy
containerMangerUIServer.proxies[info.name] = info.name + ':' + info.port;
})
.then(() => {
if(DATABOX_DEV) {
//launch in-order to preserve IPs
console.log('['+Config.localAppStoreName+'] Launching');
return conman.launchLocalAppStore();
}
})
.then(()=>{
if(DATABOX_DEV) {
var req = request.defaults({jar: true});
req.get(Config.storeUrl_dev,(error,response,body)=>{
if(error) {
console.log("[seeding manifest] get app store root to log in", error);
}
proms = Config.localAppStoreSeedManifests.map((url)=>{
return new Promise(function(resolve, reject) {
req.get(url,(error,response,body)=>{
if(error) {
console.log("[seeding manifest] Failed to get manifest from" + url, error);
}
req.post({
uri: Config.storeUrl_dev + "/app/post",
method: "POST",
form: {"manifest": body}
}, (error,response,body) => {
if(error) {
console.log("[seeding manifest] Failed to POST manifest " + url, error);
} else {
console.log("[seeding manifest]" + url + " SUCCESS ",body);
resolve();
}
});
});
});
});
return Promise.all(proms);
});
}
})
.then(()=>{
//start the CM UI
console.log("Starting UI Server!!");
return containerMangerUIServer.launch(Config.serverPort, conman, httpsHelper);
})
/*.then(info => {
containerMangerUIServer.proxies[info.name] = container.name+':' + info.port;
console.log('[databox-notification] Launching');
return conman.launchNotifications(httpsHelper);
})*/
.then(() => {
console.log("--------- Launching saved containers ----------");
return conman.getActiveSLAs();
})
.then(slas => {
return conman.restoreContainers(slas, httpsHelper);
})
.then(infos => {
console.log(infos);
for (var containers of infos) {
for (var container of containers) {
containerMangerUIServer.proxies[container.name] = container.name+':' + container.port;
}
}
console.log("--------- Done launching saved containers ----------");
console.log("Databox UI can be accessed at http://127.0.0.1:"+Config.serverPort);
})
.then(()=>{
var app = containerMangerUIServer.app;
module.exports = app;
})
.catch(err => {
console.log(err);
var stack = new Error().stack;
console.log(stack);
});