Skip to content

Commit

Permalink
fix(): multiple fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jkuri committed Aug 2, 2017
1 parent 37a0f78 commit 2da8c56
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 50 deletions.
48 changes: 22 additions & 26 deletions src/api/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export function startBuildProcess(buildId: number, jobId: number,
}, err => {
observer.next({ type: 'data', data: err });
observer.error(err);
observer.complete();
stopContainer(name).subscribe((event: ProcessOutput) => observer.next(event));
}, () => {
observer.complete();
Expand All @@ -55,40 +56,35 @@ export function startBuildProcess(buildId: number, jobId: number,

function executeInContainer(name: string, command: string): Observable<ProcessOutput> {
return new Observable(observer => {
let executed = false;
const start = nodePty.spawn('docker', ['start', name]);

start.on('exit', () => {
let exitCode = 255;
const attach = nodePty.spawn('docker', ['attach', '--detach-keys=D', name]);
let executed = false;
let attach = null;
let detachKey = null;

if (command.includes('init.d') && command.includes('start')) {
attach = nodePty.spawn('docker', ['attach', '--detach-keys=D', name]);
detachKey = 'D';
} else {
attach = nodePty.spawn('docker', ['exec', '-it', '--privileged', name, 'bash', '-l']);
}

attach.on('data', data => {
data = data.toString();
const trimmed = data.trim();

if (!executed) {
executed = true;
attach.write(command + ' && echo EXECOK || echo EXECNOK\r');
} else {
if ((data.includes('EXECNOK') || data.includes('EXECOK')) && !data.includes(command)) {
if (data.includes('EXECOK')) {
exitCode = 0;
}

attach.write('D');
return;
}

if (data.includes('> read escape sequence')) {
return;
}

if (data.includes(command)) {
data = bold(yellow(command)) + '\n';
}

if (!data.trim().includes('logout') && !data.trim().includes('exit') &&
!data.trim().includes('read escape sequence')) {
observer.next({ type: 'data', data: data });
}
attach.write(command + ' && echo EXECOK || echo EXECNOK\r\n');
observer.next({ type: 'data', data: bold(yellow(command)) + '\r\n' });
} else if (data.includes('EXECOK') && !data.includes(command)) {
exitCode = 0;
attach.write(detachKey ? detachKey : 'exit $?\r\n');
} else if (data.includes('EXECNOK') && !data.includes(command)) {
attach.write(detachKey ? detachKey : 'exit $?\r\n');
} else if (!data.includes(command)) {
observer.next({ type: 'data', data: data });
}
});

Expand Down
7 changes: 4 additions & 3 deletions src/app/components/app-job/app-job.component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component, OnInit, OnDestroy } from '@angular/core';
import { Component, OnInit, OnDestroy, NgZone } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { ApiService } from '../../services/api.service';
import { SocketService } from '../../services/socket.service';
Expand Down Expand Up @@ -29,7 +29,8 @@ export class AppJobComponent implements OnInit, OnDestroy {
constructor(
private socketService: SocketService,
private apiService: ApiService,
private route: ActivatedRoute
private route: ActivatedRoute,
private ngZone: NgZone
) {
this.loading = true;
this.status = 'queued';
Expand All @@ -49,7 +50,7 @@ export class AppJobComponent implements OnInit, OnDestroy {
this.termSub = this.socketService.outputEvents
.subscribe(event => {
if (event.type === 'data') {
this.terminalInput = event.data;
this.ngZone.run(() => this.terminalInput = event.data);
} else if (event.type === 'jobStopped' && event.data === this.id) {
this.processing = false;
} else if (event.type === 'jobRestarted' && event.data === this.id) {
Expand Down
2 changes: 1 addition & 1 deletion src/app/components/app-setup/app-setup.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ <h2>
Setting up docker image, please wait ...
</div>
<hr/>
<app-terminal [data]="terminalInput" (outputData)="terminalOutput($event)" [options]="terminalOptions"></app-terminal>
<app-terminal [data]="terminalInput" [options]="terminalOptions"></app-terminal>
</div>
<div *ngIf="step === 'done'">
<div class="message green">
Expand Down
32 changes: 12 additions & 20 deletions src/app/components/app-setup/app-setup.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,18 @@ export class AppSetupComponent implements OnInit {
this.loading = false;
if (!exists) {
this.step = 'docker';
this.socketService.onMessage().subscribe(event => {
if (event.type === 'terminalOutput') {
if (event.data.type === 'exit') {
if (event.data.data === 0) {
this.step = 'done';
}
} else {
this.terminalInput = event.data.data;
}
}
});
this.socketService.emit({ type: 'initializeDockerImage', data: 'abstruse' });
} else {
this.router.navigate(['/login']);
}
Expand All @@ -112,24 +124,4 @@ export class AppSetupComponent implements OnInit {
}
});
}

terminalOutput(e: any): void {
if (e === 'ready') {
this.socketService.onMessage().subscribe(event => {
if (event.type === 'terminalOutput') {
if (event.data.type === 'exit') {
console.log(event.data);
if (event.data.data === 0) {
this.step = 'done';
}
} else {
this.terminalInput = event.data.data;
}
}
});
this.socketService.emit({ type: 'initializeDockerImage', data: 'abstruse' });
} else if (e && e.type && e.type === 'resize') {
this.socketService.emit({ type: 'resize', data: { cols: e.cols, rows: e.rows }});
}
}
}
1 change: 1 addition & 0 deletions src/app/styles/terminal.sass
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
border-radius: 4px
box-shadow: 1px 2px 10px rgba($background, 0.7)
border: 1px solid $divider
overflow: hidden

&.large
height: 700px

0 comments on commit 2da8c56

Please sign in to comment.