dabuild.in 2.84 KB
Newer Older
Richard Mortier's avatar
Richard Mortier committed
1
2
#!/bin/sh

Richard Mortier's avatar
Richard Mortier committed
3
4
5
# Copyright (C) 2019 Richard Mortier <mort@cantab.net>
# Licensed under the MIT License, https://opensource.org/licenses/MIT

Richard Mortier's avatar
Richard Mortier committed
6
7
set -e

8
die () {
Richard Mortier's avatar
Richard Mortier committed
9
  printf >&2 "%s\n" "$@"
10
11
12
  exit 1
}

Richard Mortier's avatar
Richard Mortier committed
13
## debug
14
if [ "$DABUILD_DEBUG" = "true" ]; then
Richard Mortier's avatar
Richard Mortier committed
15
16
17
18
  set -x
  PS4='$LINENO: '
fi

19
## check running from within an `aports` tree
20
if [ "${PWD%*/aports/*}" = "$PWD" ]; then
21
  die "Error: expecting to be run from within an aports tree!" \
22
      "Could not find '/aports/' in the current path: $PWD"
23
24
25
  exit 1
fi

26
27
28
## allow setting of arch by env variable
[ ! "$DABUILD_ARCH" ] && DABUILD_ARCH=$(uname -m)
case "$DABUILD_ARCH" in
macmpi's avatar
macmpi committed
29
  x86|x86_64|aarch64|armhf|armv7 ) ;;
30
  * ) die "Unsupported arch \"$DABUILD_ARCH\" detected." \
macmpi's avatar
macmpi committed
31
32
          "Expected one of: x86|x86_64|aarch64|armhf|armv7" \
          "You may force it setting DABUILD_ARCH=\"xxx\" in invocation";;
33
34
esac

35
## use branch to figure out most appropriate alpine version
36
37
if [ "$DABUILD_VERSION" ]; then
  ABUILD_VERSION=$DABUILD_VERSION
38
39
40
41
42
43
else
  APORTS_BRANCH=$(git status | head -1)
  APORTS_BRANCH="${APORTS_BRANCH##*[ /]}"
  case $APORTS_BRANCH in
    [[:digit:]].[[:digit:]]-stable )
      ABUILD_VERSION=${APORTS_BRANCH%-stable}
44
      ;;
45

46
    * )
47
      ABUILD_VERSION=edge
48
49
50
51
      ;;
  esac
fi

52
## check $DABUILD_PACKAGES is a writable directory
53
ABUILD_PACKAGES=${DABUILD_PACKAGES:-${PWD%/aports/*}/packages/${ABUILD_VERSION}}
54

55
mkdir -p $ABUILD_PACKAGES
56
if [ ! \( -d "$ABUILD_PACKAGES" -a -w "$ABUILD_PACKAGES" \) ]; then
57
58
59
  die "Error: invalid or unwritable packages directory specified!" \
      "ABUILD_PACKAGES = '$ABUILD_PACKAGES'" \
      "DABUILD_PACKAGES = '$DABUILD_PACKAGES'"
60
61
fi

62
## setup volumes; use named volumes as cache if desired
Carlo Landmeter's avatar
Carlo Landmeter committed
63
ABUILD_VOLUMES="-v ${HOME}/.abuild:/home/builder/.abuild \
64
  -v ${PWD%/aports/*}/aports:/home/builder/aports \
Carlo Landmeter's avatar
Carlo Landmeter committed
65
66
67
  -v ${ABUILD_PACKAGES}:/home/builder/packages"

if [ -f "/etc/abuild.conf" ]; then
68
  ABUILD_VOLUMES="$ABUILD_VOLUMES -v /etc/abuild.conf:/etc/abuild.conf:ro"
Carlo Landmeter's avatar
Carlo Landmeter committed
69
70
71
72
fi

if [ -w "/var/cache/distfiles" ]; then
  ABUILD_VOLUMES="$ABUILD_VOLUMES -v /var/cache/distfiles:/var/cache/distfiles"
73
74
fi

75
if [ "$DABUILD_CACHE" = "true" ]; then
76
  for v in %%ABUILD_VOLUMES%% ; do
77
    vol=abuild-$ABUILD_VERSION-$DABUILD_ARCH-${v//\//_}
78
    if [ "$DABUILD_CLEAN" = "true" ]; then
Richard Mortier's avatar
Richard Mortier committed
79
      ## clean the cache if requested
80
      docker rm -f $(docker ps -qaf "volume=$vol") >/dev/null 2>&1 || true
81
      docker volume rm $vol >/dev/null
82
83
84
    fi

    if docker volume ls -f 'name=$v' | grep $v >/dev/null ; then
85
86
87
      docker volume create $vol >/dev/null
    fi
    ABUILD_VOLUMES="$ABUILD_VOLUMES -v $vol:/$v"
88
89
  done
fi
Richard Mortier's avatar
Richard Mortier committed
90

91
92
93
94
95
ABUILD_RM="--rm"
if [ "$DABUILD_RM" = "false" ]; then
  ABUILD_RM=""
fi

96
## go!
97
98
99
100
101
102
103
docker run --tty --interactive \
       $ABUILD_RM \
       $ABUILD_VOLUMES \
       -e DABUILD_DEBUG \
       $DABUILD_ARGS \
       --workdir /home/builder/aports/${PWD#*/aports/} \
       %%ABUILD_IMAGE%%:$ABUILD_VERSION-$DABUILD_ARCH "$@"