-
Notifications
You must be signed in to change notification settings - Fork 17
/
install.sh
124 lines (108 loc) · 4.15 KB
/
install.sh
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
124
#!/bin/bash
set -e
# Function to check for jq and install if necessary
check_and_install_jq() {
if ! command -v jq &> /dev/null; then
# Try to install jq based on the system package manager
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
sudo apt-get update && sudo apt-get install -y jq > /dev/null
elif [[ "$OSTYPE" == "darwin"* ]]; then
brew install jq > /dev/null
else
echo "Please install jq manually: https://stedolan.github.io/jq/download/"
exit 1
fi
fi
}
echo " "
echo "=========================================="
echo "Welcome to the StarbaseDB installation script!"
echo " "
echo "This script will deploy a Cloudflare Worker and create an Outerbase Starlink session."
echo "If you don't have a paid Cloudflare account, your deployment will fail."
echo " "
echo "IMPORTANT: You _MUST_ have a paid Cloudflare account to use SQLite in Durable Objects."
echo "=========================================="
echo " "
# Step 1: Check if jq is installed
check_and_install_jq
# Step 2: Clone the repository
if ! command -v git &> /dev/null; then
echo "Git is not installed. Please install Git and try again."
exit 1
fi
echo "Cloning the repository..."
git clone https://github.com/outerbase/starbasedb.git > /dev/null 2>&1 || { echo "Error: Failed to clone the repository. Please check your internet connection and try again."; exit 1; }
cd starbasedb || { echo "Error: Failed to change to the starbasedb directory. The clone might have failed."; exit 1; }
# Step 3: Generate a secure ADMIN_AUTHORIZATION_TOKEN and update wrangler.toml
os=$(uname -s)
PLATFORM_SED="sed -i ''"
# choose correct version of sed utility for platform
case "$os" in
Linux*)
# GNU utilities
PLATFORM_SED="sed -i"
;;
Darwin*)
# BSD utilities (macOS)
PLATFORM_SED="sed -i ''"
;;
*BSD*)
# Other BSD variants (FreeBSD, OpenBSD, etc)
PLATFORM_SED="sed -i ''"
;;
*)
echo "Unknown operating system: $os"
exit 1
;;
esac
ADMIN_AUTHORIZATION_TOKEN=$(openssl rand -hex 16)
$PLATFORM_SED "s/ADMIN_AUTHORIZATION_TOKEN = \"[^\"]*\"/ADMIN_AUTHORIZATION_TOKEN = \"$ADMIN_AUTHORIZATION_TOKEN\"/" wrangler.toml
# Step 4: Prompt the user for Cloudflare account_id (force interaction)
echo " "
echo "Please enter your Cloudflare account_id (from 'wrangler whoami' or the Cloudflare dashboard):"
read -r ACCOUNT_ID </dev/tty
$PLATFORM_SED "s/^account_id = .*/account_id = \"$ACCOUNT_ID\"/" wrangler.toml || echo "account_id = \"$ACCOUNT_ID\"" >> wrangler.toml
# Step 5: Create an Outerbase Studio account
ADMIN_USER="admin"
ADMIN_PASS=$(openssl rand -hex 16)
# Step 6: Update the wrangler.toml with generated STUDIO_USER and STUDIO_PASS
$PLATFORM_SED 's/# STUDIO_USER = "admin"/STUDIO_USER = "'"$ADMIN_USER"'"/' wrangler.toml
$PLATFORM_SED 's/# STUDIO_PASS = "123456"/STUDIO_PASS = "'"$ADMIN_PASS"'"/' wrangler.toml
# Step 7: Run typegen command
npm install > /dev/null 2>&1
npm run cf-typegen > /dev/null 2>&1
# Step 8: Deploy the worker
echo " "
echo "Deploying your worker..."
# DEPLOY_OUTPUT=$(npm run deploy 2>&1) # This will hide the output
DEPLOY_OUTPUT=$(npm run deploy)
# Step 9: Extract the URL from the deploy output
HOST_URL=$(echo "$DEPLOY_OUTPUT" | grep -oE 'https://[a-zA-Z0-9.-]+\.workers\.dev')
if [ -n "$HOST_URL" ]; then
echo "Worker deployed successfully at $HOST_URL."
else
echo "Error: Failed to extract the worker URL."
echo "Verify that you are using a paid Cloudflare account and try again."
exit 1
fi
STARLINK_URL="$HOST_URL/studio"
# Step 10: Enjoy :)
if [ -n "$STARLINK_URL" ]; then
echo " "
echo "=========================================="
echo " "
echo "Outerbase Studio user account created!"
echo "Use the following URL to view your database:"
echo
echo "$STARLINK_URL"
echo
echo "Username: $ADMIN_USER"
echo "Password: $ADMIN_PASS"
echo
echo "NOTE: You can change your Outerbase Studio password in the wrangler.toml file and redeploy."
echo
echo "=========================================="
else
echo "Error: Failed to create Outerbase Starlink session."
fi