|
| 1 | +#! /bin/sh |
| 2 | + |
| 3 | +## environment variables |
| 4 | +## |
| 5 | +## - APT_LINE : "keep", "jp", "http://foobar.example.org/debian/" |
| 6 | +## - APT_UPDATE : "yes", "true", "on", 1 -> run apt-get update after init |
| 7 | +## - APT_INSTALL_PACKAGES : "pkg1 pkg2=1.2-3 pkg3- ..." |
| 8 | +## - APT_INSTALL_RECOMMENDS : "yes", "true", "on", 1 |
| 9 | +## - APT_INSTALL_SUGGESTS : "yes", "true", "on", 1 |
| 10 | +## - APT_HTTP_PROXY : "http://x.x.x.x:3142/" |
| 11 | +## |
| 12 | + |
| 13 | +set -e |
| 14 | +post_init_script=/opt/init-wrapper/post-init.d/${0##*/} |
| 15 | + |
| 16 | +if [ -z "${APT_LINE}" ]; then |
| 17 | + APT_LINE=keep |
| 18 | + if [ "$DEFAULT_TZ" = "Asia/Shanghai" ]; then |
| 19 | + APT_LINE=cn |
| 20 | + fi |
| 21 | +fi |
| 22 | + |
| 23 | +change_apt_line (){ |
| 24 | + uri=$1 |
| 25 | + sed -i -e "s@http://deb\.debian\.org/debian@${uri}@" /etc/apt/sources.list.d/debian.sources |
| 26 | +} |
| 27 | + |
| 28 | +clear_post_init (){ |
| 29 | + if [ -f ${post_init_script} ]; then |
| 30 | + : > ${post_init_script} |
| 31 | + fi |
| 32 | +} |
| 33 | + |
| 34 | +write_post_init (){ |
| 35 | + echo "#! /bin/sh" > ${post_init_script} |
| 36 | + |
| 37 | + case "${APT_UPDATE}" in |
| 38 | + 1|[tT][rR][uU][eE]|[yY][eE][sS]|[oO][nN]) |
| 39 | + echo "apt-get update";; |
| 40 | + *) |
| 41 | + echo "#apt-get update";; |
| 42 | + esac \ |
| 43 | + >> ${post_init_script} |
| 44 | + |
| 45 | + options= |
| 46 | + case "${APT_INSTALL_RECOMMENDS}" in |
| 47 | + 1|[tT][rR][uU][eE]|[yY][eE][sS]|[oO][nN]) |
| 48 | + options="$options --install-recommends";; |
| 49 | + *) |
| 50 | + options="$options --no-install-recommends";; |
| 51 | + esac |
| 52 | + case "${APT_INSTALL_SUGGESTS}" in |
| 53 | + 1|[tT][rR][uU][eE]|[yY][eE][sS]|[oO][nN]) |
| 54 | + options="$options --install-suggests";; |
| 55 | + *) |
| 56 | + options="$options --no-install-suggests";; |
| 57 | + esac |
| 58 | + if [ -n "${APT_INSTALL_PACKAGES}" ]; then |
| 59 | + echo "apt-get install -y ${options} ${APT_INSTALL_PACKAGES}" |
| 60 | + else |
| 61 | + echo "#apt-get install -y ${options} ${APT_INSTALL_PACKAGES}" |
| 62 | + fi \ |
| 63 | + >> ${post_init_script} |
| 64 | + |
| 65 | + echo "exit 0" \ |
| 66 | + >> ${post_init_script} |
| 67 | + |
| 68 | + chmod +x ${post_init_script} |
| 69 | +} |
| 70 | + |
| 71 | +if [ -n "${APT_LINE}" ]; then |
| 72 | + case "${APT_LINE}" in |
| 73 | + keep) |
| 74 | + # do nothing (use deb.debian.org) |
| 75 | + : |
| 76 | + ;; |
| 77 | + cn) |
| 78 | + change_apt_line http://mirrors.ustc.edu.cn/debian |
| 79 | + ;; |
| 80 | + jp) |
| 81 | + change_apt_line http://ftp.jp.debian.org/debian |
| 82 | + ;; |
| 83 | + *) |
| 84 | + change_apt_line "${APT_LINE}" |
| 85 | + ;; |
| 86 | + esac |
| 87 | +fi |
| 88 | + |
| 89 | +if [ -n "${APT_HTTP_PROXY}" ]; then |
| 90 | + echo "Acquire::http::proxy \"${APT_HTTP_PROXY}\";" > /etc/apt/apt.conf.d/proxy.conf |
| 91 | + echo "Acquire::https::proxy \"DIRECT\";" >> /etc/apt/apt.conf.d/proxy.conf |
| 92 | +fi |
| 93 | + |
| 94 | +clear_post_init |
| 95 | +write_post_init |
| 96 | + |
0 commit comments