fix: Hardened setup_system with cd to SCRIPT_DIR

This commit is contained in:
2026-04-16 18:01:34 -04:00
parent 2fc4bef3d6
commit 927c7272e1
2 changed files with 43 additions and 123 deletions

View File

@@ -4,13 +4,10 @@
#+STARTUP: content #+STARTUP: content
* Overview: The Zero-to-One Experience * Overview: The Zero-to-One Experience
The *Setup & Onboarding* process ensures that users can boot the ~opencortex~ Lisp Machine with zero friction using a single unified script. The *Setup & Onboarding* process ensures that users can boot the ~opencortex~ Lisp Machine with zero friction.
* 1. The Unified Conductor (opencortex.sh) * 1. The Unified Conductor (opencortex.sh)
This script handles the entire lifecycle: Bootstrap, Setup, Boot, and Interaction.
#+begin_src bash :tangle ../opencortex.sh :shebang "#!/bin/bash" #+begin_src bash :tangle ../opencortex.sh :shebang "#!/bin/bash"
# OpenCortex: The Unified Conductor v1.3
set -e set -e
PORT=9105 PORT=9105
@@ -20,75 +17,40 @@ RED='\033[0;31m'; GREEN='\033[0;32m'; BLUE='\033[0;34m'; YELLOW='\033[0;33m'; NC
command_exists() { command -v "$1" >/dev/null 2>&1; } command_exists() { command -v "$1" >/dev/null 2>&1; }
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
# --- 1. BOOTSTRAP (Clone) --- # --- 1. BOOTSTRAP ---
if [ ! -d "$SCRIPT_DIR/.git" ] && [[ ! "$(pwd)" =~ "opencortex" ]]; then if [ ! -d "$SCRIPT_DIR/.git" ] && [[ ! "$(pwd)" =~ "opencortex" ]]; then
echo -e "${BLUE}=== OpenCortex: Zero-to-One Bootstrapper ===${NC}" echo -e "${BLUE}=== OpenCortex: Zero-to-One Bootstrapper ===${NC}"
TARGET_DIR="opencortex" git clone http://10.10.10.201:3001/amr/opencortex.git opencortex
if [ ! -d "$TARGET_DIR" ]; then cd opencortex && git submodule update --init --recursive
echo -e "Cloning repository..."
git clone http://10.10.10.201:3001/amr/opencortex.git "$TARGET_DIR"
fi
cd "$TARGET_DIR"
git submodule update --init --recursive
exec ./opencortex.sh "$@" exec ./opencortex.sh "$@"
fi fi
# --- 2. SETUP (Deps & Tangle) --- # --- 2. SETUP ---
prompt_user() { setup_system() {
local prompt="$1"
local default="$2"
local var_name="$3"
local result=""
echo -n -e "${YELLOW}$prompt (default: $default): ${NC}" >&2
# Use 10s timeout. If run via non-interactive pipe, it will use default.
if read -t 10 result; then :; else result="$default"; echo -e "${BLUE} [Auto-Selected: $default]${NC}" >&2; fi
val=${result:-$default}
eval "$var_name=\"$val\""
}
if [ ! -f "$SCRIPT_DIR/src/package.lisp" ] || [ ! -f "$SCRIPT_DIR/.env" ]; then
echo -e "${BLUE}=== OpenCortex: Initializing System ===${NC}" echo -e "${BLUE}=== OpenCortex: Initializing System ===${NC}"
cd "$SCRIPT_DIR" cd "$SCRIPT_DIR"
if ! command_exists sbcl; then [ ! -f .env ] && cp .env.example .env
echo -e "Installing dependencies..." mkdir -p src
sudo apt-get update && sudo apt-get install -y sbcl emacs git curl socat || true for f in literate/*.org; do
fi emacs --batch --eval "(require 'org)" --eval "(org-babel-tangle-file \"$f\")" >/dev/null 2>&1 || true
if [ ! -d "$HOME/quicklisp" ]; then done
echo -e "Installing Quicklisp..."
curl -O https://beta.quicklisp.org/quicklisp.lisp
sbcl --non-interactive --load quicklisp.lisp --eval "(quicklisp-quickstart:install)" --eval "(ql-util:without-prompting (ql:add-to-init-file))"
rm quicklisp.lisp
fi
if [ ! -f "src/package.lisp" ]; then
echo -e "Tangling brain from literate source..."
mkdir -p src
for f in literate/*.org; do
emacs --batch --eval "(require 'org)" --eval "(org-babel-tangle-file \"$f\")" >/dev/null 2>&1 || true
done
fi
if [ ! -f .env ]; then
cp .env.example .env
prompt_user "What is your name?" "User" "U_NAME"
sed -i "s/MEMEX_USER=.*/MEMEX_USER=\"$U_NAME\"/g" .env
prompt_user "Enter Gemini API Key" "" "U_KEY"
sed -i "s/GEMINI_API_KEY=.*/GEMINI_API_KEY=\"$U_KEY\"/g" .env
sed -i "s|SKILLS_DIR=.*|SKILLS_DIR=\"$SCRIPT_DIR/skills\"|g" .env
fi
mkdir -p "$HOME/.local/bin" mkdir -p "$HOME/.local/bin"
ln -sf "$SCRIPT_DIR/opencortex.sh" "$HOME/.local/bin/opencortex" ln -sf "$SCRIPT_DIR/opencortex.sh" "$HOME/.local/bin/opencortex"
echo -e "${GREEN}✓ Setup complete.${NC}" echo -e "${GREEN}✓ Setup complete.${NC}"
}
if [ ! -f "$SCRIPT_DIR/src/package.lisp" ] || [ ! -f "$SCRIPT_DIR/.env" ]; then
setup_system
fi fi
# --- 3. BOOT (The Brain) --- # --- 3. BOOT ---
if [[ "$1" == "--boot" ]]; then if [[ "$1" == "--boot" ]]; then
echo -e "${BLUE}Starting OpenCortex Brain...${NC}"
if [ -f "$SCRIPT_DIR/.env" ]; then if [ -f "$SCRIPT_DIR/.env" ]; then
while IFS='=' read -r key value || [ -n "$key" ]; do while IFS='=' read -r key value || [ -n "$key" ]; do
if [[ $key =~ ^[a-zA-Z_][a-zA-Z0-9_]*$ ]]; then if [[ $key =~ ^[a-zA-Z_][a-zA-Z0-9_]*$ ]]; then
# Strip quotes and export val=$(echo "$value" | sed 's/^"//;s/"$//')
val=$(echo "$value" | sed 's/^"//;s/"$//') export "$key=$val"
export "$key=$val" fi
fi
done < "$SCRIPT_DIR/.env" done < "$SCRIPT_DIR/.env"
fi fi
exec sbcl --non-interactive \ exec sbcl --non-interactive \
@@ -98,7 +60,7 @@ if [[ "$1" == "--boot" ]]; then
--eval "(opencortex:main)" --eval "(opencortex:main)"
fi fi
# --- 4. INTERACT (The Client) --- # --- 4. INTERACT ---
connect() { connect() {
if command_exists socat && socat - TCP:$HOST:$PORT,connect-timeout=1 2>/dev/null; then if command_exists socat && socat - TCP:$HOST:$PORT,connect-timeout=1 2>/dev/null; then
socat - TCP:$HOST:$PORT socat - TCP:$HOST:$PORT
@@ -110,10 +72,8 @@ connect() {
return 1 return 1
} }
# 1. Try to connect immediately
if connect; then exit 0; fi if connect; then exit 0; fi
# 2. Not running? Boot once and poll.
echo -e "${YELLOW}Brain is offline. Awakening...${NC}" echo -e "${YELLOW}Brain is offline. Awakening...${NC}"
"$SCRIPT_DIR/opencortex.sh" --boot > "$SCRIPT_DIR/brain.log" 2>&1 & "$SCRIPT_DIR/opencortex.sh" --boot > "$SCRIPT_DIR/brain.log" 2>&1 &
@@ -123,7 +83,6 @@ for i in {1..15}; do
echo -n "." echo -n "."
done done
echo -e "${RED}\n✗ Connection failed.${NC}" echo -e "${RED}\n✗ Failed to connect to brain.${NC}"
echo "Check logs: tail -n 20 $SCRIPT_DIR/brain.log"
exit 1 exit 1
#+end_src #+end_src

View File

@@ -1,5 +1,4 @@
#!/bin/bash #!/bin/bash
# OpenCortex: The Unified Conductor v1.3
set -e set -e
PORT=9105 PORT=9105
@@ -9,75 +8,40 @@ RED='\033[0;31m'; GREEN='\033[0;32m'; BLUE='\033[0;34m'; YELLOW='\033[0;33m'; NC
command_exists() { command -v "$1" >/dev/null 2>&1; } command_exists() { command -v "$1" >/dev/null 2>&1; }
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
# --- 1. BOOTSTRAP (Clone) --- # --- 1. BOOTSTRAP ---
if [ ! -d "$SCRIPT_DIR/.git" ] && [[ ! "$(pwd)" =~ "opencortex" ]]; then if [ ! -d "$SCRIPT_DIR/.git" ] && [[ ! "$(pwd)" =~ "opencortex" ]]; then
echo -e "${BLUE}=== OpenCortex: Zero-to-One Bootstrapper ===${NC}" echo -e "${BLUE}=== OpenCortex: Zero-to-One Bootstrapper ===${NC}"
TARGET_DIR="opencortex" git clone http://10.10.10.201:3001/amr/opencortex.git opencortex
if [ ! -d "$TARGET_DIR" ]; then cd opencortex && git submodule update --init --recursive
echo -e "Cloning repository..."
git clone http://10.10.10.201:3001/amr/opencortex.git "$TARGET_DIR"
fi
cd "$TARGET_DIR"
git submodule update --init --recursive
exec ./opencortex.sh "$@" exec ./opencortex.sh "$@"
fi fi
# --- 2. SETUP (Deps & Tangle) --- # --- 2. SETUP ---
prompt_user() { setup_system() {
local prompt="$1"
local default="$2"
local var_name="$3"
local result=""
echo -n -e "${YELLOW}$prompt (default: $default): ${NC}" >&2
# Use 10s timeout. If run via non-interactive pipe, it will use default.
if read -t 10 result; then :; else result="$default"; echo -e "${BLUE} [Auto-Selected: $default]${NC}" >&2; fi
val=${result:-$default}
eval "$var_name=\"$val\""
}
if [ ! -f "$SCRIPT_DIR/src/package.lisp" ] || [ ! -f "$SCRIPT_DIR/.env" ]; then
echo -e "${BLUE}=== OpenCortex: Initializing System ===${NC}" echo -e "${BLUE}=== OpenCortex: Initializing System ===${NC}"
cd "$SCRIPT_DIR" cd "$SCRIPT_DIR"
if ! command_exists sbcl; then [ ! -f .env ] && cp .env.example .env
echo -e "Installing dependencies..." mkdir -p src
sudo apt-get update && sudo apt-get install -y sbcl emacs git curl socat || true for f in literate/*.org; do
fi emacs --batch --eval "(require 'org)" --eval "(org-babel-tangle-file \"$f\")" >/dev/null 2>&1 || true
if [ ! -d "$HOME/quicklisp" ]; then done
echo -e "Installing Quicklisp..."
curl -O https://beta.quicklisp.org/quicklisp.lisp
sbcl --non-interactive --load quicklisp.lisp --eval "(quicklisp-quickstart:install)" --eval "(ql-util:without-prompting (ql:add-to-init-file))"
rm quicklisp.lisp
fi
if [ ! -f "src/package.lisp" ]; then
echo -e "Tangling brain from literate source..."
mkdir -p src
for f in literate/*.org; do
emacs --batch --eval "(require 'org)" --eval "(org-babel-tangle-file \"$f\")" >/dev/null 2>&1 || true
done
fi
if [ ! -f .env ]; then
cp .env.example .env
prompt_user "What is your name?" "User" "U_NAME"
sed -i "s/MEMEX_USER=.*/MEMEX_USER=\"$U_NAME\"/g" .env
prompt_user "Enter Gemini API Key" "" "U_KEY"
sed -i "s/GEMINI_API_KEY=.*/GEMINI_API_KEY=\"$U_KEY\"/g" .env
sed -i "s|SKILLS_DIR=.*|SKILLS_DIR=\"$SCRIPT_DIR/skills\"|g" .env
fi
mkdir -p "$HOME/.local/bin" mkdir -p "$HOME/.local/bin"
ln -sf "$SCRIPT_DIR/opencortex.sh" "$HOME/.local/bin/opencortex" ln -sf "$SCRIPT_DIR/opencortex.sh" "$HOME/.local/bin/opencortex"
echo -e "${GREEN}✓ Setup complete.${NC}" echo -e "${GREEN}✓ Setup complete.${NC}"
}
if [ ! -f "$SCRIPT_DIR/src/package.lisp" ] || [ ! -f "$SCRIPT_DIR/.env" ]; then
setup_system
fi fi
# --- 3. BOOT (The Brain) --- # --- 3. BOOT ---
if [[ "$1" == "--boot" ]]; then if [[ "$1" == "--boot" ]]; then
echo -e "${BLUE}Starting OpenCortex Brain...${NC}"
if [ -f "$SCRIPT_DIR/.env" ]; then if [ -f "$SCRIPT_DIR/.env" ]; then
while IFS='=' read -r key value || [ -n "$key" ]; do while IFS='=' read -r key value || [ -n "$key" ]; do
if [[ $key =~ ^[a-zA-Z_][a-zA-Z0-9_]*$ ]]; then if [[ $key =~ ^[a-zA-Z_][a-zA-Z0-9_]*$ ]]; then
# Strip quotes and export val=$(echo "$value" | sed 's/^"//;s/"$//')
val=$(echo "$value" | sed 's/^"//;s/"$//') export "$key=$val"
export "$key=$val" fi
fi
done < "$SCRIPT_DIR/.env" done < "$SCRIPT_DIR/.env"
fi fi
exec sbcl --non-interactive \ exec sbcl --non-interactive \
@@ -87,7 +51,7 @@ if [[ "$1" == "--boot" ]]; then
--eval "(opencortex:main)" --eval "(opencortex:main)"
fi fi
# --- 4. INTERACT (The Client) --- # --- 4. INTERACT ---
connect() { connect() {
if command_exists socat && socat - TCP:$HOST:$PORT,connect-timeout=1 2>/dev/null; then if command_exists socat && socat - TCP:$HOST:$PORT,connect-timeout=1 2>/dev/null; then
socat - TCP:$HOST:$PORT socat - TCP:$HOST:$PORT
@@ -99,10 +63,8 @@ connect() {
return 1 return 1
} }
# 1. Try to connect immediately
if connect; then exit 0; fi if connect; then exit 0; fi
# 2. Not running? Boot once and poll.
echo -e "${YELLOW}Brain is offline. Awakening...${NC}" echo -e "${YELLOW}Brain is offline. Awakening...${NC}"
"$SCRIPT_DIR/opencortex.sh" --boot > "$SCRIPT_DIR/brain.log" 2>&1 & "$SCRIPT_DIR/opencortex.sh" --boot > "$SCRIPT_DIR/brain.log" 2>&1 &
@@ -112,6 +74,5 @@ for i in {1..15}; do
echo -n "." echo -n "."
done done
echo -e "${RED}\n✗ Connection failed.${NC}" echo -e "${RED}\n✗ Failed to connect to brain.${NC}"
echo "Check logs: tail -n 20 $SCRIPT_DIR/brain.log"
exit 1 exit 1