#!/usr/bin/env bash # Coide — install your own cloud dev box on a fresh Ubuntu server. # # curl -fsSL https://YOUR-SITE/install.sh | sudo bash -s -- https://YOUR-SITE # # What it does (takes 5-15 min): # - creates a "dev" user, installs Node 22, Git, build tools, Python # - installs Foundry (forge, cast, anvil) for Robinhood Chain and coding agents # - installs the Coide workspace as a service that restarts on its own # - puts it behind Caddy with automatic HTTPS (free certificate) # - closes every port except SSH, HTTP and HTTPS # - prints your workspace URL and a one-time setup code # # Options (environment variables): # COIDE_DOMAIN=dev.example.com your own domain (A record -> this server). Default: .sslip.io # COIDE_AGENTS=0 skip coding agents # COIDE_SUDO=1 give the "dev" user passwordless sudo (default: no sudo) # COIDE_NO_SUDO=1 kept for compatibility; no sudo is already the default # COIDE_SKIP_SYSTEM=1 (testing) skip apt, firewall, Caddy and systemd # COIDE_SKIP_TOOLS=1 (testing) skip Foundry and agents # COIDE_NETWORK=mainnet point forge/cast at Robinhood Chain mainnet (default: testnet) # COIDE_SSH_PORT=2222 SSH port to keep open (auto-detected by default) # COIDE_SWAP=0 skip creating a swap file # COIDE_SWAP_MB=2048 swap size when one is created set -euo pipefail # Options are documented as COIDE_*; the older CTC_* names keep working. for _n in DOMAIN AGENTS NO_SUDO SUDO SKIP_SYSTEM SKIP_TOOLS USER SOURCE NETWORK SSH_PORT SWAP SWAP_MB; do _v="$(eval "printf '%s' \"\${COIDE_${_n}:-}\"")" if [ -n "$_v" ]; then export "CTC_${_n}=${_v}"; fi done unset _n _v SOURCE="${1:-${CTC_SOURCE:-}}" APP_DIR=/opt/coide DEV_USER="${CTC_USER:-dev}" PORT=3100 LOG=/var/log/coide-install.log NETWORK="${CTC_NETWORK:-testnet}" case "$NETWORK" in testnet|mainnet) ;; *) printf 'Error: COIDE_NETWORK must be testnet or mainnet\n' >&2; exit 1;; esac c_dim=$'\e[2m'; c_ok=$'\e[32m'; c_warn=$'\e[33m'; c_err=$'\e[31m'; c_b=$'\e[1m'; c_0=$'\e[0m' step() { printf '\n%s==>%s %s\n' "$c_b" "$c_0" "$*"; } ok() { printf ' %s✓%s %s\n' "$c_ok" "$c_0" "$*"; } warn() { printf ' %s!%s %s\n' "$c_warn" "$c_0" "$*"; } die() { printf '\n%sError:%s %s\n' "$c_err" "$c_0" "$*" >&2; exit 1; } run() { "$@" >>"$LOG" 2>&1 || die "command failed: $* (details in $LOG)"; } as_dev() { sudo -u "$DEV_USER" -H bash -lc "$1" >>"$LOG" 2>&1; } [ "$(id -u)" -eq 0 ] || die "run as root: curl -fsSL /install.sh | sudo bash -s -- " [ -n "$SOURCE" ] || die "missing site URL. Usage: curl -fsSL https://SITE/install.sh | sudo bash -s -- https://SITE" case "$SOURCE" in https://*|http://127.0.0.1*|http://localhost*) ;; *) die "site URL must start with https://";; esac SOURCE="${SOURCE%/}" if [ -z "${CTC_SKIP_SYSTEM:-}" ]; then . /etc/os-release 2>/dev/null || true [ "${ID:-}" = "ubuntu" ] || [ "${ID:-}" = "debian" ] || die "this installer supports Ubuntu (22.04/24.04) and Debian 12." fi : >"$LOG" printf '%sCoide installer%s %s(log: %s)%s\n' "$c_b" "$c_0" "$c_dim" "$LOG" "$c_0" # ------------------------------------------------------------------ system if [ -z "${CTC_SKIP_SYSTEM:-}" ]; then step "System packages" export DEBIAN_FRONTEND=noninteractive run apt-get update -y run apt-get install -y --no-install-recommends ca-certificates curl gnupg git build-essential pkg-config \ libssl-dev python3 python3-pip python3-venv ufw unattended-upgrades jq unzip tar sudo tmux run dpkg-reconfigure -f noninteractive unattended-upgrades ok "base packages + automatic security updates" if ! command -v node >/dev/null || [ "$(node -p 'process.versions.node.split(".")[0]')" -lt 20 ]; then run bash -c "curl -fsSL https://deb.nodesource.com/setup_22.x | bash -" run apt-get install -y nodejs fi ok "Node $(node --version)" # Agents, npm installs and contract builds can spike memory. A small swap file # keeps the OOM killer from turning them into mysterious failures on 2-4 GB servers. if [ "${CTC_SWAP:-1}" != "0" ]; then step "Swap" if swapon --show 2>/dev/null | grep -q .; then ok "swap already enabled" else MEM_MB=$(awk '/MemTotal/ {print int($2/1024)}' /proc/meminfo 2>/dev/null || echo 0) if [ "${MEM_MB:-0}" -lt 8192 ]; then SWAP_MB="${CTC_SWAP_MB:-2048}" if fallocate -l "${SWAP_MB}M" /swapfile 2>/dev/null || dd if=/dev/zero of=/swapfile bs=1M count="$SWAP_MB" status=none; then chmod 600 /swapfile mkswap /swapfile >/dev/null swapon /swapfile grep -q '^/swapfile ' /etc/fstab 2>/dev/null || echo '/swapfile none swap sw 0 0' >>/etc/fstab ok "${SWAP_MB} MB swap" else warn "could not create swap (builds may run out of memory)" fi else ok "enough RAM, no swap needed" fi fi fi fi # ------------------------------------------------------------------ user step "Workspace user" if ! id "$DEV_USER" >/dev/null 2>&1; then run useradd -m -s /bin/bash "$DEV_USER" fi # Safer default: the workspace user does not get passwordless sudo. Agents and # terminals run as this user, so root-by-default would turn any prompt injection # or leaked key into full control of the server. Opt in explicitly if you need it. if [ -n "${CTC_SUDO:-}" ] && [ -z "${CTC_NO_SUDO:-}" ]; then echo "$DEV_USER ALL=(ALL) NOPASSWD:ALL" >/etc/sudoers.d/90-coide chmod 440 /etc/sudoers.d/90-coide ok "user '$DEV_USER' (sudo enabled with COIDE_SUDO=1)" else rm -f /etc/sudoers.d/90-coide ok "user '$DEV_USER' (no sudo - safer default)" fi DEV_HOME=$(getent passwd "$DEV_USER" | cut -d: -f6) # ------------------------------------------------------------------ app step "Coide workspace" TMP=$(mktemp -d) KEEP_TMP=0 BACKUP_READY=0 cleanup() { if [ "$KEEP_TMP" = "1" ]; then rm -rf "$TMP" elif [ "$BACKUP_READY" = "1" ]; then printf '\n%sUpdate failed. Your previous data and .env are kept in: %s%s\n' "$c_warn" "$TMP" "$c_0" >&2 else printf '\n%sInstall failed before touching your existing install.%s\n' "$c_warn" "$c_0" >&2 fi } trap cleanup EXIT run curl -fsSL "$SOURCE/release/coide.tar.gz" -o "$TMP/app.tar.gz" # Unpack and validate the new release BEFORE deleting the old one. This makes # updates atomic enough: a broken download never destroys the running install. mkdir -p "$TMP/release" "$TMP/backup" run tar -xzf "$TMP/app.tar.gz" -C "$TMP/release" [ -f "$TMP/release/server/server.mjs" ] || die "release archive is incomplete (missing server/server.mjs)" # Keep user data and settings across re-installs / updates. Copy, never move: # the old install stays intact until the new release is fully unpacked. if [ -d "$APP_DIR/server/data" ]; then run cp -a "$APP_DIR/server/data" "$TMP/backup/data"; fi if [ -f "$APP_DIR/server/.env" ]; then run cp -a "$APP_DIR/server/.env" "$TMP/backup/env"; fi BACKUP_READY=1 mkdir -p "$APP_DIR" find "$APP_DIR" -mindepth 1 -maxdepth 1 -exec rm -rf {} + run cp -a "$TMP/release/." "$APP_DIR/" if [ -d "$TMP/backup/data" ]; then rm -rf "$APP_DIR/server/data" run cp -a "$TMP/backup/data" "$APP_DIR/server/data" fi if [ -f "$TMP/backup/env" ]; then run cp -a "$TMP/backup/env" "$APP_DIR/server/.env"; fi mkdir -p "$APP_DIR/server/data" KEEP_TMP=1 # workspace files live in the dev user's home so the terminal and the app see the same folder mkdir -p "$DEV_HOME/workspace" chown -R "$DEV_USER:$DEV_USER" "$APP_DIR" "$DEV_HOME/workspace" NPM_AS_DEV=(sudo -u "$DEV_USER" -H env "npm_config_cache=$DEV_HOME/.npm" npm) (cd "$APP_DIR/server" && run "${NPM_AS_DEV[@]}" install --omit=dev --omit=optional --no-audit --no-fund --ignore-scripts) (cd "$APP_DIR/server" && run "${NPM_AS_DEV[@]}" rebuild @homebridge/node-pty-prebuilt-multiarch) ok "installed in $APP_DIR" # ------------------------------------------------------------------ toolchain if [ -z "${CTC_SKIP_TOOLS:-}" ]; then step "Robinhood Chain toolchain (Foundry)" if ! as_dev 'test -x ~/.foundry/bin/foundryup'; then as_dev 'curl -fsSL https://foundry.paradigm.xyz | bash' || die "Foundry installer download failed (see $LOG)" fi as_dev '~/.foundry/bin/foundryup' || die "Foundry install failed (see $LOG)" as_dev 'mkdir -p ~/.foundry/keystores' || true ok "Foundry $(sudo -u "$DEV_USER" -H bash -c '~/.foundry/bin/forge --version 2>/dev/null | head -n1 | cut -d" " -f3' || true) (forge, cast, anvil, chisel)" ok "Robinhood Chain $NETWORK by default" if [ "${CTC_AGENTS:-1}" = "1" ]; then step "Coding agents" as_dev 'mkdir -p ~/.npm-global && npm config set prefix ~/.npm-global && npm install -g opencode-ai @anthropic-ai/claude-code @openai/codex @google/gemini-cli' \ && ok "OpenCode, Claude Code, Codex, Gemini CLI (sign in with your own accounts)" \ || warn "some agents failed to install — install them later from the Agents tab" as_dev 'npm install -g @xai-official/grok' \ && ok "Grok Build (sign in with your Grok account)" \ || warn "Grok Build failed to install — install it later from the Agents tab" fi fi # CTC_SKIP_TOOLS # shell profile for the web terminal cat >"$DEV_HOME/.coide_profile" <<'EOF' export PATH="$HOME/.npm-global/bin:$HOME/.foundry/bin:$HOME/.local/bin:$PATH" export COIDE_NETWORK="${COIDE_NETWORK:-__NETWORK__}" if [ "$COIDE_NETWORK" = "mainnet" ]; then export RH_CHAIN_ID=4663 export ETH_RPC_URL="${ETH_RPC_URL:-https://rpc.mainnet.chain.robinhood.com}" export RH_EXPLORER=https://robinhoodchain.blockscout.com else export RH_CHAIN_ID=46630 export ETH_RPC_URL="${ETH_RPC_URL:-https://rpc.testnet.chain.robinhood.com}" export RH_EXPLORER=https://explorer.testnet.chain.robinhood.com export RH_FAUCET=https://faucet.testnet.chain.robinhood.com fi alias ll='ls -lah' EOF sed -i "s/__NETWORK__/$NETWORK/" "$DEV_HOME/.coide_profile" grep -q coide_profile "$DEV_HOME/.bashrc" 2>/dev/null || echo '[ -f ~/.coide_profile ] && . ~/.coide_profile' >>"$DEV_HOME/.bashrc" grep -q coide_profile "$DEV_HOME/.profile" 2>/dev/null || echo '[ -f ~/.coide_profile ] && . ~/.coide_profile' >>"$DEV_HOME/.profile" chown "$DEV_USER:$DEV_USER" "$DEV_HOME/.coide_profile" "$DEV_HOME/.bashrc" "$DEV_HOME/.profile" # ------------------------------------------------------------------ config step "Configuration" if [ -f "$APP_DIR/server/.env" ]; then SETUP_CODE=$(grep -E '^SETUP_CODE=' "$APP_DIR/server/.env" | cut -d= -f2 || true) ok "kept your existing settings" else SETUP_CODE=$(tr -dc 'A-HJ-NP-Z2-9' "$APP_DIR/server/.env" </etc/systemd/system/coide.service </dev/null 2>&1 && break; sleep 1; done curl -fsS "http://127.0.0.1:$PORT/api/config" >/dev/null 2>&1 || die "the workspace did not start: journalctl -u coide -n 50" ok "running (restarts automatically, starts on boot)" # ------------------------------------------------------------------ HTTPS step "HTTPS" IP=$(curl -fsS4 https://api.ipify.org 2>/dev/null || hostname -I | awk '{print $1}') DOMAIN="${CTC_DOMAIN:-${IP//./-}.sslip.io}" if ! command -v caddy >/dev/null; then run bash -c "curl -fsSL 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | gpg --dearmor --yes -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg" run bash -c "curl -fsSL 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' -o /etc/apt/sources.list.d/caddy-stable.list" run apt-get update -y run apt-get install -y caddy fi cat >/etc/caddy/Caddyfile </dev/null 2>&1; then SSH_PORT=$(sshd -T 2>/dev/null | awk '/^port /{print $2; exit}' || true) fi case "$SSH_PORT" in ''|*[!0-9]*) SSH_PORT=22 ;; esac run ufw default deny incoming run ufw default allow outgoing if ufw app list 2>/dev/null | grep -q 'OpenSSH'; then run ufw allow OpenSSH; fi run ufw allow "${SSH_PORT}/tcp" run ufw allow 80/tcp run ufw allow 443/tcp run ufw --force enable ok "only SSH (port $SSH_PORT), HTTP and HTTPS are open" # ------------------------------------------------------------------ done cat <