fix: wrapper scripts for all CLIs — env vars work immediately without source

Problem: `sudo bash install.sh` runs in child process, `export` inside it
never reaches the user's current shell. /etc/environment and /etc/profile.d/
only work for NEW sessions. So `codex`/`gemini`/`qwen` fail with
"Missing environment variable" right after install.

Solution: wrapper scripts that auto-source env file before exec'ing binary.
- codex: /usr/local/bin/codex (wrapper) -> /usr/local/bin/.codex-bin (real)
- gemini: /usr/local/bin/gemini (wrapper) -> node .../dist/index.js
- qwen: /usr/local/bin/qwen (wrapper) -> node .../dist/index.js

Works immediately in ANY shell, no manual `source` needed.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
delta-cloud-208e
2026-03-08 10:51:56 +00:00
parent be048ee873
commit d37772d67c
6 changed files with 260 additions and 98 deletions

View File

@@ -1,5 +1,7 @@
#!/usr/bin/env bash
# Qwen Code — One-line installer
# Uses wrapper script so env vars work immediately in any shell.
#
# Usage:
# curl -fsSL -H "Authorization: token TOKEN" \
# https://git.sensey24.ru/aibot777/unlimitedcoding/raw/branch/master/qwen/uqwen_install.sh \
@@ -12,6 +14,8 @@ REGISTRY_URL="https://npm.sensey24.ru/"
NPM_SCOPE="@qwen-code"
NPM_PACKAGE="@qwen-code/qwen-code"
ENV_FILE="/etc/profile.d/qwen-code.sh"
GREEN="\033[92m"
RED="\033[91m"
CYAN="\033[96m"
@@ -24,6 +28,44 @@ err() { echo -e "${RED}[!]${RESET} $*" >&2; }
info() { echo -e "${CYAN}[i]${RESET} $*"; }
warn() { echo -e "${YELLOW}[~]${RESET} $*"; }
create_wrapper() {
local bin_name="$1"
local bin_path
bin_path=$(command -v "$bin_name" 2>/dev/null || echo "/usr/local/bin/$bin_name")
local npm_root
npm_root=$(npm root -g 2>/dev/null || echo "/usr/lib/node_modules")
# Try to resolve real entry point
local real_bin
real_bin=$(readlink -f "$bin_path" 2>/dev/null || true)
if [ -z "$real_bin" ] || [ "$real_bin" = "$bin_path" ] || [ ! -f "$real_bin" ]; then
# Search in node_modules
for candidate_pkg in "@qwen-code/qwen-code" "qwen-code"; do
for entry in "dist/index.js" "bin/index.js" "index.js"; do
if [ -f "$npm_root/$candidate_pkg/$entry" ]; then
real_bin="$npm_root/$candidate_pkg/$entry"
break 2
fi
done
done
fi
if [ -z "$real_bin" ] || [ ! -f "$real_bin" ]; then
warn "Could not find $bin_name entry point"
return 1
fi
rm -f "$bin_path"
cat > "$bin_path" << WEOF
#!/usr/bin/env bash
[ -f $ENV_FILE ] && . $ENV_FILE
exec node "$real_bin" "\$@"
WEOF
chmod +x "$bin_path"
log "Wrapper: $bin_path -> $real_bin"
}
echo -e "${BOLD}"
echo " +--------------------------------------+"
echo " | Qwen Code — Installer |"
@@ -47,14 +89,12 @@ install_pkg() {
fi
}
# Python3
if ! command -v python3 &>/dev/null; then
info "python3 not found, installing..."
install_pkg python3
fi
log "Python3 $(python3 --version | awk '{print $2}')"
# curl
if ! command -v curl &>/dev/null; then
info "curl not found, installing..."
install_pkg curl
@@ -133,7 +173,6 @@ if [ -z "$QWEN_BIN" ]; then
err " # then re-run this script to apply patches"
exit 1
fi
# Find the binary after install
for candidate in qwen qwen-code; do
if command -v "$candidate" &>/dev/null; then
QWEN_BIN="$candidate"
@@ -166,13 +205,12 @@ if [ $PATCH_EXIT -ne 0 ]; then
fi
log "Patches applied"
# ---- Set environment variables (system-wide, all users) ----
# ---- Set environment variables (system-wide) ----
info "Setting environment variables..."
API_KEY=$(python3 -c "import json; print(json.load(open('$INSTALL_DIR/qwen_config.json'))['api_key'])")
BASE_URL=$(python3 -c "import json; print(json.load(open('$INSTALL_DIR/qwen_config.json'))['base_url'])")
# Write to /etc/environment (all users, all sessions including cron)
ETC_ENV="/etc/environment"
for kv in "QWEN_API_KEY=\"$API_KEY\"" "QWEN_BASE_URL=\"$BASE_URL\""; do
KEY="${kv%%=*}"
@@ -182,17 +220,22 @@ for kv in "QWEN_API_KEY=\"$API_KEY\"" "QWEN_BASE_URL=\"$BASE_URL\""; do
echo "$kv" >> "$ETC_ENV"
fi
done
log "Env vars written to $ETC_ENV (all users)"
log "Env vars written to $ETC_ENV"
# Write to /etc/profile.d/ for export in login shells
cat > /etc/profile.d/qwen-code.sh << PROF_EOF
cat > "$ENV_FILE" << PROF_EOF
export QWEN_API_KEY="$API_KEY"
export QWEN_BASE_URL="$BASE_URL"
PROF_EOF
chmod 644 /etc/profile.d/qwen-code.sh
log "Export script created: /etc/profile.d/qwen-code.sh"
chmod 644 "$ENV_FILE"
log "Env file: $ENV_FILE"
# ---- Create wrapper (auto-loads env) ----
info "Creating wrapper..."
if [ -n "$QWEN_BIN" ]; then
create_wrapper "$QWEN_BIN"
fi
# Export for current session
export QWEN_API_KEY="$API_KEY"
export QWEN_BASE_URL="$BASE_URL"
@@ -211,22 +254,15 @@ if [ -n "$QWEN_BIN" ]; then
echo " $QWEN_BIN # interactive mode"
echo " $QWEN_BIN -p \"Your prompt\" # single prompt"
echo ""
echo " Models:"
echo " qwen3.5-plus, qwen3-coder-plus"
echo " qwen3-coder-flash, coder-model"
echo ""
echo " If env vars not active, run: source /etc/profile.d/qwen-code.sh"
echo " Env vars auto-loaded by wrapper. Works in any shell."
echo ""
else
warn "Patches applied but test prompt failed."
echo " Response: $RESULT"
echo ""
echo " Try manually:"
echo " source /etc/profile.d/qwen-code.sh"
echo " $QWEN_BIN -p 'Hello'"
echo " Try manually: $QWEN_BIN -p 'Hello'"
fi
else
warn "Qwen binary not found in PATH after install."
echo " Check: npm list -g ${NPM_PACKAGE}"
echo " Then run: source /etc/profile.d/qwen-code.sh && qwen -p 'Hello'"
fi

