|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -xe -o pipefail |
| 3 | + |
| 4 | +PHP_versions=(8.1 8.2 8.3) |
| 5 | +libc_variants=(glibc musl) |
| 6 | + |
| 7 | +show_help() { |
| 8 | + echo "Usage: $0 --ext-ver <opentelemetry extension version> --dest-dir <destination directory>" |
| 9 | + echo |
| 10 | + echo "Arguments:" |
| 11 | + echo " <opentelemetry extension version> - opentelemetry PHP extension version to use. This argument is mandatory." |
| 12 | + echo " <destination directory> - Directory to store files for docker image. All existing files in this directory will be deleted. This argument is mandatory." |
| 13 | + echo |
| 14 | + echo "Example:" |
| 15 | + echo " $0 ./files_for_docker_image" |
| 16 | +} |
| 17 | + |
| 18 | +parse_args() { |
| 19 | + while [[ "$#" -gt 0 ]]; do |
| 20 | + case $1 in |
| 21 | + --ext-ver) |
| 22 | + opentelemetry_extension_version="$2" |
| 23 | + shift |
| 24 | + ;; |
| 25 | + --dest-dir) |
| 26 | + destination_directory="$2" |
| 27 | + shift |
| 28 | + ;; |
| 29 | + --help) |
| 30 | + show_help |
| 31 | + exit 0 |
| 32 | + ;; |
| 33 | + *) |
| 34 | + echo "Unknown parameter passed: $1" |
| 35 | + show_help |
| 36 | + exit 1 |
| 37 | + ;; |
| 38 | + esac |
| 39 | + shift |
| 40 | + done |
| 41 | + |
| 42 | + if [ -z "${opentelemetry_extension_version}" ] ; then |
| 43 | + echo "<opentelemetry extension version> argument is missing" |
| 44 | + show_help |
| 45 | + exit 1 |
| 46 | + fi |
| 47 | + if [ -z "${destination_directory}" ] ; then |
| 48 | + echo "<destination directory> argument is missing" |
| 49 | + show_help |
| 50 | + exit 1 |
| 51 | + fi |
| 52 | +} |
| 53 | + |
| 54 | +ensure_dir_exists_and_empty() { |
| 55 | + local dir_to_clean="${1:?}" |
| 56 | + |
| 57 | + if [ -d "${dir_to_clean}" ]; then |
| 58 | + rm -rf "${dir_to_clean}" |
| 59 | + if [ -d "${dir_to_clean}" ]; then |
| 60 | + echo "Directory ${dir_to_clean} still exists. Directory content:" |
| 61 | + ls -l "${dir_to_clean}" |
| 62 | + exit 1 |
| 63 | + fi |
| 64 | + else |
| 65 | + mkdir -p "${dir_to_clean}" |
| 66 | + fi |
| 67 | +} |
| 68 | + |
| 69 | +build_native_binaries_for_PHP_version_libc_variant() { |
| 70 | + local PHP_version="${1:?}" |
| 71 | + local libc_variant="${2:?}" |
| 72 | + local dest_dir_for_current_args |
| 73 | + dest_dir_for_current_args="${destination_directory}/native_binaries/PHP_${PHP_version}_${libc_variant}" |
| 74 | + |
| 75 | + echo "Building extension binaries for PHP version: ${PHP_version} and libc variant: ${libc_variant} to ${dest_dir_for_current_args} ..." |
| 76 | + |
| 77 | + ensure_dir_exists_and_empty "${dest_dir_for_current_args}" |
| 78 | + |
| 79 | + local PHP_docker_image="php:${PHP_version}-cli" |
| 80 | + local install_compiler_command="" |
| 81 | + case "${libc_variant}" in |
| 82 | + glibc) |
| 83 | + ;; |
| 84 | + musl) |
| 85 | + PHP_docker_image="${PHP_docker_image}-alpine" |
| 86 | + install_compiler_command="&& apk update && apk add autoconf build-base" |
| 87 | + ;; |
| 88 | + *) |
| 89 | + echo "Unexpected libc variant: ${libc_variant}" |
| 90 | + exit 1 |
| 91 | + ;; |
| 92 | + esac |
| 93 | + |
| 94 | + local current_user_id |
| 95 | + current_user_id="$(id -u)" |
| 96 | + local current_user_group_id |
| 97 | + current_user_group_id="$(id -g)" |
| 98 | + docker run --rm \ |
| 99 | + -v "${dest_dir_for_current_args}:/dest_dir" \ |
| 100 | + "${PHP_docker_image}" sh -c "\ |
| 101 | + mkdir -p /app && cd /app \ |
| 102 | + ${install_compiler_command} \ |
| 103 | + && pecl install opentelemetry-${opentelemetry_extension_version} \ |
| 104 | + && cp /usr/local/lib/php/extensions/no-debug-non-zts-*/opentelemetry.so /dest_dir/ \ |
| 105 | + && chown -R ${current_user_id}:${current_user_group_id} /dest_dir/" |
| 106 | + |
| 107 | + echo "Built extension binaries for PHP version: ${PHP_version} and libc variant: ${libc_variant}" |
| 108 | +} |
| 109 | + |
| 110 | +build_native_binaries() { |
| 111 | + echo "Building extension binaries..." |
| 112 | + |
| 113 | + for PHP_version in "${PHP_versions[@]}" ; do |
| 114 | + for libc_variant in "${libc_variants[@]}" ; do |
| 115 | + build_native_binaries_for_PHP_version_libc_variant "${PHP_version}" "${libc_variant}" |
| 116 | + done |
| 117 | + done |
| 118 | + |
| 119 | + echo "Built extension binaries" |
| 120 | +} |
| 121 | + |
| 122 | +is_earlier_major_minor_version() { |
| 123 | + local lhs_version="${1:?}" |
| 124 | + local rhs_version="${2:?}" |
| 125 | + local lhs_version_major |
| 126 | + lhs_version_major=$(echo "${lhs_version}" | cut -d. -f1) |
| 127 | + local rhs_version_major |
| 128 | + rhs_version_major=$(echo "${rhs_version}" | cut -d. -f1) |
| 129 | + |
| 130 | + if [ "${lhs_version_major}" -lt "${rhs_version_major}" ]; then |
| 131 | + echo "true" |
| 132 | + return |
| 133 | + fi |
| 134 | + |
| 135 | + if [ "${lhs_version_major}" -gt "${rhs_version_major}" ]; then |
| 136 | + echo "false" |
| 137 | + return |
| 138 | + fi |
| 139 | + |
| 140 | + local lhs_version_minor |
| 141 | + lhs_version_minor=$(echo "${lhs_version}" | cut -d. -f2) |
| 142 | + local rhs_version_minor |
| 143 | + rhs_version_minor=$(echo "${rhs_version}" | cut -d. -f2) |
| 144 | + |
| 145 | + if [ "${lhs_version_minor}" -lt "${rhs_version_minor}" ]; then |
| 146 | + echo "true" |
| 147 | + return |
| 148 | + fi |
| 149 | + |
| 150 | + echo "false" |
| 151 | +} |
| 152 | + |
| 153 | +select_composer_json_for_PHP_version() { |
| 154 | + local PHP_version="${1:?}" |
| 155 | + # |
| 156 | + # Supported instrumentations are different for PHP prior to 8.2 and for PHP 8.2 and later |
| 157 | + # because PHP 8.2 added ability to instrument internal functions |
| 158 | + # |
| 159 | + local is_PHP_version_before_8_2 |
| 160 | + is_PHP_version_before_8_2=$(is_earlier_major_minor_version "${PHP_version}" "8.2") |
| 161 | + if [ "${is_PHP_version_before_8_2}" == "true" ]; then |
| 162 | + echo "composer_for_PHP_before_8.2.json" |
| 163 | + else |
| 164 | + echo "composer_for_PHP_8.2_and_later.json" |
| 165 | + fi |
| 166 | +} |
| 167 | + |
| 168 | +download_PHP_packages_for_PHP_version() { |
| 169 | + local PHP_version="${1:?}" |
| 170 | + local dest_dir_for_current_args |
| 171 | + dest_dir_for_current_args="${destination_directory}/PHP_packages/PHP_${PHP_version}" |
| 172 | + |
| 173 | + echo "Downloading PHP packages for PHP version: ${PHP_version} to ${dest_dir_for_current_args} ..." |
| 174 | + |
| 175 | + ensure_dir_exists_and_empty "${dest_dir_for_current_args}" |
| 176 | + local composer_json_file_name |
| 177 | + composer_json_file_name=$(select_composer_json_for_PHP_version "${PHP_version}") |
| 178 | + local current_user_id |
| 179 | + current_user_id="$(id -u)" |
| 180 | + local current_user_group_id |
| 181 | + current_user_group_id="$(id -g)" |
| 182 | + docker run --rm \ |
| 183 | + -v "${dest_dir_for_current_args}:/app/vendor" \ |
| 184 | + -v "${PWD}/${composer_json_file_name}:/app/composer.json" \ |
| 185 | + -w /app \ |
| 186 | + "php:${PHP_version}"-cli sh -c "\ |
| 187 | + apt-get update && apt-get install -y unzip \ |
| 188 | + && curl -sS https://getcomposer.org/installer | php -- --filename=composer --install-dir=/usr/local/bin \ |
| 189 | + && composer --ignore-platform-req=ext-opentelemetry --no-dev install \ |
| 190 | + && chown -R ${current_user_id}:${current_user_group_id} ./vendor/" |
| 191 | + |
| 192 | + echo "Downloaded PHP packages for PHP version: ${PHP_version} to ${dest_dir_for_current_args}" |
| 193 | +} |
| 194 | + |
| 195 | +download_PHP_packages() { |
| 196 | + echo "Downloading PHP packages..." |
| 197 | + |
| 198 | + for PHP_version in "${PHP_versions[@]}" ; do |
| 199 | + download_PHP_packages_for_PHP_version "${PHP_version}" |
| 200 | + done |
| 201 | + |
| 202 | + echo "Downloaded PHP packages" |
| 203 | +} |
| 204 | + |
| 205 | +main() { |
| 206 | + parse_args "$@" |
| 207 | + |
| 208 | + echo "Preparing files for docker image into directory ${destination_directory} ..." |
| 209 | + |
| 210 | + ensure_dir_exists_and_empty "${destination_directory}" |
| 211 | + build_native_binaries |
| 212 | + download_PHP_packages |
| 213 | + |
| 214 | + echo "Prepared files for docker image into directory ${destination_directory}" |
| 215 | +} |
| 216 | + |
| 217 | +main "$@" |
0 commit comments