-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpost-install.sh
More file actions
executable file
·260 lines (230 loc) · 10.3 KB
/
post-install.sh
File metadata and controls
executable file
·260 lines (230 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#!/usr/bin/env bash
######### Let's support alpine installations
PATH=${PATH}:/usr/local/bin
################################################################################
############################ GLOBAL VARIABLES ##################################
################################################################################
PHP_AGENT_DIR=/opt/opentelemetry/php/distro
EXTENSION_DIR="${PHP_AGENT_DIR}"
EXTENSION_CFG_DIR="${PHP_AGENT_DIR}/etc"
BOOTSTRAP_FILE_PATH="${PHP_AGENT_DIR}/php/bootstrap_php_part.php"
BACKUP_EXTENSION=".agent.bck"
OTEL_INI_FILE_NAME="opentelemetry-distro.ini"
CUSTOM_INI_FILE_NAME="opentelemetry-distro-custom.ini"
################################################################################
########################## FUNCTION CALLS BELOW ################################
################################################################################
################################################################################
#### Function php_command ######################################################
function php_command() {
PHP_BIN=$(command -v php)
${PHP_BIN} -d memory_limit=128M "$@"
}
################################################################################
#### Function php_ini_file_path ################################################
function php_ini_file_path() {
php_command -i \
| grep 'Configuration File (php.ini) Path =>' \
| sed -e 's#Configuration File (php.ini) Path =>##g' \
| head -n 1 \
| awk '{print $1}'
}
################################################################################
#### Function php_api ##########################################################
function php_api() {
php_command -i \
| grep 'PHP API' \
| sed -e 's#.* =>##g' \
| awk '{print $1}'
}
################################################################################
#### Function php_config_d_path ################################################
function php_config_d_path() {
php_command -i \
| grep 'Scan this dir for additional .ini files =>' \
| sed -e 's#Scan this dir for additional .ini files =>##g' \
| head -n 1 \
| awk '{print $1}'
}
################################################################################
#### Function is_extension_enabled #############################################
function is_extension_enabled() {
php_command -m | grep -q 'opentelemetry_distro'
}
################################################################################
#### Function install_conf_d_files #############################################
function install_conf_d_files() {
PHP_CONFIG_D_PATH=$1
INI_FILE_PATH="${EXTENSION_CFG_DIR}/$OTEL_INI_FILE_NAME"
CUSTOM_INI_FILE_PATH="${EXTENSION_CFG_DIR}/${CUSTOM_INI_FILE_NAME}"
generate_configuration_files "${INI_FILE_PATH}" "${CUSTOM_INI_FILE_PATH}"
echo "Configuring ${OTEL_INI_FILE_NAME} for supported SAPI's"
# Detect installed SAPI's
SAPI_DIR=${PHP_CONFIG_D_PATH%/*/conf.d}/
SAPI_CONFIG_DIRS=()
if [ "${PHP_CONFIG_D_PATH}" != "${SAPI_DIR}" ]; then
# CLI
CLI_CONF_D_PATH="${SAPI_DIR}cli/conf.d"
if [ -d "${CLI_CONF_D_PATH}" ]; then
SAPI_CONFIG_DIRS+=("${CLI_CONF_D_PATH}")
fi
# Apache
APACHE_CONF_D_PATH="${SAPI_DIR}apache2/conf.d"
if [ -d "${APACHE_CONF_D_PATH}" ]; then
SAPI_CONFIG_DIRS+=("${APACHE_CONF_D_PATH}")
fi
## FPM
FPM_CONF_D_PATH="${SAPI_DIR}fpm/conf.d"
if [ -d "${FPM_CONF_D_PATH}" ]; then
SAPI_CONFIG_DIRS+=("${FPM_CONF_D_PATH}")
fi
fi
if [ ${#SAPI_CONFIG_DIRS[@]} -eq 0 ]; then
SAPI_CONFIG_DIRS+=("$PHP_CONFIG_D_PATH")
fi
for SAPI_CONFIG_D_PATH in "${SAPI_CONFIG_DIRS[@]}" ; do
echo "Found SAPI config directory: ${SAPI_CONFIG_D_PATH}"
link_file "${INI_FILE_PATH}" "${SAPI_CONFIG_D_PATH}/98-${OTEL_INI_FILE_NAME}"
link_file "${CUSTOM_INI_FILE_PATH}" "${SAPI_CONFIG_D_PATH}/99-${CUSTOM_INI_FILE_NAME}"
done
}
################################################################################
#### Function generate_configuration_files #####################################
function generate_configuration_files() {
INI_FILE_PATH="${1}"
CUSTOM_INI_FILE_PATH="${2}"
## IMPORTANT: This file will be always override if already exists for a
## previous installation.
echo "Creating ${INI_FILE_PATH}"
CONTENT=$(add_extension_configuration)
tee "${INI_FILE_PATH}" <<EOF
; ***** DO NOT EDIT THIS FILE *****
; THIS IS AN AUTO-GENERATED FILE by the OpenTelemetry PHP Distro post-install.sh script
; To overwrite the INI settings for this extension, edit
; the INI file in this directory "${CUSTOM_INI_FILE_PATH}"
[opentelemetry_distro]
${CONTENT}
; END OF AUTO-GENERATED by the OpenTelemetry PHP Distro post-install.sh script
EOF
echo "${INI_FILE_PATH} created"
if [ ! -f "${CUSTOM_INI_FILE_PATH}" ]; then
touch "${CUSTOM_INI_FILE_PATH}"
echo "Created empty ${CUSTOM_INI_FILE_PATH}"
fi
}
################################################################################
#### Function link_file ########################################################
function link_file() {
echo "Linking ${1} to ${2}"
test -f "${2}" && rm "${2}"
ln -s "${1}" "${2}"
}
################################################################################
#### Function add_extension_configuration_to_file ##############################
function add_extension_configuration_to_file() {
CONTENT=$(add_extension_configuration)
## IMPORTANT: The below content is also used in the before-uninstall.sh
## script.
tee -a "$1" <<EOF
; THIS IS AN AUTO-GENERATED FILE by the OpenTelemetry PHP Distro post-install.sh script
${CONTENT}
; END OF AUTO-GENERATED by the OpenTelemetry PHP Distro post-install.sh script
EOF
}
################################################################################
#### Function add_extension_configuration ######################################
function add_extension_configuration() {
cat <<EOF
extension=${EXTENSION_FILE_PATH}
opentelemetry_distro.bootstrap_php_part_file=${BOOTSTRAP_FILE_PATH}
EOF
}
################################################################################
#### Function manual_extension_agent_setup #####################################
function manual_extension_agent_setup() {
echo 'Set up OpenTelemetry PHP distro manually by adding the following to your php.ini file:'
if [ -e "${EXTENSION_FILE_PATH}" ] ; then
echo "extension=${EXTENSION_FILE_PATH}"
echo "opentelemetry_distro.bootstrap_php_part_file=${BOOTSTRAP_FILE_PATH}"
else
echo "extension=/path/to/opentelemetry_php_distro_loader.so"
echo "opentelemetry_distro.bootstrap_php_part_file=/path/to/bootstrap_php_part.php"
fi
}
################################################################################
#### Function agent_extension_not_supported ####################################
function agent_extension_not_supported() {
PHP_API=$(php_api)
echo 'Failed. OpenTelemetry PHP Distro extension not supported for the current PHP API version.'
echo " PHP API => ${PHP_API}"
}
################################################################################
#### Function get_extension_file ###############################################
function get_extension_file() {
echo "${EXTENSION_DIR}/opentelemetry_php_distro_loader.so"
}
################################################################################
#### Function is_php_supported #################################################
function is_php_supported() {
PHP_MAJOR_MINOR=$(php_command -r 'echo PHP_MAJOR_VERSION;').$(php_command -r 'echo PHP_MINOR_VERSION;')
echo "Detected PHP version '${PHP_MAJOR_MINOR}'"
# Make sure list of PHP versions supported by the OpenTelemetry PHP Distro is in sync.
# See the comment in .ci/shared.sh
if [ "${PHP_MAJOR_MINOR}" == "8.1" ] || \
[ "${PHP_MAJOR_MINOR}" == "8.2" ] || \
[ "${PHP_MAJOR_MINOR}" == "8.3" ] || \
[ "${PHP_MAJOR_MINOR}" == "8.4" ]
then
return 0
else
echo 'Failed. The supported PHP versions are 8.1-8.4.'
return 1
fi
}
################################################################################
############################### MAIN ###########################################
################################################################################
echo 'Installing OpenTelemetry PHP Distro'
EXTENSION_FILE_PATH=$(get_extension_file)
PHP_INI_FILE_PATH="$(php_ini_file_path)/php.ini"
PHP_CONFIG_D_PATH="$(php_config_d_path)"
echo "DEBUG: after-install parameter is '$1'"
if ! is_php_supported ; then
echo 'Failed. OpenTelemetry PHP Distro extension is not supported for the existing PHP installation.'
exit 1
fi
if [ -e "${PHP_CONFIG_D_PATH}" ]; then
install_conf_d_files "${PHP_CONFIG_D_PATH}"
else
if [ -e "${PHP_INI_FILE_PATH}" ] ; then
if [ -e "${EXTENSION_FILE_PATH}" ] ; then
if grep -q "${EXTENSION_FILE_PATH}" "${PHP_INI_FILE_PATH}" ; then
echo ' extension configuration already exists for the OpenTelemetry PHP Distro.'
echo ' skipping ... '
else
echo "${PHP_INI_FILE_PATH} has been configured with the OpenTelemetry PHP Distro setup."
cp -fa "${PHP_INI_FILE_PATH}" "${PHP_INI_FILE_PATH}${BACKUP_EXTENSION}"
add_extension_configuration_to_file "${PHP_INI_FILE_PATH}"
fi
else
agent_extension_not_supported
fi
else
if [ -e "${EXTENSION_FILE_PATH}" ] ; then
echo "${PHP_INI_FILE_PATH} has been created with the OpenTelemetry PHP Distro setup."
add_extension_configuration_to_file "${PHP_INI_FILE_PATH}"
else
agent_extension_not_supported
fi
fi
fi
if is_extension_enabled ; then
echo 'Extension enabled successfully for OpenTelemetry PHP Distro'
else
echo 'Failed. OpenTelemetry PHP Distro extension is not enabled'
if [ -e "${PHP_INI_FILE_PATH}${BACKUP_EXTENSION}" ] ; then
echo "Reverted changes in the file ${PHP_INI_FILE_PATH}"
mv -f "${PHP_INI_FILE_PATH}${BACKUP_EXTENSION}" "${PHP_INI_FILE_PATH}"
fi
manual_extension_agent_setup
fi