|
| 1 | +#!/usr/bin/env bash |
| 2 | +#------------------------------------------------------------------------------------------------------------- |
| 3 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 4 | +# Licensed under the MIT License. See https://go.microsoft.com/fwlink/?linkid=2090316 for license information. |
| 5 | +#------------------------------------------------------------------------------------------------------------- |
| 6 | + |
| 7 | +# Syntax: ./common-debian.sh <install zsh flag> <username> <user UID> <user GID> <upgrade packages flag> |
| 8 | + |
| 9 | +set -e |
| 10 | + |
| 11 | +INSTALL_ZSH=${1:-"true"} |
| 12 | +USERNAME=${2:-"$(awk -v val=1000 -F ":" '$3==val{print $1}' /etc/passwd)"} |
| 13 | +USER_UID=${3:-1000} |
| 14 | +USER_GID=${4:-1000} |
| 15 | +UPGRADE_PACKAGES=${5:-"true"} |
| 16 | + |
| 17 | +if [ "$(id -u)" -ne 0 ]; then |
| 18 | + echo 'Script must be run a root. Use sudo or set "USER root" before running the script.' |
| 19 | + exit 1 |
| 20 | +fi |
| 21 | + |
| 22 | +# Treat a user name of "none" as root |
| 23 | +if [ "${USERNAME}" = "none" ] || [ "${USERNAME}" = "root" ]; then |
| 24 | + USERNAME=root |
| 25 | + USER_UID=0 |
| 26 | + USER_GID=0 |
| 27 | +fi |
| 28 | + |
| 29 | +# Ensure apt is in non-interactive to avoid prompts |
| 30 | +export DEBIAN_FRONTEND=noninteractive |
| 31 | + |
| 32 | +# Install apt-utils to avoid debconf warning |
| 33 | +apt-get -y install --no-install-recommends apt-utils 2> >( grep -v 'debconf: delaying package configuration, since apt-utils is not installed' >&2 ) |
| 34 | + |
| 35 | +# Get to latest versions of all packages |
| 36 | +if [ "${UPGRADE_PACKAGES}" = "true" ]; then |
| 37 | + apt-get -y upgrade --no-install-recommends |
| 38 | +fi |
| 39 | + |
| 40 | +# Install common developer tools and dependencies |
| 41 | +apt-get -y install --no-install-recommends \ |
| 42 | + git \ |
| 43 | + openssh-client \ |
| 44 | + less \ |
| 45 | + iproute2 \ |
| 46 | + procps \ |
| 47 | + curl \ |
| 48 | + wget \ |
| 49 | + unzip \ |
| 50 | + nano \ |
| 51 | + jq \ |
| 52 | + lsb-release \ |
| 53 | + ca-certificates \ |
| 54 | + apt-transport-https \ |
| 55 | + dialog \ |
| 56 | + gnupg2 \ |
| 57 | + libc6 \ |
| 58 | + libgcc1 \ |
| 59 | + libgssapi-krb5-2 \ |
| 60 | + libicu[0-9][0-9] \ |
| 61 | + liblttng-ust0 \ |
| 62 | + libstdc++6 \ |
| 63 | + zlib1g \ |
| 64 | + locales |
| 65 | + |
| 66 | +# Ensure at least the en_US.UTF-8 UTF-8 locale is available. |
| 67 | +# Common need for both applications and things like the agnoster ZSH theme. |
| 68 | +echo "en_US.UTF-8 UTF-8" >> /etc/locale.gen |
| 69 | +locale-gen |
| 70 | + |
| 71 | +# Install libssl1.1 if available |
| 72 | +if [[ ! -z $(apt-cache --names-only search ^libssl1.1$) ]]; then |
| 73 | + apt-get -y install --no-install-recommends libssl1.1 |
| 74 | +fi |
| 75 | + |
| 76 | +# Install appropriate version of libssl1.0.x if available |
| 77 | +LIBSSL=$(dpkg-query -f '${db:Status-Abbrev}\t${binary:Package}\n' -W 'libssl1\.0\.?' 2>&1 || echo '') |
| 78 | +if [ "$(echo "$LIBSSL" | grep -o 'libssl1\.0\.[0-9]:' | uniq | sort | wc -l)" -eq 0 ]; then |
| 79 | + if [[ ! -z $(apt-cache --names-only search ^libssl1.0.2$) ]]; then |
| 80 | + # Debian 9 |
| 81 | + apt-get -y install --no-install-recommends libssl1.0.2 |
| 82 | + elif [[ ! -z $(apt-cache --names-only search ^libssl1.0.0$) ]]; then |
| 83 | + # Ubuntu 18.04, 16.04, earlier |
| 84 | + apt-get -y install --no-install-recommends libssl1.0.0 |
| 85 | + fi |
| 86 | +fi |
| 87 | + |
| 88 | +# Create or update a non-root user to match UID/GID - see https://aka.ms/vscode-remote/containers/non-root-user. |
| 89 | +if id -u $USERNAME > /dev/null 2>&1; then |
| 90 | + # User exists, update if needed |
| 91 | + if [ "$USER_GID" != "$(id -G $USERNAME)" ]; then |
| 92 | + groupmod --gid $USER_GID $USERNAME |
| 93 | + usermod --gid $USER_GID $USERNAME |
| 94 | + fi |
| 95 | + if [ "$USER_UID" != "$(id -u $USERNAME)" ]; then |
| 96 | + usermod --uid $USER_UID $USERNAME |
| 97 | + fi |
| 98 | +else |
| 99 | + # Create user |
| 100 | + groupadd --gid $USER_GID $USERNAME |
| 101 | + useradd -s /bin/bash --uid $USER_UID --gid $USER_GID -m $USERNAME |
| 102 | +fi |
| 103 | + |
| 104 | +# Add add sudo support for non-root user |
| 105 | +apt-get install -y sudo |
| 106 | +echo $USERNAME ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/$USERNAME |
| 107 | +chmod 0440 /etc/sudoers.d/$USERNAME |
| 108 | + |
| 109 | +# Ensure ~/.local/bin is in the PATH for root and non-root users for bash. (zsh is later) |
| 110 | +echo "export PATH=\$PATH:\$HOME/.local/bin" | tee -a /root/.bashrc >> /home/$USERNAME/.bashrc |
| 111 | +chown $USER_UID:$USER_GID /home/$USERNAME/.bashrc |
| 112 | + |
| 113 | +# Optionally install and configure zsh |
| 114 | +if [ "$INSTALL_ZSH" = "true" ] && [ ! -d "/root/.oh-my-zsh" ]; then |
| 115 | + apt-get install -y zsh |
| 116 | + sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)" |
| 117 | + echo "export PATH=\$PATH:\$HOME/.local/bin" >> /root/.zshrc |
| 118 | + cp -R /root/.oh-my-zsh /home/$USERNAME |
| 119 | + cp /root/.zshrc /home/$USERNAME |
| 120 | + sed -i -e "s/\/root\/.oh-my-zsh/\/home\/$USERNAME\/.oh-my-zsh/g" /home/$USERNAME/.zshrc |
| 121 | + chown -R $USER_UID:$USER_GID /home/$USERNAME/.oh-my-zsh /home/$USERNAME/.zshrc |
| 122 | +fi |
| 123 | + |
0 commit comments