From f6a820ec21a54294016fb9909b2915c762a59238 Mon Sep 17 00:00:00 2001 From: Chuck Lantz Date: Tue, 18 Aug 2020 17:37:16 -0700 Subject: [PATCH] Script fixes, additions (#503) * Refinements * Git LFS script * GH CLI script * Go script * Java script * kubectl / helm script * PowerShell script * Rust script * Test refinements * Bug fix * Script to build git from source * Output "Done!" when script has finished --- script-library/azcli-debian.sh | 1 + script-library/common-alpine.sh | 1 + script-library/common-debian.sh | 39 ++++-- script-library/common-redhat.sh | 2 +- script-library/docker-debian.sh | 4 +- script-library/docker-redhat.sh | 2 + script-library/git-from-src-debian.sh | 27 ++++ script-library/git-lfs-debian.sh | 32 +++++ script-library/github-debian.sh | 35 ++++++ script-library/go-debian.sh | 116 ++++++++++++++++++ script-library/gradle-debian.sh | 16 +-- script-library/java-debian.sh | 78 ++++++++++++ script-library/kubectl-helm-debian.sh | 33 +++++ script-library/maven-debian.sh | 17 +-- script-library/node-debian.sh | 56 ++++----- script-library/powershell-debian.sh | 33 +++++ script-library/rust-debian.sh | 67 ++++++++++ script-library/terraform-debian.sh | 2 +- script-library/test/Dockerfile | 4 +- script-library/test/debian/.devcontainer.json | 8 +- script-library/test/run-scripts.sh | 40 +++--- 21 files changed, 523 insertions(+), 90 deletions(-) create mode 100644 script-library/git-from-src-debian.sh create mode 100644 script-library/git-lfs-debian.sh create mode 100644 script-library/github-debian.sh create mode 100644 script-library/go-debian.sh create mode 100644 script-library/java-debian.sh create mode 100644 script-library/kubectl-helm-debian.sh create mode 100644 script-library/powershell-debian.sh create mode 100644 script-library/rust-debian.sh diff --git a/script-library/azcli-debian.sh b/script-library/azcli-debian.sh index b2ac3a8556..cc6baeff95 100644 --- a/script-library/azcli-debian.sh +++ b/script-library/azcli-debian.sh @@ -28,3 +28,4 @@ echo "deb [arch=amd64] https://packages.microsoft.com/repos/azure-cli/ $(lsb_rel curl -sL https://packages.microsoft.com/keys/microsoft.asc | (OUT=$(apt-key add - 2>&1) || echo $OUT) apt-get update apt-get install -y azure-cli +echo "Done!" \ No newline at end of file diff --git a/script-library/common-alpine.sh b/script-library/common-alpine.sh index 4761044831..7192f0892b 100755 --- a/script-library/common-alpine.sh +++ b/script-library/common-alpine.sh @@ -79,3 +79,4 @@ if [ "$INSTALL_ZSH" = "true" ] && [ ! -d "/root/.oh-my-zsh" ]; then sed -i -e "s/\/root\/.oh-my-zsh/\/home\/$USERNAME\/.oh-my-zsh/g" /home/$USERNAME/.zshrc chown -R $USER_UID:$USER_GID /home/$USERNAME/.oh-my-zsh /home/$USERNAME/.zshrc fi +echo "Done!" diff --git a/script-library/common-debian.sh b/script-library/common-debian.sh index e756809afb..9229a8e89c 100755 --- a/script-library/common-debian.sh +++ b/script-library/common-debian.sh @@ -61,6 +61,7 @@ if [ "${PACKAGES_ALREADY_INSTALLED}" != "true" ]; then curl \ wget \ unzip \ + zip \ nano \ jq \ lsb-release \ @@ -133,17 +134,32 @@ else fi # Add add sudo support for non-root user -if [ "${EXISTING_NON_ROOT_USER}" != "${USERNAME}" ]; then +if [ "${USERNAME}" != "root" ] && [ "${EXISTING_NON_ROOT_USER}" != "${USERNAME}" ]; then echo $USERNAME ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/$USERNAME chmod 0440 /etc/sudoers.d/$USERNAME EXISTING_NON_ROOT_USER="${USERNAME}" fi +# .bashrc/.zshrc snippet +RC_SNIPPET="$(cat << EOF +export USER=\$(whoami) + +export PATH=\$PATH:\$HOME/.local/bin + +if [[ \$(which code-insiders 2>&1) && ! \$(which code 2>&1) ]]; then + alias code=code-insiders +fi +EOF +)" + # Ensure ~/.local/bin is in the PATH for root and non-root users for bash. (zsh is later) -if [ "${DOT_LOCAL_ALREADY_ADDED}" != "true" ]; then - echo "export PATH=\$PATH:\$HOME/.local/bin" | tee -a /root/.bashrc >> /home/$USERNAME/.bashrc - chown $USER_UID:$USER_GID /home/$USERNAME/.bashrc - DOT_LOCAL_ALREADY_ADDED="true" +if [ "${RC_SNIPPET_ALREADY_ADDED}" != "true" ]; then + echo "${RC_SNIPPET}" | tee -a /root/.bashrc >> /etc/skel/.bashrc + if [ "${USERNAME}" != "root" ]; then + echo "${RC_SNIPPET}" >> /home/$USERNAME/.bashrc + chown $USER_UID:$USER_GID /home/$USERNAME/.bashrc + fi + RC_SNIPPET_ALREADY_ADDED="true" fi # Optionally install and configure zsh @@ -151,11 +167,12 @@ if [ "${INSTALL_ZSH}" = "true" ] && [ ! -d "/root/.oh-my-zsh" ] && [ "${ZSH_ALRE apt-get-update-if-needed apt-get install -y zsh curl -fsSLo- https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh | bash 2>&1 - echo "export PATH=\$PATH:\$HOME/.local/bin" >> /root/.zshrc + echo -e "${RC_SNIPPET}\nDEFAULT_USER=\$USER\nprompt_context(){}" >> /root/.zshrc + cp -fR /root/.oh-my-zsh /etc/skel + cp -f /root/.zshrc /etc/skel + sed -i -e "s/\/root\/.oh-my-zsh/\/home\/\$(whoami)\/.oh-my-zsh/g" /etc/skel/.zshrc if [ "${USERNAME}" != "root" ]; then - cp -fR /root/.oh-my-zsh /home/$USERNAME - cp -f /root/.zshrc /home/$USERNAME - sed -i -e "s/\/root\/.oh-my-zsh/\/home\/$USERNAME\/.oh-my-zsh/g" /home/$USERNAME/.zshrc + cp -fR /etc/skel/.oh-my-zsh /etc/skel/.zshrc /home/$USERNAME chown -R $USER_UID:$USER_GID /home/$USERNAME/.oh-my-zsh /home/$USERNAME/.zshrc fi ZSH_ALREADY_INSTALLED="true" @@ -167,5 +184,7 @@ echo -e "\ PACKAGES_ALREADY_INSTALLED=${PACKAGES_ALREADY_INSTALLED}\n\ LOCALE_ALREADY_SET=${LOCALE_ALREADY_SET}\n\ EXISTING_NON_ROOT_USER=${EXISTING_NON_ROOT_USER}\n\ - DOT_LOCAL_ALREADY_ADDED=${DOT_LOCAL_ALREADY_ADDED}\n\ + RC_SNIPPET_ALREADY_ADDED=${RC_SNIPPET_ALREADY_ADDED}\n\ ZSH_ALREADY_INSTALLED=${ZSH_ALREADY_INSTALLED}" > "${MARKER_FILE}" + +echo "Done!" \ No newline at end of file diff --git a/script-library/common-redhat.sh b/script-library/common-redhat.sh index c724fd6216..bf73da6eaa 100755 --- a/script-library/common-redhat.sh +++ b/script-library/common-redhat.sh @@ -84,4 +84,4 @@ if [ "$INSTALL_ZSH" = "true" ] && [ ! -d "/root/.oh-my-zsh" ]; then sed -i -e "s/\/root\/.oh-my-zsh/\/home\/$USERNAME\/.oh-my-zsh/g" /home/$USERNAME/.zshrc chown -R $USER_UID:$USER_GID /home/$USERNAME/.oh-my-zsh /home/$USERNAME/.zshrc fi - +echo "Done!" \ No newline at end of file diff --git a/script-library/docker-debian.sh b/script-library/docker-debian.sh index d375ac0778..e711dbd9bf 100644 --- a/script-library/docker-debian.sh +++ b/script-library/docker-debian.sh @@ -4,7 +4,7 @@ # Licensed under the MIT License. See https://go.microsoft.com/fwlink/?linkid=2090316 for license information. #------------------------------------------------------------------------------------------------------------- -# Syntax: ./docker-debian.sh +# Syntax: ./docker-debian.sh [enable non-root docker socket access flag] [source socket] [target socket] [non-root user] ENABLE_NONROOT_DOCKER=${1:-"true"} SOURCE_SOCKET=${2:-"/var/run/docker-host.sock"} @@ -145,7 +145,9 @@ fi # Execute whatever commands were passed in (if any). This allows us # to set this script to ENTRYPOINT while still executing the default CMD. +set +e exec "\$@" EOF chmod +x /usr/local/share/docker-init.sh chown ${USERNAME}:root /usr/local/share/docker-init.sh +echo "Done!" \ No newline at end of file diff --git a/script-library/docker-redhat.sh b/script-library/docker-redhat.sh index 0b9064b552..713e61719f 100644 --- a/script-library/docker-redhat.sh +++ b/script-library/docker-redhat.sh @@ -125,9 +125,11 @@ fi # Execute whatever commands were passed in (if any). This allows us # to set this script to ENTRYPOINT while still executing the default CMD. +set +e exec "\$@" EOF else echo '/usr/bin/env bash -c "\$@"' > /usr/local/share/docker-init.sh fi chmod +x /usr/local/share/docker-init.sh +echo "Done!" \ No newline at end of file diff --git a/script-library/git-from-src-debian.sh b/script-library/git-from-src-debian.sh new file mode 100644 index 0000000000..ddad53e77a --- /dev/null +++ b/script-library/git-from-src-debian.sh @@ -0,0 +1,27 @@ +# Syntax: ./git-from-src-debian.sh [version] + +GIT_VERSION=${1:-"2.27.0"} + +set -e + +if [ "$(id -u)" -ne 0 ]; then + echo -e 'Script must be run a root. Use sudo, su, or add "USER root" to your Dockerfile before running this script.' + exit 1 +fi + +export DEBIAN_FRONTEND=noninteractive + +# Install required packages to build if missing +if ! dpkg -s build-essential curl ca-certificates tar gettext libssl-dev zlib1g-dev libexpat1-dev> /dev/null 2>&1; then + if [ ! -d "/var/lib/apt/lists" ] || [ "$(ls /var/lib/apt/lists/ | wc -l)" = "0" ]; then + apt-get update + fi + apt-get -y install --no-install-recommends build-essential curl ca-certificates tar gettext libssl-dev zlib1g-dev libcurl?-openssl-dev libexpat1-dev +fi + +echo "Downloading source for ${GIT_VERSION}..." +curl -sL https://github.com/git/git/archive/v${GIT_VERSION}.tar.gz | tar -xzC /tmp +echo "Building..." +(cd /tmp/git-${GIT_VERSION} && make -s prefix=/usr/local all && make -s prefix=/usr/local install) +rm -rf /tmp/git-${GIT_VERSION} +echo "Done!" diff --git a/script-library/git-lfs-debian.sh b/script-library/git-lfs-debian.sh new file mode 100644 index 0000000000..ed000e8d3b --- /dev/null +++ b/script-library/git-lfs-debian.sh @@ -0,0 +1,32 @@ +#!/usr/bin/env bash +#------------------------------------------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See https://go.microsoft.com/fwlink/?linkid=2090316 for license information. +#------------------------------------------------------------------------------------------------------------- + +# Syntax: ./git-lfs-debian.sh + +set -e + +if [ "$(id -u)" -ne 0 ]; then + echo -e 'Script must be run a root. Use sudo, su, or add "USER root" to your Dockerfile before running this script.' + exit 1 +fi + +export DEBIAN_FRONTEND=noninteractive + +# Install git and curl if missing +if ! dpkg -s git curl ca-certificates > /dev/null 2>&1; then + if [ ! -d "/var/lib/apt/lists" ] || [ "$(ls /var/lib/apt/lists/ | wc -l)" = "0" ]; then + apt-get update + fi + apt-get -y install --no-install-recommends git curl ca-certificates +fi + +# Install Git LFS +echo "Downloading Git LFS..." +curl -s https://packagecloud.io/install/repositories/github/git-lfs/script.deb.sh | bash +echo "Installing Git LFS..." +apt-get install -yq git-lfs +git lfs install +echo "Done!" \ No newline at end of file diff --git a/script-library/github-debian.sh b/script-library/github-debian.sh new file mode 100644 index 0000000000..3bae046848 --- /dev/null +++ b/script-library/github-debian.sh @@ -0,0 +1,35 @@ +#!/usr/bin/env bash +#------------------------------------------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See https://go.microsoft.com/fwlink/?linkid=2090316 for license information. +#------------------------------------------------------------------------------------------------------------- + +# Syntax: ./github-debian.sh [version] + +CLI_VERSION=${1:-"0.11.0"} + +set -e + +if [ "$(id -u)" -ne 0 ]; then + echo -e 'Script must be run a root. Use sudo, su, or add "USER root" to your Dockerfile before running this script.' + exit 1 +fi + +export DEBIAN_FRONTEND=noninteractive + +# Install curl, apt-transport-https or gpg if missing +if ! dpkg -s curl ca-certificates > /dev/null 2>&1; then + if [ ! -d "/var/lib/apt/lists" ] || [ "$(ls /var/lib/apt/lists/ | wc -l)" = "0" ]; then + apt-get update + fi + apt-get -y install --no-install-recommends curl ca-certificates +fi + +# Install the GitHub CLI +echo "Downloading github CLI..." +curl -OsSL https://github.com/cli/cli/releases/download/v${CLI_VERSION}/gh_${CLI_VERSION}_linux_amd64.deb +echo "Installing github CLI..." +apt-get install ./gh_${CLI_VERSION}_linux_amd64.deb +echo "Removing github CLI deb file after installation..." +rm -rf ./gh_${CLI_VERSION}_linux_amd64.deb +echo "Done!" diff --git a/script-library/go-debian.sh b/script-library/go-debian.sh new file mode 100644 index 0000000000..85bda531a8 --- /dev/null +++ b/script-library/go-debian.sh @@ -0,0 +1,116 @@ +#!/usr/bin/env bash +#------------------------------------------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See https://go.microsoft.com/fwlink/?linkid=2090316 for license information. +#------------------------------------------------------------------------------------------------------------- + +# Syntax: ./go-debian.sh [Go version] [Go install path] [GOPATH] [non-root user] [Add GOPATH, GOROOT to rc files flag] [install tools] + +TARGET_GO_VERSION=${1:-"1.15"} +TARGET_GOROOT=${2:-"/usr/local/go"} +TARGET_GOPATH=${3:-"/go"} +USERNAME=${4:-"vscode"} +UPDATE_RC=${5:-"true"} +INSTALL_GO_TOOLS=${6:-"true"} + +set -e + +if [ "$(id -u)" -ne 0 ]; then + echo -e 'Script must be run a root. Use sudo, su, or add "USER root" to your Dockerfile before running this script.' + exit 1 +fi + +# Treat a user name of "none" or non-existant user as root +if [ "${USERNAME}" = "none" ] || ! id -u ${USERNAME} > /dev/null 2>&1; then + USERNAME=root +fi + +export DEBIAN_FRONTEND=noninteractive + +# Install curl, tar, git if missing +if ! dpkg -s curl ca-certificates tar git > /dev/null 2>&1; then + if [ ! -d "/var/lib/apt/lists" ] || [ "$(ls /var/lib/apt/lists/ | wc -l)" = "0" ]; then + apt-get update + fi + apt-get -y install --no-install-recommends curl ca-certificates tar git +fi + + +# Install Go +GO_INSTALL_SCRIPT="$(cat < /dev/null 2>&1; then + mkdir -p "${TARGET_GOROOT}" "${TARGET_GOPATH}" + chown ${USERNAME}:root "${TARGET_GOROOT}" "${TARGET_GOPATH}" + su ${USERNAME} -c "${GO_INSTALL_SCRIPT}" +else + echo "Go already installed. Skipping." +fi + +# Install Go tools +GO_TOOLS_WITH_MODULES="\ + golang.org/x/tools/gopls \ + honnef.co/go/tools/... \ + golang.org/x/tools/cmd/gorename \ + golang.org/x/tools/cmd/goimports \ + golang.org/x/tools/cmd/guru \ + golang.org/x/lint/golint \ + github.com/mdempsky/gocode \ + github.com/cweill/gotests/... \ + github.com/haya14busa/goplay/cmd/goplay \ + github.com/sqs/goreturns \ + github.com/josharian/impl \ + github.com/davidrjenni/reftools/cmd/fillstruct \ + github.com/uudashr/gopkgs/v2/cmd/gopkgs \ + github.com/ramya-rao-a/go-outline \ + github.com/acroca/go-symbols \ + github.com/godoctor/godoctor \ + github.com/rogpeppe/godef \ + github.com/zmb3/gogetdoc \ + github.com/fatih/gomodifytags \ + github.com/mgechev/revive \ + github.com/go-delve/delve/cmd/dlv" +if [ "${INSTALL_GO_TOOLS}" = "true" ]; then + echo "Installing common Go tools..." + export PATH=${TARGET_GOROOT}/bin:${PATH} + mkdir -p /tmp/gotools + cd /tmp/gotools + export GOPATH=/tmp/gotools + + # Go tools w/module support + export GO111MODULE=on + (echo "${GO_TOOLS_WITH_MODULES}" | xargs -n 1 go get -v )2>&1 + + # gocode-gomod + export GO111MODULE=auto + go get -v -d github.com/stamblerre/gocode 2>&1 + go build -o gocode-gomod github.com/stamblerre/gocode + + # golangci-lint + curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b /usr/local/bin 2>&1 + + # Move Go tools into path and clean up + mv /tmp/gotools/bin/* /usr/local/bin/ + mv gocode-gomod /usr/local/bin/ + rm -rf /tmp/gotools +fi + +# Add GOPATH variable and bin directory into PATH in bashrc/zshrc files (unless disabled) +if [ "${UPDATE_RC}" = "true" ]; then + RC_SNIPPET="export GOPATH=\"${TARGET_GOPATH}\"\nexport GOROOT=\"${TARGET_GOROOT}\"\nexport PATH=\"\${GOROOT}/bin:\${PATH}\"" + echo -e ${RC_SNIPPET} | tee -a /root/.bashrc /root/.zshrc >> /etc/skel/.bashrc + if [ "${USERNAME}" != "root" ]; then + echo -e ${RC_SNIPPET} | tee -a /home/${USERNAME}/.bashrc /home/${USERNAME}/.zshrc + fi + echo "Done!" +else + echo "Done! Be sure to add ${GO_HOME}/bin to the PATH." +fi + diff --git a/script-library/gradle-debian.sh b/script-library/gradle-debian.sh index f18cbcec28..8e39b47cbd 100644 --- a/script-library/gradle-debian.sh +++ b/script-library/gradle-debian.sh @@ -29,7 +29,7 @@ if [ "$(id -u)" -ne 0 ]; then fi # Treat a user name of "none" or non-existant user as root -if [ "${USERNAME}" = "none" ] && ! id -u ${USERNAME} > /dev/null 2>&1; then +if [ "${USERNAME}" = "none" ] || ! id -u ${USERNAME} > /dev/null 2>&1; then USERNAME=root fi @@ -42,19 +42,11 @@ if ! dpkg -s curl ca-certificates unzip > /dev/null 2>&1; then apt-get -y install --no-install-recommends curl ca-certificates unzip fi -# Function to su if user exists and is not root -suIf() { - if [ "${USERNAME}" != "root" ]; then - su ${USERNAME} -c "$@" - else - "$@" - fi -} # Install Gradle echo "Downloading Gradle..." -suIf "$(cat \ -<< EOF +su ${USERNAME} -c "$(cat << EOF + set -e mkdir -p /tmp/downloads curl -sSL --output /tmp/downloads/archive-gradle.zip https://services.gradle.org/distributions/gradle-${GRADLE_VERSION}-bin.zip ([ "${GRADLE_DOWNLOAD_SHA}" = "no-check" ] || echo "${GRADLE_DOWNLOAD_SHA} */tmp/downloads/archive-gradle.zip" | sha256sum --check - ) @@ -65,6 +57,6 @@ mv -f /tmp/downloads/gradle* ${GRADLE_HOME} chown ${USERNAME}:root ${GRADLE_HOME} ln -s ${GRADLE_HOME}/bin/gradle /usr/local/bin/gradle rm -rf /tmp/downloads -echo "Done." +echo "Done!" diff --git a/script-library/java-debian.sh b/script-library/java-debian.sh new file mode 100644 index 0000000000..8615acc882 --- /dev/null +++ b/script-library/java-debian.sh @@ -0,0 +1,78 @@ +#!/usr/bin/env bash +#------------------------------------------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See https://go.microsoft.com/fwlink/?linkid=2090316 for license information. +#------------------------------------------------------------------------------------------------------------- + +# Syntax: ./java-debian.sh [JDK major version] [JAVA_HOME] [non-root user] [Add JAVA_HOME to rc files flag] + +export JAVA_VERSION=${1:-"11"} +export JAVA_HOME=${2:-"/opt/java/openjdk-${JAVA_VERSION}"} +USERNAME=${3:-"vscode"} +UPDATE_RC=${4:-"true"} + +set -e + +JDK_11_URI="https://github.com/AdoptOpenJDK/openjdk11-upstream-binaries/releases/download/jdk-11.0.8%2B10/OpenJDK11U-jdk_x64_linux_11.0.8_10.tar.gz" +JDK_8_URI="https://github.com/AdoptOpenJDK/openjdk8-upstream-binaries/releases/download/jdk8u265-b01/OpenJDK8U-jdk_x64_linux_8u265b01.tar.gz" + +JAVA_URI_VAR="JDK_${JAVA_VERSION}_URI" +JAVA_URI=${!JAVA_URI_VAR} + +if [ "${JAVA_URI}" = "" ]; then + echo 'Only Java versions 8 and 11 are supported by this script.' + exit 1 +fi + +if [ "$(id -u)" -ne 0 ]; then + echo -e 'Script must be run a root. Use sudo, su, or add "USER root" to your Dockerfile before running this script.' + exit 1 +fi + +# Treat a user name of "none" or non-existant user as root +if [ "${USERNAME}" = "none" ] || ! id -u ${USERNAME} > /dev/null 2>&1; then + USERNAME=root +fi + +export DEBIAN_FRONTEND=noninteractive + +# Install curl if missing +if ! dpkg -s curl ca-certificates > /dev/null 2>&1; then + if [ ! -d "/var/lib/apt/lists" ] || [ "$(ls /var/lib/apt/lists/ | wc -l)" = "0" ]; then + apt-get update + fi + apt-get -y install --no-install-recommends curl ca-certificates +fi + +# Install Java +if [ -d "${JAVA_HOME}" ]; then + echo "${JAVA_HOME} already exists. Assuming Java is already installed." + exit 0 +fi + +mkdir -p "${JAVA_HOME}" +chown "${USERNAME}" "${JAVA_HOME}" + + + +su ${USERNAME} -c "$(cat << EOF + set -e + echo "Downloading JDK ${JAVA_VERSION}..." + curl -fsSL -o /tmp/openjdk.tar.gz ${JAVA_URI} + echo "Installing JDK ${JAVA_VERSION}..." + tar -xzf /tmp/openjdk.tar.gz -C ${JAVA_HOME} --strip-components=1 + rm -f /tmp/openjdk.tar.gz +EOF +)" + +# Add JAVA_HOME and bin directory into bashrc/zshrc files (unless disabled) +if [ "${UPDATE_RC}" = "true" ]; then + RC_SNIPPET="export JAVA_HOME=\"${JAVA_HOME}\"\nexport PATH=\"\${JAVA_HOME}/bin:\${PATH}\"" + echo -e ${RC_SNIPPET} | tee -a /root/.bashrc /root/.zshrc >> /etc/skel/.bashrc + if [ "${USERNAME}" != "root" ]; then + echo -e ${RC_SNIPPET} | tee -a /home/${USERNAME}/.bashrc /home/${USERNAME}/.zshrc + fi + echo "Done!" +else + echo "Done! Be sure to add ${JAVA_HOME}/bin to the PATH." +fi \ No newline at end of file diff --git a/script-library/kubectl-helm-debian.sh b/script-library/kubectl-helm-debian.sh new file mode 100644 index 0000000000..3ab295f76a --- /dev/null +++ b/script-library/kubectl-helm-debian.sh @@ -0,0 +1,33 @@ +#!/usr/bin/env bash +#------------------------------------------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See https://go.microsoft.com/fwlink/?linkid=2090316 for license information. +#------------------------------------------------------------------------------------------------------------- + +# Syntax: ./kubectl-helm-debian.sh + +set -e + +if [ "$(id -u)" -ne 0 ]; then + echo -e 'Script must be run a root. Use sudo, su, or add "USER root" to your Dockerfile before running this script.' + exit 1 +fi + +export DEBIAN_FRONTEND=noninteractive + +# Install curl if missing +if ! dpkg -s curl ca-certificates > /dev/null 2>&1; then + if [ ! -d "/var/lib/apt/lists" ] || [ "$(ls /var/lib/apt/lists/ | wc -l)" = "0" ]; then + apt-get update + fi + apt-get -y install --no-install-recommends curl ca-certificates +fi + +# Install the kubectl +echo "Downloading kubectl..." +curl -sSL -o /usr/local/bin/kubectl https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl +chmod +x /usr/local/bin/kubectl +# Install Helm +echo "Installing Helm..." +curl -s https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3 | bash - +echo "Done!" \ No newline at end of file diff --git a/script-library/maven-debian.sh b/script-library/maven-debian.sh index 8f6c14e77d..98e8a3bc7c 100644 --- a/script-library/maven-debian.sh +++ b/script-library/maven-debian.sh @@ -29,7 +29,7 @@ if [ "$(id -u)" -ne 0 ]; then fi # Treat a user name of "none" or non-existant user as root -if [ "${USERNAME}" = "none" ] && ! id -u ${USERNAME} > /dev/null 2>&1; then +if [ "${USERNAME}" = "none" ] || ! id -u ${USERNAME} > /dev/null 2>&1; then USERNAME=root fi @@ -42,15 +42,6 @@ if ! dpkg -s curl ca-certificates tar > /dev/null 2>&1; then apt-get -y install --no-install-recommends curl ca-certificates tar fi -# Function to su if user exists and is not root -suIf() { - if [ "${USERNAME}" != "root" ]; then - su ${USERNAME} -c "$@" - else - "$@" - fi -} - # Creat folder, add maven settings mkdir -p ${MAVEN_HOME} ${MAVEN_HOME}/ref tee ${MAVEN_HOME}/ref/maven-settings.xml > /dev/null \ @@ -63,8 +54,8 @@ chown -R ${USERNAME}:root ${MAVEN_HOME} # Install Maven echo "Downloading Maven..." -suIf "$(cat \ -<< EOF +su ${USERNAME} -c "$(cat << EOF + set -e curl -fsSL -o /tmp/maven.tar.gz https://archive.apache.org/dist/maven/maven-3/${MAVEN_VERSION}/binaries/apache-maven-${MAVEN_VERSION}-bin.tar.gz ([ "${MAVEN_DOWNLOAD_SHA}" = "no-check" ] || echo "${MAVEN_DOWNLOAD_SHA} */tmp/maven.tar.gz" | sha512sum -c - ) tar -xzf /tmp/maven.tar.gz -C ${MAVEN_HOME} --strip-components=1 @@ -72,4 +63,4 @@ suIf "$(cat \ EOF )" ln -s ${MAVEN_HOME}/bin/mvn /usr/local/bin/mvn -echo "Done." +echo "Done!" diff --git a/script-library/node-debian.sh b/script-library/node-debian.sh index 2837fc77b4..99d80cfee0 100644 --- a/script-library/node-debian.sh +++ b/script-library/node-debian.sh @@ -4,7 +4,7 @@ # Licensed under the MIT License. See https://go.microsoft.com/fwlink/?linkid=2090316 for license information. #------------------------------------------------------------------------------------------------------------- -# Syntax: ./node-debian.sh +# Syntax: ./node-debian.sh [directory to install nvm] [node version to install (use "none" to skip)] [non-root user] export NVM_DIR=${1:-"/usr/local/share/nvm"} export NODE_VERSION=${2:-"lts/*"} @@ -17,8 +17,8 @@ if [ "$(id -u)" -ne 0 ]; then exit 1 fi -# Treat a user name of "none" as root -if [ "${USERNAME}" = "none" ]; then +# Treat a user name of "none" or non-existant user as root +if [ "${USERNAME}" = "none" ] || ! id -u ${USERNAME} > /dev/null 2>&1; then USERNAME=root fi @@ -51,46 +51,40 @@ fi if [ -d "${NVM_DIR}" ]; then echo "NVM already installed." if [ "${NODE_VERSION}" != "" ]; then - suIf "nvm install ${NODE_VERSION}" + su ${USERNAME} -c "nvm install ${NODE_VERSION}" fi exit 0 fi mkdir -p ${NVM_DIR} +NVM_INIT=$(cat <> /etc/skel/.bashrc # Set up non-root user if applicable -if [ "${USERNAME}" != "root" ] && id -u $USERNAME > /dev/null 2>&1; then +if [ "${USERNAME}" != "root" ]; then # Add NVM init and add code to update NVM ownership if UID/GID changes - tee -a /root/.bashrc /root/.zshrc /home/${USERNAME}/.bashrc >> /home/${USERNAME}/.zshrc \ -<> /home/${USERNAME}/.zshrc chown ${USERNAME} ${NVM_DIR} /home/${USERNAME}/.bashrc /home/${USERNAME}/.zshrc fi -# Function to su if user exists and is not root -suIf() { - if [ "${USERNAME}" != "root" ] && id -u ${USERNAME} > /dev/null 2>&1; then - su ${USERNAME} -c "$@" - else - "$@" - fi -} - # Run NVM installer as non-root if needed -suIf "$(cat \ -<< EOF - curl -so- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash - if [ "${NODE_VERSION}" != "" ]; then - source $NVM_DIR/nvm.sh - nvm alias default ${NODE_VERSION} - fi +su ${USERNAME} -c "$(cat << EOF + set -e + curl -so- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash + if [ "${NODE_VERSION}" != "" ]; then + source $NVM_DIR/nvm.sh + nvm alias default ${NODE_VERSION} + fi EOF )" 2>&1 +echo "Done!" \ No newline at end of file diff --git a/script-library/powershell-debian.sh b/script-library/powershell-debian.sh new file mode 100644 index 0000000000..fac4ca8de5 --- /dev/null +++ b/script-library/powershell-debian.sh @@ -0,0 +1,33 @@ +#!/usr/bin/env bash +#------------------------------------------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See https://go.microsoft.com/fwlink/?linkid=2090316 for license information. +#------------------------------------------------------------------------------------------------------------- + +# Syntax: ./powershell-debian.sh + +set -e + +if [ "$(id -u)" -ne 0 ]; then + echo -e 'Script must be run a root. Use sudo, su, or add "USER root" to your Dockerfile before running this script.' + exit 1 +fi + +export DEBIAN_FRONTEND=noninteractive + +# Install curl, apt-transport-https, lsb-release, or gpg if missing +if ! dpkg -s apt-transport-https curl ca-certificates lsb-release > /dev/null 2>&1 || ! type gpg > /dev/null 2>&1; then + if [ ! -d "/var/lib/apt/lists" ] || [ "$(ls /var/lib/apt/lists/ | wc -l)" = "0" ]; then + apt-get update + fi + apt-get -y install --no-install-recommends apt-transport-https curl ca-certificates lsb-release gnupg2 +fi + +# Use correct source for distro (Ubuntu/Debian) and Codename (stretch, buster, bionic, focal) +DISTRO=$(lsb_release -is | tr '[:upper:]' '[:lower:]') +CODENAME=$(lsb_release -cs) +curl -s https://packages.microsoft.com/keys/microsoft.asc | (OUT=$(apt-key add - 2>&1) || echo $OUT) +echo "deb [arch=amd64] https://packages.microsoft.com/repos/microsoft-${DISTRO}-${CODENAME}-prod ${CODENAME} main" > /etc/apt/sources.list.d/microsoft.list +apt-get update -yq +apt-get install -yq powershell +echo "Done!" \ No newline at end of file diff --git a/script-library/rust-debian.sh b/script-library/rust-debian.sh new file mode 100644 index 0000000000..2e2c054046 --- /dev/null +++ b/script-library/rust-debian.sh @@ -0,0 +1,67 @@ +#!/usr/bin/env bash +#------------------------------------------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See https://go.microsoft.com/fwlink/?linkid=2090316 for license information. +#------------------------------------------------------------------------------------------------------------- + +# Syntax: ./rust-debian.sh [CARGO_HOME] [RUSTUP_HOME] [non-root user] [add CARGO/RUSTUP_HOME to rc files flag] + +export CARGO_HOME=${1:-"/usr/local/cargo"} +export RUSTUP_HOME=${2:-"/usr/local/rustup"} +USERNAME=${3:-"vscode"} +UPDATE_RC=${4:-"true"} + +set -e + +if [ "$(id -u)" -ne 0 ]; then + echo -e 'Script must be run a root. Use sudo, su, or add "USER root" to your Dockerfile before running this script.' + exit 1 +fi + +# Treat a user name of "none" or non-existant user as root +if [ "${USERNAME}" = "none" ] || ! id -u ${USERNAME} > /dev/null 2>&1; then + USERNAME=root +fi + +export DEBIAN_FRONTEND=noninteractive + +# Install curl, lldb, python3-minimal if missing +if ! dpkg -s curl ca-certificates lldb python3-minimal > /dev/null 2>&1; then + if [ ! -d "/var/lib/apt/lists" ] || [ "$(ls /var/lib/apt/lists/ | wc -l)" = "0" ]; then + apt-get update + fi + apt-get -y install --no-install-recommends curl ca-certificates + apt-get -y install lldb python3-minimal libpython3.? +fi + +# Install Rust +if ! type rustup > /dev/null 2>&1; then + echo "Installing Rust..." + mkdir -p "${CARGO_HOME}" "${RUSTUP_HOME}" + chown ${USERNAME}:root "${CARGO_HOME}" "${RUSTUP_HOME}" + su ${USERNAME} -c "curl --tlsv1.2 https://sh.rustup.rs -sSf | bash -s -- -y --no-modify-path 2>&1" +else + echo "Rust already installed. Skipping." +fi + +echo "Installing common Rust dependencies..." +su ${USERNAME} -c "$(cat << EOF + set -e + export PATH=${PATH}:${CARGO_HOME}/bin + rustup update 2>&1 + rustup component add rls rust-analysis rust-src rustfmt clippy 2>&1 +EOF +)" + +# Add CARGO_HOME, RUSTUP_HOME and bin directory into bashrc/zshrc files (unless disabled) +if [ "${UPDATE_RC}" = "true" ]; then + RC_SNIPPET="export CARGO_HOME=\"${CARGO_HOME}\"\nexport RUSTUP_HOME=\"${RUSTUP_HOME}\"\nexport PATH=\"\${CARGO_HOME}/bin:\${PATH}\"" + echo -e ${RC_SNIPPET} | tee -a /root/.bashrc /root/.zshrc >> /etc/skel/.bashrc + if [ "${USERNAME}" != "root" ]; then + echo -e ${RC_SNIPPET} | tee -a /home/${USERNAME}/.bashrc /home/${USERNAME}/.zshrc + fi + echo "Done!" +else + echo "Done! Be sure to add ${CARGO_HOME}/bin to the PATH." +fi + diff --git a/script-library/terraform-debian.sh b/script-library/terraform-debian.sh index 4e0db26f70..b3601180f0 100644 --- a/script-library/terraform-debian.sh +++ b/script-library/terraform-debian.sh @@ -45,4 +45,4 @@ if [ "${TFLINT_VERSION}" != "none" ]; then fi rm -rf /tmp/tf-downloads -echo "Done." +echo "Done!" diff --git a/script-library/test/Dockerfile b/script-library/test/Dockerfile index 5a6c0c0c40..ee3dd431c5 100644 --- a/script-library/test/Dockerfile +++ b/script-library/test/Dockerfile @@ -14,7 +14,9 @@ RUN chmod +x /tmp/*.sh ARG DISTRO="debian" ARG USE_DEFAULTS="true" ARG USERNAME="vscode" -RUN /tmp/run-scripts.sh /tmp ${DISTRO} ${USE_DEFAULTS} ${USERNAME} false false 10 +ARG RUN_COMMON_SCRIPT="true" +ARG UPGRADE_PACKAGES="false" +RUN /tmp/run-scripts.sh /tmp ${DISTRO} ${USE_DEFAULTS} ${USERNAME} ${RUN_COMMON_SCRIPT} ${UPGRADE_PACKAGES} ENTRYPOINT [ "/usr/local/share/docker-init.sh" ] CMD [ "sleep", "infinity" ] diff --git a/script-library/test/debian/.devcontainer.json b/script-library/test/debian/.devcontainer.json index 5ca85d3838..27c25a456a 100644 --- a/script-library/test/debian/.devcontainer.json +++ b/script-library/test/debian/.devcontainer.json @@ -6,11 +6,13 @@ "args": { "IMAGE_TO_TEST": "debian:10", "DISTRO": "debian", - "USERNAME": "tester", - "USE_DEFAULTS": "false" + "USERNAME": "vscode", + "UPGRADE_PACKAGES": "false", + "RUN_COMMON_SCRIPT": "true", + "USE_DEFAULTS": "true" } }, - "remoteUser": "tester", + "remoteUser": "vscode", "mounts": [ "source=/var/run/docker.sock,target=/var/run/docker-host.sock,type=bind" ], "overrideCommand": true, diff --git a/script-library/test/run-scripts.sh b/script-library/test/run-scripts.sh index b6273bb5a7..03eb6945ac 100644 --- a/script-library/test/run-scripts.sh +++ b/script-library/test/run-scripts.sh @@ -1,18 +1,13 @@ #!/bin/sh SCRIPT_DIR=${1-"/tmp"} -DISTRO=${2:-debian} -USE_DEFAULTS=${3:-true} -USERNAME=${4:-vscode} -UPGRADE_PAGKES=${5:-true} -ENABLE_NONROOT=${6:-true} -NODE_VERSION=${7:-"lts/*"} -MAVEN_VERSION=${8:-"3.6.3"} -MAVEN_DOWNLOAD_SHA=${9:-"c35a1803a6e70a126e80b2b3ae33eed961f83ed74d18fcd16909b2d44d7dada3203f1ffe726c17ef8dcca2dcaa9fca676987befeadc9b9f759967a8cb77181c0"} -GRADLE_VERSION=${10:-"5.4.1"} -GRADLE_DOWNLOAD_SHA=${11:-"7bdbad1e4f54f13c8a78abc00c26d44dd8709d4aedb704d913fb1bb78ac025dc"} -TERRAFORM_VERSION=${12:-"0.12.16"} -TFLINT_VERSION=${13:-"0.8.2"} +DISTRO=${2:-"debian"} +USE_DEFAULTS=${3:-"true"} +USERNAME=${4:-"vscode"} +RUN_COMMON_SCRIPT=${5:-"true"} +UPGRADE_PAGKES=${6:-"true"} + +set -e runScript() { @@ -30,13 +25,24 @@ runScript() echo "**** Done! ****\n" } -runScript ${SCRIPT_DIR}/common-${DISTRO}.sh "true ${USERNAME} 1000 1000 ${UPGRADE_PACKAGES}" +if [ "${RUN_COMMON_SCRIPT}" = "true" ]; then + runScript ${SCRIPT_DIR}/common-${DISTRO}.sh "true ${USERNAME} 1000 1000 ${UPGRADE_PACKAGES}" +fi if [ "${DISTRO}" = "debian" ]; then - runScript ${SCRIPT_DIR}/node-${DISTRO}.sh "/usr/local/share/nvm ${NODE_VERSION} ${USERNAME}" - runScript ${SCRIPT_DIR}/maven-${DISTRO}.sh "/usr/local/share/maven ${USERNAME} ${MAVEN_DOWNLOAD_SHA}" "${MAVEN_VERSION}" - runScript ${SCRIPT_DIR}/gradle-${DISTRO}.sh "/usr/local/share/gradle ${USERNAME} ${GRADLE_DOWNLOAD_SHA}" "${GRADLE_VERSION}" - runScript ${SCRIPT_DIR}/terraform-${DISTRO}.sh "${TFLINT_VERSION}" "${TERRAFORM_VERSION}" + runScript ${SCRIPT_DIR}/azcli-${DISTRO}.sh + runScript ${SCRIPT_DIR}/git-from-src-${DISTRO}.sh "2.26.2" + runScript ${SCRIPT_DIR}/git-lfs-${DISTRO}.sh + runScript ${SCRIPT_DIR}/github-${DISTRO}.sh + runScript ${SCRIPT_DIR}/go-${DISTRO}.sh "1.14 /opt/go /go ${USERNAME} false" + runScript ${SCRIPT_DIR}/gradle-${DISTRO}.sh "/usr/local/share/gradle ${USERNAME} 7bdbad1e4f54f13c8a78abc00c26d44dd8709d4aedb704d913fb1bb78ac025dc" "5.4.1" + runScript ${SCRIPT_DIR}/java-${DISTRO}.sh "8 /usr/local/share/java ${USERNAME} false" + runScript ${SCRIPT_DIR}/kubectl-helm-${DISTRO}.sh + runScript ${SCRIPT_DIR}/maven-${DISTRO}.sh "/usr/local/share/maven ${USERNAME} c35a1803a6e70a126e80b2b3ae33eed961f83ed74d18fcd16909b2d44d7dada3203f1ffe726c17ef8dcca2dcaa9fca676987befeadc9b9f759967a8cb77181c0" "3.6.3" + runScript ${SCRIPT_DIR}/node-${DISTRO}.sh "/usr/local/share/nvm 10 ${USERNAME}" + runScript ${SCRIPT_DIR}/powershell-${DISTRO}.sh + runScript ${SCRIPT_DIR}/rust-${DISTRO}.sh "/opt/rust/cargo /opt/rust/rustup ${USERNAME} false" + runScript ${SCRIPT_DIR}/terraform-${DISTRO}.sh "0.12.16" "0.8.2" fi # Run Docker script