11const fse = require ( 'fs-extra' ) ;
22const path = require ( 'path' ) ;
33const { spawnSync} = require ( 'child_process' ) ;
4- const isWsl = require ( 'is-wsl' ) ;
54const { quote} = require ( 'shell-quote' ) ;
5+ const { buildImage, getBindPath} = require ( './docker' ) ;
66
77/**
88 * pip install the requirements to the .serverless/requirements directory
@@ -53,35 +53,18 @@ function installRequirements() {
5353 if ( this . options . dockerizePip ) {
5454 cmd = 'docker' ;
5555
56- this . serverless . cli . log ( `Docker Image: ${ this . options . dockerImage } ` ) ;
57-
58- // Determine os platform of docker CLI from 'docker version'
59- options = [ 'version' , '--format' , '{{with .Client}}{{.Os}}{{end}}' ] ;
60- const ps = spawnSync ( cmd , options , { 'timeout' : 10000 , 'encoding' : 'utf-8' } ) ;
61- if ( ps . error ) {
62- if ( ps . error . code === 'ENOENT' ) {
63- throw new Error ( 'docker not found! Please install it.' ) ;
64- }
65- throw new Error ( ps . error ) ;
66- } else if ( ps . status !== 0 ) {
67- throw new Error ( ps . stderr ) ;
68- }
69-
70- let bindPath ;
71- const cliPlatform = ps . stdout . trim ( ) ;
72- if ( process . platform === 'win32' ) {
73- bindPath = this . servicePath . replace ( / \\ ( [ ^ \s ] ) / g, '/$1' ) ;
74- if ( cliPlatform === 'windows' ) {
75- bindPath = bindPath . replace ( / ^ \/ ( \w ) \/ / i, '$1:/' ) ;
76- }
77- } else if ( isWsl ) {
78- bindPath = this . servicePath . replace ( / ^ \/ m n t \/ / , '/' ) ;
79- if ( cliPlatform === 'windows' ) {
80- bindPath = bindPath . replace ( / ^ \/ ( \w ) \/ / i, '$1:/' ) ;
81- }
56+ // Build docker image if required
57+ let dockerImage ;
58+ if ( this . options . dockerFile ) {
59+ this . serverless . cli . log ( `Building custom docker image from ${ this . options . dockerFile } ...` ) ;
60+ dockerImage = buildImage ( this . options . dockerFile ) ;
8261 } else {
83- bindPath = this . servicePath ;
62+ dockerImage = this . options . dockerImage ;
8463 }
64+ this . serverless . cli . log ( `Docker Image: ${ dockerImage } ` ) ;
65+
66+ // Prepare bind path depending on os platform
67+ const bindPath = getBindPath ( this . servicePath ) ;
8568
8669 options = [
8770 'run' , '--rm' ,
@@ -103,7 +86,7 @@ function installRequirements() {
10386 ] ) ;
10487 pipCmd = [ '/bin/bash' , '-c' , '"' + pipCmd + ' && ' + chownCmd + '"' ] ;
10588 }
106- options . push ( this . options . dockerImage ) ;
89+ options . push ( dockerImage ) ;
10790 options . push ( ...pipCmd ) ;
10891 } else {
10992 cmd = pipCmd [ 0 ] ;
0 commit comments