setup-php/src/scripts/linux.sh

287 lines
8.7 KiB
Bash
Raw Normal View History

# Function to log start of a operation
2019-10-17 01:41:13 +05:30
step_log() {
message=$1
printf "\n\033[90;1m==> \033[0m\033[37;1m%s\033[0m\n" "$message"
}
# Function to log result of a operation
2019-10-17 01:41:13 +05:30
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 11:29:27 +05:30
2020-07-07 16:36:32 +05:30
# Function to backup and cleanup package lists.
cleanup_lists() {
if [ ! -e /etc/apt/sources.list.d.save ]; then
2020-07-20 20:57:09 +05:30
sudo mv /etc/apt/sources.list.d /etc/apt/sources.list.d.save || true
2020-07-07 16:36:32 +05:30
sudo mkdir /etc/apt/sources.list.d
2020-07-20 20:57:09 +05:30
sudo mv /etc/apt/sources.list.d.save/*ondrej*.list /etc/apt/sources.list.d/ || true
2020-07-07 16:36:32 +05:30
trap "sudo mv /etc/apt/sources.list.d.save/*.list /etc/apt/sources.list.d/" exit
fi
}
# Function to update php ppa
update_lists() {
if [ "$lists_updated" = "false" ]; then
2020-07-20 20:57:09 +05:30
cleanup_lists >/dev/null 2>&1
2020-03-20 19:01:35 +05:30
sudo "$debconf_fix" apt-get update >/dev/null 2>&1
fi
}
2020-05-05 14:57:36 +05:30
# Function to configure PECL
2020-01-27 20:06:04 +05:30
configure_pecl() {
if [ "$pecl_config" = "false" ] && [ -e /usr/bin/pecl ]; then
for tool in pear pecl; do
2020-05-13 20:48:18 +05:30
sudo "$tool" config-set php_ini "$pecl_file" >/dev/null 2>&1
2020-01-30 12:03:30 +05:30
sudo "$tool" config-set auto_discover 1 >/dev/null 2>&1
sudo "$tool" channel-update "$tool".php.net >/dev/null 2>&1
2020-01-27 20:06:04 +05:30
done
pecl_config="true"
fi
}
2020-01-30 12:03:30 +05:30
# Fuction to get the PECL version
get_pecl_version() {
extension=$1
2020-07-14 14:12:50 +05:30
stability="$(echo "$2" | grep -m 1 -Eio "(alpha|beta|rc|snapshot)")"
2020-01-30 12:03:30 +05:30
pecl_rest='https://pecl.php.net/rest/r/'
response=$(curl -q -sSL "$pecl_rest$extension"/allreleases.xml)
2020-07-14 14:12:50 +05:30
pecl_version=$(echo "$response" | grep -m 1 -Pio "(\d*\.\d*\.\d*$stability\d*)")
2020-01-30 12:03:30 +05:30
if [ ! "$pecl_version" ]; then
pecl_version=$(echo "$response" | grep -m 1 -Po "(\d*\.\d*\.\d*)")
fi
echo "$pecl_version"
}
2020-03-01 06:20:48 +05:30
# Function to test if extension is loaded
check_extension() {
2019-12-19 11:29:27 +05:30
extension=$1
2020-03-16 06:38:50 +05:30
if [ "$extension" != "mysql" ]; then
php -m | grep -i -q -w "$extension"
else
php -m | grep -i -q "$extension"
fi
}
2020-01-30 12:03:30 +05:30
# Function to delete extensions
delete_extension() {
extension=$1
2020-01-30 12:03:30 +05:30
sudo sed -i "/$extension/d" "$ini_file"
2020-05-13 20:48:18 +05:30
sudo sed -i "/$extension/d" "$pecl_file"
2020-03-01 06:20:48 +05:30
sudo rm -rf "$scan_dir"/*"$extension"* >/dev/null 2>&1
2020-01-30 12:03:30 +05:30
sudo rm -rf "$ext_dir"/"$extension".so >/dev/null 2>&1
}
2020-01-30 12:03:30 +05:30
# Function to disable and delete extensions
2019-12-26 18:31:18 +05:30
remove_extension() {
extension=$1
2019-12-27 06:56:49 +05:30
if [ -e /etc/php/"$version"/mods-available/"$extension".ini ]; then
sudo phpdismod -v "$version" "$extension"
2019-12-26 18:31:18 +05:30
fi
2020-01-30 12:03:30 +05:30
delete_extension "$extension"
}
2020-03-01 06:20:48 +05:30
# Function to setup extensions
add_extension() {
extension=$1
install_command=$2
prefix=$3
if ! check_extension "$extension" && [ -e "$ext_dir/$extension.so" ]; then
echo "$prefix=$extension.so" >>"$ini_file" && add_log "$tick" "$extension" "Enabled"
elif check_extension "$extension"; then
add_log "$tick" "$extension" "Enabled"
elif ! check_extension "$extension"; then
eval "$install_command" >/dev/null 2>&1 ||
(update_lists && eval "$install_command" >/dev/null 2>&1) ||
2020-03-01 06:20:48 +05:30
sudo pecl install -f "$extension" >/dev/null 2>&1
(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-01-30 12:03:30 +05:30
# Function to install a PECL version
add_pecl_extension() {
extension=$1
pecl_version=$2
2020-03-01 06:20:48 +05:30
prefix=$3
2020-07-14 14:12:50 +05:30
if [[ $pecl_version =~ .*(alpha|beta|rc|snapshot).* ]]; then
pecl_version=$(get_pecl_version "$extension" "$pecl_version")
fi
2020-03-01 06:20:48 +05:30
if ! check_extension "$extension" && [ -e "$ext_dir/$extension.so" ]; then
2020-05-13 20:48:18 +05:30
echo "$prefix=$ext_dir/$extension.so" >>"$pecl_file"
2020-03-01 06:20:48 +05:30
fi
ext_version=$(php -r "echo phpversion('$extension');")
if [ "$ext_version" = "$pecl_version" ]; then
add_log "$tick" "$extension" "Enabled"
else
delete_extension "$extension"
(
sudo pecl install -f "$extension-$pecl_version" >/dev/null 2>&1 &&
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 12:03:30 +05:30
}
# Function to pre-release extensions using PECL
add_unstable_extension() {
extension=$1
stability=$2
prefix=$3
pecl_version=$(get_pecl_version "$extension" "$stability")
2020-03-01 06:20:48 +05:30
add_pecl_extension "$extension" "$pecl_version" "$prefix"
2019-12-27 06:56:49 +05:30
}
2020-07-07 16:36:32 +05:30
# 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
}
# Function to setup a remote tool.
2019-12-27 06:56:49 +05:30
add_tool() {
url=$1
tool=$2
2020-03-01 06:20:48 +05:30
tool_path="$tool_path_dir/$tool"
if [ ! -e "$tool_path" ]; then
rm -rf "$tool_path"
2019-12-27 06:56:49 +05:30
fi
status_code=$(sudo curl -s -w "%{http_code}" -o "$tool_path" -L "$url")
if [ "$status_code" = "200" ]; then
sudo chmod a+x "$tool_path"
2020-01-30 12:03:30 +05:30
if [ "$tool" = "composer" ]; then
2020-07-07 16:36:32 +05:30
configure_composer "$tool_path"
elif [ "$tool" = "cs2pr" ]; then
sudo sed -i 's/\r$//; s/exit(9)/exit(0)/' "$tool_path"
2020-01-30 12:03:30 +05:30
elif [ "$tool" = "phive" ]; then
2020-03-01 06:20:48 +05:30
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-01-30 12:03:30 +05:30
fi
add_log "$tick" "$tool" "Added"
else
add_log "$cross" "$tool" "Could not setup $tool"
fi
}
2020-01-30 12:03:30 +05:30
# Function to setup a tool using composer
2020-03-01 06:20:48 +05:30
add_composertool() {
tool=$1
release=$2
prefix=$3
(
2020-01-30 12:03:30 +05:30
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 18:31:18 +05:30
}
2020-01-17 12:58:28 +05:30
# Function to setup phpize and php-config
add_devtools() {
if ! [ -e "/usr/bin/phpize$version" ] || ! [ -e "/usr/bin/php-config$version" ]; then
update_lists && $apt_install php"$version"-dev php"$version"-xml >/dev/null 2>&1
2020-01-17 12:58:28 +05:30
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-01-27 20:06:04 +05:30
configure_pecl
2020-01-17 12:58:28 +05:30
}
# Function to setup the nightly build from master branch
setup_master() {
curl -sSL https://github.com/shivammathur/php-builder/releases/latest/download/install.sh | bash -s "github"
2019-12-19 11:29:27 +05:30
}
# Function to setup PECL
2019-12-27 06:56:49 +05:30
add_pecl() {
2020-01-17 12:58:28 +05:30
add_devtools
2020-01-30 12:03:30 +05:30
if [ ! -e /usr/bin/pecl ]; then
$apt_install php-pear >/dev/null 2>&1
fi
2020-01-27 20:06:04 +05:30
configure_pecl
2019-12-27 06:56:49 +05:30
add_log "$tick" "PECL" "Added"
}
# 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
sudo update-alternatives --set $tool /usr/bin/"$tool$version"
fi
done
}
2020-03-01 06:20:48 +05:30
# Function to get PHP version in semver format
php_semver() {
if [ ! "$version" = "8.0" ]; then
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
}
# Variables
tick="✓"
cross="✗"
lists_updated="false"
2020-01-27 20:06:04 +05:30
pecl_config="false"
version=$1
2020-03-01 06:20:48 +05:30
debconf_fix="DEBIAN_FRONTEND=noninteractive"
apt_install="sudo $debconf_fix apt-fast install -y"
tool_path_dir="/usr/local/bin"
2020-05-13 20:36:57 +05:30
existing_version=$(php-config --version 2>/dev/null | cut -c 1-3)
# Setup PHP
2019-12-27 06:56:49 +05:30
step_log "Setup PHP"
2020-05-13 20:48:18 +05:30
sudo mkdir -p /var/run /run/php
2020-07-03 15:18:11 +05:30
. /etc/lsb-release
if [ "$DISTRIB_RELEASE" = "20.04" ]; then
2020-07-20 17:20:16 +05:30
if ! apt-cache policy | grep -q ondrej/php; then
2020-07-20 20:57:09 +05:30
cleanup_lists >/dev/null 2>&1
LC_ALL=C.UTF-8 sudo apt-add-repository ppa:ondrej/php -y >/dev/null 2>&1
2020-07-20 17:20:16 +05:30
fi
2020-07-03 15:18:11 +05:30
fi
if [ "$existing_version" != "$version" ]; then
if [ ! -e "/usr/bin/php$version" ]; then
2020-01-30 12:03:30 +05:30
if [ "$version" = "8.0" ]; then
setup_master >/dev/null 2>&1
else
update_lists
2020-07-20 17:03:29 +05:30
IFS=' ' read -r -a packages <<< "$(echo "cli curl mbstring xml intl" | sed "s/[^ ]*/php$version-&/g")"
2020-07-20 20:57:09 +05:30
$apt_install "${packages[@]}" >/dev/null 2>&1
fi
2020-03-01 06:20:48 +05:30
status="Installed"
else
2020-03-01 06:20:48 +05:30
status="Switched to"
fi
switch_version >/dev/null 2>&1
else
2020-03-01 06:20:48 +05:30
status="Found"
2019-10-17 01:41:13 +05:30
fi
2020-03-01 06:20:48 +05:30
semver=$(php_semver)
2020-05-13 20:48:18 +05:30
scan_dir=$(php --ini | grep additional | sed -e "s|.*: s*||")
2019-10-17 01:41:13 +05:30
ini_file=$(php --ini | grep "Loaded Configuration" | sed -e "s|.*:s*||" | sed "s/ //g")
2020-03-01 06:20:48 +05:30
ext_dir=$(php -i | grep "extension_dir => /" | sed -e "s|.*=> s*||")
2020-05-13 20:48:18 +05:30
pecl_file="$scan_dir"/99-pecl.ini
2020-05-26 06:41:18 +05:30
echo '' | sudo tee "$pecl_file" >/dev/null 2>&1
sudo chmod 777 "$ini_file" "$pecl_file" "$tool_path_dir"
2020-03-01 06:20:48 +05:30
add_log "$tick" "PHP" "$status PHP $semver"