setup-php/src/scripts/linux.sh

313 lines
9.8 KiB
Bash
Raw Normal View History

2022-01-12 20:14:12 +05:30
# Function to add sudo
add_sudo() {
if ! command -v sudo >/dev/null; then
2022-01-12 20:14:12 +05:30
check_package sudo || apt-get update
apt-get install -y sudo || (apt-get update && apt-get install -y sudo)
fi
2022-01-12 20:14:12 +05:30
}
2024-06-16 08:07:19 +05:30
# Function to link apt-fast to apt-get
link_apt_fast() {
if ! command -v apt-fast >/dev/null; then
sudo ln -sf /usr/bin/apt-get /usr/bin/apt-fast
trap "sudo rm -f /usr/bin/apt-fast 2>/dev/null" exit
fi
}
2022-01-12 20:14:12 +05:30
# Function to setup environment for self-hosted runners.
self_hosted_helper() {
2020-11-08 13:06:21 +05:30
if ! command -v apt-fast >/dev/null; then
sudo ln -sf /usr/bin/apt-get /usr/bin/apt-fast
trap "sudo rm -f /usr/bin/apt-fast 2>/dev/null" exit
2019-10-17 01:41:13 +05:30
fi
2021-08-10 01:37:46 +05:30
install_packages apt-transport-https ca-certificates curl file make jq unzip autoconf automake gcc g++ gnupg
2020-07-25 12:27:51 +05:30
}
2023-04-02 22:03:11 +05:30
# Function to fix broken packages.
fix_broken_packages() {
sudo apt --fix-broken install >/dev/null 2>&1
}
2020-11-08 13:06:21 +05:30
# Function to install a package
install_packages() {
packages=("$@")
if ! [ -e /etc/dpkg/dpkg.cfg.d/force-confnew ]; then
echo "force-confnew" | sudo tee /etc/dpkg/dpkg.cfg.d/force-confnew >/dev/null 2>&1
trap "sudo rm -f /etc/dpkg/dpkg.cfg.d/force-confnew 2>/dev/null" exit
fi
2023-04-02 22:03:11 +05:30
$apt_install "${packages[@]}" >/dev/null 2>&1 || (update_lists && fix_broken_packages && $apt_install "${packages[@]}" >/dev/null 2>&1)
}
2021-01-05 16:52:41 +05:30
# Function to disable an extension.
disable_extension_helper() {
local extension=$1
local disable_dependents=${2:-false}
get_extension_map
if [ "$disable_dependents" = "true" ]; then
disable_extension_dependents "$extension"
fi
sudo sed -Ei "/=(.*\/)?\"?$extension(.so)?\"?$/d" "${ini_file[@]}" "$pecl_file"
sudo find "$ini_dir"/.. -name "*-$extension.ini" -not -path "*phar.ini" -not -path "*pecl.ini" -not -path "*mods-available*" -delete >/dev/null 2>&1 || true
2022-03-06 11:38:28 +05:30
sudo rm -f /tmp/php"$version"_extensions
mkdir -p /tmp/extdisabled/"$version"
echo '' | sudo tee /tmp/extdisabled/"$version"/"$extension" >/dev/null 2>&1
2021-01-05 16:52:41 +05:30
}
2020-10-08 17:27:52 +05:30
# Function to add PDO extension.
2020-03-05 05:58:24 +05:30
add_pdo_extension() {
pdo_ext="pdo_$1"
if check_extension "$pdo_ext"; then
2020-11-08 13:06:21 +05:30
add_log "${tick:?}" "$pdo_ext" "Enabled"
2020-03-05 05:58:24 +05:30
else
ext=$1
ext_name=$1
if shared_extension pdo; then
disable_extension_helper pdo
echo "extension=pdo.so" | sudo tee "${ini_file[@]/php.ini/conf.d/10-pdo.ini}" >/dev/null 2>&1
fi
2020-03-05 05:58:24 +05:30
if [ "$ext" = "mysql" ]; then
enable_extension "mysqlnd" "extension"
2021-02-19 13:30:02 +05:30
ext_name='mysqli'
elif [ "$ext" = "dblib" ]; then
ext_name="sybase"
elif [ "$ext" = "firebird" ]; then
install_packages libfbclient2 >/dev/null 2>&1
enable_extension "pdo_firebird" "extension"
ext_name="interbase"
2020-05-17 11:19:52 +05:30
elif [ "$ext" = "sqlite" ]; then
ext="sqlite3"
ext_name="sqlite3"
2020-03-05 05:58:24 +05:30
fi
2020-11-08 13:06:21 +05:30
add_extension "$ext_name" "extension" >/dev/null 2>&1
add_extension "$pdo_ext" "extension" >/dev/null 2>&1
2020-07-26 15:48:19 +05:30
add_extension_log "$pdo_ext" "Enabled"
2020-03-05 05:58:24 +05:30
fi
}
2021-10-15 17:53:09 +05:30
# Function to check if a package exists
check_package() {
2022-01-12 20:14:12 +05:30
apt-cache policy "$1" 2>/dev/null | grep -q 'Candidate'
2021-10-15 17:53:09 +05:30
}
2022-01-13 06:33:07 +05:30
# Helper function to add an extension.
add_extension_helper() {
2021-09-19 15:33:45 +05:30
local extension=$1
2022-09-26 09:42:27 +05:30
packages=(php"$version"-"$extension")
2022-01-13 06:33:07 +05:30
add_ppa ondrej/php >/dev/null 2>&1 || update_ppa ondrej/php
2022-10-24 10:28:08 +05:30
[ "${debug:?}" = "debug" ] && check_package php"$version"-"$extension"-dbgsym && packages+=(php"$version"-"$extension"-dbgsym)
2022-09-26 09:42:27 +05:30
(check_package "${packages[0]}" && install_packages "${packages[@]}") || pecl_install "$extension"
2022-01-13 06:33:07 +05:30
add_extension_log "$extension" "Installed and enabled"
sudo chmod 777 "${ini_file[@]}"
2020-02-23 19:00:40 +05:30
}
2020-05-27 16:23:52 +05:30
# Function to setup phpize and php-config.
2020-01-17 12:58:28 +05:30
add_devtools() {
2020-09-28 10:58:02 +05:30
tool=$1
2020-11-08 13:06:21 +05:30
if ! command -v "$tool$version" >/dev/null; then
2022-01-13 05:22:02 +05:30
install_packages "php$version-dev"
2020-01-17 12:58:28 +05:30
fi
switch_version "phpize" "php-config"
add_extension xml extension >/dev/null 2>&1
2020-11-08 13:06:21 +05:30
add_log "${tick:?}" "$tool" "Added $tool $semver"
2020-01-17 12:58:28 +05:30
}
2024-11-04 08:20:56 +05:30
# Function to setup PHP from the shivammathur/php-builder builds.
setup_php_builder() {
run_script "php-builder" "${runner:?}" "$version" "${debug:?}" "${ts:?}"
}
2020-05-27 16:23:52 +05:30
# Function to setup PHP 5.3, PHP 5.4 and PHP 5.5.
setup_old_versions() {
2020-11-11 02:23:45 +05:30
run_script "php5-ubuntu" "$version"
}
# Function to setup PHP from the cached builds.
2023-11-26 03:34:57 +05:30
setup_cached_versions() {
run_script "php-ubuntu" "$version" "${debug:?}" "${ts:?}"
}
2020-05-27 16:23:52 +05:30
# Function to add PECL.
2019-12-27 06:56:49 +05:30
add_pecl() {
2020-09-28 10:58:02 +05:30
add_devtools phpize >/dev/null 2>&1
2020-11-08 13:06:21 +05:30
if ! command -v pecl >/dev/null; then
install_packages php-pear
2020-01-30 12:03:30 +05:30
fi
2020-05-27 16:23:52 +05:30
configure_pecl >/dev/null 2>&1
pear_version=$(get_tool_version "pecl" "version")
add_log "${tick:?}" "PECL" "Added PECL $pear_version"
}
2020-05-27 16:23:52 +05:30
# Function to switch versions of PHP binaries.
switch_version() {
2022-03-07 08:22:08 +05:30
tools=("$@")
to_wait=()
2022-03-07 08:22:08 +05:30
if ! (( ${#tools[@]} )); then
tools+=(pear pecl php phar phar.phar php-cgi php-config phpize phpdbg)
[ -e /usr/lib/cgi-bin/php"$version" ] && sudo update-alternatives --set php-cgi-bin /usr/lib/cgi-bin/php"$version" & to_wait+=($!)
[ -e /usr/sbin/php-fpm"$version" ] && sudo update-alternatives --set php-fpm /usr/sbin/php-fpm"$version" & to_wait+=($!)
[ -e /run/php/php"$version"-fpm.sock ] && sudo update-alternatives --set php-fpm.sock /run/php/php"$version"-fpm.sock & to_wait+=($!)
fi
for tool in "${tools[@]}"; do
if [ -e "/usr/bin/$tool$version" ]; then
sudo update-alternatives --set "$tool" /usr/bin/"$tool$version" &
to_wait+=($!)
fi
done
2020-11-08 13:06:21 +05:30
wait "${to_wait[@]}"
2020-02-21 07:58:19 +05:30
}
2022-09-26 09:42:27 +05:30
# Function to get packages to install
get_php_packages() {
sed "s/[^ ]*/php$version-&/g" "$src"/configs/php_packages | tr '\n' ' '
if [ "${debug:?}" = "debug" ]; then
sed "s/[^ ]*/php$version-&-dbgsym/g" "$src"/configs/php_debug_packages | tr '\n' ' '
fi
}
2020-05-27 16:23:52 +05:30
# Function to install packaged PHP
add_packaged_php() {
2023-11-26 03:34:57 +05:30
add_ppa ondrej/php >/dev/null 2>&1 || update_ppa ondrej/php
IFS=' ' read -r -a packages <<<"$(get_php_packages)"
install_packages "${packages[@]}"
2020-05-27 16:23:52 +05:30
}
# Function to update PHP.
2020-02-21 07:58:19 +05:30
update_php() {
2021-11-18 19:40:47 +05:30
initial_version="$(php_semver)$(php_extra_version)"
add_php
updated_version="$(php_semver)$(php_extra_version)"
2020-02-21 07:58:19 +05:30
if [ "$updated_version" != "$initial_version" ]; then
status="Updated to"
else
2021-11-18 19:40:47 +05:30
status="Found"
2020-02-21 07:58:19 +05:30
fi
}
2020-05-27 16:23:52 +05:30
# Function to install PHP.
add_php() {
2023-11-26 03:34:57 +05:30
if [ "${runner:?}" = "self-hosted" ] || [ "${use_package_cache:-true}" = "false" ]; then
2024-11-04 08:20:56 +05:30
if [[ "$version" =~ ${nightly_versions:?} || "$ts" = "zts" ]]; then
setup_php_builder
else
2023-11-26 03:34:57 +05:30
add_packaged_php
switch_version >/dev/null 2>&1
add_pecl
fi
2020-11-08 13:06:21 +05:30
elif [[ "$version" =~ ${old_versions:?} ]]; then
2020-05-27 16:23:52 +05:30
setup_old_versions
else
2023-11-26 03:34:57 +05:30
setup_cached_versions
2020-05-27 16:23:52 +05:30
fi
status="Installed"
}
# Function to ini file for pear and link it to each SAPI.
link_pecl_file() {
echo '' | sudo tee "$pecl_file" >/dev/null 2>&1
for file in "${ini_file[@]}"; do
sapi_scan_dir="$(realpath -m "$(dirname "$file")")/conf.d"
if [ "$sapi_scan_dir" != "$scan_dir" ] && ! [ -h "$sapi_scan_dir" ]; then
sudo mkdir -p "$sapi_scan_dir"
sudo ln -sf "$pecl_file" "$sapi_scan_dir/99-pecl.ini"
fi
done
}
2021-07-12 17:00:40 +05:30
# Function to get extra version.
php_extra_version() {
2021-09-19 02:38:31 +05:30
if [ -e /etc/php/"$version"/COMMIT ]; then
2021-07-12 17:00:40 +05:30
echo " ($(cat "/etc/php/$version/COMMIT"))"
fi
}
2022-01-25 13:43:49 +05:30
# Function to set php.ini
add_php_config() {
php_lib_dir=/usr/lib/php/"$version"
current_ini="$php_lib_dir"/php.ini-current
current=$(cat "$current_ini" 2>/dev/null)
if [ "$current" = "$ini" ]; then
return;
fi
if [[ "$ini" = "production" && "x$current" != "xproduction" ]]; then
echo "${ini_file[@]}" | xargs -n 1 -P 6 sudo cp "$php_lib_dir"/php.ini-production
if [ -e "$php_lib_dir"/php.ini-production.cli ]; then
sudo cp "$php_lib_dir"/php.ini-production.cli "$ini_dir"/php.ini
fi
elif [ "$ini" = "development" ]; then
echo "${ini_file[@]}" | xargs -n 1 -P 6 sudo cp "$php_lib_dir"/php.ini-development
elif [ "$ini" = "none" ]; then
echo '' | sudo tee "${ini_file[@]}" >/dev/null 2>&1
fi
echo "$ini" | sudo tee "$current_ini" >/dev/null 2>&1
}
2020-11-08 13:06:21 +05:30
# Function to Setup PHP
setup_php() {
step_log "Setup PHP"
sudo mkdir -m 777 -p /var/run /run/php
2022-01-18 08:44:46 +05:30
php_config="$(command -v php-config)"
2024-12-23 16:04:30 +05:30
check_pre_installed
2022-01-18 08:44:46 +05:30
if [[ -z "$php_config" ]] || [ "$(php_semver | cut -c 1-3)" != "$version" ]; then
if [ ! -e "/usr/bin/php$version" ] || [ ! -e "/usr/bin/php-config$version" ]; then
2020-11-08 13:06:21 +05:30
add_php >/dev/null 2>&1
else
2021-11-18 19:40:47 +05:30
if ! [[ "$version" =~ ${old_versions:?} ]]; then
switch_version >/dev/null 2>&1
fi
2020-11-08 13:06:21 +05:30
if [ "${update:?}" = "true" ]; then
update_php >/dev/null 2>&1
else
status="Switched to"
fi
fi
2022-01-18 08:44:46 +05:30
php_config="$(command -v php-config)"
2020-11-08 13:06:21 +05:30
else
if [ "$update" = "true" ]; then
update_php >/dev/null 2>&1
else
status="Found"
fi
fi
2020-12-17 04:20:39 +05:30
if ! command -v php"$version" >/dev/null; then
2022-01-12 20:14:12 +05:30
add_log "${cross:?}" "PHP" "Could not setup PHP $version"
2020-12-17 04:20:39 +05:30
exit 1
fi
2022-01-16 12:03:31 +05:30
ext_dir="/usr/$(grep -Po "extension_dir=..[^/]*/\K[^'\"]*" "$php_config")"
2022-01-18 08:44:46 +05:30
ini_dir="$(php_ini_path)"
2022-01-16 12:03:31 +05:30
scan_dir="$ini_dir"/conf.d
2020-11-08 13:06:21 +05:30
pecl_file="$scan_dir"/99-pecl.ini
2022-01-18 08:44:46 +05:30
semver="$(php_semver)"
extra_version="$(php_extra_version)"
2021-09-02 19:24:08 +05:30
export ext_dir
mapfile -t ini_file < <(sudo find "$ini_dir/.." -name "php.ini" -exec readlink -m {} +)
link_pecl_file
2020-12-17 20:49:20 +05:30
configure_php
set_output "php-version" "$semver"
2020-11-08 13:06:21 +05:30
sudo rm -rf /usr/local/bin/phpunit >/dev/null 2>&1
sudo chmod 777 "${ini_file[@]}" "$pecl_file" "${tool_path_dir:?}"
2022-01-29 12:14:42 +05:30
sudo cp "$src"/configs/pm/*.json "$RUNNER_TOOL_CACHE/"
add_log "${tick:?}" "PHP" "$status PHP $semver$extra_version"
2020-11-08 13:06:21 +05:30
}
# Variables
version=${1:-'8.4'}
2022-01-25 13:43:49 +05:30
ini=${2:-'production'}
2022-01-29 12:14:42 +05:30
src=${0%/*}/..
debconf_fix="DEBIAN_FRONTEND=noninteractive"
apt_install="sudo $debconf_fix apt-fast install -y --no-install-recommends"
2022-01-29 12:14:42 +05:30
scripts="$src"/scripts
2022-02-08 00:51:20 +05:30
add_sudo >/dev/null 2>&1
2024-06-16 08:07:19 +05:30
link_apt_fast >/dev/null 2>&1
2022-01-12 20:14:12 +05:30
. /etc/os-release
2020-11-08 13:06:21 +05:30
# shellcheck source=.
2022-01-02 17:50:00 +05:30
. "${scripts:?}"/unix.sh
2021-08-10 01:37:46 +05:30
. "${scripts:?}"/tools/ppa.sh
. "${scripts:?}"/tools/add_tools.sh
. "${scripts:?}"/extensions/source.sh
. "${scripts:?}"/extensions/add_extensions.sh
2020-05-27 16:23:52 +05:30
read_env
2020-11-08 13:06:21 +05:30
self_hosted_setup
setup_php