|
1 | 1 | const fse = require('fs-extra');
|
2 | 2 | const path = require('path');
|
3 | 3 | const {spawnSync} = require('child_process');
|
4 |
| -const isWsl = require('is-wsl'); |
5 | 4 | const {quote} = require('shell-quote');
|
| 5 | +const {buildImage, getBindPath} = require('./docker'); |
6 | 6 |
|
7 | 7 | /**
|
8 | 8 | * pip install the requirements to the .serverless/requirements directory
|
@@ -53,60 +53,18 @@ function installRequirements() {
|
53 | 53 | if (this.options.dockerizePip) {
|
54 | 54 | cmd = 'docker';
|
55 | 55 |
|
56 |
| - let dockerImage = this.options.dockerImage; |
| 56 | + // Build docker image if required |
| 57 | + let dockerImage; |
57 | 58 | if (this.options.dockerFile) {
|
58 | 59 | this.serverless.cli.log(`Building custom docker image from ${this.options.dockerFile}...`);
|
59 |
| - // Build docker image from provided Dockerfile |
60 |
| - const imageName = 'sls-py-reqs-custom'; |
61 |
| - options = [ |
62 |
| - 'build', '-f', this.options.dockerFile, '-t', imageName, '.' |
63 |
| - ]; |
64 |
| - const ps = spawnSync(cmd, options, { |
65 |
| - 'timeout': 10000, |
66 |
| - 'encoding': 'utf-8' |
67 |
| - }); |
68 |
| - if (ps.error) { |
69 |
| - if (ps.error.code === 'ENOENT') { |
70 |
| - throw new Error('docker not found! Please install it.'); |
71 |
| - } |
72 |
| - throw new Error(ps.error); |
73 |
| - } else if (ps.status !== 0) { |
74 |
| - throw new Error(ps.stderr); |
75 |
| - } |
76 |
| - |
77 |
| - // Set dockerImage option value |
78 |
| - dockerImage = imageName; |
| 60 | + dockerImage = buildImage(this.options.dockerFile); |
| 61 | + } else { |
| 62 | + dockerImage = this.options.dockerImage; |
79 | 63 | }
|
80 |
| - |
81 | 64 | this.serverless.cli.log(`Docker Image: ${dockerImage}`);
|
82 | 65 |
|
83 |
| - // Determine os platform of docker CLI from 'docker version' |
84 |
| - options = ['version', '--format', '{{with .Client}}{{.Os}}{{end}}']; |
85 |
| - const ps = spawnSync(cmd, options, {'timeout': 10000, 'encoding': 'utf-8'}); |
86 |
| - if (ps.error) { |
87 |
| - if (ps.error.code === 'ENOENT') { |
88 |
| - throw new Error('docker not found! Please install it.'); |
89 |
| - } |
90 |
| - throw new Error(ps.error); |
91 |
| - } else if (ps.status !== 0) { |
92 |
| - throw new Error(ps.stderr); |
93 |
| - } |
94 |
| - |
95 |
| - let bindPath; |
96 |
| - const cliPlatform = ps.stdout.trim(); |
97 |
| - if (process.platform === 'win32') { |
98 |
| - bindPath = this.servicePath.replace(/\\([^\s])/g, '/$1'); |
99 |
| - if (cliPlatform === 'windows') { |
100 |
| - bindPath = bindPath.replace(/^\/(\w)\//i, '$1:/'); |
101 |
| - } |
102 |
| - } else if (isWsl) { |
103 |
| - bindPath = this.servicePath.replace(/^\/mnt\//, '/'); |
104 |
| - if (cliPlatform === 'windows') { |
105 |
| - bindPath = bindPath.replace(/^\/(\w)\//i, '$1:/'); |
106 |
| - } |
107 |
| - } else { |
108 |
| - bindPath = this.servicePath; |
109 |
| - } |
| 66 | + // Prepare bind path depending on os platform |
| 67 | + const bindPath = getBindPath(this.servicePath); |
110 | 68 |
|
111 | 69 | options = [
|
112 | 70 | 'run', '--rm',
|
|
0 commit comments