Skip to content

Commit 5e81a44

Browse files
authored
Merge pull request #745 from sassoftware/shellcheck-mon-scripts
Shellcheck mon scripts
2 parents db76ac5 + bb6e1ff commit 5e81a44

15 files changed

+986
-1002
lines changed

.shellcheckrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22
disable=SC1091
33
# Disables the check for expanding an array, as it predictably gives first element
44
disable=SC2128
5+
# Disables check for var not set in same file
6+
disable=SC2154

logging/bin/deploy_fluentbit_azmonitor.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ log_info "Deploying Fluent Bit (Azure Monitor)"
4141
# Generate yaml file with all container-related keys
4242
generateImageKeysFile "$FB_FULL_IMAGE" "logging/fb/fb_container_image.template"
4343
# Copy imageKeysFile since next call will replace existing one
44-
#shellcheck disable=SC2154
4544
cp "$imageKeysFile" "$TMP_DIR/fb_imagekeysfile.yaml"
4645

4746
generateImageKeysFile "$FB_INITCONTAINER_FULL_IMAGE" "logging/fb/fb_initcontainer_image.template" "" "true"

logging/bin/deploy_fluentbit_k8sevents_opensearch.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ versionstring="$(get_helm_versionstring "$FLUENTBIT_HELM_CHART_VERSION")"
9494
log_debug "Installing Helm chart from artifact [$chart2install]"
9595

9696
# Deploy Fluent Bit via Helm chart
97-
#shellcheck disable=SC2154,SC2086
97+
#shellcheck disable=SC2086
9898
helm $helmDebug upgrade --install --namespace "$LOG_NS" v4m-fb-events \
9999
$versionstring \
100100
--values "$imageKeysFile" \

logging/bin/deploy_fluentbit_opensearch.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ fi
7474
#Generate yaml file with all container-related keys
7575
generateImageKeysFile "$FB_FULL_IMAGE" "logging/fb/fb_container_image.template"
7676
#Copy imageKeysFile since next call will replace existing one
77-
#shellcheck disable=SC2154
7877
cp "$imageKeysFile" "$TMP_DIR/fb_imagekeysfile.yaml"
7978

8079
generateImageKeysFile "$FB_INITCONTAINER_FULL_IMAGE" "logging/fb/fb_initcontainer_image.template" "" "true"

monitoring/bin/change_grafana_admin_password.sh

Lines changed: 96 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -3,140 +3,145 @@
33
# Copyright © 2022, SAS Institute Inc., Cary, NC, USA. All Rights Reserved.
44
# SPDX-License-Identifier: Apache-2.0
55

6-
cd "$(dirname $BASH_SOURCE)/../.."
6+
cd "$(dirname "$BASH_SOURCE")/../.." || exit 1
77
source monitoring/bin/common.sh
88

9-
this_script=`basename "$0"`
9+
this_script=$(basename "$0")
1010

1111
function show_usage {
12-
log_message "Usage: $this_script [--password PASSWORD --namespace NAMESPACE --tenant TENANT]"
13-
log_message ""
14-
log_message "Changes the password of the Grafana admin user."
15-
log_message ""
16-
log_message "To change the Grafana admin user at the cluster level, you need to provide"
17-
log_message "the following argument:"
18-
log_message " -p, --password PASSWORD - The new password you want to use."
19-
log_message ""
20-
log_message "To change the Grafana admin user at the tenant level, you need"
21-
log_message "to provide the following arguments:"
22-
log_message " -ns, --namespace NAMESPACE - The namespace where the SAS Viya tenant resides."
23-
log_message " -t, --tenant TENANT - The tenant whose Grafana admin password you want to change."
24-
log_message " -p, --password PASSWORD - The new password you want to use."
25-
log_message ""
12+
log_message "Usage: $this_script [--password PASSWORD --namespace NAMESPACE --tenant TENANT]"
13+
log_message ""
14+
log_message "Changes the password of the Grafana admin user."
15+
log_message ""
16+
log_message "To change the Grafana admin user at the cluster level, you need to provide"
17+
log_message "the following argument:"
18+
log_message " -p, --password PASSWORD - The new password you want to use."
19+
log_message ""
20+
log_message "To change the Grafana admin user at the tenant level, you need"
21+
log_message "to provide the following arguments:"
22+
log_message " -ns, --namespace NAMESPACE - The namespace where the SAS Viya tenant resides."
23+
log_message " -t, --tenant TENANT - The tenant whose Grafana admin password you want to change."
24+
log_message " -p, --password PASSWORD - The new password you want to use."
25+
log_message ""
2626
}
2727

