FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
serverless-python-requirements/lib/docker.js at master · estahn/serverless-python-requirements · GitHub
estahn
/
serverless-python-requirements
Public
forked from
serverless/serverless-python-requirements
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
serverless-python-requirements
/
lib
/
docker.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
205 lines (188 loc) · 5.62 KB
Breadcrumbs
serverless-python-requirements
/
lib
/
docker.js
Copy path
File metadata and controls
205 lines (188 loc) · 5.62 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
const
spawn
=
require
(
'child-process-ext/spawn'
)
;
const
isWsl
=
require
(
'is-wsl'
)
;
const
fse
=
require
(
'fs-extra'
)
;
const
path
=
require
(
'path'
)
;
const
os
=
require
(
'os'
)
;
/**
* Helper function to run a docker command
*
@param
{
string[]
} options
*
@return
{
Object
}
*/
async
function
dockerCommand
(
options
,
pluginInstance
)
{
const
cmd
=
'docker'
;
try
{
return
await
spawn
(
cmd
,
options
,
{
encoding
:
'utf-8'
}
)
;
}
catch
(
e
)
{
if
(
e
.
stderrBuffer
&&
e
.
stderrBuffer
.
toString
(
)
.
includes
(
'command not found'
)
)
{
throw
new
pluginInstance
.
serverless
.
classes
.
Error
(
'docker not found! Please install it.'
,
'PYTHON_REQUIREMENTS_DOCKER_NOT_FOUND'
)
;
}
throw
e
;
}
}
/**
* Build the custom Docker image
*
@param
{
string
} dockerFile
*
@param
{
string[]
} extraArgs
*
@return
{
string
} The name of the built docker image.
*/
async
function
buildImage
(
dockerFile
,
extraArgs
,
pluginInstance
)
{
const
imageName
=
'sls-py-reqs-custom'
;
const
options
=
[
'build'
,
'-f'
,
dockerFile
,
'-t'
,
imageName
]
;
if
(
Array
.
isArray
(
extraArgs
)
)
{
options
.
push
(
...
extraArgs
)
;
}
else
{
throw
new
pluginInstance
.
serverless
.
classes
.
Error
(
'dockerRunCmdExtraArgs option must be an array'
,
'PYTHON_REQUIREMENTS_INVALID_DOCKER_EXTRA_ARGS'
)
;
}
options
.
push
(
'.'
)
;
await
dockerCommand
(
options
,
pluginInstance
)
;
return
imageName
;
}
/**
* Find a file that exists on all projects so we can test if Docker can see it too
*
@param
{
string
} servicePath
*
@return
{
string
} file name
*/
function
findTestFile
(
servicePath
,
pluginInstance
)
{
if
(
fse
.
pathExistsSync
(
path
.
join
(
servicePath
,
'serverless.yml'
)
)
)
{
return
'serverless.yml'
;
}
if
(
fse
.
pathExistsSync
(
path
.
join
(
servicePath
,
'serverless.yaml'
)
)
)
{
return
'serverless.yaml'
;
}
if
(
fse
.
pathExistsSync
(
path
.
join
(
servicePath
,
'serverless.json'
)
)
)
{
return
'serverless.json'
;
}
if
(
fse
.
pathExistsSync
(
path
.
join
(
servicePath
,
'requirements.txt'
)
)
)
{
return
'requirements.txt'
;
}
throw
new
pluginInstance
.
serverless
.
classes
.
Error
(
'Unable to find serverless.{yml|yaml|json} or requirements.txt for getBindPath()'
,
'PYTHON_REQUIREMENTS_MISSING_GET_BIND_PATH_FILE'
)
;
}
/**
* Test bind path to make sure it's working
*
@param
{
string
} bindPath
*
@return
{
boolean
}
*/
async
function
tryBindPath
(
bindPath
,
testFile
,
pluginInstance
)
{
const
{
serverless
,
log
}
=
pluginInstance
;
const
debug
=
process
.
env
.
SLS_DEBUG
;
const
options
=
[
'run'
,
'--rm'
,
'-v'
,
`
${
bindPath
}
:/test`
,
'alpine'
,
'ls'
,
`/test/
${
testFile
}
`
,
]
;
try
{
if
(
debug
)
{
if
(
log
)
{
log
.
debug
(
`Trying bindPath
${
bindPath
}
(
${
options
}
)`
)
;
}
else
{
serverless
.
cli
.
log
(
`Trying bindPath
${
bindPath
}
(
${
options
}
)`
)
;
}
}
const
ps
=
await
dockerCommand
(
options
,
pluginInstance
)
;
if
(
debug
)
{
if
(
log
)
{
log
.
debug
(
ps
.
stdoutBuffer
.
toString
(
)
.
trim
(
)
)
;
}
else
{
serverless
.
cli
.
log
(
ps
.
stdoutBuffer
.
toString
(
)
.
trim
(
)
)
;
}
}
return
ps
.
stdoutBuffer
.
toString
(
)
.
trim
(
)
===
`/test/
${
testFile
}
`
;
}
catch
(
err
)
{
if
(
debug
)
{
if
(
log
)
{
log
.
debug
(
`Finding bindPath failed with
${
err
}
`
)
;
}
else
{
serverless
.
cli
.
log
(
`Finding bindPath failed with
${
err
}
`
)
;
}
}
return
false
;
}
}
/**
* Get bind path depending on os platform
*
@param
{
object
} serverless
*
@param
{
string
} servicePath
*
@return
{
string
} The bind path.
*/
async
function
getBindPath
(
servicePath
,
pluginInstance
)
{
// Determine bind path
let
isWsl1
=
isWsl
&&
!
os
.
release
(
)
.
includes
(
'microsoft-standard'
)
;
if
(
process
.
platform
!==
'win32'
&&
!
isWsl1
)
{
return
servicePath
;
}
// test docker is available
await
dockerCommand
(
[
'version'
]
,
pluginInstance
)
;
// find good bind path for Windows
let
bindPaths
=
[
]
;
let
baseBindPath
=
servicePath
.
replace
(
/
\\
(
[
^
\s
]
)
/
g
,
'/$1'
)
;
let
drive
;
let
path
;
bindPaths
.
push
(
baseBindPath
)
;
if
(
baseBindPath
.
startsWith
(
'/mnt/'
)
)
{
// cygwin "/mnt/C/users/..."
baseBindPath
=
baseBindPath
.
replace
(
/
^
\/
m
n
t
\/
/
,
'/'
)
;
}
if
(
baseBindPath
[
1
]
==
':'
)
{
// normal windows "c:/users/..."
drive
=
baseBindPath
[
0
]
;
path
=
baseBindPath
.
substring
(
3
)
;
}
else
if
(
baseBindPath
[
0
]
==
'/'
&&
baseBindPath
[
2
]
==
'/'
)
{
// gitbash "/c/users/..."
drive
=
baseBindPath
[
1
]
;
path
=
baseBindPath
.
substring
(
3
)
;
}
else
{
throw
new
Error
(
`Unknown path format
${
baseBindPath
.
substr
(
10
)
}
...`
)
;
}
bindPaths
.
push
(
`/
${
drive
.
toLowerCase
(
)
}
/
${
path
}
`
)
;
// Docker Toolbox (seems like Docker for Windows can support this too)
bindPaths
.
push
(
`
${
drive
.
toLowerCase
(
)
}
:/
${
path
}
`
)
;
// Docker for Windows
// other options just in case
bindPaths
.
push
(
`/
${
drive
.
toUpperCase
(
)
}
/
${
path
}
`
)
;
bindPaths
.
push
(
`/mnt/
${
drive
.
toLowerCase
(
)
}
/
${
path
}
`
)
;
bindPaths
.
push
(
`/mnt/
${
drive
.
toUpperCase
(
)
}
/
${
path
}
`
)
;
bindPaths
.
push
(
`
${
drive
.
toUpperCase
(
)
}
:/
${
path
}
`
)
;
const
testFile
=
findTestFile
(
servicePath
,
pluginInstance
)
;
for
(
let
i
=
0
;
i
<
bindPaths
.
length
;
i
++
)
{
const
bindPath
=
bindPaths
[
i
]
;
if
(
await
tryBindPath
(
bindPath
,
testFile
,
pluginInstance
)
)
{
return
bindPath
;
}
}
throw
new
Error
(
'Unable to find good bind path format'
)
;
}
/**
* Find out what uid the docker machine is using
*
@param
{
string
} bindPath
*
@return
{
boolean
}
*/
async
function
getDockerUid
(
bindPath
,
pluginInstance
)
{
const
options
=
[
'run'
,
'--rm'
,
'-v'
,
`
${
bindPath
}
:/test`
,
'alpine'
,
'stat'
,
'-c'
,
'%u'
,
'/bin/sh'
,
]
;
const
ps
=
await
dockerCommand
(
options
,
pluginInstance
)
;
return
ps
.
stdoutBuffer
.
toString
(
)
.
trim
(
)
;
}
module
.
exports
=
{
buildImage
,
getBindPath
,
getDockerUid
}
;
Back
|
FazBrowse Home
|
New Git URL