-
Notifications
You must be signed in to change notification settings - Fork 2
/
perfecto-container
executable file
·184 lines (142 loc) · 2.97 KB
/
perfecto-container
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
#! /usr/bin/env bash
# shellcheck disable=SC2034,SC2317
################################################################################
# Name of registry, repository and name of official image
IMAGE_BASE="ghcr.io/essentialkaos/perfecto"
# Name of perfecto image on GitHub Container Registry with OL 8 based image
IMAGE_CENTOS="$IMAGE_BASE:ol8"
# Name of perfecto image on GitHub Container Registry with Alpine based image
IMAGE_MICRO="$IMAGE_BASE:micro"
################################################################################
engine=""
################################################################################
# Main function
#
# Code: No
# Echo: No
main() {
engine=$(getContainerEngine)
if [[ -z "$engine" ]] ; then
error "You must install Podman or Docker first"
exit 1
fi
if [[ $# -eq 0 ]] ; then
usage
exit 0
fi
check "$@"
exit $?
}
# Run perfecto check
#
# *: Specs
#
# Code: Yes
# Echo: No
check() {
local image tmp_dir args opts status
image=$(getImage)
tmp_dir=$(mktemp -d -t 'pfcnt-XXXXXXXXXXXXX')
args=$(processArgs "$tmp_dir" "$@")
if [[ -z "$CI" ]] ; then
opts="-i -t"
else
opts="-e CI=true"
fi
# shellcheck disable=SC2086
$engine run --rm $opts -v "$tmp_dir:/perfecto" "$image" ${args}
status=$?
rm -rf "$tmp_dir"
return $status
}
# Show usage info
#
# Code: Yes
# Echo: No
usage() {
local image opts
image=$(getImage)
if [[ -z "$CI" ]] ; then
opts="-i -t"
else
opts="-e CI=true"
fi
# shellcheck disable=SC2086
$engine run --rm $opts "$image" "--help"
return $?
}
# Process arguments
#
# 1: Path to temporary directory (String)
# *: Specs
#
# Code: No
# Echo: Arguments (String)
processArgs() {
local tmp_dir="$1"
local arg spec_name result
shift 1
for arg in "$@" ; do
if [[ ! -r "$arg" ]] ; then
result="$result $arg"
continue
fi
cp "$arg" "$tmp_dir/" &>/dev/null
spec_name=$(basename "$arg")
result="$result $spec_name"
done
echo "$result"
}
# Get container image name with perfecto
#
# Code: No
# Echo: Image name (String)
getImage() {
if [[ -n "$IMAGE" ]] ; then
if [[ ! $IMAGE =~ \/ ]] ; then
echo "$IMAGE_BASE:$IMAGE"
return
fi
echo "$IMAGE"
return
fi
if [[ $(printenv "GITHUB_ACTIONS") == "true" ]] ; then
echo "$IMAGE_MICRO"
return
fi
if [[ $(printenv "CI") == "true" ]] ; then
echo "$IMAGE_MICRO"
return
fi
echo "$IMAGE_CENTOS"
}
# Get used container engine
#
# Code: No
# Echo: Engine name (String)
getContainerEngine() {
if [[ -n "$ENGINE" ]] && hasApp "$ENGINE" ; then
echo "$ENGINE"
return
fi
if hasApp "docker" ; then
echo "docker"
return
fi
if hasApp "podman" ; then
echo "podman"
return
fi
}
# Check if some app is installed
#
# 1: Binray name (String)
#
# Code: Yes
# Echo: No
hasApp() {
type "$1" &> /dev/null
return $?
}
################################################################################
main "$@"