2022-05-23 20:15:57 +02:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
2021-11-23 12:25:31 +01:00
|
|
|
set -e
|
|
|
|
|
2023-10-31 00:12:35 +01:00
|
|
|
export DEBIAN_FRONTEND=noninteractive
|
2023-10-31 00:24:00 +01:00
|
|
|
# Automatically restart without asking.
|
|
|
|
# this gets around needrestart command halting for user input
|
|
|
|
export RESTART_MODE=l
|
2023-04-06 19:09:51 +02:00
|
|
|
export POSTHOG_APP_TAG="${POSTHOG_APP_TAG:-latest}"
|
2022-09-23 00:44:22 +02:00
|
|
|
export SENTRY_DSN="${SENTRY_DSN:-'https://public@sentry.example.com/1'}"
|
2022-05-23 20:15:57 +02:00
|
|
|
|
|
|
|
POSTHOG_SECRET=$(head -c 28 /dev/urandom | sha224sum -b | head -c 56)
|
|
|
|
export POSTHOG_SECRET
|
2021-11-23 12:25:31 +01:00
|
|
|
|
|
|
|
# Talk to the user
|
2023-05-09 18:31:34 +02:00
|
|
|
echo "Welcome to the single instance PostHog installer 🦔"
|
2021-11-23 12:25:31 +01:00
|
|
|
echo ""
|
|
|
|
echo "⚠️ You really need 4gb or more of memory to run this stack ⚠️"
|
|
|
|
echo ""
|
2022-09-23 00:44:22 +02:00
|
|
|
echo "Power user or aspiring power user?"
|
2023-05-09 18:31:34 +02:00
|
|
|
echo "Check out our docs on deploying PostHog! https://posthog.com/docs/self-host/deploy/hobby"
|
2022-09-23 00:44:22 +02:00
|
|
|
echo ""
|
2022-09-30 20:16:53 +02:00
|
|
|
|
|
|
|
if ! [ -z "$1" ]
|
|
|
|
then
|
|
|
|
export POSTHOG_APP_TAG=$1
|
|
|
|
else
|
2023-05-09 18:31:34 +02:00
|
|
|
echo "What version of PostHog would you like to install? (We default to 'latest')"
|
2022-06-25 00:31:13 +02:00
|
|
|
echo "You can check out available versions here: https://hub.docker.com/r/posthog/posthog/tags"
|
|
|
|
read -r POSTHOG_APP_TAG_READ
|
|
|
|
if [ -z "$POSTHOG_APP_TAG_READ" ]
|
|
|
|
then
|
|
|
|
echo "Using default and installing $POSTHOG_APP_TAG"
|
|
|
|
else
|
|
|
|
export POSTHOG_APP_TAG=$POSTHOG_APP_TAG_READ
|
|
|
|
echo "Using provided tag: $POSTHOG_APP_TAG"
|
|
|
|
fi
|
2022-09-30 20:16:53 +02:00
|
|
|
fi
|
2022-06-25 00:31:13 +02:00
|
|
|
echo ""
|
2022-09-30 20:16:53 +02:00
|
|
|
if ! [ -z "$2" ]
|
|
|
|
then
|
|
|
|
export DOMAIN=$2
|
|
|
|
else
|
2023-05-09 18:31:34 +02:00
|
|
|
echo "Let's get the exact domain PostHog will be installed on"
|
2021-11-23 12:25:31 +01:00
|
|
|
echo "Make sure that you have a Host A DNS record pointing to this instance!"
|
|
|
|
echo "This will be used for TLS 🔐"
|
2022-06-20 10:10:52 +02:00
|
|
|
echo "ie: test.posthog.net (NOT an IP address)"
|
2021-11-23 12:25:31 +01:00
|
|
|
read -r DOMAIN
|
|
|
|
export DOMAIN=$DOMAIN
|
2022-09-30 20:16:53 +02:00
|
|
|
fi
|
2021-11-23 12:25:31 +01:00
|
|
|
echo "Ok we'll set up certs for https://$DOMAIN"
|
|
|
|
echo ""
|
|
|
|
echo "We will need sudo access so the next question is for you to give us superuser access"
|
|
|
|
echo "Please enter your sudo password now:"
|
|
|
|
sudo echo ""
|
|
|
|
echo "Thanks! 🙏"
|
|
|
|
echo ""
|
|
|
|
echo "Ok! We'll take it from here 🚀"
|
|
|
|
|
|
|
|
echo "Making sure any stack that might exist is stopped"
|
2022-01-25 07:14:50 +01:00
|
|
|
sudo -E docker-compose -f docker-compose.yml stop &> /dev/null || true
|
2021-11-23 12:25:31 +01:00
|
|
|
|
2022-09-23 00:44:22 +02:00
|
|
|
# send log of this install for continued support!
|
2022-09-24 01:42:09 +02:00
|
|
|
curl -o /dev/null -L --header "Content-Type: application/json" -d "{
|
|
|
|
\"api_key\": \"sTMFPsFhdP1Ssg\",
|
|
|
|
\"distinct_id\": \"${DOMAIN}\",
|
|
|
|
\"properties\": {\"domain\": \"${DOMAIN}\"},
|
|
|
|
\"type\": \"capture\",
|
|
|
|
\"event\": \"magic_curl_install_start\"
|
2024-04-22 11:03:14 +02:00
|
|
|
}" https://us.i.posthog.com/batch/ &> /dev/null
|
2022-09-23 00:44:22 +02:00
|
|
|
|
2021-11-23 12:25:31 +01:00
|
|
|
# update apt cache
|
|
|
|
echo "Grabbing latest apt caches"
|
|
|
|
sudo apt update
|
|
|
|
|
|
|
|
# clone posthog
|
2023-05-09 18:31:34 +02:00
|
|
|
echo "Installing PostHog 🦔 from Github"
|
2021-11-23 12:25:31 +01:00
|
|
|
sudo apt install -y git
|
2022-01-25 07:14:50 +01:00
|
|
|
# try to clone - if folder is already there pull latest for that branch
|
|
|
|
git clone https://github.com/PostHog/posthog.git &> /dev/null || true
|
|
|
|
cd posthog
|
2023-03-29 17:07:34 +02:00
|
|
|
|
|
|
|
if [[ "$POSTHOG_APP_TAG" = "latest-release" ]]
|
|
|
|
then
|
|
|
|
git fetch --tags
|
|
|
|
latestReleaseTag=$(git describe --tags `git rev-list --tags --max-count=1`)
|
|
|
|
echo "Checking out latest PostHog release: $latestReleaseTag"
|
|
|
|
git checkout $latestReleaseTag
|
|
|
|
elif [[ "$POSTHOG_APP_TAG" = "latest" ]]
|
|
|
|
then
|
|
|
|
echo "Pulling latest from current branch: $(git branch --show-current)"
|
|
|
|
git pull
|
2023-11-30 00:16:15 +01:00
|
|
|
elif [[ "$POSTHOG_APP_TAG" =~ ^[0-9a-f]{40}$ ]]
|
|
|
|
then
|
|
|
|
echo "Checking out specific commit hash: $POSTHOG_APP_TAG"
|
|
|
|
git checkout $POSTHOG_APP_TAG
|
2023-03-29 17:07:34 +02:00
|
|
|
else
|
|
|
|
releaseTag="${POSTHOG_APP_TAG/release-/""}"
|
|
|
|
git fetch --tags
|
|
|
|
echo "Checking out PostHog release: $releaseTag"
|
|
|
|
git checkout $releaseTag
|
|
|
|
fi
|
|
|
|
|
2022-01-25 07:14:50 +01:00
|
|
|
cd ..
|
|
|
|
|
2022-12-02 05:45:00 +01:00
|
|
|
if [ -n "$3" ]
|
2022-10-22 04:47:13 +02:00
|
|
|
then
|
|
|
|
export TLS_BLOCK="acme_ca https://acme-staging-v02.api.letsencrypt.org/directory"
|
|
|
|
fi
|
|
|
|
|
2023-11-30 00:16:15 +01:00
|
|
|
if [ "$REGISTRY_URL" == "" ]
|
|
|
|
then
|
|
|
|
export REGISTRY_URL="posthog/posthog"
|
|
|
|
fi
|
|
|
|
|
2021-11-23 12:25:31 +01:00
|
|
|
# rewrite caddyfile
|
|
|
|
rm -f Caddyfile
|
|
|
|
envsubst > Caddyfile <<EOF
|
2022-12-02 05:45:00 +01:00
|
|
|
{
|
2022-02-18 19:28:28 +01:00
|
|
|
$TLS_BLOCK
|
2022-12-02 05:45:00 +01:00
|
|
|
}
|
2023-11-30 00:16:15 +01:00
|
|
|
$DOMAIN, http://, https:// {
|
2024-06-04 12:08:25 +02:00
|
|
|
encode gzip zstd
|
2021-11-23 12:25:31 +01:00
|
|
|
reverse_proxy http://web:8000
|
|
|
|
}
|
|
|
|
EOF
|
|
|
|
|
2022-01-27 17:45:17 +01:00
|
|
|
# Write .env file
|
|
|
|
envsubst > .env <<EOF
|
|
|
|
POSTHOG_SECRET=$POSTHOG_SECRET
|
|
|
|
SENTRY_DSN=$SENTRY_DSN
|
|
|
|
DOMAIN=$DOMAIN
|
|
|
|
EOF
|
|
|
|
|
2021-11-23 12:25:31 +01:00
|
|
|
# write entrypoint
|
2022-10-06 18:25:57 +02:00
|
|
|
# NOTE: this is duplicated in bin/upgrade-hobby, so if you change it here,
|
|
|
|
# change it there too.
|
2021-11-23 12:25:31 +01:00
|
|
|
rm -rf compose
|
|
|
|
mkdir -p compose
|
|
|
|
cat > compose/start <<EOF
|
|
|
|
#!/bin/bash
|
2022-02-18 19:27:45 +01:00
|
|
|
/compose/wait
|
2021-12-15 00:02:59 +01:00
|
|
|
./bin/migrate
|
2021-11-23 12:25:31 +01:00
|
|
|
./bin/docker-server
|
|
|
|
EOF
|
|
|
|
chmod +x compose/start
|
|
|
|
|
2023-03-27 16:59:29 +02:00
|
|
|
cat > compose/temporal-django-worker <<EOF
|
|
|
|
#!/bin/bash
|
|
|
|
./bin/temporal-django-worker
|
|
|
|
EOF
|
|
|
|
chmod +x compose/temporal-django-worker
|
|
|
|
|
2022-02-18 19:27:45 +01:00
|
|
|
# write wait script
|
|
|
|
cat > compose/wait <<EOF
|
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import socket
|
|
|
|
import time
|
|
|
|
|
|
|
|
def loop():
|
2022-05-23 20:15:57 +02:00
|
|
|
print("Waiting for ClickHouse and Postgres to be ready")
|
|
|
|
try:
|
2022-02-18 19:27:45 +01:00
|
|
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
|
|
|
s.connect(('clickhouse', 9000))
|
2022-05-23 20:15:57 +02:00
|
|
|
print("Clickhouse is ready")
|
2022-02-18 19:27:45 +01:00
|
|
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
|
|
|
s.connect(('db', 5432))
|
2022-05-23 20:15:57 +02:00
|
|
|
print("Postgres is ready")
|
2022-02-18 19:27:45 +01:00
|
|
|
except ConnectionRefusedError as e:
|
2022-05-23 20:15:57 +02:00
|
|
|
time.sleep(5)
|
2022-02-18 19:27:45 +01:00
|
|
|
loop()
|
|
|
|
|
|
|
|
loop()
|
|
|
|
EOF
|
|
|
|
chmod +x compose/wait
|
|
|
|
|
2021-11-23 12:25:31 +01:00
|
|
|
# setup docker
|
2023-11-30 00:16:15 +01:00
|
|
|
# setup docker
|
|
|
|
# Check if Docker is already installed
|
|
|
|
if ! command -v docker &> /dev/null; then
|
|
|
|
echo "Docker is not installed. Setting up Docker."
|
|
|
|
|
|
|
|
# Setup Docker
|
|
|
|
sudo apt install -y apt-transport-https ca-certificates curl software-properties-common
|
|
|
|
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo -E apt-key add -
|
2024-06-21 18:45:42 +02:00
|
|
|
sudo add-apt-repository -y "deb [arch=amd64] https://download.docker.com/linux/ubuntu jammy stable"
|
2023-11-30 00:16:15 +01:00
|
|
|
sudo apt update
|
|
|
|
sudo apt-cache policy docker-ce
|
|
|
|
sudo apt install -y docker-ce
|
|
|
|
else
|
|
|
|
echo "Docker is already installed. Skipping installation."
|
|
|
|
fi
|
2021-11-23 12:25:31 +01:00
|
|
|
|
|
|
|
# setup docker-compose
|
|
|
|
echo "Setting up Docker Compose"
|
2022-11-29 21:50:42 +01:00
|
|
|
sudo curl -L "https://github.com/docker/compose/releases/download/v2.13.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose || true
|
2021-11-23 12:25:31 +01:00
|
|
|
sudo chmod +x /usr/local/bin/docker-compose
|
|
|
|
|
|
|
|
# enable docker without sudo
|
2022-10-10 18:43:22 +02:00
|
|
|
sudo usermod -aG docker "${USER}" || true
|
2021-11-23 12:25:31 +01:00
|
|
|
|
2022-01-25 07:14:50 +01:00
|
|
|
# start up the stack
|
2022-02-18 19:27:45 +01:00
|
|
|
echo "Configuring Docker Compose...."
|
2022-01-25 07:14:50 +01:00
|
|
|
rm -f docker-compose.yml
|
2022-11-29 21:50:42 +01:00
|
|
|
cp posthog/docker-compose.base.yml docker-compose.base.yml
|
2022-01-25 07:14:50 +01:00
|
|
|
cp posthog/docker-compose.hobby.yml docker-compose.yml.tmpl
|
|
|
|
envsubst < docker-compose.yml.tmpl > docker-compose.yml
|
|
|
|
rm docker-compose.yml.tmpl
|
2023-11-30 00:16:15 +01:00
|
|
|
echo "Starting the stack!"
|
2022-01-25 07:14:50 +01:00
|
|
|
sudo -E docker-compose -f docker-compose.yml up -d
|
2021-11-23 12:25:31 +01:00
|
|
|
|
|
|
|
echo "We will need to wait ~5-10 minutes for things to settle down, migrations to finish, and TLS certs to be issued"
|
|
|
|
echo ""
|
2023-05-09 18:31:34 +02:00
|
|
|
echo "⏳ Waiting for PostHog web to boot (this will take a few minutes)"
|
2022-09-22 22:50:06 +02:00
|
|
|
bash -c 'while [[ "$(curl -s -o /dev/null -w ''%{http_code}'' localhost/_health)" != "200" ]]; do sleep 5; done'
|
2023-05-09 18:31:34 +02:00
|
|
|
echo "⌛️ PostHog looks up!"
|
2021-11-23 12:25:31 +01:00
|
|
|
echo ""
|
|
|
|
echo "🎉🎉🎉 Done! 🎉🎉🎉"
|
2022-09-23 00:44:22 +02:00
|
|
|
# send log of this install for continued support!
|
2022-09-24 01:42:09 +02:00
|
|
|
curl -o /dev/null -L --header "Content-Type: application/json" -d "{
|
|
|
|
\"api_key\": \"sTMFPsFhdP1Ssg\",
|
|
|
|
\"distinct_id\": \"${DOMAIN}\",
|
|
|
|
\"properties\": {\"domain\": \"${DOMAIN}\"},
|
|
|
|
\"type\": \"capture\",
|
|
|
|
\"event\": \"magic_curl_install_complete\"
|
2024-04-22 11:03:14 +02:00
|
|
|
}" https://us.i.posthog.com/batch/ &> /dev/null
|
2021-11-23 12:25:31 +01:00
|
|
|
echo ""
|
|
|
|
echo "To stop the stack run 'docker-compose stop'"
|
|
|
|
echo "To start the stack again run 'docker-compose start'"
|
|
|
|
echo "If you have any issues at all delete everything in this directory and run the curl command again"
|
|
|
|
echo ""
|
2022-03-07 23:44:24 +01:00
|
|
|
echo 'To upgrade: run /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/posthog/posthog/HEAD/bin/upgrade-hobby)"'
|
2022-01-25 07:14:50 +01:00
|
|
|
echo ""
|
2023-05-09 18:31:34 +02:00
|
|
|
echo "PostHog will be up at the location you provided!"
|
2021-11-23 12:25:31 +01:00
|
|
|
echo "https://${DOMAIN}"
|
|
|
|
echo ""
|
2022-01-25 07:14:50 +01:00
|
|
|
echo "It's dangerous to go alone! Take this: 🦔"
|