2828
# Assigning passed in parameters as variables for the script:
2929
POS_PARMS=""
30+
tenantNS=""
31+
tenant=""
32+
password=""
3033

3134
# Setting passed in variables:
32-
while (( "$#" )); do
33-
case "$1" in
34-
-ns|--namespace)
35-
if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then
36-
tenantNS=$2
37-
shift 2
38-
else
39-
log_error "A value for parameter [NAMESPACE] has not been provided." >&2
35+
while (("$#")); do
36+
case "$1" in
37+
-ns | --namespace)
38+
if [ -n "$2" ] && [ "${2:0:1}" != "-" ]; then
39+
tenantNS=$2
40+
shift 2
41+
else
42+
log_error "A value for parameter [NAMESPACE] has not been provided." >&2
43+
show_usage
44+
exit 2
45+
fi
46+
;;
47+
-t | --tenant)
48+
if [ -n "$2" ] && [ "${2:0:1}" != "-" ]; then
49+
tenant=$2
50+
shift 2
51+
else
52+
log_error "A value for parameter [TENANT] has not been provided." >&2
53+
show_usage
54+
exit 2
55+
fi
56+
;;
57+
-p | --password)
58+
if [ -n "$2" ] && [ "${2:0:1}" != "-" ]; then
59+
password=$2
60+
shift 2
61+
else
62+
log_error "A value for parameter [PASSWORD] has not been provided." >&2
63+
show_usage
64+
exit 2
65+
fi
66+
;;
67+
-h | --help)
4068
show_usage
41-
exit 2
42-
fi
43-
;;
44-
-t|--tenant)
45-
if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then
46-
tenant=$2
47-
shift 2
48-
else
49-
log_error "A value for parameter [TENANT] has not been provided." >&2
69+
exit 0
70+
;;
71+
--*=*) # handle --arg=value style args
72+
log_error "Unsupported flag $1" >&2
5073
show_usage
51-
exit 2
52-
fi
53-
;;
54-
-p|--password)
55-
if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then
56-
password=$2
57-
shift 2
58-
else
59-
log_error "A value for parameter [PASSWORD] has not been provided." >&2
74+
exit 1
75+
;;
76+
-*) # handle other flags
77+
log_error "Unsupported flag $1" >&2
6078
show_usage
61-
exit 2
62-
fi
63-
;;
64-
-h|--help)
65-
show_usage
66-
exit
67-
;;
68-
-*|--*=) # unsupported flags
69-
log_error "Unsupported flag $1" >&2
70-
show_usage
71-
exit 1
72-
;;
79+
exit 1
80+
;;
7381
*) # preserve positional arguments
74-
POS_PARMS="$POS_PARMS $1"
75-
shift
76-
;;
77-
esac
82+
POS_PARMS="$POS_PARMS $1"
83+
shift
84+
;;
85+
esac
7886
done
7987

8088
# Convert namespace and tenant to all lower-case
81-
tenantNS=$(echo "$tenantNS"| tr '[:upper:]' '[:lower:]')
82-
tenant=$(echo "$tenant"| tr '[:upper:]' '[:lower:]')
89+
tenantNS=$(echo "$tenantNS" | tr '[:upper:]' '[:lower:]')
90+
tenant=$(echo "$tenant" | tr '[:upper:]' '[:lower:]')
8391

