|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# Distribution and package manager detection are licensed by Microsoft |
| 4 | +# Corporation under the MIT License, please refer to: |
| 5 | +# https://github.com/devcontainers/features/blob/main/src/go/install.sh: |
| 6 | +# |
| 7 | +# Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the |
| 8 | +# MIT License. See https://go.microsoft.com/fwlink/?linkid=2090316 for license |
| 9 | +# information |
| 10 | + |
| 11 | +set -e |
| 12 | + |
| 13 | +NERDCTL_VERSION="${VERSION:-"latest"}" |
| 14 | +CONTAINERD_API="${CONTAINERD_API:-"unix:///run/containerd/containerd.sock"}" |
| 15 | + |
| 16 | +REPOSLUG="containerd/nerdctl" |
| 17 | +QUERYLATEST_URL="https://api.github.com/repos/${REPOSLUG}/releases/latest" |
| 18 | +RELEASE_URL="https://github.com/${REPOSLUG}/releases/download/" |
| 19 | + |
| 20 | +echo "installing feature nerdctl..." |
| 21 | + |
| 22 | +if [ "$(id -u)" -ne 0 ]; then |
| 23 | + echo -e 'Script must be run as root. Use sudo, su, or add "USER root" to your Dockerfile before running this script.' |
| 24 | + exit 1 |
| 25 | +fi |
| 26 | + |
| 27 | +# Bring in ID, ID_LIKE, VERSION_ID, VERSION_CODENAME |
| 28 | +. /etc/os-release |
| 29 | +# Get an adjusted ID independent of distro variants |
| 30 | +MAJOR_VERSION_ID=$(echo ${VERSION_ID} | cut -d . -f 1) |
| 31 | +if [ "${ID}" = "debian" ] || [ "${ID_LIKE}" = "debian" ]; then |
| 32 | + ADJUSTED_ID="debian" |
| 33 | +elif [[ "${ID}" = "rhel" || "${ID}" = "fedora" || "${ID}" = "mariner" || "${ID_LIKE}" = *"rhel"* || "${ID_LIKE}" = *"fedora"* || "${ID_LIKE}" = *"mariner"* ]]; then |
| 34 | + ADJUSTED_ID="rhel" |
| 35 | + if [[ "${ID}" = "rhel" ]] || [[ "${ID}" = *"alma"* ]] || [[ "${ID}" = *"rocky"* ]]; then |
| 36 | + VERSION_CODENAME="rhel${MAJOR_VERSION_ID}" |
| 37 | + else |
| 38 | + VERSION_CODENAME="${ID}${MAJOR_VERSION_ID}" |
| 39 | + fi |
| 40 | +else |
| 41 | + echo "Linux distro ${ID} not supported." |
| 42 | + exit 1 |
| 43 | +fi |
| 44 | + |
| 45 | +if [ "${ADJUSTED_ID}" = "rhel" ] && [ "${VERSION_CODENAME-}" = "centos7" ]; then |
| 46 | + # As of 1 July 2024, mirrorlist.centos.org no longer exists. |
| 47 | + # Update the repo files to reference vault.centos.org. |
| 48 | + sed -i s/mirror.centos.org/vault.centos.org/g /etc/yum.repos.d/*.repo |
| 49 | + sed -i s/^#.*baseurl=http/baseurl=http/g /etc/yum.repos.d/*.repo |
| 50 | + sed -i s/^mirrorlist=http/#mirrorlist=http/g /etc/yum.repos.d/*.repo |
| 51 | +fi |
| 52 | + |
| 53 | +# Setup INSTALL_CMD & PKG_MGR_CMD |
| 54 | +if type apt-get > /dev/null 2>&1; then |
| 55 | + PKG_MGR_CMD=apt-get |
| 56 | + INSTALL_CMD="${PKG_MGR_CMD} -y install --no-install-recommends" |
| 57 | +elif type microdnf > /dev/null 2>&1; then |
| 58 | + PKG_MGR_CMD=microdnf |
| 59 | + INSTALL_CMD="${PKG_MGR_CMD} ${INSTALL_CMD_ADDL_REPOS} -y install --refresh --best --nodocs --noplugins --setopt=install_weak_deps=0" |
| 60 | +elif type dnf > /dev/null 2>&1; then |
| 61 | + PKG_MGR_CMD=dnf |
| 62 | + INSTALL_CMD="${PKG_MGR_CMD} ${INSTALL_CMD_ADDL_REPOS} -y install --refresh --best --nodocs --noplugins --setopt=install_weak_deps=0" |
| 63 | +else |
| 64 | + PKG_MGR_CMD=yum |
| 65 | + INSTALL_CMD="${PKG_MGR_CMD} ${INSTALL_CMD_ADDL_REPOS} -y install --noplugins --setopt=install_weak_deps=0" |
| 66 | +fi |
| 67 | + |
| 68 | +# Clean up |
| 69 | +clean_up() { |
| 70 | + case ${ADJUSTED_ID} in |
| 71 | + debian) |
| 72 | + rm -rf /var/lib/apt/lists/* |
| 73 | + ;; |
| 74 | + rhel) |
| 75 | + rm -rf /var/cache/dnf/* /var/cache/yum/* |
| 76 | + rm -rf /tmp/yum.log |
| 77 | + rm -rf ${GPG_INSTALL_PATH} |
| 78 | + ;; |
| 79 | + esac |
| 80 | +} |
| 81 | +clean_up |
| 82 | + |
| 83 | +pkg_mgr_update() { |
| 84 | + case $ADJUSTED_ID in |
| 85 | + debian) |
| 86 | + if [ "$(find /var/lib/apt/lists/* | wc -l)" = "0" ]; then |
| 87 | + echo "Running apt-get update..." |
| 88 | + ${PKG_MGR_CMD} update -y |
| 89 | + fi |
| 90 | + ;; |
| 91 | + rhel) |
| 92 | + if [ ${PKG_MGR_CMD} = "microdnf" ]; then |
| 93 | + if [ "$(ls /var/cache/yum/* 2>/dev/null | wc -l)" = 0 ]; then |
| 94 | + echo "Running ${PKG_MGR_CMD} makecache ..." |
| 95 | + ${PKG_MGR_CMD} makecache |
| 96 | + fi |
| 97 | + else |
| 98 | + if [ "$(ls /var/cache/${PKG_MGR_CMD}/* 2>/dev/null | wc -l)" = 0 ]; then |
| 99 | + echo "Running ${PKG_MGR_CMD} check-update ..." |
| 100 | + set +e |
| 101 | + ${PKG_MGR_CMD} check-update |
| 102 | + rc=$? |
| 103 | + if [ $rc != 0 ] && [ $rc != 100 ]; then |
| 104 | + exit 1 |
| 105 | + fi |
| 106 | + set -e |
| 107 | + fi |
| 108 | + fi |
| 109 | + ;; |
| 110 | + esac |
| 111 | +} |
| 112 | + |
| 113 | +# Checks if packages are installed and installs them if not |
| 114 | +check_packages() { |
| 115 | + case ${ADJUSTED_ID} in |
| 116 | + debian) |
| 117 | + if ! dpkg -s "$@" > /dev/null 2>&1; then |
| 118 | + pkg_mgr_update |
| 119 | + ${INSTALL_CMD} "$@" |
| 120 | + fi |
| 121 | + ;; |
| 122 | + rhel) |
| 123 | + if ! rpm -q "$@" > /dev/null 2>&1; then |
| 124 | + pkg_mgr_update |
| 125 | + ${INSTALL_CMD} "$@" |
| 126 | + fi |
| 127 | + ;; |
| 128 | + esac |
| 129 | +} |
| 130 | + |
| 131 | +case $(uname -m) in |
| 132 | + x86_64) ARCH="amd64";; |
| 133 | + aarch64 | armv8*) ARCH="arm64";; |
| 134 | + *) echo "Unsupported architecture: $(uname -m)"; exit 1;; |
| 135 | +esac |
| 136 | + |
| 137 | +export DEBIAN_FRONTEND=noninteractive |
| 138 | + |
| 139 | +if ! type curl > /dev/null 2>&1; then |
| 140 | + check_packages curl |
| 141 | +fi |
| 142 | + |
| 143 | +if [ "$NERDCTL_VERSION" = "latest" ]; then |
| 144 | + # get latest release |
| 145 | + NERDCTL_VERSION=$(curl -s ${QUERYLATEST_URL} | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/') |
| 146 | +fi |
| 147 | + |
| 148 | +echo version: $NERDCTL_VERSION |
| 149 | +echo for arch: $ARCH |
| 150 | + |
| 151 | +URL="${RELEASE_URL}${NERDCTL_VERSION}/nerdctl-${NERDCTL_VERSION#v}-linux-${ARCH}.tar.gz" |
| 152 | +echo "${URL}" |
| 153 | + |
| 154 | +curl -sSL -o /tmp/nerdctl.tar.gz "${URL}" |
| 155 | +ls -lH /tmp/nerdctl.tar.gz |
| 156 | +tar xzof /tmp/nerdctl.tar.gz -C /usr/local/bin/ nerdctl |
| 157 | +chmod 0755 /usr/local/bin/nerdctl |
| 158 | +rm /tmp/nerdctl.tar.gz |
| 159 | + |
| 160 | +mkdir -p /etc/nerdctl |
| 161 | +cat <<EOF >"/etc/nerdctl/nerdctl.toml" |
| 162 | +debug = false |
| 163 | +debug_full = false |
| 164 | +address = "${CONTAINERD_API}" |
| 165 | +EOF |
| 166 | + |
| 167 | +clean_up |
| 168 | + |
| 169 | +echo "Done!" |
0 commit comments