From 1c2a292eebdde8fd6954b564fa78bf28c6a206ec Mon Sep 17 00:00:00 2001 From: Samruddhi Khandale Date: Wed, 25 May 2022 20:25:05 +0000 Subject: [PATCH 1/7] add php --- src/php/feature.json | 27 +++++++ src/php/install.sh | 172 +++++++++++++++++++++++++++++++++++++++++ v1/feature-scripts.env | 1 + 3 files changed, 200 insertions(+) create mode 100644 src/php/feature.json create mode 100644 src/php/install.sh diff --git a/src/php/feature.json b/src/php/feature.json new file mode 100644 index 000000000..09949d08e --- /dev/null +++ b/src/php/feature.json @@ -0,0 +1,27 @@ +{ + "id": "php", + "name": "PHP", + "options": { + "version": { + "type": "string", + "proposals": ["latest", "8.0.16"], + "default": "latest", + "description": "Select or enter a PHP version" + }, + "installComposer": { + "type": "boolean", + "default": true, + "description": "Install PHP Composer?" + } + }, + "extensions": [ + "xdebug.php-debug", + "bmewburn.vscode-intelephense-client", + "xdebug.php-pack", + "devsense.phptools-vscode" + ], + "containerEnv": { + "PHP_PATH": "/usr/local/php", + "PATH":"${PHP_PATH}:${PHP_PATH}/bin:${PATH}" + } +} \ No newline at end of file diff --git a/src/php/install.sh b/src/php/install.sh new file mode 100644 index 000000000..cd05ea51f --- /dev/null +++ b/src/php/install.sh @@ -0,0 +1,172 @@ +#!/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. +#------------------------------------------------------------------------------------------------------------- +# +# Maintainer: The VS Code and Codespaces Teams +# +# Syntax: ./php-debian.sh [PHP version] [PHP_DIR] [Add Composer flag] [Non-root user] [Add rc files flag] + +VERSION=${1:-"latest"} +export PHP_DIR=${2:-"/usr/local/php"} +INSTALL_COMPOSER=${3:-"true"} +USERNAME=${4:-"automatic"} +UPDATE_RC=${5:-"true"} + +set -eux +export DEBIAN_FRONTEND=noninteractive + +if [ "$(id -u)" -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 + + +# Ensure that login shells get the correct path if the user updated the PATH using ENV. +rm -f /etc/profile.d/00-restore-env.sh +echo "export PATH=${PATH//$(sh -lc 'echo $PATH')/\$PATH}" > /etc/profile.d/00-restore-env.sh +chmod +x /etc/profile.d/00-restore-env.sh + +# Determine the appropriate non-root user +if [ "${USERNAME}" = "auto" ] || [ "${USERNAME}" = "automatic" ]; then + USERNAME="" + POSSIBLE_USERS=("vscode" "node" "codespace" "$(awk -v val=1000 -F ":" '$3==val{print $1}' /etc/passwd)") + for CURRENT_USER in ${POSSIBLE_USERS[@]}; do + if id -u ${CURRENT_USER} > /dev/null 2>&1; then + USERNAME=${CURRENT_USER} + break + fi + done + if [ "${USERNAME}" = "" ]; then + USERNAME=root + fi +elif [ "${USERNAME}" = "none" ] || ! id -u ${USERNAME} > /dev/null 2>&1; then + USERNAME=root +fi + +architecture="$(uname -m)" +if [ "${architecture}" != "amd64" ] && [ "${architecture}" != "x86_64" ] && [ "${architecture}" != "arm64" ] && [ "${architecture}" != "aarch64" ]; then + echo "(!) Architecture $architecture unsupported" + exit 1 +fi + +updaterc() { + if [ "${UPDATE_RC}" = "true" ]; then + echo "Updating /etc/bash.bashrc and /etc/zsh/zshrc..." + if [[ "$(cat /etc/bash.bashrc)" != *"$1"* ]]; then + echo -e "$1" >> /etc/bash.bashrc + fi + if [ -f "/etc/zsh/zshrc" ] && [[ "$(cat /etc/zsh/zshrc)" != *"$1"* ]]; then + echo -e "$1" >> /etc/zsh/zshrc + fi + 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 +} + +# Figure out correct version of PHP +find_version_from_git_tags() { + local repository="https://github.com/php/php-src" + local separator="." + local escaped_separator=${separator//./\\.} + local last_part="${escaped_separator}[0-9]+" + local regex="\\K[0-9]+${escaped_separator}[0-9]+${last_part}$" + local version_list="$(git ls-remote --tags ${repository} | grep -oP "${regex}" | tr -d ' ' | tr "${separator}" "." | sort -rV)" + VERSION="$(echo "${version_list}" | head -n 1)" +} + +# Install PHP Composer +addcomposer() { + php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" + php -r "if (hash_file('sha384', 'composer-setup.php') === '906a84df04cea2aa72f40b5f787e49f22d4c2f19492ac310e8cba5b96ac8b64115ac402c8cd292b8a03482574915d1a8') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;" + php composer-setup.php + php -r "unlink('composer-setup.php');" + + mv composer.phar $PHP_DIR/composer + updaterc "export PHP_DIR=${PHP_DIR}" +} + +# Install PHP if it's missing +if ! php --version &> /dev/null ; then + # Persistent / runtime dependencies + RUNTIME_DEPS="wget ca-certificates git build-essential xz-utils" + + # PHP dependencies + PHP_DEPS="libssl-dev libcurl4-openssl-dev libedit-dev libsqlite3-dev libxml2-dev zlib1g-dev libsodium-dev libargon2-dev libonig-dev" + + # Dependencies required for running "phpize" + PHPIZE_DEPS="autoconf dpkg-dev file g++ gcc libc-dev make pkg-config re2c" + + # Install dependencies + check_packages $RUNTIME_DEPS $PHP_DEPS $PHPIZE_DEPS + + # Fetch latest version of PHP if needed + if [ "${VERSION}" = "latest" ] || [ "${VERSION}" = "lts" ]; then + find_version_from_git_tags + fi + + PHP_URL="https://www.php.net/distributions/php-${VERSION}.tar.gz" + + PHP_INI_DIR="$PHP_DIR/ini" + CONF_DIR="$PHP_INI_DIR/conf.d" + mkdir -p $CONF_DIR; + + PHP_EXT_DIR="$PHP_DIR/extensions" + mkdir -p $PHP_EXT_DIR + + PHP_SRC_DIR="/usr/src/php" + mkdir -p $PHP_SRC_DIR + cd $PHP_SRC_DIR + wget -O php.tar.xz "$PHP_URL" + + tar -xf $PHP_SRC_DIR/php.tar.xz -C "$PHP_SRC_DIR" --strip-components=1 + cd $PHP_SRC_DIR; + + # PHP 7.4+, the pecl/pear installers are officially deprecated and are removed in PHP 8+ + # Thus, requiring an explicit "--with-pear" + IFS="." + read -a versions <<< "$VERSION" + PHP_MAJOR_VERSION=${versions[0]} + PHP_MINOR_VERSION=${versions[1]} + + VERSION_CONFIG="" + if (( $(($PHP_MAJOR_VERSION)) >= 8 )) || (( $(($PHP_MAJOR_VERSION)) == 7 && $(($PHP_MINOR_VERSION)) >= 4 )); then + VERSION_CONFIG="--with-pear" + fi + + ./configure --prefix="$PHP_DIR" --with-config-file-path="$PHP_INI_DIR" --with-config-file-scan-dir="$CONF_DIR" --enable-option-checking=fatal --with-curl --with-libedit --with-openssl --with-zlib --with-password-argon2 --with-sodium=shared "$VERSION_CONFIG" EXTENSION_DIR="$PHP_EXT_DIR"; + + make -j "$(nproc)" + find -type f -name '*.a' -delete + make install + find $PHP_DIR -type f -executable -exec strip --strip-all '{}' + || true + make clean + + cp -v $PHP_SRC_DIR/php.ini-* "$PHP_INI_DIR/"; + cp "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini" + PATH=$PATH:${PHP_DIR}/bin + + # Install xdebug + pecl install xdebug + XDEBUG_INI="$CONF_DIR/xdebug.ini" + echo "zend_extension=$(find PHP_EXT_DIR -name xdebug.so)" > XDEBUG_INI + echo "xdebug.mode = debug" >> XDEBUG_INI + echo "xdebug.start_with_request = yes" >> XDEBUG_INI + echo "xdebug.client_port = 9003" >> XDEBUG_INI + + # Install PHP Composer if needed + if [ "${INSTALL_COMPOSER}" = "true" ]; then + addcomposer + fi + + updaterc "export PHP_DIR=${PHP_DIR}/bin" +fi + +echo "Done!" \ No newline at end of file diff --git a/v1/feature-scripts.env b/v1/feature-scripts.env index 8b7f7346f..68573dacc 100644 --- a/v1/feature-scripts.env +++ b/v1/feature-scripts.env @@ -21,3 +21,4 @@ _BUILD_ARG_POWERSHELL="./powershell/install.sh ${_B _BUILD_ARG_DESKTOP_LITE="./desktop-lite/install.sh automatic ${_BUILD_ARG_DESKTOP_LITE_PASSWORD:-vscode} true ${_BUILD_ARG_DESKTOP_LITE_VNCPORT:-5901} ${_BUILD_ARG_DESKTOP_LITE_WEBPORT:-6080}" _BUILD_ARG_DOTNET="./dotnet/install.sh ${_BUILD_ARG_DOTNET_VERSION:-latest} ${_BUILD_ARG_DOTNET_RUNTIMEONLY:-false} automatic true /usr/local/dotnet dotnet" _BUILD_ARG_JUPYTERLAB="./jupyterlab/install.sh ${_BUILD_ARG_JUPYTERLAB_VERSION:-latest}" automatic ${_BUILD_ARG_JUPYTERLAB_PYTHONBINARY:-python}" true +_BUILD_ARG_PHP="./php/install.sh ${_BUILD_ARG_PHP_VERSION:-latest} /usr/local/php ${_BUILD_ARG_PHP_INSTALLCOMPOSER:-true} true automatic true" From 68ce021455c7ec30269cad19d6b50ebad1efaa4b Mon Sep 17 00:00:00 2001 From: Samruddhi Khandale Date: Wed, 25 May 2022 20:27:12 +0000 Subject: [PATCH 2/7] add test --- test/php/test.sh | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 test/php/test.sh diff --git a/test/php/test.sh b/test/php/test.sh new file mode 100644 index 000000000..35a3057c9 --- /dev/null +++ b/test/php/test.sh @@ -0,0 +1,13 @@ + + #!/bin/bash + + set -e + + # Optional: Import test library + source dev-container-features-test-lib + + # Definition specific tests + check "version" php --version + + # Report result + reportResults \ No newline at end of file From ea1b033efb5a52d1889fe1a367c6df8d1895bbc5 Mon Sep 17 00:00:00 2001 From: Samruddhi Khandale Date: Wed, 25 May 2022 20:34:02 +0000 Subject: [PATCH 3/7] fix test --- test/php/test.sh | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/test/php/test.sh b/test/php/test.sh index 35a3057c9..344663c07 100644 --- a/test/php/test.sh +++ b/test/php/test.sh @@ -1,13 +1,11 @@ +#!/bin/bash - #!/bin/bash +set -e - set -e +# Optional: Import test library +source dev-container-features-test-lib - # Optional: Import test library - source dev-container-features-test-lib +check "version" php --version - # Definition specific tests - check "version" php --version - - # Report result - reportResults \ No newline at end of file +# Report result +reportResults \ No newline at end of file From 73bf0d850762c0f2f5f38b8633e0c087f51b95b5 Mon Sep 17 00:00:00 2001 From: Samruddhi Khandale Date: Wed, 25 May 2022 20:39:50 +0000 Subject: [PATCH 4/7] add to workflow --- .github/workflows/test.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index d705a13af..6ed7962ca 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -12,7 +12,7 @@ jobs: continue-on-error: true strategy: matrix: - features: [ "anaconda", "aws-cli", "azure-cli", "common", "desktop-lite", "docker-from-docker", "docker-in-docker", "dotnet", "git", "github-cli", "git-lfs", "go", "gradle", "hugo", "java", "jupyterlab", "kubectl-helm-minikube", "maven", "node", "powershell", "python", "ruby", "rust", "sshd", "terraform" ] + features: [ "anaconda", "aws-cli", "azure-cli", "common", "desktop-lite", "docker-from-docker", "docker-in-docker", "dotnet", "git", "github-cli", "git-lfs", "go", "gradle", "hugo", "java", "jupyterlab", "kubectl-helm-minikube", "maven", "node", "powershell", "python", "php", "ruby", "rust", "sshd", "terraform" ] baseImage: [ "ubuntu:focal" ] steps: - uses: actions/checkout@v2 From ec1d4536aef8b58137c306d82fb004cdfb374aba Mon Sep 17 00:00:00 2001 From: Samruddhi Khandale Date: Wed, 25 May 2022 20:41:11 +0000 Subject: [PATCH 5/7] fix test --- src/php/feature.json | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/php/feature.json b/src/php/feature.json index 09949d08e..e5bfd24cc 100644 --- a/src/php/feature.json +++ b/src/php/feature.json @@ -23,5 +23,9 @@ "containerEnv": { "PHP_PATH": "/usr/local/php", "PATH":"${PHP_PATH}:${PHP_PATH}/bin:${PATH}" + }, + "install": { + "app": "", + "file": "install.sh" } } \ No newline at end of file From 88adcddb5f62e4f865b2babdd6cf3c83dadbcfd4 Mon Sep 17 00:00:00 2001 From: Samruddhi Khandale Date: Wed, 25 May 2022 21:17:21 +0000 Subject: [PATCH 6/7] fix bug --- src/php/install.sh | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/php/install.sh b/src/php/install.sh index cd05ea51f..4d4d21194 100644 --- a/src/php/install.sh +++ b/src/php/install.sh @@ -85,7 +85,8 @@ find_version_from_git_tags() { # Install PHP Composer addcomposer() { php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" - php -r "if (hash_file('sha384', 'composer-setup.php') === '906a84df04cea2aa72f40b5f787e49f22d4c2f19492ac310e8cba5b96ac8b64115ac402c8cd292b8a03482574915d1a8') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;" + HASH="$(wget -q -O - https://composer.github.io/installer.sig)" + php -r "if (hash_file('sha384', 'composer-setup.php') === '$HASH') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;" php composer-setup.php php -r "unlink('composer-setup.php');" @@ -169,4 +170,4 @@ if ! php --version &> /dev/null ; then updaterc "export PHP_DIR=${PHP_DIR}/bin" fi -echo "Done!" \ No newline at end of file +echo "Done!" From 7bf7983016e372830f359c3bf2e4d2261b17bcf2 Mon Sep 17 00:00:00 2001 From: Samruddhi Khandale Date: Wed, 25 May 2022 21:33:45 +0000 Subject: [PATCH 7/7] test composer --- test/php/test.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/php/test.sh b/test/php/test.sh index 344663c07..3e4e7223a 100644 --- a/test/php/test.sh +++ b/test/php/test.sh @@ -5,7 +5,8 @@ set -e # Optional: Import test library source dev-container-features-test-lib -check "version" php --version +check "PHP version" php --version +check "Composer version" composer --version # Report result reportResults \ No newline at end of file