Skip to content

Commit

Permalink
feat(dockerode): dockerode integration
Browse files Browse the repository at this point in the history
  • Loading branch information
jkuri committed Sep 16, 2017
1 parent b713cba commit f581320
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 18 deletions.
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
"@types/dockerode": "^2.5.0",
"@types/express": "^4.0.37",
"@types/express-session": "^1.15.3",
"@types/jasmine": "^2.5.54",
"@types/jasmine": "^2.6.0",
"@types/jsonwebtoken": "^7.2.3",
"@types/knex": "0.0.61",
"@types/minimist": "^1.2.0",
Expand Down
17 changes: 15 additions & 2 deletions src/api/docker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,23 @@ export function killContainer(id: string): Promise<void> {
let container = null;
try {
container = docker.getContainer(id);
container.kill()
container.inspect()
.then(containerInfo => {
if (containerInfo.State.Running) {
return container.kill();
} else {
return Promise.resolve();
}
})
.then(() => container.remove())
.then(() => resolve())
.catch(err => reject(err));
.catch(err => {
if (err.statusCode === 404) {
resolve();
} else {
console.error(err);
}
});
} catch (e) {
resolve();
}
Expand Down
26 changes: 16 additions & 10 deletions src/api/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,22 @@ export class SocketServer {
return this.createRxSocket(data.conn);
})
.subscribe(conn => {
this.clients.push({ connection: conn, sub: null, session: this.connectingClient });
this.clients.push({
connection: conn,
sub: null,
session: this.connectingClient,
subs: []
});

const clientIndex = this.clients.length - 1;
let statsSubIndex = null;

// send server time for sync
conn.next({ type: 'time', data: new Date().getTime() });

// send client latest status about jobs
jobEvents.subscribe(event => conn.next(event));

// subscriptions
let statsSub: Subscription;

conn.subscribe(event => {
if (event.type === 'disconnected') {
const index = this.clients.findIndex(client => client.connection === conn);
Expand Down Expand Up @@ -171,14 +176,15 @@ export class SocketServer {
break;

case 'subscribeToStats':
statsSub = Observable.merge(...[memory(), cpu()])
.subscribe(event => conn.next(event));

this.clients[clientIndex].subs.push({ id: 'stats', sub: null });
statsSubIndex = this.clients[clientIndex].subs.length - 1;
this.clients[clientIndex].subs[statsSubIndex] =
Observable.merge(...[memory(), cpu()])
.subscribe(event => conn.next(event));
break;

case 'unsubscribeFromStats':
if (statsSub) {
statsSub.unsubscribe();
if (!this.clients[clientIndex].subs[statsSubIndex]) {
this.clients[clientIndex].subs[statsSubIndex].unsubscribe();
}
break;
}
Expand Down
2 changes: 2 additions & 0 deletions src/app/components/app-job/app-job.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ export class AppJobComponent implements OnInit, OnDestroy {
this.loading = true;
this.terminalOptions = { size: 'large' };
this.id = null;
this.sshd = null;
this.vnc = null;
}

ngOnInit() {
Expand Down
8 changes: 6 additions & 2 deletions src/app/components/app-terminal/app-terminal.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,13 @@ export class AppTerminalComponent implements OnInit {
t = t ? parseFloat(<any>(<any>t / 10)).toFixed(0) : null;
let time = t && parseInt(<any>t, 10);

this.commands[this.commands.length - 1].time = time ? this.getDuration(time) : null;
if (this.commands[this.commands.length - 1]) {
this.commands[this.commands.length - 1].time = time ? this.getDuration(time) : null;
}
} else {
this.commands[this.commands.length - 1].output += output;
if (this.commands[this.commands.length - 1]) {
this.commands[this.commands.length - 1].output += output;
}
}
}

Expand Down

0 comments on commit f581320

Please sign in to comment.