teleapi / init.sh
privateone's picture
Update init.sh
2343246 verified
#!/bin/bash
set -e # Exit if any command fails
# echo "πŸ” GITHUB_PAT: ${GITHUB_PAT}" # Mask token in logs
# Define variables
REPO_URL="https://github.com/blendersb/teleapi.git"
BRANCH="${BRANCH:=main}"
TARGET_DIR="/app/teleapi"
APP_DIR="/app"
# Only clone if GITHUB_PAT is set
if [ -n "$GITHUB_PAT" ]; then
echo "πŸ“₯ GITHUB_PAT detected. Cloning private repository..."
# Clone only if the target directory does not exist
if [ ! -d "$TARGET_DIR" ]; then
if [ -n "$CODE_REPO" ]; then
echo "πŸ“₯ CODE_REPOSITRY detected. Cloning private repository..."
git clone -b "$BRANCH" "$CODE_REPO" "$TARGET_DIR"
else
echo "πŸ“₯ Cloning private repository..."
git clone -b "$BRANCH" "https://blendersb:${GITHUB_PAT}@github.com/blendersb/teleapi.git" "$TARGET_DIR"
fi
cd "$TARGET_DIR"
echo "πŸ“ Latest Git Commit:"
git log -1 --pretty=format:"πŸ”Ή %h πŸ”Ή %s πŸ”Ή by %an πŸ”Ή on %ad" --date=short
echo
echo "πŸ“‚ Moving contents from $TARGET_DIR to $APP_DIR..."
shopt -s dotglob # include hidden files like .env, .gitignore
mv "$TARGET_DIR"/* "$APP_DIR"/
rm -rf "$TARGET_DIR"
shopt -u dotglob
# Set permissions (optional)
echo "πŸ”§ Setting main folder permissions..."
chmod -R +x "$APP_DIR"
# chmod -R 777 "$APP_DIR/*"
ls -la "$APP_DIR"
echo "πŸ”§ Setting up sessions & permissions..."
SESSION_DIR="$APP_DIR/sessions"
chmod -R 777 "$APP_DIR/sessions"
echo "πŸ” Scanning files in: $SESSION_DIR"
echo
find "$SESSION_DIR" -type f | while read -r file; do
perms=$(stat -c "%A" "$file") # Get permission string
if [ -w "$file" ]; then
writable="βœ… Writable"
else
writable="❌ Not Writable"
fi
echo "$perms $writable β†’ $file"
# rm -rf "$file"
done
else
echo "βœ… Repository already cloned at $TARGET_DIR"
fi
# Change to repo directory
cd "$APP_DIR"
# Install Python dependencies
if [ -f "$APP_DIR/requirements.txt" ]; then
echo "πŸ“¦ Installing Python dependencies..."
pip3 install --no-cache-dir --upgrade -r "requirements.txt"
else
echo "⚠️ No requirements.txt found in $APP_DIR"
fi
echo "βœ… Init script complete. Starting application..."
# Start the actual app (from CMD)
exec "$@"
else
echo "ℹ️ GITHUB_PAT not set. Skipping clone step."
fi