compose: Support changing the tty option for compose services

This enables the switch to be added to the compose, and the handling of
docker messages has been changed to ensure that the multiplexed logs
which result are handled properly.

Change-type: minor
Signed-off-by: Cameron Diver <cameron@resin.io>
This commit is contained in:
Cameron Diver 2018-09-21 15:49:43 +01:00
parent e8a5edf774
commit 06bbf9751a
No known key found for this signature in database
GPG Key ID: 69264F9C923F55C1
7 changed files with 53 additions and 11 deletions

View File

@ -55,6 +55,7 @@ const supportedComposeFields = [
'shmSize',
'user',
'workingDir',
'tty',
];
export function sanitiseComposeConfig(

View File

@ -255,6 +255,7 @@ export class Service {
// Sanity check the incoming boolean values
config.oomKillDisable = Boolean(config.oomKillDisable);
config.readOnly = Boolean(config.readOnly);
config.tty = Boolean(config.tty);
if (_.isArray(config.sysctls)) {
config.sysctls = _.fromPairs(_.map(config.sysctls, (v) => _.split(v, '=')));
@ -340,6 +341,7 @@ export class Service {
hostname: '',
user: '',
workingDir: '',
tty: false,
});
// Mutate service with extra features
@ -467,6 +469,7 @@ export class Service {
macAddress: (container.Config as any).MacAddress || '',
user: container.Config.User || '',
workingDir: container.Config.WorkingDir || '',
tty: container.Config.Tty || false,
};
svc.appId = checkInt(container.Config.Labels['io.resin.app-id']) || null;
@ -491,7 +494,7 @@ export class Service {
});
return {
Tty: true,
Tty: this.config.tty,
Cmd: this.config.command,
Volumes: volumes,
// Typings are wrong here, the docker daemon accepts a string or string[],

View File

@ -86,6 +86,7 @@ export interface ServiceComposeConfig {
shmSize?: string;
user?: string;
workingDir?: string;
tty?: boolean;
}
// This is identical to ServiceComposeConfig, except for the
@ -150,6 +151,7 @@ export interface ServiceConfig {
shmSize: number;
user: string;
workingDir: string;
tty: boolean;
}
export type ServiceConfigArrayField = 'volumes' |

View File

@ -375,13 +375,15 @@ export class Logger {
this.attached[streamType][containerId] = false;
})
.pipe(es.split())
.on('data', (logBuf: Buffer) => {
const logLine = logBuf.toString();
const space = logLine.indexOf(' ');
if (space > 0) {
.on('data', (logBuf: Buffer | string) => {
if (_.isString(logBuf)) {
logBuf = Buffer.from(logBuf);
}
const logMsg = Logger.extractContainerMessage(logBuf);
if (logMsg != null) {
const message: LogMessage = {
timestamp: (new Date(logLine.substr(0, space))).getTime(),
message: logLine.substr(space + 1),
message: logMsg.message,
timestamp: logMsg.timestamp,
serviceId,
imageId,
};
@ -433,4 +435,27 @@ export class Logger {
return null;
}
private static extractContainerMessage(
msgBuf: Buffer,
): { message: string, timestamp: number } | null {
// Non-tty message format from:
// https://docs.docker.com/engine/api/v1.30/#operation/ContainerAttach
if (
msgBuf[0] in [0, 1, 2] &&
_.every(msgBuf.slice(1, 7), (c) => c === 0)
) {
// Take the header from this message, and parse it as normal
msgBuf = msgBuf.slice(8);
}
const logLine = msgBuf.toString();
const space = logLine.indexOf(' ');
if (space > 0) {
return {
timestamp: (new Date(logLine.substr(0, space))).getTime(),
message: logLine.substr(space + 1),
};
}
return null;
}
}

View File

@ -105,3 +105,12 @@ describe 'Logger', ->
msg = JSON.parse(lines[0])
expect(msg).to.deep.equal({ message: 'Hello there!', timestamp: 0, isSystem: true })
it 'should support non-tty log lines', ->
message = '\u0001\u0000\u0000\u0000\u0000\u0000\u0000?2018-09-21T12:37:09.819134000Z this is the message'
buffer = Buffer.from(message)
expect(Logger.extractContainerMessage(buffer)).to.deep.equal({
message: 'this is the message',
timestamp: 1537533429819
})

View File

@ -9,5 +9,6 @@
"releaseId": 597007,
"serviceId": 43697,
"commit": "ff300a701054ac15281de1f9c0e84b8c",
"imageName": "registry2.resin.io/v2/bf9c649a5ac2fe147bbe350875042388@sha256:3a5c17b715b4f8265539c1a006dd1abdd2ff3b758aa23df99f77c792f40c3d43"
}
"imageName": "registry2.resin.io/v2/bf9c649a5ac2fe147bbe350875042388@sha256:3a5c17b715b4f8265539c1a006dd1abdd2ff3b758aa23df99f77c792f40c3d43",
"tty": true
}

View File

@ -7,5 +7,6 @@
"releaseId": 572579,
"serviceId": 43697,
"commit": "b14730d691467ab0f448a308af6bf839",
"imageName": "registry2.resin.io/v2/8ddbe4a22e881f06def0f31400bfb6de@sha256:09b0db9e71cead5f91107fc9254b1af7088444cc6da55afa2da595940f72a34a"
}
"imageName": "registry2.resin.io/v2/8ddbe4a22e881f06def0f31400bfb6de@sha256:09b0db9e71cead5f91107fc9254b1af7088444cc6da55afa2da595940f72a34a",
"tty": true
}