forked from InfluxCommunity/influxdb3-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinfluxdb-setup.sh
More file actions
executable file
·124 lines (112 loc) · 3.38 KB
/
Copy pathinfluxdb-setup.sh
File metadata and controls
executable file
·124 lines (112 loc) · 3.38 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
#!/usr/bin/env bash
#
# The MIT License
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
set -e
DEFAULT_INFLUXDB_DATABASE=my-db
INFLUXDB_DATABASE="${INFLUXDB_DATABASE:-$DEFAULT_INFLUXDB_DATABASE}"
#
# Parse command line arguments
#
EXPORT_URL_ENV_VAR=""
EXPORT_DB_ENV_VAR=""
EXPORT_TOKEN_ENV_VAR=""
while [[ $# -gt 0 ]]; do
case $1 in
--export-url-as)
EXPORT_URL_ENV_VAR="$2"
shift 2
;;
--export-db-as)
EXPORT_DB_ENV_VAR="$2"
shift 2
;;
--export-token-as)
EXPORT_TOKEN_ENV_VAR="$2"
shift 2
;;
*)
echo "Unknown option $1"
exit 1
;;
esac
done
#
# Check prerequisites
#
for cmd in curl jq; do
command -v ${cmd} &>/dev/null || { echo "'${cmd}' is not installed"; exit 1; }
done
echo
echo "Wait to start InfluxDB 3.0"
echo
for i in {1..30}; do
if curl -s -o /dev/null -w "%{http_code}" http://localhost:8181/ping | grep -q "401"; then
break
fi
echo "Attempt $i/30: Waiting for InfluxDB to respond with 401..."
sleep 1
done
echo "Done"
echo
echo "Create admin token"
echo
ADMIN_TOKEN=$(curl -s -X POST http://localhost:8181/api/v3/configure/token/admin | jq -r '.token')
if [[ -z "$ADMIN_TOKEN" || "$ADMIN_TOKEN" == "null" ]]; then
echo "Failed to create admin token"
exit 1
fi
echo "ADMIN_TOKEN=$ADMIN_TOKEN"
echo
echo "Test the token"
echo
HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" -H "Authorization: Bearer ${ADMIN_TOKEN}" http://localhost:8181/ping)
if [[ "$HTTP_CODE" != "200" ]]; then
echo "Token test failed with HTTP $HTTP_CODE"
exit 1
fi
echo "Done"
echo
echo "Create database"
echo
HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" -X POST "http://localhost:8181/api/v3/configure/database" \
-H "Authorization: Bearer ${ADMIN_TOKEN}" \
-H "Content-Type: application/json" \
-d "{\"db\":\"${INFLUXDB_DATABASE}\"}")
if [[ "$HTTP_CODE" != "200" ]]; then
echo "Database creation failed with HTTP $HTTP_CODE"
exit 1
fi
echo "Done"
#
# Export results
#
export_var() {
local var_name="$1" var_value="$2"
[[ -n "$var_name" ]] || return
[[ -n "$BASH_ENV" ]] || { echo "\$BASH_ENV not available (not in CircleCI), cannot export variables."; exit 1; }
echo "Exporting $var_name=$var_value"
echo "export $var_name=$var_value" >> "$BASH_ENV"
}
echo
export_var "$EXPORT_URL_ENV_VAR" "http://localhost:8181"
export_var "$EXPORT_DB_ENV_VAR" "$INFLUXDB_DATABASE"
export_var "$EXPORT_TOKEN_ENV_VAR" "$ADMIN_TOKEN"