-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
126 lines (106 loc) · 3.86 KB
/
server.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
var express = require('express');
var Docker = require('dockerode');
var path = require('path');
var fs = require('fs');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var ss = require('socket.io-stream');
app.use('/public', express.static('public'));
app.get('/', function (req,res) {
res.sendFile(path.join(__dirname , 'ui.html'))
});
http.listen(3000, function(){
console.log('listening on *:3000');
});
/*
Writing /Users/henry/.boot2docker/certs/boot2docker-vm/ca.pem
Writing /Users/henry/.boot2docker/certs/boot2docker-vm/cert.pem
Writing /Users/henry/.boot2docker/certs/boot2docker-vm/key.pem
To connect the Docker client to the Docker daemon, please set:
export DOCKER_HOST=tcp://192.168.59.103:2376
export DOCKER_CERT_PATH=/Users/henry/.boot2docker/certs/boot2docker-vm
export DOCKER_TLS_VERIFY=1
*/
io.on('connection' , function (socket){
global.socket = socket;
console.log('hi lo')
})
app.get('/create-container', function (req,res,next){
console.log('CREAATENOW');
var clientConfig = {
DOCKER_HOST : req.query['DOCKER_HOST'],
DOCKER_CERT_PATH : req.query['DOCKER_CERT_PATH']
}
try {
clientConfig.host = clientConfig.DOCKER_HOST.slice(clientConfig.DOCKER_HOST.indexOf('tcp://') + 6 , clientConfig.DOCKER_HOST.lastIndexOf(':'))
clientConfig.port = clientConfig.DOCKER_HOST.slice(clientConfig.DOCKER_HOST.lastIndexOf(':') + 1, clientConfig.DOCKER_HOST.length)
var fields = ['ca', 'cert', 'key'];
fields.forEach(function (item) {
clientConfig[item] = path.join(clientConfig.DOCKER_CERT_PATH , item + '.pem')
})
var dockerConfig = {
host: clientConfig.host || '192.168.59.103',
port: clientConfig.port || 2376,
ca: fs.readFileSync(clientConfig.ca || '/Users/henry/.boot2docker/certs/boot2docker-vm/ca.pem'),
cert: fs.readFileSync(clientConfig.cert || '/Users/henry/.boot2docker/certs/boot2docker-vm/cert.pem'),
key: fs.readFileSync(clientConfig.key || '/Users/henry/.boot2docker/certs/boot2docker-vm/key.pem')
};
} catch (e) {
io.emit('error!',JSON.stringify(e));
console.log(e);
}
console.log(JSON.stringify(
dockerConfig
))
var ourDocker = new Docker(dockerConfig);
function stopAndRemoveAll (callback) {
ourDocker.listContainers(function (err, containers) {
console.log(JSON.stringify(containers));
console.log(JSON.stringify(err));
if (containers.length == 0) callback();
containers.forEach(function (containerInfo) {
ourDocker.getContainer(containerInfo.Id).stop(function (err, data) {
console.log(err);
ourDocker.getContainer(containerInfo.Id).remove(function (err, data) {
console.log(err);
callback();
});
});
});
});
}
stopAndRemoveAll(function (){
ourDocker.createContainer({
Tty: true ,
Image: 'ubuntu',
Cmd: ['sh'],
name: (Math.random()*100000).toString(),
'OpenStdin': true,
}, function(err, container) {
console.log(err);
console.log(JSON.stringify(container));
container.attach({stream: true, stdout: true, stderr: true, stdin:true}, function (err, stream) {
stream.pipe(process.stdout);
var isRaw = process.isRaw;
process.stdin.resume();
process.stdin.setRawMode(true);
//process.stdin.pipe(stream);
stream.on('data', function (chunk) {
io.emit('docker-print', chunk.toString());
})
socket.on('keyboard-hit', function(content, data) {
var character = String.fromCharCode(parseInt(content));
stream.write(character);
});
process.stdin.on('data', function (chunk) {
if ((new String(chunk)).charCodeAt(0) === 3) {
process.exit();
}
})
});
container.start(function (err, data) {
});
})
});
})