#!/usr/bin/env bash # # The Fastcruise setup wizard. # # A wizard walks a human through the steps that only the human can do. This # wizard adds the TeslaMateApi service to the TeslaMate Compose file, starts # the service, and does a test of the API. # # Run it on the TeslaMate server: # # curl -fsSL https://fastcruise.app/skills/teslamateapi-setup/setup-wizard.sh -o setup-wizard.sh # bash setup-wizard.sh # # Options: # --port N host port for TeslaMateApi (default 8080) # --dry-run show each step, change nothing # # No password and no key go to an AI agent. This wizard asks for no secret. # The stage flow follows the /wizard skill of Matt Pocock (MIT): # https://github.com/mattpocock/skills set -euo pipefail BASE_URL="${FASTCRUISE_BASE_URL:-https://fastcruise.app/skills/teslamateapi-setup}" # ── Library: the same frame in every run. Do not edit. ───────────────────── TOTAL_STAGES=5 _STAGE=0 if [[ -t 1 ]] && command -v tput >/dev/null 2>&1 && [[ "$(tput colors 2>/dev/null || echo 0)" -ge 8 ]]; then BOLD=$(tput bold); DIM=$(tput dim); RESET=$(tput sgr0) BLUE=$(tput setaf 4); GREEN=$(tput setaf 2); YELLOW=$(tput setaf 3); RED=$(tput setaf 1) else BOLD=""; DIM=""; RESET=""; BLUE=""; GREEN=""; YELLOW=""; RED="" fi _clear() { [[ -t 1 ]] || return 0 tput clear 2>/dev/null || printf '\033[2J\033[3J\033[H' } stage() { _clear _STAGE=$((_STAGE + 1)) printf '\n%s%s> Stage %s/%s - %s%s\n' "$BOLD" "$BLUE" "$_STAGE" "$TOTAL_STAGES" "$1" "$RESET" } say() { printf ' %s\n' "$1"; } step() { printf ' %s-%s %s\n' "$BLUE" "$RESET" "$1"; } note() { printf ' %s%s%s\n' "$DIM" "$1" "$RESET"; } ok() { printf ' %sOK%s %s\n' "$GREEN" "$RESET" "$1"; } warn() { printf ' %s!%s %s\n' "$YELLOW" "$RESET" "$1"; } bad() { printf ' %sX%s %s\n' "$RED" "$RESET" "$1"; } pause() { printf ' %s%s%s ' "$DIM" "${1:-Press Enter to continue}" "$RESET" read -r _ || true } confirm() { local reply="" printf ' %s? %s [y/N] ' "$YELLOW" "$1" read -r reply || true [[ "$reply" =~ ^[Yy] ]] } ask_value() { local name="$1" prompt="$2" input="" printf ' %s%s%s ' "$BOLD" "$prompt" "$RESET" read -r input || true printf -v "$name" '%s' "$input" } open_url() { local url="$1" printf ' %s>%s %s\n' "$GREEN" "$RESET" "$url" { if command -v wslview >/dev/null 2>&1; then wslview "$url" elif command -v explorer.exe >/dev/null 2>&1; then explorer.exe "$url" elif command -v xdg-open >/dev/null 2>&1; then xdg-open "$url" elif command -v open >/dev/null 2>&1; then open "$url" else false; fi } >/dev/null 2>&1 || warn "The browser did not open. Go to this address yourself: $url" } # ── Steps ────────────────────────────────────────────────────────────────── COMPOSE="" PORT=8080 DRY_RUN=0 ENGINE="" BACKUP="" while [[ $# -gt 0 ]]; do case "$1" in --port) PORT="${2:?--port needs a number}"; shift 2 ;; --dry-run) DRY_RUN=1; shift ;; -h | --help) sed -n '2,18p' "$0"; exit 0 ;; *) printf 'Unknown option: %s\n' "$1" >&2; exit 2 ;; esac done run_engine() { set +e local out out=$(python3 "$ENGINE" "$@" 2>&1) local rc=$? set -e printf '%s\n' "$out" return $rc } find_compose() { local name for name in compose.yaml compose.yml docker-compose.yml docker-compose.yaml; do if [[ -f "$name" ]]; then printf '%s' "$name" return 0 fi done return 1 } get_engine() { local dir if [[ -n "${BASH_SOURCE[0]:-}" && -f "${BASH_SOURCE[0]}" ]]; then dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" else dir="$(mktemp -d)" fi ENGINE="$dir/add-service.py" if [[ ! -f "$ENGINE" ]]; then note "Get the script of this skill from $BASE_URL" curl -fsSL "$BASE_URL/add-service.py" -o "$ENGINE" || return 1 fi return 0 } lan_ip() { local ip="" if command -v ip >/dev/null 2>&1; then ip=$(ip -4 route get 1.1.1.1 2>/dev/null | awk '{print $7; exit}') || true fi [[ -z "$ip" ]] && ip=$(hostname -I 2>/dev/null | awk '{print $1}') || true if [[ -z "$ip" ]] && command -v ifconfig >/dev/null 2>&1; then ip=$(ifconfig 2>/dev/null | awk '/inet /{print $2}' | grep -v '^127\.' | head -1) || true fi printf '%s' "$ip" } api_cars() { local body count body=$(curl -sS --max-time 8 "$1/api/v1/cars" 2>/dev/null) || return 1 [[ "$body" == *'"cars"'* ]] || return 1 count=$(printf '%s' "$body" | grep -o '"car_id"' | wc -l | tr -d ' ') printf '%s' "$count" } port_in_use() { (exec 3<>"/dev/tcp/127.0.0.1/$1") 2>/dev/null } restart_count() { local id id=$(docker compose -f "$COMPOSE" ps -q teslamateapi 2>/dev/null) || return 1 [[ -n "$id" ]] || return 1 # Read the restart count only. The command reads no environment value. docker inspect -f '{{.RestartCount}}' "$id" 2>/dev/null } log_has() { docker compose -f "$COMPOSE" logs --tail=100 teslamateapi 2>&1 | grep -qi "$1" } db_auth_error() { log_has 'password authentication failed' } run_wizard() { _clear printf '\n%s%s Fastcruise setup wizard%s\n' "$BOLD" "$GREEN" "$RESET" say "This wizard adds the TeslaMateApi service to your TeslaMate server." say "Then it starts the service and does a test of the API." say "It asks for no password and no key. Every value stays on this server." say "It makes a backup before it changes a file. Stop it with Ctrl-C." pause "Press Enter to start." local missing=() command -v docker >/dev/null 2>&1 || missing+=("docker") command -v python3 >/dev/null 2>&1 || missing+=("python3") command -v curl >/dev/null 2>&1 || missing+=("curl") if (( ${#missing[@]} )); then bad "These commands are not in this system: ${missing[*]}" say "Run the wizard on the TeslaMate server." exit 1 fi stage "Find the Compose file" if COMPOSE=$(find_compose); then ok "Found $COMPOSE" else COMPOSE="" warn "No Compose file in $(pwd)" if command -v docker >/dev/null 2>&1; then say "The active projects:" docker compose ls 2>/dev/null || true fi ask_value COMPOSE "Type the path of the Compose file of TeslaMate: " fi if [[ -z "$COMPOSE" || ! -f "$COMPOSE" ]]; then bad "No readable Compose file. Run this wizard on the TeslaMate server, in the directory of the file." exit 1 fi note "The wizard uses this file for every docker compose command. Run it on the TeslaMate server." stage "Change the Compose file" say "The wizard does these steps:" step "Make one backup of $COMPOSE" step "Add the teslamateapi service with the image tobiasehlert/teslamateapi:latest" step "Copy the encryption key and the database credentials from the teslamate service" note "The values stay in the shell of this wizard. The script writes the key names only." if ! confirm "Change $COMPOSE now?"; then warn "Nothing changed." exit 1 fi if ((DRY_RUN)); then note "dry run: python3 add-service.py --compose $COMPOSE --port $PORT" else get_engine || { bad "Cannot get the script of the skill. Check the network."; exit 1; } local out rc set +e out=$(run_engine --compose "$COMPOSE" --port "$PORT") rc=$? set -e printf '%s\n' "$out" | sed 's/^/ /' BACKUP=$(printf '%s\n' "$out" | sed -n 's/^Backup: //p') case "$rc" in 0) ok "The service is in the file." ;; 3) ok "The file has the service already. Nothing changed." ;; *) bad "The script stopped with an error."; exit 1 ;; esac fi stage "Start the service" if ((DRY_RUN)); then note "dry run: docker compose -f $COMPOSE up -d teslamateapi" else if port_in_use "$PORT"; then if docker compose -f "$COMPOSE" ps teslamateapi 2>/dev/null | grep -qi "up"; then note "Port $PORT is in use by the teslamateapi container. The wizard starts that service again." else warn "Another program uses port $PORT." say "Stop the other program, or run the wizard again with a free port:" say " bash setup-wizard.sh --port 8081" confirm "Continue with port $PORT?" || exit 1 fi fi if ! docker compose -f "$COMPOSE" up -d teslamateapi; then bad "docker compose up failed. Read the message above." exit 1 fi ok "The service starts." say "The first start makes the database ready. This step can need a few minutes." local waited=0 ready=0 now="" count="" while ((waited < 300)); do if count=$(api_cars "http://127.0.0.1:$PORT"); then ready=1 break fi if db_auth_error; then break fi now=$(restart_count) || now="" if [[ -n "$now" && "$now" -ge 2 ]]; then break fi sleep 5 waited=$((waited + 5)) printf '.' done printf '\n' if ((ready)); then ok "The API answered. The container operates." else if db_auth_error; then bad "The database refuses the connection: password authentication failed." say "DATABASE_PASS in the teslamateapi service does not agree with the database." say "Correct the value in $COMPOSE and run the wizard again. Keep the value on this server." elif [[ -n "$now" && "$now" -ge 2 ]]; then bad "The container restarts again and again (restart count $now). The last lines of the log:" docker compose -f "$COMPOSE" logs --tail=20 teslamateapi 2>&1 || true say "Compare DATABASE_USER, DATABASE_PASS, and DATABASE_NAME with the database service." else bad "No answer from the API after $waited seconds. The last lines of the log:" docker compose -f "$COMPOSE" logs --tail=20 teslamateapi 2>&1 || true say "A different DATABASE_PASS gives the error 'password authentication failed'." fi exit 1 fi say "The check compares the two services. Each line must say match:" local check_out check_rc set +e check_out=$(run_engine --compose "$COMPOSE" --check) check_rc=$? set -e printf '%s\n' "$check_out" | sed 's/^/ /' if ((check_rc)); then warn "A line does not say match. Compare the two services in $COMPOSE." fi fi stage "Do a test of the API" local url="http://127.0.0.1:$PORT" app_url="" count="" if ((DRY_RUN)); then note "dry run: curl $url/api/v1/cars" else if count=$(api_cars "$url"); then ok "The API answered on $url. Cars: $count" else warn "No answer on $url. Give the wizard the correct port with --port, or read the log of the container." fi local ip ip=$(lan_ip) if [[ -n "$ip" ]] && count=$(api_cars "http://$ip:$PORT"); then app_url="http://$ip:$PORT" ok "The API answered on the network address. Cars: $count" else warn "No answer on the network address. The API must listen on the network interface for the iPhone." fi fi stage "Set up the app on the iPhone" if [[ -n "$app_url" ]]; then say "Open the Fastcruise app and enter this URL:" printf ' %s%s%s\n' "$BOLD" "$app_url" "$RESET" else say "Open the Fastcruise app and enter the URL of your TeslaMateApi: http://:$PORT" fi step "Give the local network permission to the app at the first connection." step "Tap the connection status again when the first connection shows an error." step "Add the widgets of the app to your home screen." open_url "https://apps.apple.com/us/app/fastcruise-tesla/id6747918869" open_url "https://fastcruise.app/blog/teslamate-fastcruise-setupguide" warn "Do not send your password, your encryption key, or your access token to an AI agent." warn "TeslaMateApi asks for no password by default. Every device in your network can read your Tesla data." say "For more safety, set API_TOKEN in the service, and use a reverse proxy with HTTPS and authentication." warn "An open API port on the internet shows your Tesla data to other persons. Use a VPN, or a reverse proxy with HTTPS and authentication." pause "Press Enter when the app shows your data." _clear printf '\n%s%s Setup complete%s\n' "$BOLD" "$GREEN" "$RESET" note "Compose file: $COMPOSE" [[ -n "$BACKUP" ]] && note "Backup: $BACKUP" [[ -n "$app_url" ]] && note "URL for the app: $app_url" say "No password and no key left this machine. Keep them on the server." } run_wizard "$@" exit 0