Skip to content

Commit 39dce15

Browse files
General cleanup of shell bits:
- Avoid `sed` use for functionality built into shell parameter expansion - Avoid piping into a `while read` loop, which introduces the surprising behaviors discussed in [BashFAQ 24](https://mywiki.wooledge.org/BashFAQ/024) - Switch from external command `which` to built-in, POSIX-standardized `command -v` - Avoid using pushd and popd (which are interactive-extension options, not guaranteed to be compiled into a noninteractive shell at all). With this applied, `shellcheck scripts/*` runs clean. Not yet addressed: - Failure to sanitize or escape strings substituted into JSON (base image does not provide jq or this would be trivial) - Use of [`set -e`](https://mywiki.wooledge.org/BashFAQ/105)
1 parent 6763cba commit 39dce15

File tree

5 files changed

+55
-41
lines changed

5 files changed

+55
-41
lines changed

bin/setup-cgroups

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,37 +10,40 @@ fi
1010
mkdir -p /sys/fs/cgroup
1111
mount -t tmpfs -o uid=0,gid=0,mode=0755 cgroup /sys/fs/cgroup
1212

13-
sed -e 1d /proc/cgroups | while read sys hierarchy num enabled; do
14-
if [ "$enabled" != "1" ]; then
15-
# subsystem disabled; skip
16-
continue
17-
fi
13+
{
14+
read -r _ # skip first line
15+
while read -r sys _hierarchy _num enabled; do
16+
if [ "$enabled" != "1" ]; then
17+
# subsystem disabled; skip
18+
continue
19+
fi
1820

19-
grouping="$(cat /proc/self/cgroup | cut -d: -f2 | grep "\\<$sys\\>")" || true
20-
if [ -z "$grouping" ]; then
21-
# subsystem not mounted anywhere; mount it on its own
22-
grouping="$sys"
23-
fi
21+
grouping="$(</proc/self/cgroup cut -d: -f2 | grep "\\<$sys\\>")" || true
22+
if [ -z "$grouping" ]; then
23+
# subsystem not mounted anywhere; mount it on its own
24+
grouping="$sys"
25+
fi
2426

25-
mountpoint="/sys/fs/cgroup/$grouping"
27+
mountpoint="/sys/fs/cgroup/$grouping"
2628

27-
mkdir -p "$mountpoint"
29+
mkdir -p "$mountpoint"
2830

29-
# clear out existing mount to make sure new one is read-write
30-
if mountpoint -q "$mountpoint"; then
31-
umount "$mountpoint"
32-
fi
31+
# clear out existing mount to make sure new one is read-write
32+
if mountpoint -q "$mountpoint"; then
33+
umount "$mountpoint"
34+
fi
3335

34-
mount -n -t cgroup -o "$grouping" cgroup "$mountpoint"
36+
mount -n -t cgroup -o "$grouping" cgroup "$mountpoint"
3537

36-
if [ "$grouping" != "$sys" ]; then
37-
if [ -L "/sys/fs/cgroup/$sys" ]; then
38-
rm "/sys/fs/cgroup/$sys"
39-
fi
38+
if [ "$grouping" != "$sys" ]; then
39+
if [ -L "/sys/fs/cgroup/$sys" ]; then
40+
rm "/sys/fs/cgroup/$sys"
41+
fi
4042

41-
ln -s "$mountpoint" "/sys/fs/cgroup/$sys"
42-
fi
43-
done
43+
ln -s "$mountpoint" "/sys/fs/cgroup/$sys"
44+
fi
45+
done
46+
} </proc/cgroups
4447

4548
if ! test -e /sys/fs/cgroup/systemd ; then
4649
mkdir /sys/fs/cgroup/systemd

scripts/build-image

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22

33
set -e -u -x
44

5-
cd $(dirname $0)/..
5+
script=${BASH_SOURCE[0]}
6+
cd "${script%/*}"/.. || exit
67

78
export PATH=$PWD/bin:$PATH
89

910
. ./scripts/setup-buildkit.sh
1011

1112
mkdir -p image
1213

13-
build
14+
exec build

scripts/push-image

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,21 @@ version=""
77

88
case $GITHUB_REF in
99
refs/heads/*)
10-
tag=$(echo $GITHUB_REF | sed 's|refs/heads/||')
10+
tag=${GITHUB_REF#refs/heads/}
1111
;;
1212

1313
refs/tags/v[0-9]*)
14-
version=$(echo $GITHUB_REF | sed 's|refs/tags/v||')
14+
version=${GITHUB_REF#refs/tags/v}
1515
;;
1616

1717
refs/tags/*)
18-
tag=$(echo $GITHUB_REF | sed 's|refs/tags/||')
18+
tag=${GITHUB_REF#refs/tags/}
1919
;;
2020

2121
refs/pull/[0-9]*/merge)
22-
tag=pr$(echo $GITHUB_REF | sed 's|refs/pull/\([0-9]\+\)/merge|\1|')
22+
tag=${GITHUB_REF#refs/pull/}
23+
tag=${tag%/merge}
24+
tag=pr$tag
2325
;;
2426

2527
*)

scripts/setup-buildkit.sh

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
1-
if ! which buildctl >/dev/null || ! which buildkitd >/dev/null; then
1+
#!/usr/bin/env bash
2+
# this is sourced, not executed; the shebang above is a hint for shellcheck and/or editors
3+
4+
uname_arch=$(uname -m)
5+
case $uname_arch in
6+
x86_64) arch=amd64;;
7+
aarch64) arch=arm64;;
8+
*) arch=$uname_arch;;
9+
esac
10+
11+
if ! command -v buildctl >/dev/null || ! command -v buildkitd >/dev/null; then
212
BUILDKIT_VERSION=0.9.1
3-
BUILDKIT_URL=https://github.com/moby/buildkit/releases/download/v$BUILDKIT_VERSION/buildkit-v$BUILDKIT_VERSION.linux-amd64.tar.gz
13+
BUILDKIT_URL=https://github.com/moby/buildkit/releases/download/v$BUILDKIT_VERSION/buildkit-v$BUILDKIT_VERSION.linux-${arch}.tar.gz
414

515
curl -fL "$BUILDKIT_URL" | tar zxf -
616
fi
717

8-
if [ "$(id -u)" != "0" ]; then
9-
if ! which newuidmap >/dev/null || ! which newgidmap >/dev/null; then
10-
echo "newuidmap and newgidmap must be installed"
18+
if [ "$UID" != "0" ]; then
19+
if ! command -v newuidmap >/dev/null || ! command -v newgidmap >/dev/null; then
20+
echo "newuidmap and newgidmap must be installed" >&2
1121
exit 1
1222
fi
1323

14-
if ! which rootlesskit >/dev/null; then
15-
pushd rootlesskit
16-
make
17-
popd
18-
24+
if ! command -v rootlesskit >/dev/null; then
25+
(cd rootlesskit && exec make)
1926
cp rootlesskit/bin/* bin/
2027
fi
2128
fi

scripts/test

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
set -e -u
44

5-
cd $(dirname $0)/..
5+
script=${BASH_SOURCE[0]}
6+
cd "${script%/*}/.." || exit
67

78
export PATH=$PWD/bin:$PATH
89

0 commit comments

Comments
 (0)