|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 4 | +# or more contributor license agreements. See the NOTICE file |
| 5 | +# distributed with this work for additional information |
| 6 | +# regarding copyright ownership. The ASF licenses this file |
| 7 | +# to you under the Apache License, Version 2.0 (the |
| 8 | +# "License"); you may not use this file except in compliance |
| 9 | +# with the License. You may obtain a copy of the License at |
| 10 | +# |
| 11 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +# |
| 13 | +# Unless required by applicable law or agreed to in writing, |
| 14 | +# software distributed under the License is distributed on an |
| 15 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 | +# KIND, either express or implied. See the License for the |
| 17 | +# specific language governing permissions and limitations |
| 18 | +# under the License. |
| 19 | + |
| 20 | +set -euo pipefail |
| 21 | + |
| 22 | +HOST_URL="http://localhost:9047" |
| 23 | +NEW_USER_URL="$HOST_URL/apiv2/bootstrap/firstuser" |
| 24 | +LOGIN_URL="$HOST_URL/apiv2/login" |
| 25 | +SQL_URL="$HOST_URL/api/v3/sql" |
| 26 | + |
| 27 | +ADMIN_USER="admin" |
| 28 | +ADMIN_PASSWORD="admin2025" |
| 29 | + |
| 30 | +MAX_WAIT_ATTEMPTS=60 |
| 31 | +WAIT_INTERVAL_SECONDS=5 |
| 32 | + |
| 33 | +# Wait for Dremio to be available. |
| 34 | +attempt=1 |
| 35 | +until status_code="$( |
| 36 | + curl --silent \ |
| 37 | + --output /dev/null \ |
| 38 | + --write-out '%{http_code}' \ |
| 39 | + "$NEW_USER_URL" |
| 40 | +)"; [[ "$status_code" == "200" || "$status_code" == "404" || "$status_code" == "405" ]]; do |
| 41 | + |
| 42 | + if [[ "$attempt" -ge "$MAX_WAIT_ATTEMPTS" ]]; then |
| 43 | + echo "Timed out waiting for Dremio to start." >&2 |
| 44 | + exit 1 |
| 45 | + fi |
| 46 | + |
| 47 | + echo "Waiting for Dremio to start... HTTP $status_code" |
| 48 | + attempt=$((attempt + 1)) |
| 49 | + sleep "$WAIT_INTERVAL_SECONDS" |
| 50 | +done |
| 51 | + |
| 52 | +echo "" |
| 53 | +echo "Creating admin user..." |
| 54 | + |
| 55 | +# Create new admin account. |
| 56 | +curl --fail --silent --show-error \ |
| 57 | + -X PUT "$NEW_USER_URL" \ |
| 58 | + -H "Content-Type: application/json" \ |
| 59 | + -d "$(python3 - <<EOF |
| 60 | +import json |
| 61 | +print(json.dumps({ |
| 62 | + "userName": "$ADMIN_USER", |
| 63 | + "password": "$ADMIN_PASSWORD" |
| 64 | +})) |
| 65 | +EOF |
| 66 | +)" |
| 67 | + |
| 68 | +echo "" |
| 69 | +echo "Created admin user." |
| 70 | + |
| 71 | +echo "Logging in as admin user..." |
| 72 | + |
| 73 | +# Login and capture response body. |
| 74 | +LOGIN_RESPONSE="$(curl --fail --silent --show-error \ |
| 75 | + -X POST "$LOGIN_URL" \ |
| 76 | + -H "Content-Type: application/json" \ |
| 77 | + -d "$(python3 - <<EOF |
| 78 | +import json |
| 79 | +print(json.dumps({ |
| 80 | + "userName": "$ADMIN_USER", |
| 81 | + "password": "$ADMIN_PASSWORD" |
| 82 | +})) |
| 83 | +EOF |
| 84 | +)")" |
| 85 | + |
| 86 | +# Extract token safely using Python JSON parsing. |
| 87 | +TOKEN="$(python3 - <<EOF |
| 88 | +import json |
| 89 | +import sys |
| 90 | +
|
| 91 | +try: |
| 92 | + response = json.loads("""$LOGIN_RESPONSE""") |
| 93 | +except json.JSONDecodeError as exc: |
| 94 | + print(f"Failed to parse login response JSON: {exc}", file=sys.stderr) |
| 95 | + sys.exit(1) |
| 96 | +
|
| 97 | +token = response.get("token") |
| 98 | +
|
| 99 | +if not token: |
| 100 | + print("Login response did not contain a token.", file=sys.stderr) |
| 101 | + sys.exit(1) |
| 102 | +
|
| 103 | +print(token) |
| 104 | +EOF |
| 105 | +)" |
| 106 | + |
| 107 | +SQL_QUERY=" |
| 108 | +Create Table \$scratch.ODBCTest As |
| 109 | + SELECT CAST(2147483647 AS INTEGER) AS sinteger_max, |
| 110 | + CAST(9223372036854775807 AS BIGINT) AS sbigint_max, |
| 111 | + CAST(999999999 AS DECIMAL(38,0)) AS decimal_positive, |
| 112 | + CAST(3.40282347E38 AS FLOAT) AS float_max, |
| 113 | + CAST(1.7976931348623157E308 AS DOUBLE) AS double_max, |
| 114 | + CAST(true AS BOOLEAN) AS bit_true, |
| 115 | + CAST(DATE '9999-12-31' AS DATE) AS date_max, |
| 116 | + CAST(TIME '23:59:59' AS TIME) AS time_max, |
| 117 | + CAST(TIMESTAMP '9999-12-31 23:59:59' AS TIMESTAMP) AS timestamp_max; |
| 118 | +" |
| 119 | + |
| 120 | +echo "Creating \$scratch.ODBCTest table." |
| 121 | + |
| 122 | +# Create a new table by sending a SQL query. |
| 123 | +curl --fail --silent --show-error \ |
| 124 | + -X POST "$SQL_URL" \ |
| 125 | + -H "Authorization: _dremio$TOKEN" \ |
| 126 | + -H "Content-Type: application/json" \ |
| 127 | + -d "$(python3 - <<EOF |
| 128 | +import json |
| 129 | +print(json.dumps({"sql": """$SQL_QUERY"""})) |
| 130 | +EOF |
| 131 | +)" |
| 132 | + |
| 133 | +echo "" |
| 134 | +echo "Finished setting up dremio docker instance." |
0 commit comments