-
Notifications
You must be signed in to change notification settings - Fork 0
/
rep-container
executable file
·144 lines (116 loc) · 2.49 KB
/
rep-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
#! /usr/bin/env bash
########################################################################################
BOLD=1
RED=31
################################################################################
# Official container image (String)
IMAGE="ghcr.io/essentialkaos/rep:latest"
# Path to working dir (String)
WORKDIR="${REP_DIR:-/opt/rep}"
################################################################################
engine=""
################################################################################
# Main function
#
# Code: No
# Echo: No
main() {
engine=$(getContainerEngine)
checkEnv
prepareEnv
$engine run -it --rm -v "$WORKDIR":'/rep' -v "$(pwd)":'/input' "$IMAGE" "$@"
}
# Check environment
#
# Code: No
# Echo: No
checkEnv() {
if [[ -z "$engine" ]] ; then
error "You must install Podman or Docker runtime first"
exit 1
fi
if [[ ! -e "$WORKDIR" ]] ; then
error "Working directory \"$WORKDIR\" doesn't exist"
exit 1
fi
if [[ ! -d "$WORKDIR" ]] ; then
error "\"$WORKDIR\" is not directory"
exit 1
fi
if [[ ! -w "$WORKDIR" ]] ; then
error "Working directory \"$WORKDIR\" is not writable"
exit 1
fi
if [[ ! -r "$WORKDIR" ]] ; then
error "Working directory \"$WORKDIR\" is not readable"
exit 1
fi
}
# Prepare environment for running image
#
# Code: No
# Echo: No
prepareEnv() {
if ! isImageDownloaded ; then
show "Downloading official rep image…\n" $BOLD
$engine pull "$IMAGE"
fi
if [[ ! -e "$WORKDIR/conf" ]] ; then
mkdir "$WORKDIR/conf"
fi
if [[ ! -e "$WORKDIR/data" ]] ; then
mkdir "$WORKDIR/data"
fi
if [[ ! -e "$WORKDIR/cache" ]] ; then
mkdir "$WORKDIR/cache"
fi
if [[ ! -e "$WORKDIR/logs" ]] ; then
mkdir "$WORKDIR/logs"
fi
}
# Check if container image is downloaded or not
#
# Code: Yes
# Echo: No
isImageDownloaded() {
if [[ -z "$($engine images -q $IMAGE 2> /dev/null)" ]] ; then
return 1
fi
return 0
}
# Check if some app is installed
#
# 1: Binary name (String)
#
# Code: Yes
# Echo: No
hasApp() {
type "$1" &> /dev/null
return $?
}
# Show message
#
# 1: Message (String)
# 2: Message color (Number) [Optional]
#
# Code: No
# Echo: No
show() {
if [[ -n "$2" ]] ; then
echo -e "\e[${2}m${1}\e[0m"
else
echo -e "$*"
fi
}
# Print error message
#
# 1: Message (String)
# 2: Message color (Number) [Optional]
#
# Code: No
# Echo: No
error() {
show "$*" $RED 1>&2
}
################################################################################
main "$@"