Debian Codegraph @

contrib/bst-here

raw
1 #!/bin/bash
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 #
15 # Authors:
16 # Charles Bailey <[email protected]>
17 # Sam Thursfield <[email protected]>
18
19 # This is a helper script for using BuildStream via Docker. See
20 # docs/source/install.rst for documentation.
21
22 usage() {
23 cat <<EOF
24
25 USAGE: $(basename "$0") [-i BST_HERE_IMAGE] [-j TAG] [-p] [-t] [-T] [-v VOLUME ...] [-h] [COMMAND [ARG..]]
26
27 Run a bst command in a new BuildStream container.
28
29 If no command is specified, an interactive shell is launched
30 using "/bin/bash -i".
31
32 See https://hub.docker.com/r/buildstream/buildstream for details on image
33 variants.
34
35 OPTIONS:
36 -i IMAGE Specify Docker image to use; can also be specified by setting
37 BST_HERE_IMAGE environment variable.
38 (default: buildstream/buildstream)
39 -j TAG Specify the tag of the Docker image to use.
40 (default: latest)
41 -p Pull the latest buildstream image before running.
42 -t Force pseudo-terminal allocation.
43 -T Disable pseudo-terminal allocation.
44 -v VOLUME Specify additional volumes to mount; should be in format
45 'host-src:container-dest' same as 'docker run -v'.
46 -h Print this help text and exit.
47
48 EOF
49 exit "$1"
50 }
51
52 bst_here_image="${BST_HERE_IMAGE:-buildstream/buildstream}"
53 bst_here_tag=
54
55 is_tty=
56 update=false
57 extra_volumes_opt=
58
59 if test -t 0
60 then
61 is_tty=y
62 fi
63
64 while getopts i:j:ptTv:h arg
65 do
66 case $arg in
67 i)
68 bst_here_image="$OPTARG"
69 ;;
70 j)
71 bst_here_tag="$OPTARG"
72 ;;
73 p)
74 update=true
75 ;;
76 T)
77 is_tty=
78 ;;
79 t)
80 is_tty=y
81 ;;
82 v)
83 extra_volumes_opt="$extra_volumes_opt --volume $OPTARG"
84 ;;
85 h)
86 usage 0
87 ;;
88 \?)
89 usage 1
90 esac
91 done
92
93 if [ -n "$bst_here_tag" ]; then
94 bst_here_image="$bst_here_image:$bst_here_tag"
95 fi
96
97 test "$OPTIND" -gt 1 &&
98 shift $(( OPTIND - 1 ))
99
100 for vol in buildstream-cache buildstream-config
101 do
102 docker volume create "$vol" >/dev/null
103 done
104
105 BST_HERE_PS1="\[\033[01;34m\]\w\[\033[00m\]> "
106
107 if [ "$#" -eq 0 ]; then
108 command="/bin/bash -i"
109 else
110 command="/usr/local/bin/bst $@"
111 fi
112
113 if "$update" == true
114 then
115 docker pull "$bst_here_image"
116 fi
117
118 # FIXME: We run with --privileged to allow bwrap to mount system
119 # directories, but this is overkill. We should add the correct
120 # --cap-add calls, or seccomp settings, but we are not sure
121 # what those are yet.
122 #
123 # Old settings:
124 # --cap-add SYS_ADMIN
125 # --security-opt seccomp=unconfined
126 #
127 exec docker run --rm -i${is_tty:+ -t} \
128 --privileged \
129 --env PS1="$BST_HERE_PS1" \
130 --device /dev/fuse \
131 --volume buildstream-cache:/root/.cache/buildstream \
132 --volume buildstream-config:/root/.config \
133 --volume "$PWD":/src \
134 $extra_volumes_opt \
135 --workdir /src \
136 "$bst_here_image" \
137 $command