|
| 1 | +#!/bin/bash |
| 2 | +# Copyright Broadcom, Inc. All Rights Reserved. |
| 3 | +# SPDX-License-Identifier: APACHE-2.0 |
| 4 | +# |
| 5 | +# Library for managing files |
| 6 | + |
| 7 | +# shellcheck disable=SC1091 |
| 8 | + |
| 9 | +# Load Generic Libraries |
| 10 | +. /opt/bitnami/scripts/libos.sh |
| 11 | + |
| 12 | +# Functions |
| 13 | + |
| 14 | +######################## |
| 15 | +# Replace a regex-matching string in a file |
| 16 | +# Arguments: |
| 17 | +# $1 - filename |
| 18 | +# $2 - match regex |
| 19 | +# $3 - substitute regex |
| 20 | +# $4 - use POSIX regex. Default: true |
| 21 | +# Returns: |
| 22 | +# None |
| 23 | +######################### |
| 24 | +replace_in_file() { |
| 25 | + local filename="${1:?filename is required}" |
| 26 | + local match_regex="${2:?match regex is required}" |
| 27 | + local substitute_regex="${3:?substitute regex is required}" |
| 28 | + local posix_regex=${4:-true} |
| 29 | + |
| 30 | + local result |
| 31 | + |
| 32 | + # We should avoid using 'sed in-place' substitutions |
| 33 | + # 1) They are not compatible with files mounted from ConfigMap(s) |
| 34 | + # 2) We found incompatibility issues with Debian10 and "in-place" substitutions |
| 35 | + local -r del=$'\001' # Use a non-printable character as a 'sed' delimiter to avoid issues |
| 36 | + if [[ $posix_regex = true ]]; then |
| 37 | + result="$(sed -E "s${del}${match_regex}${del}${substitute_regex}${del}g" "$filename")" |
| 38 | + else |
| 39 | + result="$(sed "s${del}${match_regex}${del}${substitute_regex}${del}g" "$filename")" |
| 40 | + fi |
| 41 | + echo "$result" > "$filename" |
| 42 | +} |
| 43 | + |
| 44 | +######################## |
| 45 | +# Replace a regex-matching multiline string in a file |
| 46 | +# Arguments: |
| 47 | +# $1 - filename |
| 48 | +# $2 - match regex |
| 49 | +# $3 - substitute regex |
| 50 | +# Returns: |
| 51 | +# None |
| 52 | +######################### |
| 53 | +replace_in_file_multiline() { |
| 54 | + local filename="${1:?filename is required}" |
| 55 | + local match_regex="${2:?match regex is required}" |
| 56 | + local substitute_regex="${3:?substitute regex is required}" |
| 57 | + |
| 58 | + local result |
| 59 | + local -r del=$'\001' # Use a non-printable character as a 'sed' delimiter to avoid issues |
| 60 | + result="$(perl -pe "BEGIN{undef $/;} s${del}${match_regex}${del}${substitute_regex}${del}sg" "$filename")" |
| 61 | + echo "$result" > "$filename" |
| 62 | +} |
| 63 | + |
| 64 | +######################## |
| 65 | +# Remove a line in a file based on a regex |
| 66 | +# Arguments: |
| 67 | +# $1 - filename |
| 68 | +# $2 - match regex |
| 69 | +# $3 - use POSIX regex. Default: true |
| 70 | +# Returns: |
| 71 | +# None |
| 72 | +######################### |
| 73 | +remove_in_file() { |
| 74 | + local filename="${1:?filename is required}" |
| 75 | + local match_regex="${2:?match regex is required}" |
| 76 | + local posix_regex=${3:-true} |
| 77 | + local result |
| 78 | + |
| 79 | + # We should avoid using 'sed in-place' substitutions |
| 80 | + # 1) They are not compatible with files mounted from ConfigMap(s) |
| 81 | + # 2) We found incompatibility issues with Debian10 and "in-place" substitutions |
| 82 | + if [[ $posix_regex = true ]]; then |
| 83 | + result="$(sed -E "/$match_regex/d" "$filename")" |
| 84 | + else |
| 85 | + result="$(sed "/$match_regex/d" "$filename")" |
| 86 | + fi |
| 87 | + echo "$result" > "$filename" |
| 88 | +} |
| 89 | + |
| 90 | +######################## |
| 91 | +# Appends text after the last line matching a pattern |
| 92 | +# Arguments: |
| 93 | +# $1 - file |
| 94 | +# $2 - match regex |
| 95 | +# $3 - contents to add |
| 96 | +# Returns: |
| 97 | +# None |
| 98 | +######################### |
| 99 | +append_file_after_last_match() { |
| 100 | + local file="${1:?missing file}" |
| 101 | + local match_regex="${2:?missing pattern}" |
| 102 | + local value="${3:?missing value}" |
| 103 | + |
| 104 | + # We read the file in reverse, replace the first match (0,/pattern/s) and then reverse the results again |
| 105 | + result="$(tac "$file" | sed -E "0,/($match_regex)/s||${value}\n\1|" | tac)" |
| 106 | + echo "$result" > "$file" |
| 107 | +} |
| 108 | + |
| 109 | +######################## |
| 110 | +# Wait until certain entry is present in a log file |
| 111 | +# Arguments: |
| 112 | +# $1 - entry to look for |
| 113 | +# $2 - log file |
| 114 | +# $3 - max retries. Default: 12 |
| 115 | +# $4 - sleep between retries (in seconds). Default: 5 |
| 116 | +# Returns: |
| 117 | +# Boolean |
| 118 | +######################### |
| 119 | +wait_for_log_entry() { |
| 120 | + local -r entry="${1:-missing entry}" |
| 121 | + local -r log_file="${2:-missing log file}" |
| 122 | + local -r retries="${3:-12}" |
| 123 | + local -r interval_time="${4:-5}" |
| 124 | + local attempt=0 |
| 125 | + |
| 126 | + check_log_file_for_entry() { |
| 127 | + if ! grep -qE "$entry" "$log_file"; then |
| 128 | + debug "Entry \"${entry}\" still not present in ${log_file} (attempt $((++attempt))/${retries})" |
| 129 | + return 1 |
| 130 | + fi |
| 131 | + } |
| 132 | + debug "Checking that ${log_file} log file contains entry \"${entry}\"" |
| 133 | + if retry_while check_log_file_for_entry "$retries" "$interval_time"; then |
| 134 | + debug "Found entry \"${entry}\" in ${log_file}" |
| 135 | + true |
| 136 | + else |
| 137 | + error "Could not find entry \"${entry}\" in ${log_file} after ${retries} retries" |
| 138 | + debug_execute cat "$log_file" |
| 139 | + return 1 |
| 140 | + fi |
| 141 | +} |
0 commit comments