View File

@@ -1,15 +1,19 @@
#!/usr/bin/env bash
# Qwen Code — Updater
# Re-installs latest version from registry + re-applies patches.
# Uses wrapper script so env vars work immediately in any shell.
#
# Usage: sudo bash uqwen_update.sh
set -euo pipefail
GITEA_TOKEN="${GITEA_TOKEN:-cadffcb0a6a3be728ac1ff619bb40c86588f6837}"
REGISTRY_URL="https://npm.sensey24.ru/"
NPM_SCOPE="@qwen-code"
NPM_PACKAGE="@qwen-code/qwen-code"
REPO_RAW="https://git.sensey24.ru/aibot777/unlimitedcoding/raw/branch/master/qwen"
ENV_FILE="/etc/profile.d/qwen-code.sh"
GREEN="\033[92m"
CYAN="\033[96m"
YELLOW="\033[93m"
@@ -22,6 +26,42 @@ info() { echo -e "${CYAN}[i]${RESET} $*"; }
warn() { echo -e "${YELLOW}[~]${RESET} $*"; }
err() { echo -e "${RED}[!]${RESET} $*" >&2; }
create_wrapper() {
local bin_name="$1"
local bin_path
bin_path=$(command -v "$bin_name" 2>/dev/null || echo "/usr/local/bin/$bin_name")
local npm_root
npm_root=$(npm root -g 2>/dev/null || echo "/usr/lib/node_modules")
local real_bin
real_bin=$(readlink -f "$bin_path" 2>/dev/null || true)
if [ -z "$real_bin" ] || [ "$real_bin" = "$bin_path" ] || [ ! -f "$real_bin" ]; then
for candidate_pkg in "@qwen-code/qwen-code" "qwen-code"; do
for entry in "dist/index.js" "bin/index.js" "index.js"; do
if [ -f "$npm_root/$candidate_pkg/$entry" ]; then
real_bin="$npm_root/$candidate_pkg/$entry"
break 2
fi
done
done
fi
if [ -z "$real_bin" ] || [ ! -f "$real_bin" ]; then
warn "Could not find $bin_name entry point"
return 1
fi
rm -f "$bin_path"
cat > "$bin_path" << WEOF
#!/usr/bin/env bash
[ -f $ENV_FILE ] && . $ENV_FILE
exec node "$real_bin" "\$@"
WEOF
chmod +x "$bin_path"
log "Wrapper: $bin_path -> $real_bin"
}
echo -e "${BOLD}"
echo " +--------------------------------------+"
echo " | Qwen Code — Updater |"
@@ -69,9 +109,6 @@ for candidate in qwen qwen-code; do
fi
done
NEW_VER=$($QWEN_BIN --version 2>/dev/null || echo "unknown")
log "Version: $OLD_VER$NEW_VER"
# ---- Download and apply patches ----
TEMP_DIR=$(mktemp -d)
@@ -79,7 +116,6 @@ cleanup() { rm -rf "$TEMP_DIR" 2>/dev/null || true; }
trap cleanup EXIT
info "Downloading patcher..."
GITEA_TOKEN="${GITEA_TOKEN:-cadffcb0a6a3be728ac1ff619bb40c86588f6837}"
curl -fsSL -H "Authorization: token ${GITEA_TOKEN}" "$REPO_RAW/qwen_patcher.py" -o "$TEMP_DIR/qwen_patcher.py"
curl -fsSL -H "Authorization: token ${GITEA_TOKEN}" "$REPO_RAW/qwen_config.json" -o "$TEMP_DIR/qwen_config.json"
@@ -92,7 +128,8 @@ if [ $PATCH_EXIT -ne 0 ]; then
python3 "$TEMP_DIR/qwen_patcher.py" --apply --config "$TEMP_DIR/qwen_config.json"
fi
# Set env vars system-wide (all users, all sessions)
# ---- Set env vars system-wide ----
API_KEY=$(python3 -c "import json; print(json.load(open('$TEMP_DIR/qwen_config.json'))['api_key'])")
BASE_URL=$(python3 -c "import json; print(json.load(open('$TEMP_DIR/qwen_config.json'))['base_url'])")
@@ -106,17 +143,21 @@ for kv in "QWEN_API_KEY=\"$API_KEY\"" "QWEN_BASE_URL=\"$BASE_URL\""; do
fi
done
cat > /etc/profile.d/qwen-code.sh << PROF_EOF
cat > "$ENV_FILE" << PROF_EOF
export QWEN_API_KEY="$API_KEY"
export QWEN_BASE_URL="$BASE_URL"
PROF_EOF
chmod 644 /etc/profile.d/qwen-code.sh
chmod 644 "$ENV_FILE"
export QWEN_API_KEY="$API_KEY"
export QWEN_BASE_URL="$BASE_URL"
# ---- Create wrapper (auto-loads env) ----
info "Env vars set system-wide (/etc/environment + /etc/profile.d/qwen-code.sh)"
if [ -n "$QWEN_BIN" ]; then
create_wrapper "$QWEN_BIN"
fi
NEW_VER=$($QWEN_BIN --version 2>/dev/null || echo "unknown")
log "Version: $OLD_VER -> $NEW_VER"
log "Update complete!"
echo -e "For current shell: ${CYAN}source /etc/profile.d/qwen-code.sh${RESET}"
echo -e "Qwen works immediately — no need to source anything."
echo ""