171 lines
3.7 KiB
Bash
Executable File
171 lines
3.7 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
|
|
CONFIG_FILE="/opt/appfactory/config/appfactory.env"
|
|
source "$CONFIG_FILE"
|
|
|
|
APP_ID="${1:-}"
|
|
APP_NAME="${2:-$APP_ID}"
|
|
|
|
DB_FILE="/opt/appfactory/data/appfactory/appfactory.db"
|
|
TEMPLATE_DIR="$WORKSPACE_DIR/python-fastapi-template"
|
|
TARGET_DIR="$WORKSPACE_DIR/$APP_ID"
|
|
TOOLS_DIR="$WORKSPACE_DIR/appfactory-tools"
|
|
|
|
if [ -z "$APP_ID" ]; then
|
|
echo "Usage: ./new-python-app.sh <app-id> [app-name]"
|
|
exit 1
|
|
fi
|
|
|
|
if [ ! -f "$DB_FILE" ]; then
|
|
echo "Missing database: $DB_FILE"
|
|
echo "Run: $TOOLS_DIR/scripts/init-appfactory-db.sh"
|
|
exit 1
|
|
fi
|
|
|
|
if [ ! -d "$TEMPLATE_DIR" ]; then
|
|
echo "Template not found: $TEMPLATE_DIR"
|
|
exit 1
|
|
fi
|
|
|
|
if [ -d "$TARGET_DIR" ]; then
|
|
echo "Target already exists: $TARGET_DIR"
|
|
exit 1
|
|
fi
|
|
|
|
echo "Creating app from template..."
|
|
cp -R "$TEMPLATE_DIR" "$TARGET_DIR"
|
|
|
|
cd "$TARGET_DIR"
|
|
rm -rf .git
|
|
|
|
sed -i "s/Python FastAPI Template/$APP_NAME/g" app/main.py
|
|
|
|
cat > app.yml <<EOF_APP
|
|
id: $APP_ID
|
|
name: $APP_NAME
|
|
language: python
|
|
version: 1.0.0
|
|
base_path: /apps/$APP_ID
|
|
port: 8000
|
|
status: development
|
|
EOF_APP
|
|
|
|
cat > README.md <<EOF_README
|
|
# $APP_NAME
|
|
|
|
Generated by AppFactory.
|
|
EOF_README
|
|
|
|
echo "Creating Gitea repository..."
|
|
|
|
curl -sS -X POST \
|
|
"$GITEA_URL/api/v1/orgs/$GITEA_ORG/repos" \
|
|
-H "Authorization: token $GITEA_TOKEN" \
|
|
-H "Content-Type: application/json" \
|
|
-d "{
|
|
\"name\": \"$APP_ID\",
|
|
\"description\": \"$APP_NAME\",
|
|
\"private\": false,
|
|
\"auto_init\": false
|
|
}" > /tmp/appfactory-create-repo-response.json
|
|
|
|
if grep -q '"message"' /tmp/appfactory-create-repo-response.json; then
|
|
cat /tmp/appfactory-create-repo-response.json
|
|
fi
|
|
|
|
echo "Creating Gitea webhook..."
|
|
|
|
curl -sS -X POST \
|
|
"$GITEA_URL/api/v1/repos/$GITEA_ORG/$APP_ID/hooks" \
|
|
-H "Authorization: token $GITEA_TOKEN" \
|
|
-H "Content-Type: application/json" \
|
|
-d "{
|
|
\"type\": \"gitea\",
|
|
\"active\": true,
|
|
\"events\": [\"push\"],
|
|
\"config\": {
|
|
\"url\": \"$WEBHOOK_INTERNAL_URL\",
|
|
\"content_type\": \"json\",
|
|
\"secret\": \"$WEBHOOK_SECRET\"
|
|
}
|
|
}" > /tmp/appfactory-create-hook-response.json
|
|
|
|
if grep -q '"message"' /tmp/appfactory-create-hook-response.json; then
|
|
cat /tmp/appfactory-create-hook-response.json
|
|
fi
|
|
|
|
git init
|
|
git config user.name "AppFactory Bot"
|
|
git config user.email "appfactory@local"
|
|
git branch -M main
|
|
|
|
if [[ "$GITEA_URL" == http://* ]]; then
|
|
GITEA_PUSH_URL="${GITEA_URL/http:\/\//http:\/\/${GITEA_TOKEN}@}"
|
|
elif [[ "$GITEA_URL" == https://* ]]; then
|
|
GITEA_PUSH_URL="${GITEA_URL/https:\/\//https:\/\/${GITEA_TOKEN}@}"
|
|
else
|
|
echo "Unsupported GITEA_URL: $GITEA_URL"
|
|
exit 1
|
|
fi
|
|
git remote add origin "$GITEA_PUSH_URL/$GITEA_ORG/$APP_ID.git"
|
|
|
|
git add .
|
|
git commit -m "Initial $APP_NAME"
|
|
git push -u origin main
|
|
|
|
echo "Saving app to database..."
|
|
|
|
python3 - "$DB_FILE" "$APP_ID" "$APP_NAME" <<'PY'
|
|
import sqlite3
|
|
import sys
|
|
|
|
db_file = sys.argv[1]
|
|
app_id = sys.argv[2]
|
|
app_name = sys.argv[3]
|
|
|
|
con = sqlite3.connect(db_file)
|
|
|
|
con.execute(
|
|
"""
|
|
INSERT INTO apps (
|
|
id,
|
|
name,
|
|
language,
|
|
version,
|
|
status
|
|
)
|
|
VALUES (?, ?, ?, ?, ?)
|
|
ON CONFLICT(id) DO UPDATE SET
|
|
name = excluded.name,
|
|
language = excluded.language,
|
|
version = excluded.version,
|
|
status = excluded.status,
|
|
updated_at = CURRENT_TIMESTAMP
|
|
""",
|
|
(
|
|
app_id,
|
|
app_name or app_id,
|
|
"python",
|
|
"1.0.0",
|
|
"created",
|
|
),
|
|
)
|
|
|
|
con.commit()
|
|
con.close()
|
|
PY
|
|
|
|
"$TOOLS_DIR/scripts/generate-catalog.sh"
|
|
"$TOOLS_DIR/scripts/generate-apps-compose.sh"
|
|
"$TOOLS_DIR/scripts/generate-caddyfile.sh"
|
|
"$TOOLS_DIR/scripts/generate-apps-page.sh"
|
|
|
|
echo ""
|
|
echo "DONE"
|
|
echo "Repository:"
|
|
echo "$GITEA_URL/$GITEA_ORG/$APP_ID"
|
|
echo ""
|
|
echo "Local path:"
|
|
echo "$TARGET_DIR"
|