-
-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathenroot.sh
More file actions
executable file
·137 lines (111 loc) · 2.69 KB
/
Copy pathenroot.sh
File metadata and controls
executable file
·137 lines (111 loc) · 2.69 KB
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
#!/bin/bash
#
# Enroot image builder script
#
# This script builds a Docker image and converts it to an Enroot squashfs
# image for use with Slurm container integration.
#
FILE="${PWD}/Dockerfile"
IMAGE=""
INPUT="${PWD}"
PROGRAM="$0"
OUTPUT="${PWD}"
# Log error message with timestamp to stderr
err() {
echo -e "[$(date +'%Y-%m-%dT%H:%M:%S%z')][error] $*" >&2
}
# Display usage information
usage() {
set +x
cat <<EOF
usage: $PROGRAM [OPTIONS] params
options:
-h,--help show this help
-f,--file [path] input docker file. [default: ${PWD}/Dockerfile]
-i,--input [path] input directory for docker build. [default: ${INPUT}]
-o,--output [path] output directory. [default: ${OUTPUT}]
-n,--image [image] image name.
example:
./enroot.sh -n "nccl-tests" -f "Dockerfile"
EOF
}
# Convert Docker image to Enroot squashfs format
# Args:
# $1 - Docker image name
# $2 - Output directory for squashfs file
run() {
local image="${1}"
local output="${2}"
local arr=()
local url
local tag
if ! command -v enroot &> /dev/null; then
err "please install 'enroot' cli"
return 1
fi
# split string
IFS=$'\n' read -d "" -ra arr <<< "${image//\//$'\n'}"
url="${arr[0]}"
tag="${arr[1]//\:/-}"
if [ -z "${url}" ]; then
err "parse ${image} fail"
return 1
fi
if [ -z "${tag}" ]; then
tag="latest"
fi
if ! enroot import -o "${output}/${url}+${tag}.sqsh" "dockerd://${image}"; then
err "enroot import ${image} fail"
return 1
fi
}
# Build Docker image from Dockerfile
# Args:
# $1 - Path to Dockerfile
# $2 - Docker image name/tag
# $3 - Build context directory
build() {
local file="${1}"
local image="${2}"
local input="${3}"
local arr=()
local url
if ! command -v docker &> /dev/null; then
err "please install 'docker' cli"
return 1
fi
if [ -z "${image}" ]; then
err "please specify a image name"
return 1
fi
IFS=$'\n' read -d "" -ra arr <<< "${image//\//$'\n'}"
url="${arr[0]}"
if [ -z "${url}" ]; then
err "parse ${image} url fail"
return 1
fi
docker images "${url}" -q | xargs -I{} docker rmi -f {}
if ! docker build -f "${file}" -t "${image}" "${input}"; then
err "docker bukld -f $file -t $image $input fail"
return 1
fi
}
while (( "$#" )); do
case "$1" in
-h|-\?|--help) usage; exit 0 ;;
-f|--file) FILE="$2"; shift 2 ;;
-n|--image) IMAGE="${2}"; shift 2 ;;
-i|--input) INPUT="${2}"; shift 2 ;;
-o|--output) OUTPUT="${2}"; shift 2 ;;
*) break
esac
done
set -exo pipefail
if ! build "${FILE}" "${IMAGE}" "${INPUT}"; then
err "build image fail"
exit 1
fi
if ! run "${IMAGE}" "${OUTPUT}"; then
err "create enroot image fail"
exit 1
fi