From a39f3991a95c9d6faedd226ea4420b3fe7d3ff66 Mon Sep 17 00:00:00 2001 From: Ragdata Date: Sun, 25 Jun 2023 20:11:16 +1000 Subject: [PATCH] feat(feature): add git feature --- src/git/NOTES.md | 19 ++++ src/git/devcontainer-feature.json | 26 +++++ src/git/install.sh | 164 ++++++++++++++++++++++++++++++ 3 files changed, 209 insertions(+) create mode 100644 src/git/NOTES.md create mode 100644 src/git/devcontainer-feature.json create mode 100644 src/git/install.sh diff --git a/src/git/NOTES.md b/src/git/NOTES.md new file mode 100644 index 0000000..e753776 --- /dev/null +++ b/src/git/NOTES.md @@ -0,0 +1,19 @@ +

+ +Ragdata + +RagsWorks Git Feature + +

+ +## ⚓ [OS Support](#top) + +This feature should work on recent versions of: + +- ✅ **Alpine** +- ✅ **Debian/Ubuntu** + +Requires the following tools / utilities: + +- `bash` +- `apt` diff --git a/src/git/devcontainer-feature.json b/src/git/devcontainer-feature.json new file mode 100644 index 0000000..3a5db0a --- /dev/null +++ b/src/git/devcontainer-feature.json @@ -0,0 +1,26 @@ +{ + "id": "git", + "version": "1.0.0", + "name": "Git Feature", + "documentationURL": "https://github.com/ragsworks/devcontainer-features/tree/master/src/git", + "description": "Installs the latest version of Git.", + "options": { + "version": { + "type": "string", + "proposals": [ + "latest", + "os-provided" + ], + "default": "os-provided", + "description": "Select or enter preferred Git version" + }, + "ppa": { + "type": "boolean", + "default": true, + "description": "Install from PPA if available" + } + }, + "installsAfter": [ + "ghcr.io/ragsworks/devcontainers-features/common-utils" + ] +} diff --git a/src/git/install.sh b/src/git/install.sh new file mode 100644 index 0000000..209985f --- /dev/null +++ b/src/git/install.sh @@ -0,0 +1,164 @@ +#!/usr/bin/env bash +# shellcheck disable=SC2034 +# ================================================================== +# install.sh +# ================================================================== +# Install Git Feature +# +# File: install.sh +# Author: Ragdata +# Date: 25/06/2023 +# License: MIT License +# Copyright: Copyright © 2023 Darren (Ragdata) Poulton +# ================================================================== +# ATTRIBUTION: This script has been HEAVILY influenced & guided by +# the work of the Official Organization for the +# Development Containers Specification. (see +# ATTRIBUTION section of README.md for a link.) +# COPYRIGHT: Copyright © Microsoft Corporation. All rights reserved. +# ================================================================== +# PREFLIGHT +# ================================================================== +GIT_VERSION=${VERSION} # 'system' checks the base image first, else installs 'latest' +USE_PPA_IF_AVAILABLE=${PPA} +GIT_CORE_PPA_ARCHIVE_GPG_KEY=E1DD270288B4E6030699E45FA1715D88E1DF1F24 +GPG_KEY_SERVERS="keyserver hkp://keyserver.ubuntu.com +keyserver hkps://keys.openpgp.org +keyserver hkp://keyserver.pgp.com" +# ================================================================== +# MAIN +# ================================================================== +# Clean up +rm -rf /var/lib/apt/lists/* + +# check that script is running as root +if [ "$EUID" -ne 0 ]; then + echo -e "Script MUST be run as root. Use sudo, su or add 'User root' to your Dockerfile before running this script." + exit 1 +fi + +# Import the specified key in a variable name passed in as +receive_gpg_keys() { + local keys=${!1} + local keyring_args="" + if [ -n "$2" ]; then + mkdir -p "$(dirname \"$2\")" + keyring_args="--no-default-keyring --keyring $2" + fi + + # Use a temporary location for gpg keys to avoid polluting image + export GNUPGHOME="/tmp/tmp-gnupg" + mkdir -p ${GNUPGHOME} + chmod 700 ${GNUPGHOME} + echo -e "disable-ipv6\n${GPG_KEY_SERVERS}" > ${GNUPGHOME}/dirmngr.conf + # GPG key download sometimes fails for some reason and retrying fixes it. + local retry_count=0 + local gpg_ok="false" + set +e + until [ "${gpg_ok}" = "true" ] || [ "${retry_count}" -eq "5" ]; + do + echo "(*) Downloading GPG key..." + ( echo "${keys}" | xargs -n 1 gpg -q ${keyring_args} --recv-keys) 2>&1 && gpg_ok="true" + if [ "${gpg_ok}" != "true" ]; then + echo "(*) Failed getting key, retring in 10s..." + (( retry_count++ )) + sleep 10s + fi + done + set -e + if [ "${gpg_ok}" = "false" ]; then + echo "(!) Failed to get gpg key." + exit 1 + fi +} + +apt_get_update() +{ + if [ "$(find /var/lib/apt/lists/* | wc -l)" = "0" ]; then + echo "Running apt-get update..." + apt-get update -y + fi +} + +# Checks if packages are installed and installs them if not +check_packages() { + if ! dpkg -s "$@" > /dev/null 2>&1; then + apt_get_update + apt-get -y install --no-install-recommends "$@" + fi +} + +export DEBIAN_FRONTEND=noninteractive + +# Source /etc/os-release to get OS info +. /etc/os-release + +# If the os provided version is "good enough", just install that. +if [ "${GIT_VERSION}" = "os-provided" ] || [ "${GIT_VERSION}" = "system" ]; then + if type git > /dev/null 2>&1; then + echo "Detected existing system install: $(git version)" + # Clean up + rm -rf /var/lib/apt/lists/* + exit 0 + fi + + echo "Installing git from OS apt repository" + check_packages git + # Clean up + rm -rf /var/lib/apt/lists/* + exit 0 +fi + +# If ubuntu, PPAs allowed, and latest - install from there +# shellcheck disable=SC2235 +if ([ "${GIT_VERSION}" = "latest" ] || [ "${GIT_VERSION}" = "lts" ] || [ "${GIT_VERSION}" = "current" ]) && [ "${ID}" = "ubuntu" ] && [ "${USE_PPA_IF_AVAILABLE}" = "true" ]; then + echo "Using PPA to install latest git..." + check_packages apt-transport-https curl ca-certificates gnupg2 dirmngr + receive_gpg_keys GIT_CORE_PPA_ARCHIVE_GPG_KEY /usr/share/keyrings/gitcoreppa-archive-keyring.gpg + echo -e "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/gitcoreppa-archive-keyring.gpg] http://ppa.launchpad.net/git-core/ppa/ubuntu ${VERSION_CODENAME} main\ndeb-src [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/gitcoreppa-archive-keyring.gpg] http://ppa.launchpad.net/git-core/ppa/ubuntu ${VERSION_CODENAME} main" > /etc/apt/sources.list.d/git-core-ppa.list + apt-get update + apt-get -y install --no-install-recommends git + rm -rf "/tmp/tmp-gnupg" + rm -rf /var/lib/apt/lists/* + exit 0 +fi + +# Install required packages to build if missing +check_packages build-essential curl ca-certificates tar gettext libssl-dev zlib1g-dev libcurl?-openssl-dev libexpat1-dev + +# Partial version matching +if [ "$(echo "${GIT_VERSION}" | grep -o '\.' | wc -l)" != "2" ]; then + requested_version="${GIT_VERSION}" + version_list="$(curl -sSL -H "Accept: application/vnd.github.v3+json" "https://api.github.com/repos/git/git/tags" | grep -oP '"name":\s*"v\K[0-9]+\.[0-9]+\.[0-9]+"' | tr -d '"' | sort -rV )" + if [ "${requested_version}" = "latest" ] || [ "${requested_version}" = "lts" ] || [ "${requested_version}" = "current" ]; then + GIT_VERSION="$(echo "${version_list}" | head -n 1)" + else + set +e + GIT_VERSION="$(echo "${version_list}" | grep -E -m 1 "^${requested_version//./\\.}([\\.\\s]|$)")" + set -e + fi + if [ -z "${GIT_VERSION}" ] || ! echo "${version_list}" | grep "^${GIT_VERSION//./\\.}$" > /dev/null 2>&1; then + echo "Invalid git version: ${requested_version}" >&2 + exit 1 + fi +fi + +check_packages libpcre2-dev + +if [ "${VERSION_CODENAME}" = "focal" ] || [ "${VERSION_CODENAME}" = "bullseye" ]; then + check_packages libpcre2-posix2 +elif [ "${VERSION_CODENAME}" = "bionic" ] || [ "${VERSION_CODENAME}" = "buster" ]; then + check_packages libpcre2-posix0 +else + check_packages libpcre2-posix3 +fi + +echo "Downloading source for ${GIT_VERSION}..." +curl -sL https://github.com/git/git/archive/v"${GIT_VERSION}".tar.gz | tar -xzC /tmp 2>&1 +echo "Building..." +cd /tmp/git-"${GIT_VERSION}" +make -s USE_LIBPCRE=YesPlease prefix=/usr/local sysconfdir=/etc all && make -s USE_LIBPCRE=YesPlease prefix=/usr/local sysconfdir=/etc install 2>&1 +rm -rf /tmp/git-"${GIT_VERSION}" +rm -rf /var/lib/apt/lists/* +echo "Done!" +