Skip to content

Commit 0caae9d

Browse files
authored
Merge pull request #145 from kichik/fix_docker_cache
Fix --cache-dir with Docker
2 parents 78225f3 + ca40274 commit 0caae9d

File tree

4 files changed

+32
-12
lines changed

4 files changed

+32
-12
lines changed

lib/docker.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,16 @@ function getBindPath(servicePath) {
101101
throw new Error('Unable to find good bind path format');
102102
};
103103

104-
module.exports = {buildImage, getBindPath};
104+
/**
105+
* Find out what uid the docker machine is using
106+
* @param {string} bindPath
107+
* @return {boolean}
108+
*/
109+
function getDockerUid(bindPath) {
110+
const options = ['run', '--rm', '-v', `${bindPath}:/test`,
111+
'alpine', 'stat', '-c', '%u', '/test/.serverless'];
112+
const ps = dockerCommand(options);
113+
return ps.stdout.trim();
114+
};
115+
116+
module.exports = {buildImage, getBindPath, getDockerUid};

lib/pip.js

+6-9
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@ const path = require('path');
33
const get = require('lodash.get');
44
const set = require('lodash.set');
55
const {spawnSync} = require('child_process');
6-
const {quote} = require('shell-quote');
76
const values = require('lodash.values');
8-
const {buildImage, getBindPath} = require('./docker');
7+
const {buildImage, getBindPath, getDockerUid} = require('./docker');
98

109
/**
1110
* Install requirements described in requirementsPath to targetPath
@@ -82,19 +81,17 @@ function installRequirements(requirementsPath, targetFolder, serverless, service
8281
cmdOptions.push('-e', 'SSH_AUTH_SOCK=/tmp/ssh_sock');
8382
}
8483
if (process.platform === 'linux') {
85-
// Set the ownership of the .serverless/requirements folder to current user
86-
pipCmd = quote(pipCmd);
87-
const chownCmd = quote([
88-
'chown', '-R', `${process.getuid()}:${process.getgid()}`,
89-
targetRequirementsFolder,
90-
]);
91-
pipCmd = ['/bin/bash', '-c', '"' + pipCmd + ' && ' + chownCmd + '"'];
84+
// Use same user so requirements folder is not root and so --cache-dir works
85+
cmdOptions.push('-u', `${process.getuid()}`);
9286
// const stripCmd = quote([
9387
// 'find', targetRequirementsFolder,
9488
// '-name', '"*.so"',
9589
// '-exec', 'strip', '{}', '\;',
9690
// ]);
9791
// pipCmd = ['/bin/bash', '-c', '"' + pipCmd + ' && ' + stripCmd + ' && ' + chownCmd + '"'];
92+
} else {
93+
// Use same user so --cache-dir works
94+
cmdOptions.push('-u', getDockerUid(bindPath));
9895
}
9996
cmdOptions.push(dockerImage);
10097
cmdOptions.push(...pipCmd);

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@
5454
"lodash.get": "^4.4.2",
5555
"lodash.set": "^4.3.2",
5656
"lodash.values": "^4.3.0",
57-
"shell-quote": "^1.6.1",
5857
"zip-local": "^0.3.4"
5958
}
6059
}

test.bats

+13-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33

44
setup() {
55
export SLS_DEBUG=t
6+
if ! [ -z "$CI" ]; then
7+
export LC_ALL=C.UTF-8
8+
export LANG=C.UTF-8
9+
fi
610

711
cd test
812

@@ -11,7 +15,7 @@ setup() {
1115

1216
teardown() {
1317
sls requirements clean
14-
rm -rf puck puck2 puck3 node_modules .serverless
18+
rm -rf puck puck2 puck3 node_modules .serverless .requirements.zip .requirements-cache
1519
if [ -f serverless.yml.bak ]; then mv serverless.yml.bak serverless.yml; fi
1620
}
1721

@@ -58,6 +62,14 @@ teardown() {
5862
ls puck/flask
5963
}
6064

65+
@test "py3.6 uses cache with dockerizePip option" {
66+
[ -z "$CIRCLE_BRANCH" ] || skip "Volumes are weird in CircleCI https://circleci.com/docs/2.0/building-docker-images/#mounting-folders"
67+
! uname -sm|grep Linux || groups|grep docker || id -u|egrep '^0$' || skip "can't dockerize on linux if not root & not in docker group"
68+
perl -p -i'.bak' -e 's/(pythonRequirements:$)/\1\n pipCmdExtraArgs: ["--cache-dir", ".requirements-cache"]/' serverless.yml
69+
sls --dockerizePip=true package
70+
ls .requirements-cache/http
71+
}
72+
6173
@test "py2.7 can package flask with default options" {
6274
sls --runtime=python2.7 package
6375
unzip .serverless/sls-py-req-test.zip -d puck

0 commit comments

Comments
 (0)