/
fresh-node10
executable file
·116 lines (107 loc) · 3.94 KB
/
fresh-node10
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/bin/bash -eu
#
# Fresh 19.10.1 (<https://github.com/wikimedia/fresh>)
#
# This is free and unencumbered software released into the public domain.
scriptversion='19.10.1 (2019-10-01)'
imagename=docker-registry.wikimedia.org/releng/node10-test-browser
imageversion=0.6.0-s1
shortname=fresh-node10
mountsrc="$PWD"
mountdest="/"$(basename "$PWD")
welcometxt_suffix=
docker_args=()
# Parse options
allow_env_mw=
allow_net=
allow_root=
bind_git_ro=
for arg in "$@"; do
case "$arg" in
# Option: -env
# Forward "MW_*" and "MEDIAWIKI_*" environment variables from the host machine
# to the Fresh container. This is designed for use with MediaWiki browser tests.
-env|-e|-env-mw) allow_env_mw=1 ;;
# Option: -net
# This exposes network ports from the host machine to the Fresh container.
# For example, to enable access to a MediaWiki web server.
# https://docs.docker.com/network/network-tutorial-host/
-net) allow_net=1 ;;
# Option: -root
# Log into to the Fresh container user 'root' instead of 'nobody'.
# This means you can use sudo commands inside the container, such as apt-get.
-root) allow_root=1 ;;
*)
echo "unknown option: $arg" 1>&2
echo "usage: fresh-node [-env] [-git] [-net] [-root]" 1>&2
exit 1
;;
esac
done
# Color codes. – http://linux.101hacks.com/ps1-examples/prompt-color-using-tput/
CLR_NONE=`tput sgr0`
CLR_BOLD=`tput bold`
CLR_GREEN=`tput setaf 2`
CLR_YELLOW=`tput setaf 3`
CLR_GREY=`tput setaf 7`
if [ -e "$mountsrc/.git" ]; then
bind_git_ro=1
welcometxt_suffix+="# $mountdest/.git ➟ $mountsrc/.git (read-only)\n"
fi
if [ -n "$allow_env_mw" ]; then
envfile="$(mktemp)"
# Backslash for grep bypasses user-defined aliases. – https://github.com/wikimedia/fresh/pull/7
env | \grep -E 'MW_|MEDIAWIKI_' > "$envfile"
welcometxt_suffix+="# ${CLR_YELLOW}env: MW_*, MEDIAWIKI_*${CLR_NONE}\n"
docker_args+=("--env-file" "$envfile")
# Teardown: unlink tmp env file, restore window title
trap "rm -f \"$envfile\"; echo -e '\033]2;\007'" 0 SIGTERM SIGINT
else
# Teardown: restore window title
trap "echo -e '\033]2;\007'" 0 SIGTERM SIGINT
fi
if [ -n "$allow_net" ]; then
welcometxt_suffix+="# ${CLR_YELLOW}net: expose host${CLR_NONE}\n"
docker_args+=("--network" "host")
fi
if [ -n "$allow_root" ]; then
docker_args+=("--user" "root")
fi
# Setup: Change window title
echo -e '\033]2;'"$shortname"'\007\c'
welcomecmd="echo \"$CLR_GREY# fresh: $CLR_BOLD$scriptversion$CLR_NONE$CLR_GREY
# image: $imagename:$imageversion
# software: Debian Linux 9 Stretch
# Node.js v10.15.2 (npm 6.5.0)
# Chromium 71.0.3578.80
# Mozilla Firefox 60.8.0
# JSDuck 5.3.4 (Ruby 2.3.3)
# mount: $mountdest ➟ $mountsrc (read-write)
$welcometxt_suffix
🌱 ${CLR_BOLD}${CLR_GREEN}Fresh!
$CLR_NONE\""
docker run --rm --interactive --tty -e 'HOME=/tmp' \
--mount type=bind,source="$mountsrc",target="$mountdest",consistency=delegated \
$( if [ -n "$bind_git_ro" ]; then printf %s "--mount type=bind,source="$mountsrc"/.git,target="$mountdest"/.git,readonly,consistency=cached"; fi ) \
${docker_args[@]+"${docker_args[@]}"} \
--entrypoint /bin/sh \
"$imagename:$imageversion" \
-c "cd $mountdest/;$welcomecmd;bash"
# The $docker_args variable is expanded in this way to avoid passing "",
# which would be misinterpreted as docker-run argument.
# See <https://stackoverflow.com/a/7577209/319266>
# Avoid file reads and shell commands as part of the welcome message.
# Instead, update $welcomecmd before a release by running the below
# instead of the above
#
# welcomecmd=". /etc/os-release
# echo \"$CLR_GREY# image: $imagename:$imageversion
# # software: \$PRETTY_NAME
# # Node.js \$(node --version) (npm \$(npm --version))
# # Chromium \$(chromium --product-version)
# # \$(firefox --version)
# # \$(jsduck --version) \$(ruby --version)
# $CLR_NONE\""
# docker run --rm --interactive --tty --entrypoint /bin/sh \
# "$imagename:$imageversion" \
# -c "$welcomecmd;bash"