-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrpmbuilder-docker
executable file
·170 lines (139 loc) · 2.76 KB
/
rpmbuilder-docker
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
#!/usr/bin/env bash
# shellcheck disable=SC2034
################################################################################
NORM=0
BOLD=1
UNLN=4
RED=31
GREEN=32
YELLOW=33
BLUE=34
MAG=35
CYAN=36
GREY=37
DARK=90
CL_NORM="\e[0m"
CL_BOLD="\e[0;${BOLD};49m"
CL_UNLN="\e[0;${UNLN};49m"
CL_RED="\e[0;${RED};49m"
CL_GREEN="\e[0;${GREEN};49m"
CL_YELLOW="\e[0;${YELLOW};49m"
CL_BLUE="\e[0;${BLUE};49m"
CL_MAG="\e[0;${MAG};49m"
CL_CYAN="\e[0;${CYAN};49m"
CL_GREY="\e[0;${GREY};49m"
CL_DARK="\e[0;${DARK};49m"
CL_BL_RED="\e[1;${RED};49m"
CL_BL_GREEN="\e[1;${GREEN};49m"
CL_BL_YELLOW="\e[1;${YELLOW};49m"
CL_BL_BLUE="\e[1;${BLUE};49m"
CL_BL_MAG="\e[1;${MAG};49m"
CL_BL_CYAN="\e[1;${CYAN};49m"
CL_BL_GREY="\e[1;${GREY};49m"
################################################################################
# Default image repo
IMAGE_REPO="ghcr.io/essentialkaos/rpmbuilder"
# Default image name
DEFAULT_IMAGE="$IMAGE_REPO:ol8"
################################################################################
# Main function
#
# Code: No
# Echo: No
main() {
if ! hasApp "docker" ; then
error "You must install Docker runtime first"
exit 1
fi
run "$@"
exit $?
}
# Run docker image
#
# Code: Yes
# Echo: No
run() {
local image user_id
image=$(getImage)
user_id=$(getUserID)
if ! hasImage "$image" ; then
show "\n${CL_BOLD}Pulling image ${CL_BL_BLUE}$image${CL_NORM}${CL_BOLD}…${CL_NORM}\n"
if ! docker pull "$image" ; then
exit 1
fi
fi
show "\nIMAGE: $image" $DARK
docker run --rm -e LOCAL_USER_ID="$user_id" -v "$(pwd):/rpmbuilder" "$image" rpmbuilder "$@"
return $?
}
# Get target image for using
#
# Code: No
# Echo: Image name (String)
getImage() {
local image="${IMAGE:-$DEFAULT_IMAGE}"
if [[ ! $image =~ \: ]] ; then
image="$IMAGE_REPO:$IMAGE"
fi
echo "$image"
}
# Get current user ID
#
# Code: No
# Echo: User ID (Number)
getUserID() {
local user_id
user_id=$(id -u)
if [[ $user_id -eq 0 ]] ; then
user_id="${SUDO_UID:-0}"
fi
echo "$user_id"
}
# Check if image already pulled
#
# 1: Image name (String)
#
# Code: Yes
# Echo: No
hasImage() {
if [[ -n $(docker images -q "$1" 2> /dev/null) ]] ; then
return 0
fi
return 1
}
# Check if some app is installed
#
# 1: Binray 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" && -z "$no_colors" ]] ; 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 "$@"