-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunpod
executable file
·302 lines (285 loc) · 7.42 KB
/
runpod
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#!/bin/sh
# WARNING: This file was auto-generated. Do not edit!
#
# $Id: runpod.sh,v 1.10 2022-12-08 09:40:09+05:30 Cprogrammer Exp mbhangui $
#
usage()
{
(
echo "`basename $0` -i|--id imageid [-n|--name name]"
echo " [-a|--args extra_args1 -a|--args extra_args2 ... -a|--args extra_argsn]"
echo " [-h|--host host] [-g|--cgroup cgroup]"
echo " [-p|--port lport1:rport1 -p|--port lport2:rport2 ... -p|--port lportn:rportn]"
echo " [-v|--volume.dir:mount_point1 -v|--volume dir:mount_point2 ... -v|--volume dir:mount_pointn]"
echo " [-C|--capability cap1 -C|--capability cap2 ... -C|--capability cap3] | [-C|--capability auto]"
echo " [-c|--cmd podman|docker] prog args args ..."
echo
echo "lport - local port, rport - remote port"
) 1>&2
exit $1
}
set_volume()
{
word_count=$(echo $1 | cut --output-delimiter=" " -d: -f1,2,3 | wc -w)
if [ $word_count -ne 2 ] ; then
echo "volume should be of the form \"dir:mount_point\"" 1>&2
exit 1
fi
dir=$(echo $1|cut -d: -f1)
first_char=$(echo $dir | cut -c1)
if [ " $first_char" = " /" ] ; then
if [ ! -d $dir ] ; then
echo "$dir: No such directory" 1>&2
usage 1
fi
else
$command volume inspect $dir > /dev/null
if [ $? -ne 0 ] ; then
(
echo "$dir: No such volume"
echo "List of volumes are"
$command volume ls
) 1>&2
usage 1
fi
fi
dir=$(echo $1|cut -d: -f2)
first_char=$(echo $dir | cut -c1)
if [ " $first_char" != " /" ] ; then
echo "$dir should be a valid path" 1>&2
usage 1
fi
vol_args="$vol_args""-v $1 "
}
set_defaults()
{
case $name in
indimail|indimail-mta|webmail|devel|svscan|test|mail)
if [ -z "$port_args" ] ; then
case $name in
indimail|indimail-mta|webmail)
port_args="$port_args-p 2025:25 -p 2106:106 -p 2110:110 -p 2143:143 "
port_args="$port_args-p 2209:209 -p 2366:366 -p 2465:465 -p 2587:587 "
port_args="$port_args-p 2628:628 -p 2993:993 -p 2995:995 -p 3110:4110 "
port_args="$port_args-p 3143:4143 -p 5110:9110 -p 5143:9143 -p 8080:80 -p 8081:443"
# port_str is used just for display purpose
NEWLINE=$'\n'
port_str=""
port_str="${port_str} -p 2025:25 -p 2106:106 -p 2110:110 -p 2143:143${NEWLINE}"
port_str="${port_str} -p 2209:209 -p 2366:366 -p 2465:465 -p 2587:587${NEWLINE}"
port_str="${port_str} -p 2628:628 -p 2993:993 -p 2995:995 -p 3110:4110${NEWLINE}"
port_str="${port_str} -p 3143:4143 -p 5110:9110 -p 5143:9143 -p 8080:80 -p 8081:443"
;;
esac
fi
if [ -z "$cap_args" ] ; then
cap_args="$cap_args--cap-add SYS_PTRACE --cap-add SYS_ADMIN "
cap_args="$cap_args--cap-add IPC_LOCK --cap-add SYS_RESOURCE "
cap_args="$cap_args--cap-add=NET_ADMIN --cap-add=CAP_NET_RAW "
cap_args="$cap_args--cap-add=SYS_NICE "
else
echo $cap_args|grep SYS_ADMIN > /dev/null
if [ $? -ne 0 ] ; then
cap_args="$cap_args--cap-add SYS_ADMIN "
fi
fi
if [ -z "$host_args" ] ; then
host_args="-h indimail.org"
fi
if [ -z "$vol_args" ] ; then
if [ -d /home/$command/queue ] ; then
vol_args="$vol_args-v /home/$command/queue:/var/queue "
fi
if [ -d /home/$command/mail ] ; then
vol_args="$vol_args-v /home/$command/mail:/home/mail "
fi
if [ " $name" = " devel" -a -d /usr/local/src ] ; then
vol_args="$vol_args-v /usr/local/src:/usr/local/src"
fi
fi
if [ " $name" = " devel" -o " $name" = " test" ] ; then
if [ $# -eq 0 ] ; then
systemd="bash"
extra_args="$extra_args ""-ti --rm"
fi
elif [ " $name" = " svscan" ] ; then
extra_args="$extra_args ""-ti"
fi
if [ -z "$extra_args" ] ; then
detached=1
extra_args="-d"
fi
;;
esac
if [ -n "$cgroup" -a "$cgroup" = "auto" ] ; then
cgroup="-v /sys/fs/cgroup:/sys/fs/cgroup:rw"
fi
}
options=$(getopt -a -n runpod -o "a:n:h:c:i:p:C:g:v:" -l args:,name:,host:,cmd:,id:,port:,capa:,cgroups:,volume: -- "$@")
if [ $? != 0 ]; then
usage
fi
command=podman
name="test"
detached=0
port_str=""
port_args=""
host_args=""
vol_args=""
cap_args=""
extra_args=""
systemd=""
imageid=""
eval set -- "$options"
while :
do
case "$1" in
-a | --args)
echo $2 | grep "\-d" >/dev/null && detached=1
extra_args="$extra_args ""$2"
shift 2
;;
-n | --name)
name=$2
shift 2
;;
-h | --host)
host_args="-h $2"
shift 2
;;
-c | --cmd)
command=$2
shift 2
if [ " $command" != " podman" -a " $command" != " docker" ] ; then
echo "command should be podman or docker" 1>&2
usage 1
fi
;;
-i | --id)
imageid=$2
shift 2
;;
-p | --port)
port_args="$port_args""-p $2 "
shift 2
;;
-C | --capability)
if [ " $2" = " auto" ] ; then
cap_args="$cap_args--cap-add SYS_PTRACE --cap-add SYS_ADMIN "
cap_args="$cap_args--cap-add IPC_LOCK --cap-add SYS_RESOURCE "
cap_args="$cap_args--cap-add SYS_NICE "
else
cap_args="$cap_args--cap-add $2 "
fi
shift 2
;;
-g | --cgroup)
cgroups="$optarg"
;;
-v | --volume)
set_volume $2
shift 2
;;
--) # end of options
shift
break
;;
*)
echo "Unexpected option: $1 - this should'nt happen." 1>&2
usage
;;
esac
done
if [ -z "$imageid" ] ; then
usage 1
fi
set_defaults $*
if [ $# -gt 0 -a "$1" = "auto" ] ; then
tag=`$command images | grep $imageid | awk '{print $2}'`
case $tag in
xenial*|debian*|focal*|bionic*|archlinux*|ubi*)
systemd=/lib/systemd/systemd
;;
alpine*|gentoo*)
systemd="/sbin/init"
;;
*)
systemd=/usr/lib/systemd/systemd
;;
esac
if [ -n "$systemd" ] ; then
set "$systemd $*"
fi
fi
echo "$command run $cgroup $extra_args"
echo " --publish-all --name $name"
echo " --device /dev/fuse"
if [ -n "$host_args" ] ; then
echo " $host_args"
fi
if [ -n "$cap_args" ] ; then
echo $extra_args | grep "\--privileged" >/dev/null
if [ $? -eq 0 ] ; then
cap_args=""
else
echo " $cap_args"
fi
fi
if [ -z "$port_args" ] ; then
if [ -n "$port_str" ] ; then
port_args=$port_str
fi
fi
if [ -n "$port_args" ] ; then
echo " $port_args"
fi
if [ -n "$vol_args" ] ; then
echo " $vol_args"
fi
echo " $imageid $*"
$command run $cgroup $extra_args \
--publish-all --name $name \
--device /dev/fuse \
$host_args $cap_args $port_args $vol_args \
$imageid $*
if [ $? -eq 0 ] ; then
if [ $detached -eq 1 ] ; then
$command exec -ti $name bash
fi
else
echo "Removing container id $name" 1>&2
$command rm $name > /dev/null 2>&1
fi
#
# $Log: runpod.sh,v $
# Revision 1.10 2022-12-08 09:40:09+05:30 Cprogrammer
# fixed setting of systemd
#
# Revision 1.9 2022-12-07 20:51:56+05:30 Cprogrammer
# added port 8081 for mapping to port 443 on container OS
#
# Revision 1.8 2022-12-07 00:56:36+05:30 Cprogrammer
# run in foreground if name is test or devel
#
# Revision 1.7 2022-11-10 16:55:40+05:30 Cprogrammer
# redirect all error message to fd 2
#
# Revision 1.6 2022-11-10 12:49:11+05:30 Cprogrammer
# fixed extra_args variable getting clobbered
#
# Revision 1.5 2022-11-08 23:27:52+05:30 Cprogrammer
# added case for name=svscan
#
# Revision 1.4 2022-11-07 22:43:38+05:30 Cprogrammer
# prevent output of test on stdout
#
# Revision 1.3 2022-11-01 23:53:11+05:30 Cprogrammer
# fixed port clash
#
# Revision 1.2 2022-09-19 21:21:22+05:30 Cprogrammer
# added SYS_NICE capability to defaults
#
# Revision 1.1 2022-09-12 22:50:12+05:30 Cprogrammer
# Initial revision
#
#