|
3 | 3 | # Copyright © 2022, SAS Institute Inc., Cary, NC, USA. All Rights Reserved.
|
4 | 4 | # SPDX-License-Identifier: Apache-2.0
|
5 | 5 |
|
6 |
| -cd "$(dirname $BASH_SOURCE)/../.." |
| 6 | +cd "$(dirname "$BASH_SOURCE")/../.." || exit 1 |
7 | 7 | source monitoring/bin/common.sh
|
8 | 8 |
|
9 |
| -this_script=`basename "$0"` |
| 9 | +this_script=$(basename "$0") |
10 | 10 |
|
11 | 11 | 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 "" |
26 | 26 | }
|
27 | 27 |
|
28 | 28 | # Assigning passed in parameters as variables for the script:
|
29 | 29 | POS_PARMS=""
|
| 30 | +tenantNS="" |
| 31 | +tenant="" |
| 32 | +password="" |
30 | 33 |
|
31 | 34 | # 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) |
40 | 68 | 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 |
50 | 73 | 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 |
60 | 78 | 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 | + ;; |
73 | 81 | *) # 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 |
78 | 86 | done
|
79 | 87 |
|
80 | 88 | # 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:]') |
83 | 91 |
|
84 | 92 | # Check for parameters - set with cluster or tenant
|
85 | 93 | 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" |
89 | 97 | 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}" |
93 | 101 | 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 |
96 | 104 | fi
|
97 | 105 |
|
98 | 106 | # 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 |
103 | 111 |
|
104 | 112 | 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}}') |
106 | 114 | 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}}') |
108 | 116 | fi
|
109 | 117 |
|
110 | 118 | # 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 |
115 | 123 | fi
|
116 | 124 |
|
117 | 125 | # Changes the admin password using the Grafana CLI
|
118 | 126 | 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 |
125 | 130 | fi
|
126 | 131 |
|
127 | 132 | # Patch new password in Kubernetes
|
128 |
| -encryptedPassword="$(echo -n "$password" | base64)" |
| 133 | +encryptedPassword=$(echo -n "$password" | base64) |
129 | 134 | 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'}]" |
131 | 136 |
|
132 | 137 | # 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" |
134 | 139 | 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 |
137 | 142 | log_info "Grafana password has been successfully changed."
|
138 | 143 | 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 |
141 | 146 | log_info "Grafana admin password has been successfully changed for [$tenantNS/$tenant]."
|
142 | 147 | fi
|
0 commit comments