-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunner.sh
More file actions
executable file
·274 lines (248 loc) · 6.03 KB
/
runner.sh
File metadata and controls
executable file
·274 lines (248 loc) · 6.03 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
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
#!/bin/bash
################################################################################
# Help #
################################################################################
Help()
{
# Display Help
echo "Add description of the script functions here."
echo
echo "Syntax: runner.sh start|stop|app|<command> [-v|-l]"
echo "options:"
echo "-h --help Displays this message"
echo "Start time:"
echo "-l --language The language the application is writen in"
echo "-v --version The version of the language to use"
echo "-p --publish This creates a firewall rule which maps a container port to a port on the Docker host to the outside world."
echo "-e --enviroment Specify an environemnt variable to run the container with"
echo "-m --mount Specify a volume to mount in to the docker image"
echo "-w --workdir Set if you want to copy in your local folder, but run commands on a sub-folder"
echo "Run time:"
echo "-e --enviroment Specify an environemnt variable to run the code with"
echo ""
echo "Management Commands:"
echo "start Start the container"
echo "stop Stops the container"
echo "app runs commmands direct on the app"
echo ""
echo "Commands:"
echo "run runs the code within the container"
}
################################################################################
################################################################################
# Main program #
################################################################################
################################################################################
WORKDIR="/src"
MAKEFILEDIR="/make"
repo_url="ghcr.io/byteford/runner"
image="alpine"
ENVVARS=()
MOUNTS=()
#fuction that will replace the / with an _ to make it docker name complient (this might need a limit on caracter legnth)
function get_name () {
loc=$(pwd)
echo "${loc//['\/']/}"
}
#function that will start a running container and will tell the user if it is already started (maybe fail silently)
function start_container(){
echo $image
ENV="${ENVVARS[@]/#/--env }"
echo $ENV
VOLUMES="${MOUNTS[@]/#/--volume }"
echo $VOLUMES
#uses rm to delete the container once finished as the container shouldn't save anything in it
docker run -it --detach --rm --name $(get_name) $PUBLISH --network host --volume $(pwd):$WORKDIR $VOLUMES --env "WORKDIR=$WORKDIR/$WORKDIRSUFFIX" $ENV $image
# if the error code is 125 then the container is already started
err=$?
if [ $err -ne 0 ]; then
if [ $err -eq 125 ]; then
echo "already started"
exit $err
fi
echo "failed to start"
exit $err
fi
echo "successfully Started"
}
#function will stop a running container with the name of the current pwd
function stop_container(){
docker stop $(get_name)
err=$?
if [ $err -ne 0 ]; then
echo "failed to stop"
exit $err
fi
echo "successfully Stopped"
}
#will run a command on the ocntainer
function run_app_command(){
docker exec -it -w $WORKDIR $(get_name) $*
}
#will run a make rule with in the given container
function run_make_command(){
echo $*
docker exec -it -w $MAKEFILEDIR $(get_name) make "PARAMS=${*:2}" "ENV=${ENVVARS[*]}" $1
}
#Work out language based on file extention
function LangCalc(){
for i in *.go; do
[ -f "$i" ] || break
LANGUAGE=go
return
done
for i in *.py; do
[ -f "$i" ] || break
LANGUAGE=python
return
done
for i in *.java; do
[ -f "$i" ] || break
LANGUAGE=maven
return
done
for i in *.cs; do
[ -f "$i" ] || break
LANGUAGE=cs
return
done
for i in *.tf; do
[ -f "$i" ] || break
LANGUAGE=terraform
return
done
if [ -f "package.json" ]; then
LANGUAGE=js
return
fi
if [ -f "pom.xml" ]; then
LANGUAGE=maven
return
fi
if [ -f "main.yaml" ]; then
LANGUAGE=ansible
return
fi
}
function VersionCalc(){
case $LANGUAGE in
go)
VERSION="1.18.0"
return
;;
maven)
VERSION="18"
return
;;
python)
VERSION="3.11.0b4"
return
;;
cs)
VERSION="6.0"
return
;;
js)
VERSION="18"
return
;;
terraform)
VERSION="1.2.7"
return
;;
ansible)
VERSION="8.6.1"
;;
k8s)
VERSION="1.29"
;;
ubuntu)
VERSION="24.10"
;;
crossplane)
VERSION="v1.16.0"
;;
*)
echo "default version not found"
exit 1
;;
esac
}
#if no errors print out a very basic help funtion
if [[ $# -eq 0 ]]; then
Help
exit 1
fi
POSITIONAL_ARGS=()
while [[ $# -gt 0 ]]; do
case $1 in
-v|--version)
VERSION="$2"
shift # past argument
shift # past value
;;
-l|--language)
LANGUAGE="$2"
shift # past argument
shift # past value
;;
-e|--enviroment)
ENVVARS+=("$2")
echo "set $2"
shift
shift
;;
-m|--mount)
MOUNTS+=("$2")
echo "set $2"
shift
shift
;;
-p|--publish)
PUBLISH+="--publish $2"
shift
shift
;;
-w|--workdir)
WORKDIRSUFFIX+="$2"
shift
shift
;;
-h| --help)
Help
exit 0
;;
*)
POSITIONAL_ARGS+=("$1") # save positional arg
shift # past argument
;;
esac
done
set -- "${POSITIONAL_ARGS[@]}"
echo "${ENVVARS[@]}"
case $1 in
"start")
if [ -z ${LANGUAGE} ]; then
LangCalc #gets the language by looks like the local files
fi
if [ -z ${VERSION} ]; then
VersionCalc #get a default version based on language
fi
image=$repo_url/runner-$LANGUAGE:$VERSION
start_container
shift
;;
"stop")
stop_container
shift
;;
"app")
run_app_command "${@:2}"
shift
;;
*)
run_make_command $*
shift
;;
esac
exit 0