-
Notifications
You must be signed in to change notification settings - Fork 11
/
ci-wrap
executable file
·95 lines (80 loc) · 2.02 KB
/
ci-wrap
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
#!/bin/citbash -e
# make sure to clean up after ourselves
wipetmp=no
trap cleanup EXIT
cleanup() {
if [ "$wipetmp" = "yes" ]; then
echo "Cleaning workspace!"
rm -rf $WORKSPACE
fi
}
# prep args et all
citcmd="$1"
# first pass sanity checks
if [ -z "$citcmd" ]; then
echo "no ci-tools command specified!"
exit 1
fi
# if citcmd is empty, this will fail, so check
# first, then shift for arguments
shift
# let´s try to avoid a recursive bomb of doom
# NOTE: avoiding path mangling et all, just
# basic protection vs copy/paste errors
# while we do conversions, since this wrapper
# will vanish eventually
if [ "$citcmd" = "ci-wrap" ]; then
echo "not allowed to self-call"
exit 1
fi
if [ ! -d "$HOME/ci-tools" ]; then
echo "ci-tools not installed on this node!"
exit 1
fi
# if CITHOME is already set, we are being called
# recursively and we don´t need to redo the
# git dance + env setting
if [ -n "$CITHOME" ]; then
echo "CITHOME already present in the environment. Nothing to do."
else
if [ -z "$CITBRANCH" ]; then
echo "CITBRANCH not detected! defaulting to main"
CITBRANCH=main
fi
if [ "$CITBRANCH" = "main" ]; then
CITHOME="$HOME/ci-tools"
else
if [ -z "$WORKSPACE" ]; then
WORKSPACE="$(mktemp -d -t ci-tools-XXXXX)"
wipetmp=yes
fi
echo "CITBRANCH set to $CITBRANCH"
echo "Using $WORKSPACE"
(
flock -e 200
if [ ! -d $WORKSPACE/ci-tools ]; then
echo "Copy HOME/ci-tools to WORKSPACE"
cp -rp $HOME/ci-tools $WORKSPACE/
echo "Updating WORKSPACE copy of ci-tools"
cd $WORKSPACE/ci-tools
git pull
echo "Switching WORKSPACE/ci-tools to $CITBRANCH"
git checkout $CITBRANCH
cd - > /dev/null
fi
) 200>$WORKSPACE/ci-wrap.lock
CITHOME="$WORKSPACE/ci-tools"
fi
fi
export CITHOME
export CITBRANCH
echo "CITBRANCH: $CITBRANCH"
echo "CITHOME: $CITHOME"
echo "CITCMD: $citcmd"
echo "CITCMDARGS: $@"
# second pass sanity checks (executed on checkout branch)
if [ ! -f "$CITHOME/$citcmd" ]; then
echo "command $CITHOME/$citcmd does not exist!"
exit 1
fi
$CITHOME/$citcmd "$@"