aboutsummaryrefslogtreecommitdiffstats
path: root/dabuild-admin
blob: 299cb6a0b84f1af24094760a7a48454d7645bdef (plain)
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
#!/bin/sh

set -eu

readonly CMD=${1:-help}
readonly VERSION=unknown
readonly PROGRAM=${0##*/}

die() {
	echo "$@" >&2
	exit 1
}

ask() {
	local question="$1"
	while true; do
		# SC2039 -p is non posix but widely available
		read -rp "$question" yn
		case $yn in
			[Yy]) return 0;;
			[Nn]) exit;;
			*) echo "Please answer y/n.";;
		esac
	done
}

manage_config() {
	docker run --user 1000:1000 --rm -it --workdir /mnt/abuild \
		-v dabuild-config:/mnt/abuild alpine sh
}

list_volumes() {
	docker volume ls --quiet --filter name="^dabuild"
}

multiarch() {
	case "${2:-help}" in
		enable) ask "Are you sure you want to enable multi-arch system wide? (y/n): "
			docker run --rm --privileged multiarch/qemu-user-static \
				--reset --persistent yes --credential yes ;;
		disable) sudo --prompt="Sudo password:" find /proc/sys/fs/binfmt_misc \
			-type f -name 'qemu-*' -exec sh -c 'echo -1 > {}' \; ;;
		help) multiarch_usage ;;
		*) die "Unknown subcommand \"$2\"" ;;
	esac
}

multiarch_usage() {
	cat <<- EOF
	$PROGRAM ($VERSION)

	Available subcommands:
	  enable: enable multi-arch support via binfmt_misc
	  disable: disable mutli-arch support
	  help: this help screen
	EOF
}

usage() {
	cat <<- EOF
	$PROGRAM ($VERSION)

	Available commands:
	  config: access abuild configuration
	  volumes: list created dabuild volumes
	  multiarch: enable docker multi-arch support
	  help: this help screen
	EOF
}

case $CMD in
	config) manage_config;;
	volumes) list_volumes;;
	multiarch) multiarch "$@";;
	help) usage;;
	*) usage; exit 1;;
esac