setup-php/src/scripts/linux.sh

458 lines
14 KiB
Bash
Raw Normal View History

2020-05-27 17:53:52 +07:00
# Function to log start of a operation.
2019-10-17 03:11:13 +07:00
step_log() {
message=$1
printf "\n\033[90;1m==> \033[0m\033[37;1m%s\033[0m\n" "$message"
}
2020-05-27 17:53:52 +07:00
# Function to log result of a operation.
2019-10-17 03:11:13 +07:00
add_log() {
mark=$1
subject=$2
message=$3
if [ "$mark" = "$tick" ]; then
printf "\033[32;1m%s \033[0m\033[34;1m%s \033[0m\033[90;1m%s\033[0m\n" "$mark" "$subject" "$message"
else
printf "\033[31;1m%s \033[0m\033[34;1m%s \033[0m\033[90;1m%s\033[0m\n" "$mark" "$subject" "$message"
fi
}
2019-12-19 12:59:27 +07:00
2020-05-27 17:53:52 +07:00
# Function to read env inputs.
read_env() {
. /etc/lsb-release
2020-05-27 17:53:52 +07:00
[[ -z "${update}" ]] && update='false' && UPDATE='false' || update="${update}"
[ "$update" = false ] && [[ -n ${UPDATE} ]] && update="${UPDATE}"
[[ -z "${runner}" ]] && runner='github' && RUNNER='github' || runner="${runner}"
[ "$runner" = false ] && [[ -n ${RUNNER} ]] && runner="${RUNNER}"
}
2020-07-12 06:50:08 +07:00
# Function to backup and cleanup package lists.
cleanup_lists() {
if [ ! -e /etc/apt/sources.list.d.save ]; then
sudo mv /etc/apt/sources.list.d /etc/apt/sources.list.d.save
sudo mkdir /etc/apt/sources.list.d
2020-07-22 13:10:30 +07:00
sudo mv /etc/apt/sources.list.d.save/*ondrej*.list /etc/apt/sources.list.d/
2020-07-12 06:50:08 +07:00
trap "sudo mv /etc/apt/sources.list.d.save/*.list /etc/apt/sources.list.d/" exit
fi
}
# Function to add ppa:ondrej/php.
add_ppa() {
if ! apt-cache policy | grep -q ondrej/php; then
2020-07-12 06:50:08 +07:00
cleanup_lists
LC_ALL=C.UTF-8 sudo apt-add-repository ppa:ondrej/php -y
if [ "$DISTRIB_RELEASE" = "16.04" ]; then
sudo "$debconf_fix" apt-get update
fi
fi
}
2020-07-23 07:17:11 +07:00
# Function to update the package lists.
update_lists() {
if [ ! -e /tmp/setup_php ]; then
[ "$DISTRIB_RELEASE" = "20.04" ] && add_ppa >/dev/null 2>&1
cleanup_lists
sudo "$debconf_fix" apt-get update >/dev/null 2>&1
echo '' | sudo tee "/tmp/setup_php" >/dev/null 2>&1
fi
}
2020-05-27 17:53:52 +07:00
# Function to setup environment for self-hosted runners.
self_hosted_setup() {
echo "Set disable_coredump false" | sudo tee -a /etc/sudo.conf
2020-05-27 17:53:52 +07:00
if ! command -v apt-fast >/dev/null; then
sudo ln -sf /usr/bin/apt-get /usr/bin/apt-fast
fi
update_lists && $apt_install curl make software-properties-common unzip
add_ppa
}
2020-05-27 17:53:52 +07:00
# Function to configure PECL.
2020-01-27 21:36:04 +07:00
configure_pecl() {
if [ "$pecl_config" = "false" ] && [ -e /usr/bin/pecl ]; then
2020-01-27 21:36:04 +07:00
for tool in pear pecl; do
2020-05-27 17:53:52 +07:00
sudo "$tool" config-set php_ini "$scan_dir"/99-pecl.ini
sudo "$tool" channel-update "$tool".php.net
2020-01-27 21:36:04 +07:00
done
pecl_config="true"
fi
}
2020-05-27 17:53:52 +07:00
# Fuction to get the PECL version of an extension.
2020-01-30 13:33:30 +07:00
get_pecl_version() {
extension=$1
2020-07-14 15:42:50 +07:00
stability="$(echo "$2" | grep -m 1 -Eio "(alpha|beta|rc|snapshot)")"
2020-01-30 13:33:30 +07:00
pecl_rest='https://pecl.php.net/rest/r/'
2020-07-23 07:17:11 +07:00
response=$(curl "${curl_opts[@]}" "$pecl_rest$extension"/allreleases.xml)
2020-07-14 15:42:50 +07:00
pecl_version=$(echo "$response" | grep -m 1 -Pio "(\d*\.\d*\.\d*$stability\d*)")
2020-01-30 13:33:30 +07:00
if [ ! "$pecl_version" ]; then
pecl_version=$(echo "$response" | grep -m 1 -Po "(\d*\.\d*\.\d*)")
fi
echo "$pecl_version"
}
2020-07-25 13:57:51 +07:00
# Function to install PECL extensions and accept default options
pecl_install() {
local extension=$1
yes '' | sudo pecl install -f "$extension" >/dev/null 2>&1
}
2020-05-27 17:53:52 +07:00
# Function to check if an extension is loaded.
2020-02-23 20:30:40 +07:00
check_extension() {
2020-03-16 08:23:58 +07:00
extension=$1
if [ "$extension" != "mysql" ]; then
php -m | grep -i -q -w "$extension"
else
php -m | grep -i -q "$extension"
fi
}
2020-05-27 17:53:52 +07:00
# Function to delete extensions.
2020-01-30 13:33:30 +07:00
delete_extension() {
extension=$1
2020-01-30 13:33:30 +07:00
sudo sed -i "/$extension/d" "$ini_file"
sudo sed -i "/$extension/d" "$pecl_file"
sudo rm -rf "$scan_dir"/*"$extension"* >/dev/null 2>&1
2020-01-30 13:33:30 +07:00
sudo rm -rf "$ext_dir"/"$extension".so >/dev/null 2>&1
}
2020-05-27 17:53:52 +07:00
# Function to disable and delete extensions.
2019-12-26 20:01:18 +07:00
remove_extension() {
extension=$1
2020-05-07 04:57:00 +07:00
if check_extension "$extension"; then
if [[ ! "$version" =~ $old_versions ]] && [ -e /etc/php/"$version"/mods-available/"$extension".ini ]; then
sudo phpdismod -v "$version" "$extension" >/dev/null 2>&1
fi
delete_extension "$extension"
(! check_extension "$extension" && add_log "$tick" ":$extension" "Removed") ||
add_log "$cross" ":$extension" "Could not remove $extension on PHP $semver"
else
add_log "$tick" ":$extension" "Could not find $extension on PHP $semver"
2019-12-26 20:01:18 +07:00
fi
2020-01-30 13:33:30 +07:00
}
2020-05-27 17:53:52 +07:00
# Function to enable existing extensions.
2020-03-05 07:28:24 +07:00
enable_extension() {
if ! check_extension "$1" && [ -e "$ext_dir/$1.so" ]; then
echo "$2=$1.so" >>"$pecl_file"
2020-03-05 07:28:24 +07:00
fi
}
2020-05-27 17:53:52 +07:00
# Funcion to add PDO extension.
2020-03-05 07:28:24 +07:00
add_pdo_extension() {
pdo_ext="pdo_$1"
if check_extension "$pdo_ext"; then
add_log "$tick" "$pdo_ext" "Enabled"
else
read -r ext ext_name <<< "$1 $1"
2020-06-19 05:39:13 +07:00
sudo rm -rf "$scan_dir"/*pdo.ini >/dev/null 2>&1
2020-07-19 10:43:03 +07:00
if ! check_extension "pdo" 2>/dev/null; then echo "extension=pdo.so" >> "$ini_file"; fi
2020-03-05 07:28:24 +07:00
if [ "$ext" = "mysql" ]; then
enable_extension "mysqlnd" "extension"
ext_name="mysqli"
2020-05-17 12:49:52 +07:00
elif [ "$ext" = "sqlite" ]; then
read -r ext ext_name <<< "sqlite3 sqlite3"
2020-03-05 07:28:24 +07:00
fi
add_extension "$ext_name" "$apt_install php$version-$ext" "extension" >/dev/null 2>&1
enable_extension "$pdo_ext" "extension"
(check_extension "$pdo_ext" && add_log "$tick" "$pdo_ext" "Enabled") ||
add_log "$cross" "$pdo_ext" "Could not install $pdo_ext on PHP $semver"
fi
}
2020-05-27 17:53:52 +07:00
# Function to add extensions.
2020-02-23 20:30:40 +07:00
add_extension() {
extension=$1
install_command=$2
prefix=$3
2020-03-05 07:28:24 +07:00
enable_extension "$extension" "$prefix"
if check_extension "$extension"; then
2020-02-23 20:30:40 +07:00
add_log "$tick" "$extension" "Enabled"
2020-03-05 07:28:24 +07:00
else
2020-05-27 17:53:52 +07:00
if [[ "$version" =~ 5.[4-5] ]]; then
install_command="update_lists && ${install_command/5\.[4-5]-$extension/5-$extension=$release_version}"
fi
2020-02-23 20:30:40 +07:00
eval "$install_command" >/dev/null 2>&1 ||
2020-07-25 13:57:51 +07:00
(update_lists && eval "$install_command" >/dev/null 2>&1) || pecl_install "$extension"
2020-02-23 20:30:40 +07:00
(check_extension "$extension" && add_log "$tick" "$extension" "Installed and enabled") ||
add_log "$cross" "$extension" "Could not install $extension on PHP $semver"
fi
sudo chmod 777 "$ini_file"
}
2020-05-27 17:53:52 +07:00
# Function to install a PECL version.
2020-01-30 13:33:30 +07:00
add_pecl_extension() {
extension=$1
pecl_version=$2
2020-02-23 20:30:40 +07:00
prefix=$3
2020-07-14 15:42:50 +07:00
if [[ $pecl_version =~ .*(alpha|beta|rc|snapshot).* ]]; then
pecl_version=$(get_pecl_version "$extension" "$pecl_version")
fi
2020-02-23 20:30:40 +07:00
if ! check_extension "$extension" && [ -e "$ext_dir/$extension.so" ]; then
echo "$prefix=$ext_dir/$extension.so" >>"$pecl_file"
2020-02-23 20:30:40 +07:00
fi
ext_version=$(php -r "echo phpversion('$extension');")
if [ "$ext_version" = "$pecl_version" ]; then
add_log "$tick" "$extension" "Enabled"
else
delete_extension "$extension"
(
2020-07-25 13:57:51 +07:00
pecl_install "$extension-$pecl_version" &&
2020-02-23 20:30:40 +07:00
check_extension "$extension" &&
add_log "$tick" "$extension" "Installed and enabled"
) || add_log "$cross" "$extension" "Could not install $extension-$pecl_version on PHP $semver"
fi
2020-01-30 13:33:30 +07:00
}
2020-05-27 17:53:52 +07:00
# Function to pre-release extensions using PECL.
2020-01-30 13:33:30 +07:00
add_unstable_extension() {
extension=$1
stability=$2
prefix=$3
pecl_version=$(get_pecl_version "$extension" "$stability")
2020-02-23 20:30:40 +07:00
add_pecl_extension "$extension" "$pecl_version" "$prefix"
2019-12-27 08:26:49 +07:00
}
2020-06-20 07:55:19 +07:00
# Function to install extension from source
add_extension_from_source() {
extension=$1
repo=$2
release=$3
args=$4
prefix=$5
(
add_devtools
delete_extension "$extension"
2020-07-23 07:17:11 +07:00
curl -o /tmp/"$extension".tar.gz "${curl_opts[@]}" https://github.com/"$repo"/archive/"$release".tar.gz
2020-06-20 07:55:19 +07:00
tar xf /tmp/"$extension".tar.gz -C /tmp
cd /tmp/"$extension-$release" || exit 1
phpize && ./configure "$args" && make && sudo make install
enable_extension "$extension" "$prefix"
) >/dev/null 2>&1
(
check_extension "$extension" && add_log "$tick" "$extension" "Installed and enabled"
) || add_log "$cross" "$extension" "Could not install $extension-$release on PHP $semver"
}
2020-07-07 18:06:32 +07:00
# Function to configure composer
configure_composer() {
tool_path=$1
sudo ln -sf "$tool_path" "$tool_path.phar"
php -r "try {\$p=new Phar('$tool_path.phar', 0);exit(0);} catch(Exception \$e) {exit(1);}"
if [ $? -eq 1 ]; then
add_log "$cross" "composer" "Could not download composer"
exit 1;
fi
composer -q global config process-timeout 0
echo "::add-path::/home/$USER/.composer/vendor/bin"
if [ -n "$COMPOSER_TOKEN" ]; then
composer -q global config github-oauth.github.com "$COMPOSER_TOKEN"
fi
}
2020-05-27 17:53:52 +07:00
# Function to setup a remote tool.
2019-12-27 08:26:49 +07:00
add_tool() {
url=$1
tool=$2
2020-02-22 23:11:40 +07:00
tool_path="$tool_path_dir/$tool"
if [ ! -e "$tool_path" ]; then
rm -rf "$tool_path"
2019-12-27 08:26:49 +07:00
fi
2020-07-23 07:17:11 +07:00
status_code=$(sudo curl -s -w "%{http_code}" -o "$tool_path" "${curl_opts[@]}" "$url")
if [ "$status_code" = "200" ]; then
sudo chmod a+x "$tool_path"
2020-01-30 13:33:30 +07:00
if [ "$tool" = "composer" ]; then
2020-07-07 18:06:32 +07:00
configure_composer "$tool_path"
elif [ "$tool" = "cs2pr" ]; then
sudo sed -i 's/\r$//; s/exit(9)/exit(0)/' "$tool_path"
2020-05-21 05:21:28 +07:00
elif [ "$tool" = "phan" ]; then
add_extension fileinfo "$apt_install php$version-fileinfo" extension >/dev/null 2>&1
add_extension ast "$apt_install php-ast" extension >/dev/null 2>&1
2020-01-30 13:33:30 +07:00
elif [ "$tool" = "phive" ]; then
add_extension curl "$apt_install php$version-curl" extension >/dev/null 2>&1
add_extension mbstring "$apt_install php$version-mbstring" extension >/dev/null 2>&1
add_extension xml "$apt_install php$version-xml" extension >/dev/null 2>&1
2020-02-22 11:36:14 +07:00
elif [ "$tool" = "wp-cli" ]; then
2020-02-22 23:11:40 +07:00
sudo cp -p "$tool_path" "$tool_path_dir"/wp
2020-01-30 13:33:30 +07:00
fi
add_log "$tick" "$tool" "Added"
else
add_log "$cross" "$tool" "Could not setup $tool"
fi
}
2020-05-27 17:53:52 +07:00
# Function to setup a tool using composer.
add_composertool() {
tool=$1
release=$2
prefix=$3
(
2020-01-30 13:33:30 +07:00
composer global require "$prefix$release" >/dev/null 2>&1 &&
add_log "$tick" "$tool" "Added"
) || add_log "$cross" "$tool" "Could not setup $tool"
2019-12-26 20:01:18 +07:00
}
2020-05-27 17:53:52 +07:00
# Function to setup phpize and php-config.
2020-01-17 14:28:28 +07:00
add_devtools() {
if ! [ -e "/usr/bin/phpize$version" ] || ! [ -e "/usr/bin/php-config$version" ]; then
2020-05-27 17:53:52 +07:00
update_lists && $apt_install php"$version"-dev php"$version"-xml >/dev/null 2>&1
2020-01-17 14:28:28 +07:00
fi
sudo update-alternatives --set php-config /usr/bin/php-config"$version" >/dev/null 2>&1
sudo update-alternatives --set phpize /usr/bin/phpize"$version" >/dev/null 2>&1
2020-05-27 17:53:52 +07:00
configure_pecl >/dev/null 2>&1
2020-01-17 14:28:28 +07:00
}
2020-05-27 17:53:52 +07:00
# Function to add blackfire and blackfire-agent.
add_blackfire() {
sudo mkdir -p /var/run/blackfire
2020-07-23 07:17:11 +07:00
sudo curl "${curl_opts[@]}" https://packages.blackfire.io/gpg.key | sudo apt-key add - >/dev/null 2>&1
echo "deb http://packages.blackfire.io/debian any main" | sudo tee /etc/apt/sources.list.d/blackfire.list >/dev/null 2>&1
2020-06-02 23:42:46 +07:00
sudo "$debconf_fix" apt-get update >/dev/null 2>&1
$apt_install blackfire-agent >/dev/null 2>&1
2020-06-02 23:42:46 +07:00
if [[ -n $BLACKFIRE_SERVER_ID ]] && [[ -n $BLACKFIRE_SERVER_TOKEN ]]; then
sudo blackfire-agent --register --server-id="$BLACKFIRE_SERVER_ID" --server-token="$BLACKFIRE_SERVER_TOKEN" >/dev/null 2>&1
sudo /etc/init.d/blackfire-agent restart >/dev/null 2>&1
fi
if [[ -n $BLACKFIRE_CLIENT_ID ]] && [[ -n $BLACKFIRE_CLIENT_TOKEN ]]; then
blackfire config --client-id="$BLACKFIRE_CLIENT_ID" --client-token="$BLACKFIRE_CLIENT_TOKEN" >/dev/null 2>&1
2020-06-02 23:42:46 +07:00
fi
2020-03-02 21:09:18 +07:00
add_log "$tick" "blackfire" "Added"
add_log "$tick" "blackfire-agent" "Added"
}
2020-05-27 17:53:52 +07:00
# Function to setup the nightly build from master branch.
setup_master() {
2020-07-23 07:17:11 +07:00
curl "${curl_opts[@]}" "$github"/php-builder/releases/latest/download/install.sh | bash -s "$runner"
2019-12-19 12:59:27 +07:00
}
2020-05-27 17:53:52 +07:00
# Function to setup PHP 5.3, PHP 5.4 and PHP 5.5.
setup_old_versions() {
2020-07-23 07:17:11 +07:00
curl "${curl_opts[@]}" "$github"/php5-ubuntu/releases/latest/download/install.sh | bash -s "$version"
configure_pecl
release_version=$(php -v | head -n 1 | cut -d' ' -f 2)
}
2020-05-27 17:53:52 +07:00
# Function to add PECL.
2019-12-27 08:26:49 +07:00
add_pecl() {
2020-05-27 17:53:52 +07:00
add_devtools >/dev/null 2>&1
2020-01-30 13:33:30 +07:00
if [ ! -e /usr/bin/pecl ]; then
2020-07-22 13:10:30 +07:00
$apt_install php-pear >/dev/null 2>&1 || update_lists && $apt_install php-pear >/dev/null 2>&1
2020-01-30 13:33:30 +07:00
fi
2020-05-27 17:53:52 +07:00
configure_pecl >/dev/null 2>&1
2019-12-27 08:26:49 +07:00
add_log "$tick" "PECL" "Added"
}
2020-05-27 17:53:52 +07:00
# Function to switch versions of PHP binaries.
switch_version() {
for tool in pear pecl php phar phar.phar php-cgi php-config phpize phpdbg; do
if [ -e "/usr/bin/$tool$version" ]; then
2020-05-27 17:53:52 +07:00
sudo update-alternatives --set $tool /usr/bin/"$tool$version"
fi
done
}
2020-05-27 17:53:52 +07:00
# Function to get PHP version in semver format.
2020-02-21 09:28:19 +07:00
php_semver() {
2020-05-27 17:53:52 +07:00
if [ ! "$version" = "$master_version" ]; then
2020-02-21 09:28:19 +07:00
php"$version" -v | head -n 1 | cut -f 2 -d ' ' | cut -f 1 -d '-'
else
php -v | head -n 1 | cut -f 2 -d ' '
fi
}
2020-05-27 17:53:52 +07:00
# Function to install packaged PHP
add_packaged_php() {
2020-07-22 13:10:30 +07:00
if [ "$runner" = "self-hosted" ] || [ "${use_package_cache:-true}" = "false" ]; then
update_lists
IFS=' ' read -r -a packages <<< "$(echo "cli curl mbstring xml intl" | sed "s/[^ ]*/php$version-&/g")"
$apt_install "${packages[@]}"
else
2020-07-23 07:17:11 +07:00
curl "${curl_opts[@]}" "$github"/php-ubuntu/releases/latest/download/install.sh | bash -s "$version"
2020-07-22 13:10:30 +07:00
fi
2020-05-27 17:53:52 +07:00
}
# Function to update PHP.
2020-02-21 09:28:19 +07:00
update_php() {
initial_version=$(php_semver)
2020-07-22 13:10:30 +07:00
use_package_cache="false"
2020-05-27 17:53:52 +07:00
add_packaged_php
2020-02-21 09:28:19 +07:00
updated_version=$(php_semver)
if [ "$updated_version" != "$initial_version" ]; then
status="Updated to"
else
status="Switched to"
fi
}
2020-05-27 17:53:52 +07:00
# Function to install PHP.
add_php() {
if [ "$version" = "$master_version" ]; then
setup_master
elif [[ "$version" =~ $old_versions ]]; then
setup_old_versions
else
add_packaged_php
fi
status="Installed"
}
# Variables
tick="✓"
cross="✗"
2020-01-27 21:36:04 +07:00
pecl_config="false"
version=$1
2020-05-27 17:53:52 +07:00
master_version="8.0"
old_versions="5.[3-5]"
debconf_fix="DEBIAN_FRONTEND=noninteractive"
2020-05-27 19:44:20 +07:00
github="https://github.com/shivammathur"
apt_install="sudo $debconf_fix apt-fast install -y"
tool_path_dir="/usr/local/bin"
2020-07-23 07:17:11 +07:00
curl_opts=(-sSL --retry 5 --retry-delay 1)
2020-04-03 15:10:11 +07:00
existing_version=$(php-config --version 2>/dev/null | cut -c 1-3)
2020-05-27 17:53:52 +07:00
read_env
if [ "$runner" = "self-hosted" ]; then
2020-06-07 04:11:28 +07:00
if [[ "$version" =~ $old_versions ]]; then
add_log "$cross" "PHP" "PHP $version is not supported on self-hosted runner"
exit 1
else
self_hosted_setup >/dev/null 2>&1
fi
2020-05-27 17:53:52 +07:00
fi
2020-06-07 04:11:28 +07:00
# Setup PHP
step_log "Setup PHP"
sudo mkdir -p /var/run /run/php
if [ "$existing_version" != "$version" ]; then
if [ ! -e "/usr/bin/php$version" ]; then
2020-05-27 17:53:52 +07:00
add_php >/dev/null 2>&1
else
if [ "$update" = "true" ]; then
2020-05-27 17:53:52 +07:00
update_php >/dev/null 2>&1
else
status="Switched to"
fi
fi
2020-05-27 19:44:20 +07:00
if ! [[ "$version" =~ $old_versions ]]; then
2020-05-27 17:53:52 +07:00
switch_version >/dev/null 2>&1
fi
else
if [ "$update" = "true" ]; then
2020-05-27 17:53:52 +07:00
update_php >/dev/null 2>&1
2020-01-30 13:33:30 +07:00
else
status="Found"
2020-05-27 17:53:52 +07:00
if [ "$version" = "$master_version" ]; then
switch_version >/dev/null 2>&1
fi
fi
fi
2020-02-21 09:28:19 +07:00
semver=$(php_semver)
ext_dir=$(php -i | grep "extension_dir => /" | sed -e "s|.*=> s*||")
scan_dir=$(php --ini | grep additional | sed -e "s|.*: s*||")
ini_file=$(php --ini | grep "Loaded Configuration" | sed -e "s|.*:s*||" | sed "s/ //g")
pecl_file="$scan_dir"/99-pecl.ini
2020-05-26 08:11:18 +07:00
echo '' | sudo tee "$pecl_file" >/dev/null 2>&1
sudo chmod 777 "$ini_file" "$pecl_file" "$tool_path_dir"
add_log "$tick" "PHP" "$status PHP $semver"