|
3 | 3 | # Copyright (c) 2026 by Delphix. All rights reserved. |
4 | 4 | # |
5 | 5 | # One-time InfluxDB initialization: creates org, bucket, admin token, |
6 | | -# a read-only token for DCT Smart Proxy, and appends the |
7 | | -# [[outputs.influxdb_v2]] stanza to /etc/telegraf/telegraf.base. |
| 6 | +# a read-only token for DCT Smart Proxy, and writes the |
| 7 | +# [[outputs.influxdb_v2]] stanza to /etc/telegraf/telegraf.outputs.influxdb, |
| 8 | +# which is included by delphix-telegraf-service when INFLUXDB_ENABLED flag exists. |
8 | 9 | # Skips setup if InfluxDB is already initialized. |
9 | 10 | # |
10 | 11 |
|
@@ -100,10 +101,12 @@ if [[ -f "$INFLUXDB_SETUP_STATE_FILE" ]]; then |
100 | 101 | key="${line%%=*}" |
101 | 102 | value="${line#*=}" |
102 | 103 | case "$key" in |
103 | | - ADMIN_TOKEN) ADMIN_TOKEN="$value" ;; |
104 | | - ORG_ID) ORG_ID="$value" ;; |
105 | | - BUCKET_ID) BUCKET_ID="$value" ;; |
| 104 | + ADMIN_TOKEN) ADMIN_TOKEN="$value" ;; |
| 105 | + ORG_ID) ORG_ID="$value" ;; |
| 106 | + BUCKET_ID) BUCKET_ID="$value" ;; |
106 | 107 | INFLUXDB_ADMIN_PASSWORD) INFLUXDB_ADMIN_PASSWORD="$value" ;; |
| 108 | + WRITE_TOKEN) WRITE_TOKEN="$value" ;; |
| 109 | + READ_TOKEN) READ_TOKEN="$value" ;; |
107 | 110 | esac |
108 | 111 | done <"$INFLUXDB_SETUP_STATE_FILE" |
109 | 112 | else |
@@ -134,29 +137,41 @@ else |
134 | 137 | umask "$old_umask" |
135 | 138 | fi |
136 | 139 |
|
137 | | -# |
138 | | -# Create a write-only token for Telegraf. |
139 | | -# |
140 | | -WRITE_TOKEN_RESPONSE=$(influx_post "/api/v2/authorizations" "{ |
141 | | - \"orgID\": \"$ORG_ID\", |
142 | | - \"description\": \"telegraf-write-token\", |
143 | | - \"permissions\": [ |
144 | | - {\"action\": \"write\", \"resource\": {\"type\": \"buckets\", \"id\": \"$BUCKET_ID\", \"orgID\": \"$ORG_ID\"}} |
145 | | - ] |
146 | | -}" "$ADMIN_TOKEN") || exit 1 |
147 | | -WRITE_TOKEN=$(json_field "$WRITE_TOKEN_RESPONSE" "['token']") || exit 1 |
| 140 | +# Token creation is guarded so that on crash-resume (setup state exists but |
| 141 | +# meta file not yet written), we reuse already-created tokens rather than |
| 142 | +# creating orphaned duplicates in InfluxDB on each retry. |
| 143 | +WRITE_TOKEN="${WRITE_TOKEN:-}" |
| 144 | +READ_TOKEN="${READ_TOKEN:-}" |
| 145 | + |
| 146 | +# |
| 147 | +# Create a write-only token for Telegraf (skipped if already persisted in state). |
| 148 | +# |
| 149 | +if [[ -z "$WRITE_TOKEN" ]]; then |
| 150 | + WRITE_TOKEN_RESPONSE=$(influx_post "/api/v2/authorizations" "{ |
| 151 | + \"orgID\": \"$ORG_ID\", |
| 152 | + \"description\": \"telegraf-write-token\", |
| 153 | + \"permissions\": [ |
| 154 | + {\"action\": \"write\", \"resource\": {\"type\": \"buckets\", \"id\": \"$BUCKET_ID\", \"orgID\": \"$ORG_ID\"}} |
| 155 | + ] |
| 156 | + }" "$ADMIN_TOKEN") || exit 1 |
| 157 | + WRITE_TOKEN=$(json_field "$WRITE_TOKEN_RESPONSE" "['token']") || exit 1 |
| 158 | + printf 'WRITE_TOKEN=%s\n' "$WRITE_TOKEN" >>"$INFLUXDB_SETUP_STATE_FILE" |
| 159 | +fi |
148 | 160 |
|
149 | 161 | # |
150 | | -# Create a read-only token for DCT Smart Proxy. |
| 162 | +# Create a read-only token for DCT Smart Proxy (skipped if already persisted in state). |
151 | 163 | # |
152 | | -READ_TOKEN_RESPONSE=$(influx_post "/api/v2/authorizations" "{ |
153 | | - \"orgID\": \"$ORG_ID\", |
154 | | - \"description\": \"dct-read-token\", |
155 | | - \"permissions\": [ |
156 | | - {\"action\": \"read\", \"resource\": {\"type\": \"buckets\", \"id\": \"$BUCKET_ID\", \"orgID\": \"$ORG_ID\"}} |
157 | | - ] |
158 | | -}" "$ADMIN_TOKEN") || exit 1 |
159 | | -READ_TOKEN=$(json_field "$READ_TOKEN_RESPONSE" "['token']") || exit 1 |
| 164 | +if [[ -z "$READ_TOKEN" ]]; then |
| 165 | + READ_TOKEN_RESPONSE=$(influx_post "/api/v2/authorizations" "{ |
| 166 | + \"orgID\": \"$ORG_ID\", |
| 167 | + \"description\": \"dct-read-token\", |
| 168 | + \"permissions\": [ |
| 169 | + {\"action\": \"read\", \"resource\": {\"type\": \"buckets\", \"id\": \"$BUCKET_ID\", \"orgID\": \"$ORG_ID\"}} |
| 170 | + ] |
| 171 | + }" "$ADMIN_TOKEN") || exit 1 |
| 172 | + READ_TOKEN=$(json_field "$READ_TOKEN_RESPONSE" "['token']") || exit 1 |
| 173 | + printf 'READ_TOKEN=%s\n' "$READ_TOKEN" >>"$INFLUXDB_SETUP_STATE_FILE" |
| 174 | +fi |
160 | 175 |
|
161 | 176 | # |
162 | 177 | # Write the [[outputs.influxdb_v2]] stanza to a dedicated telegraf output file |
|
0 commit comments