-
Notifications
You must be signed in to change notification settings - Fork 293
Fix Windows support for dockerizePip #116
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
const fse = require('fs-extra'); | ||
const path = require('path'); | ||
const {spawnSync} = require('child_process'); | ||
const isWsl = require('is-wsl'); | ||
|
||
/** | ||
* pip install the requirements to the .serverless/requirements directory | ||
|
@@ -50,29 +51,39 @@ function installRequirements() { | |
|
||
this.serverless.cli.log(`Docker Image: ${this.options.dockerImage}`); | ||
|
||
// Check docker server os type from 'docker version' | ||
let volPath; | ||
// Determine os platform of docker CLI from 'docker version' | ||
options = [ | ||
'version', '--format', '{{with .Server}}{{.Os}}{{end}}' | ||
'version', '--format', '{{with .Client}}{{.Os}}{{end}}' | ||
]; | ||
const ps = spawnSync(cmd, options, {'timeout': 2000, 'encoding': 'utf-8'}); | ||
const ps = spawnSync(cmd, options, {'timeout': 10000, 'encoding': 'utf-8'}); | ||
if (ps.error) { | ||
if (ps.error.code === 'ENOENT') { | ||
throw new Error('docker not found! Please install it.'); | ||
} | ||
throw new Error(ps.error); | ||
} | ||
if (ps.status !== 0) { | ||
} else if (ps.status !== 0) { | ||
throw new Error(ps.stderr); | ||
} else if (ps.stdout.trim() === 'windows') { | ||
volPath = this.servicePath.replace(/\\/g, '/').replace(/^\/mnt\//, '/'); | ||
} | ||
|
||
let bindPath; | ||
const cliPlatform = ps.stdout.trim(); | ||
if (process.platform === 'win32') { | ||
bindPath = this.servicePath.replace(/\\([^\s])/g, '/$1'); | ||
if (cliPlatform === 'windows') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Decided against using regExp positive lookahead expression as some are not familiar with it. Would a lookahead expression be clearer?
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably not, since I'm not even 100% sure what the code is trying to attempt now. Is this correct?
I think the following might cleaner/clearer: let bindPath = this.servicePath;
const cliPlatform = ps.stdout.trim();
if (process.platform === 'win32') {
bindPath = bindPath.replace(/\\([^\s])/g, '/$1');
} else if (isWsl) {
bindPath = bindPath.replace(/^\/mnt\//, '/');
}
if (cliPlatform === 'windows') {
bindPath = bindPath.replace(/^\/(\w)\//i, '$1:/');
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
bindPath = bindPath.replace(/^\/(\w)\//i, '$1:/'); | ||
} | ||
} else if (isWsl) { | ||
bindPath = this.servicePath.replace(/^\/mnt\//, '/'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like this should work in either the case where we have /mn t/c (default) or where users have linked /mnt/c to /c (as in my case). I'd be happy to try it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Awesome, I'd love any extra Windows testing seeing as I can't do it myself. You can test this PR before it's merged by installing with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Speaking of Windows testing, @heri16 or @nickweavers do either of you know if AppVeyor has useful[0] docker support? I'll probably set it up anyway to test windows support without docker, but it'd be great to test the docker options too. [0] I say useful, bc while CircleCI has docker support, it cant There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry @dschep, I can't help with AppVeyor - haven't tried it. Thanks for the instructions on how to install from the branch before it's merge... that's really useful. Here what happened when I tried it:
Maybe not having the test on the require('is-wsl') is better since it then fails with the "not defined" message rather that what was being done in the catch, which I think was to set isWsl to false making it appear we aren't under wsl? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm. Interesting. As I mentioned to @heri16, I'm not sure the try/catch is the right way to go (the dependency isn't conditional and it should run on all platforms.) But if it is necessary, I think this would avoid the above issue: let isWsl = false
try {
isWsl = require('is-wsl');
} catch (e) {} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Exactly the same result I'm afraid:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry for the confusion @nickweavers this branch hasn't been updated yet, so that's expected 😜 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi @nickweavers, apologies for the mixup. Do try again with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry for the delay, looks like the isWsl patch worked and this thing takes a while to run now! Thanks for your efforts guys, really appreciated! There's still an error, but it looks like a user error and not a system/scripting error 👍
I'll try deleting the bucket and rerunning, and confirm if it's all good when I've done that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Awesome! |
||
if (cliPlatform === 'windows') { | ||
bindPath = bindPath.replace(/^\/(\w)\//i, '$1:/'); | ||
} | ||
} else { | ||
volPath = this.servicePath; | ||
bindPath = this.servicePath; | ||
} | ||
|
||
options = [ | ||
'run', '--rm', | ||
'-v', `${volPath}:/var/task:z`, | ||
'-v', `${bindPath}:/var/task:z`, | ||
]; | ||
if (process.platform === 'linux') | ||
options.push('-u', `${process.getuid()}:${process.getgid()}`); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed timeout to 10 seconds just in case some automated build process have a hard time contacting its docker server/backend engine.