8492
# Check for parameters - set with cluster or tenant
8593
if [ -z "$tenantNS" ] && [ -z "$tenant" ]; then
86-
cluster="true"
87-
namespace=$MON_NS
88-
grafanaInstance="v4m-grafana"
94+
cluster="true"
95+
namespace=$MON_NS
96+
grafanaInstance="v4m-grafana"
8997
elif [ -n "$tenantNS" ] && [ -n "$tenant" ]; then
90-
cluster="false"
91-
namespace=$tenantNS
92-
grafanaInstance="v4m-grafana-${tenant}"
98+
cluster="false"
99+
namespace=$tenantNS
100+
grafanaInstance="v4m-grafana-${tenant}"
93101
else
94-
log_error "Both a [NAMESPACE] and a [TENANT] are required in order to change the Grafana admin password.";
95-
exit 1
102+
log_error "Both a [NAMESPACE] and a [TENANT] are required in order to change the Grafana admin password."
103+
exit 1
96104
fi
97105

98106
# Check and make sure that a password was provided.
99-
if [ -z $password ]; then
100-
log_error "A value for parameter [PASSWORD] has not been provided"
101-
exit 1
102-
fi
107+
if [ -z "$password" ]; then
108+
log_error "A value for parameter [PASSWORD] has not been provided"
109+
exit 1
110+
fi
103111

104112
if [ "$cluster" == "true" ]; then
105-
grafanaPod="$(kubectl get pods -n $namespace -l app.kubernetes.io/name=grafana --template='{{range .items}}{{.metadata.name}}{{end}}')"
113+
grafanaPod=$(kubectl get pods -n "$namespace" -l app.kubernetes.io/name=grafana --template='{{range .items}}{{.metadata.name}}{{end}}')
106114
else
107-
grafanaPod="$(kubectl get pods -n $namespace -l app.kubernetes.io/name=grafana -l app.kubernetes.io/instance=$grafanaInstance --template='{{range .items}}{{.metadata.name}}{{end}}')"
115+
grafanaPod=$(kubectl get pods -n "$namespace" -l app.kubernetes.io/name=grafana -l app.kubernetes.io/instance="$grafanaInstance" --template='{{range .items}}{{.metadata.name}}{{end}}')
108116
fi
109117

110118
# Error out if a Grafana pod has not been found
111-
if [ -z $grafanaPod ]; then
112-
log_error "Unable to update Grafana password."
113-
log_error "No Grafana pods were available to update"
114-
exit 1
119+
if [ -z "$grafanaPod" ]; then
120+
log_error "Unable to update Grafana password."
121+
log_error "No Grafana pods were available to update"
122+
exit 1
115123
fi
116124

117125
# Changes the admin password using the Grafana CLI
118126
log_info "Updating the admin password using Grafana CLI"
119-
kubectl exec -n $namespace $grafanaPod -c grafana -- bin/grafana-cli admin reset-admin-password $password
120-
121-
# Exit out of the script if the Grafana CLI call fails
122-
if (( $? != 0 )); then
123-
log_error "An error occurred when updating the password"
124-
exit 1
127+
if ! kubectl exec -n "$namespace" "$grafanaPod" -c grafana -- bin/grafana-cli admin reset-admin-password "$password"; then
128+
log_error "An error occurred when updating the password"
129+
exit 1
125130
fi
126131

127132
# Patch new password in Kubernetes
128-
encryptedPassword="$(echo -n "$password" | base64)"
133+
encryptedPassword=$(echo -n "$password" | base64)
129134
log_info "Updating Grafana secret with the new password"
130-
kubectl -n $namespace patch secret $grafanaInstance --type='json' -p="[{'op' : 'replace' ,'path' : '/data/admin-password' ,'value' : '$encryptedPassword'}]"
135+
kubectl -n "$namespace" patch secret "$grafanaInstance" --type='json' -p="[{'op' : 'replace' ,'path' : '/data/admin-password' ,'value' : '$encryptedPassword'}]"
131136

