Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#5993: Ability to pass args for docker run command during invoke local docker #5994

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/providers/aws/cli-reference/invoke-local.md
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ serverless invoke local --function functionName
* `--env` or `-e` String representing an environment variable to set when invoking your function, in the form `<name>=<value>`. Can be repeated for more than one environment variable. * `--env` or `-e` String representing an environment variable to set when invoking your function, in the form `<name>=<value>`. Can be repeated for more than one environment variable.
* `--docker` Enable docker support for NodeJS/Python/Ruby/Java. Enabled by default for other * `--docker` Enable docker support for NodeJS/Python/Ruby/Java. Enabled by default for other
runtimes. runtimes.
* `--docker-arg` Pass additional arguments to docker run command when `--docker` is option used. e.g. `--docker-arg '-p 9229:9229' --docker-arg '-v /var:/host_var'`


## Environment ## Environment


Expand Down
19 changes: 15 additions & 4 deletions lib/plugins/aws/invokeLocal/index.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -328,17 +328,28 @@ class AwsInvokeLocal {
.then((results) => new BbPromise((resolve, reject) => { .then((results) => new BbPromise((resolve, reject) => {
const imageName = results[2]; const imageName = results[2];
const artifactPath = results[3]; const artifactPath = results[3];
const dockerArgs = [ const dockerArgsFromOptions = this.getDockerArgsFromOptions();
'run', '--rm', '-v', `${artifactPath}:/var/task`, imageName, const dockerArgs = _.concat([
handler, JSON.stringify(this.options.data), 'run', '--rm', '-v', `${artifactPath}:/var/task`,
]; ], dockerArgsFromOptions, [
imageName, handler, JSON.stringify(this.options.data),
]);
const docker = spawn('docker', dockerArgs); const docker = spawn('docker', dockerArgs);
docker.stdout.on('data', (buf) => this.serverless.cli.consoleLog(buf.toString())); docker.stdout.on('data', (buf) => this.serverless.cli.consoleLog(buf.toString()));
docker.stderr.on('data', (buf) => this.serverless.cli.consoleLog(buf.toString())); docker.stderr.on('data', (buf) => this.serverless.cli.consoleLog(buf.toString()));
docker.on('exit', error => (error ? reject(error) : resolve(imageName))); docker.on('exit', error => (error ? reject(error) : resolve(imageName)));
})); }));
} }


getDockerArgsFromOptions() {
const dockerArgOptions = this.options['docker-arg'];
const dockerArgsFromOptions = _.flatMap(_.concat(dockerArgOptions || []), (dockerArgOption) => {
const splitItems = dockerArgOption.split(' ');
return [splitItems[0], splitItems.slice(1).join(' ')];
});
return dockerArgsFromOptions;
}

invokeLocalPython(runtime, handlerPath, handlerName, event, context) { invokeLocalPython(runtime, handlerPath, handlerName, event, context) {
const input = JSON.stringify({ const input = JSON.stringify({
event: event || {}, event: event || {},
Expand Down
37 changes: 37 additions & 0 deletions lib/plugins/aws/invokeLocal/index.test.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -1161,6 +1161,7 @@ describe('AwsInvokeLocal', () => {
runtime: 'nodejs8.10', runtime: 'nodejs8.10',
}, },
data: {}, data: {},
'docker-arg': '-p 9292:9292',
}; };
}); });


Expand Down Expand Up @@ -1190,11 +1191,47 @@ describe('AwsInvokeLocal', () => {
'--rm', '--rm',
'-v', '-v',
'servicePath:/var/task', 'servicePath:/var/task',
'-p',
'9292:9292',
'sls-docker', 'sls-docker',
'handler.hello', 'handler.hello',
'{}', '{}',
]]); ]]);
}) })
); );
}); });

describe('#getDockerArgsFromOptions', () => {
it('returns empty list when docker-arg option is absent', () => {
delete awsInvokeLocal.options['docker-arg'];

const dockerArgsFromOptions = awsInvokeLocal.getDockerArgsFromOptions();

expect(dockerArgsFromOptions).to.eql([]);
});

it('returns arg split by space when single docker-arg option is present', () => {
awsInvokeLocal.options['docker-arg'] = '-p 9229:9229';

const dockerArgsFromOptions = awsInvokeLocal.getDockerArgsFromOptions();

expect(dockerArgsFromOptions).to.eql(['-p', '9229:9229']);
});

it('returns args split by space when multiple docker-arg options are present', () => {
awsInvokeLocal.options['docker-arg'] = ['-p 9229:9229', '-v /var/logs:/host-var-logs'];

const dockerArgsFromOptions = awsInvokeLocal.getDockerArgsFromOptions();

expect(dockerArgsFromOptions).to.eql(['-p', '9229:9229', '-v', '/var/logs:/host-var-logs']);
});

it('returns arg split only by first space when docker-arg option has multiple space', () => {
awsInvokeLocal.options['docker-arg'] = '-v /My Docs:/docs';

const dockerArgsFromOptions = awsInvokeLocal.getDockerArgsFromOptions();

expect(dockerArgsFromOptions).to.eql(['-v', '/My Docs:/docs']);
});
});
}); });
3 changes: 3 additions & 0 deletions lib/plugins/invoke/invoke.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ class Invoke {
shortcut: 'e', shortcut: 'e',
}, },
docker: { usage: 'Flag to turn on docker use for node/python/ruby/java' }, docker: { usage: 'Flag to turn on docker use for node/python/ruby/java' },
'docker-arg': {
usage: 'Arguments to docker run command. e.g. --docker-arg "-p 9229:9229"',
},
}, },


}, },
Expand Down