132137
# Restart Grafana pods and wait for them to restart
133-
log_info "Grafana admin password has been updated. Restarting Grafana pods to apply the change"
138+
log_info "Grafana admin password has been updated. Restarting Grafana pods to apply the change"
134139
if [ "$cluster" == "true" ]; then
135-
kubectl delete pods -n $namespace -l "app.kubernetes.io/instance=v4m-prometheus-operator" -l "app.kubernetes.io/name=grafana"
136-
kubectl -n $namespace wait pods --selector "app.kubernetes.io/instance=v4m-prometheus-operator","app.kubernetes.io/name=grafana" --for condition=Ready --timeout=2m
140+
kubectl delete pods -n "$namespace" -l "app.kubernetes.io/instance=v4m-prometheus-operator" -l "app.kubernetes.io/name=grafana"
141+
kubectl -n "$namespace" wait pods --selector "app.kubernetes.io/instance=v4m-prometheus-operator,app.kubernetes.io/name=grafana" --for condition=Ready --timeout=2m
137142
log_info "Grafana password has been successfully changed."
138143
else
139-
kubectl delete pods -n $namespace -l "app.kubernetes.io/instance=$grafanaInstance"
140-
kubectl -n $namespace wait pods --selector app.kubernetes.io/instance=$grafanaInstance --for condition=Ready --timeout=2m
144+
kubectl delete pods -n "$namespace" -l "app.kubernetes.io/instance=$grafanaInstance"
145+
kubectl -n "$namespace" wait pods --selector "app.kubernetes.io/instance=$grafanaInstance" --for condition=Ready --timeout=2m
141146
log_info "Grafana admin password has been successfully changed for [$tenantNS/$tenant]."
142147
fi

monitoring/bin/common.sh

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,44 @@
1+
#! /bin/bash
2+
13
# Copyright © 2020, SAS Institute Inc., Cary, NC, USA. All Rights Reserved.
24
# SPDX-License-Identifier: Apache-2.0
35

46
# This file is not marked as executable as it is intended to be sourced
57
# Current directory must be the root directory of the repo
68

79
if [ "$SAS_MONITORING_COMMON_SOURCED" = "" ]; then
8-
source bin/common.sh
9-
10-
if [ -f "$USER_DIR/monitoring/user.env" ]; then
11-
userEnv=$(grep -v '^[[:blank:]]*$' $USER_DIR/monitoring/user.env | grep -v '^#' | xargs)
12-
log_verbose "Loading user environment file: $USER_DIR/monitoring/user.env"
13-
if [ "$userEnv" ]; then
14-
export $userEnv
15-
fi
16-
fi
10+
source bin/common.sh
1711

18-
export MON_NS="${MON_NS:-monitoring}"
19-
export TLS_ENABLE="${MON_TLS_ENABLE:-${TLS_ENABLE:-true}}"
12+
if [ -f "$USER_DIR/monitoring/user.env" ]; then
13+
userEnv=$(grep -v '^[[:blank:]]*$' "$USER_DIR/monitoring/user.env" | grep -v '^#' | xargs)
14+
log_verbose "Loading user environment file: $USER_DIR/monitoring/user.env"
15+
if [ -n "$userEnv" ]; then
16+
# shellcheck disable=SC2086,SC2163
17+
export $userEnv
18+
fi
19+
fi
2020

21-
if [ "$AIRGAP_DEPLOYMENT" == "true" ]; then
21+
export MON_NS="${MON_NS:-monitoring}"
22+
export TLS_ENABLE="${MON_TLS_ENABLE:-${TLS_ENABLE:-true}}"
2223

23-
#Special processing to handle Viya-level deployment script
24-
if [ `basename "$0"` == "deploy_monitoring_viya.sh" ]; then
25-
V4M_NS="$VIYA_NS"
26-
else
27-
export V4M_NS=$MON_NS
28-
fi
24+
if [ "$AIRGAP_DEPLOYMENT" == "true" ]; then
2925

30-
source bin/airgap-include.sh
26+
#Special processing to handle Viya-level deployment script
27+
if [ "$(basename "$0")" == "deploy_monitoring_viya.sh" ]; then
28+
V4M_NS="$VIYA_NS"
29+
else
30+
export V4M_NS=$MON_NS
31+
fi
3132

32-
fi
33+
source bin/airgap-include.sh
3334

34-
#(Re)Set V4M_NS to point to "monitoring" namespace
35-
export V4M_NS=$MON_NS
35+
fi
3636

37-
source bin/version-include.sh
38-
export SAS_MONITORING_COMMON_SOURCED=true
37+
#(Re)Set V4M_NS to point to "monitoring" namespace
38+
export V4M_NS=$MON_NS
3939

40+
source bin/version-include.sh
41+
export SAS_MONITORING_COMMON_SOURCED=true
4042

4143
fi
4244
echo ""

0 commit comments

Comments
 (0)