Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
170 changes: 141 additions & 29 deletions codegen-examples/examples/Deployer.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,52 @@ set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"

# Colors for output
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# Print banner
echo -e "${BLUE}╔════════════════════════════════════════════════════════════╗${NC}"
echo -e "${BLUE}║ Codegen Modal Deployer ║${NC}"
echo -e "${BLUE}╚════════════════════════════════════════════════════════════╝${NC}"
echo ""

# Check if Python is installed
if ! command -v python3 &> /dev/null; then
echo "Python 3 is required but not installed. Please install Python 3 and try again."
echo -e "${RED}Python 3 is required but not installed. Please install Python 3 and try again.${NC}"
exit 1
fi

# Check Python version
PYTHON_VERSION=$(python3 -c 'import sys; print(f"{sys.version_info.major}.{sys.version_info.minor}")')
PYTHON_VERSION_MAJOR=$(echo $PYTHON_VERSION | cut -d. -f1)
PYTHON_VERSION_MINOR=$(echo $PYTHON_VERSION | cut -d. -f2)

if [ "$PYTHON_VERSION_MAJOR" -lt 3 ] || ([ "$PYTHON_VERSION_MAJOR" -eq 3 ] && [ "$PYTHON_VERSION_MINOR" -lt 9 ]); then
echo -e "${YELLOW}Warning: Python 3.9+ is recommended. You are using Python $PYTHON_VERSION.${NC}"
read -p "Continue anyway? (y/n): " continue_anyway
if [ "$continue_anyway" != "y" ] && [ "$continue_anyway" != "Y" ]; then
echo "Exiting."
exit 0
fi
fi

# Check if Modal is installed
if ! python3 -c "import modal" &> /dev/null; then
echo "Modal is not installed. Installing now..."
pip install modal
echo -e "${YELLOW}Modal is not installed. Installing now...${NC}"
pip install modal==1.0.0
fi

# Check Modal version
MODAL_VERSION=$(python3 -c "import modal; print(modal.__version__)")
echo -e "${GREEN}Using Modal version: $MODAL_VERSION${NC}"

# Check if Modal token is set up
if ! modal token list &> /dev/null; then
echo "Modal token not set up. Please run 'modal token new' to set up your Modal token."
echo -e "${RED}Modal token not set up. Please run 'modal token new' to set up your Modal token.${NC}"
exit 1
fi

Expand All @@ -31,11 +62,32 @@ deploy_example() {
local example_name=$(basename "$example_dir")

if [ -f "$example_dir/deploy.sh" ]; then
echo "Deploying $example_name..."
echo -e "${BLUE}Deploying $example_name...${NC}"
(cd "$example_dir" && bash deploy.sh)
return $?
local status=$?
if [ $status -eq 0 ]; then
echo -e "${GREEN}✓ $example_name deployed successfully.${NC}"
else
echo -e "${RED}✗ $example_name deployment failed with status $status.${NC}"
fi
return $status
else
echo "No deploy.sh script found for $example_name. Skipping."
echo -e "${YELLOW}No deploy.sh script found for $example_name. Skipping.${NC}"
return 1
fi
}

# Function to verify deployment
verify_deployment() {
local example_name="$1"
local app_name="$2"

echo -e "${BLUE}Verifying deployment of $example_name...${NC}"
if modal app status "$app_name" | grep -q "RUNNING"; then
echo -e "${GREEN}✓ $example_name is running.${NC}"
return 0
else
echo -e "${YELLOW}! $example_name is not running. It may still be starting up or may have failed to deploy.${NC}"
return 1
fi
}
Expand All @@ -44,26 +96,30 @@ deploy_example() {
examples=()
for dir in "$SCRIPT_DIR"/*/; do
if [ -f "${dir}deploy.sh" ]; then
examples+=("$(basename "$dir")")
examples+=($(basename "$dir"))
fi
done

if [ ${#examples[@]} -eq 0 ]; then
echo "No deployable examples found."
echo -e "${RED}No deployable examples found.${NC}"
exit 1
fi

# Sort examples alphabetically
IFS=$'\n' examples=($(sort <<<"${examples[*]}"))
unset IFS

# Display menu
echo "Available examples for deployment:"
echo -e "${GREEN}Available examples for deployment:${NC}"
echo ""

for i in "${!examples[@]}"; do
echo "[$((i+1))] ${examples[$i]}"
echo -e "${BLUE}[$((i+1))] ${examples[$i]}${NC}"
done

echo ""
echo "[a] Deploy all examples"
echo "[q] Quit"
echo -e "${BLUE}[a] Deploy all examples${NC}"
echo -e "${BLUE}[q] Quit${NC}"
echo ""

# Get user selection
Expand All @@ -72,7 +128,7 @@ while true; do
read -p "Select examples to deploy (e.g., '1 3 5' or 'a' for all, 'q' to quit, 'd' when done): " selection

if [ "$selection" == "q" ]; then
echo "Exiting without deployment."
echo -e "${YELLOW}Exiting without deployment.${NC}"
exit 0
elif [ "$selection" == "a" ]; then
for i in "${!examples[@]}"; do
Expand All @@ -81,7 +137,7 @@ while true; do
break
elif [ "$selection" == "d" ]; then
if [ ${#selected_indices[@]} -eq 0 ]; then
echo "No examples selected. Please select at least one example."
echo -e "${YELLOW}No examples selected. Please select at least one example.${NC}"
else
break
fi
Expand All @@ -93,34 +149,34 @@ while true; do
# Check if already selected
if [[ ! " ${selected_indices[@]} " =~ " ${idx} " ]]; then
selected_indices+=($idx)
echo "Added ${examples[$idx]} to deployment list."
echo -e "${GREEN}Added ${examples[$idx]} to deployment list.${NC}"
else
echo "${examples[$idx]} is already selected."
echo -e "${YELLOW}${examples[$idx]} is already selected.${NC}"
fi
else
echo "Invalid selection: $num. Please enter numbers between 1 and ${#examples[@]}."
echo -e "${RED}Invalid selection: $num. Please enter numbers between 1 and ${#examples[@]}.${NC}"
fi
done
fi
done

# Show selected examples
echo ""
echo "Selected examples for deployment:"
echo -e "${GREEN}Selected examples for deployment:${NC}"
for idx in "${selected_indices[@]}"; do
echo "- ${examples[$idx]}"
echo -e "${BLUE}- ${examples[$idx]}${NC}"
done
echo ""

# Confirm deployment
read -p "Deploy these examples? (y/n): " confirm
if [ "$confirm" != "y" ] && [ "$confirm" != "Y" ]; then
echo "Deployment cancelled."
echo -e "${YELLOW}Deployment cancelled.${NC}"
exit 0
fi

# Deploy selected examples concurrently
echo "Starting deployment of selected examples..."
echo -e "${GREEN}Starting deployment of selected examples...${NC}"
pids=()
results=()

Expand All @@ -142,8 +198,8 @@ done

# Print summary
echo ""
echo "Deployment Summary:"
echo "=================="
echo -e "${BLUE}Deployment Summary:${NC}"
echo -e "${BLUE}==================${NC}"
success_count=0
failure_count=0

Expand All @@ -153,23 +209,79 @@ for i in "${!selected_indices[@]}"; do
result="${results[$i]}"

if [ "$result" -eq 0 ]; then
echo "✅ ${example}: SUCCESS"
echo -e "${GREEN}✓ ${example}: SUCCESS${NC}"
((success_count++))
else
echo "❌ ${example}: FAILED"
echo -e "${RED}✗ ${example}: FAILED${NC}"
((failure_count++))
fi
done

echo ""
echo "Total: $((success_count + failure_count)), Successful: $success_count, Failed: $failure_count"
echo -e "${BLUE}Total: $((success_count + failure_count)), Successful: $success_count, Failed: $failure_count${NC}"

if [ "$failure_count" -gt 0 ]; then
echo ""
echo "Some deployments failed. Check the logs above for details."
echo -e "${RED}Some deployments failed. Check the logs above for details.${NC}"
exit 1
fi

echo ""
echo "All deployments completed successfully!"
echo -e "${GREEN}All deployments completed successfully!${NC}"

# Offer to view logs
echo ""
echo -e "${BLUE}Options:${NC}"
echo -e "${BLUE}[l] View logs for a deployed example${NC}"
echo -e "${BLUE}[s] View status of all deployed examples${NC}"
echo -e "${BLUE}[q] Quit${NC}"
echo ""

read -p "Select an option: " option
if [ "$option" == "l" ]; then
echo ""
echo -e "${BLUE}Select an example to view logs:${NC}"
for i in "${!selected_indices[@]}"; do
idx="${selected_indices[$i]}"
echo -e "${BLUE}[$((i+1))] ${examples[$idx]}${NC}"
done
echo ""

read -p "Enter number: " log_selection
if [[ "$log_selection" =~ ^[0-9]+$ ]] && [ "$log_selection" -ge 1 ] && [ "$log_selection" -le ${#selected_indices[@]} ]; then
log_idx=$((log_selection-1))
selected_idx="${selected_indices[$log_idx]}"
example="${examples[$selected_idx]}"

# Extract app name from deploy.sh
app_name=$(grep -o "modal app [a-zA-Z0-9_-]*" "$SCRIPT_DIR/$example/deploy.sh" | head -1 | awk '{print $3}')
if [ -z "$app_name" ]; then
# Try to guess app name from example name
app_name=$(echo "$example" | tr '_' '-')
fi

echo -e "${BLUE}Viewing logs for $example (app: $app_name)...${NC}"
modal app logs "$app_name"
else
echo -e "${RED}Invalid selection.${NC}"
fi
elif [ "$option" == "s" ]; then
echo ""
echo -e "${BLUE}Status of deployed examples:${NC}"
for i in "${!selected_indices[@]}"; do
idx="${selected_indices[$i]}"
example="${examples[$idx]}"

# Extract app name from deploy.sh
app_name=$(grep -o "modal app [a-zA-Z0-9_-]*" "$SCRIPT_DIR/$example/deploy.sh" | head -1 | awk '{print $3}')
if [ -z "$app_name" ]; then
# Try to guess app name from example name
app_name=$(echo "$example" | tr '_' '-')
fi

echo -e "${BLUE}$example (app: $app_name):${NC}"
modal app status "$app_name" | grep -E "RUNNING|STOPPED|FAILED"
done
fi

echo -e "${GREEN}Thank you for using the Codegen Modal Deployer!${NC}"
78 changes: 78 additions & 0 deletions codegen-examples/examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# Codegen Examples

This directory contains a collection of examples demonstrating various use cases of the [Codegen](https://codegen.com) SDK.

## Types of Examples

The examples in this directory fall into two main categories:

1. **Code Transformation Examples**: One-time utilities that transform code in various ways (e.g., migrating from one library to another, converting code patterns, etc.)

2. **Modal-based Service Examples**: Applications that can be deployed as services using [Modal](https://modal.com), such as chatbots, webhooks handlers, and analytics tools.

## Using the Modal Deployer

For Modal-based examples, we provide a convenient deployment tool called `Deployer.sh` that allows you to interactively select and deploy multiple examples concurrently.

### Prerequisites

- Python 3.9 or higher
- [Modal](https://modal.com/) account and CLI
- Git

### Running the Deployer

To use the deployer:

```bash
# Navigate to the examples directory
cd examples

# Run the deployer script
bash Deployer.sh
```

The deployer will:

1. Check for required dependencies (Python, Modal)
2. Display a list of deployable examples
3. Allow you to select which examples to deploy
4. Deploy the selected examples concurrently
5. Provide a summary of deployment results
6. Offer options to view logs or status of deployed examples

### Available Modal Examples

The following examples can be deployed using the Deployer.sh script:

- `ai_impact_analysis`: Analyze the impact of AI on codebases
- `codegen-mcp-server`: MCP server implementation
- `codegen_app`: Codegen web application
- `cyclomatic_complexity`: Calculate cyclomatic complexity of code
- `deep_code_research`: Deep research on code repositories
- `delete_dead_code`: Identify and remove dead code
- `document_functions`: Automatically document functions
- `github_checks`: GitHub checks integration
- `linear_webhooks`: Linear webhooks handler
- `modal_repo_analytics`: Repository analytics using Modal
- `pr_review_bot`: PR review automation
- `repo_analytics`: Repository analytics tools
- `slack_chatbot`: Slack chatbot integration
- `snapshot_event_handler`: Event handler for snapshots
- `swebench_agent_run`: SWE benchmark agent
- `ticket-to-pr`: Convert tickets to PRs

Each of these examples has its own `deploy.sh` script and README with specific deployment instructions.

## Running Non-Modal Examples

For examples that don't have a `deploy.sh` script, you can run them locally following the instructions in their respective README files. These examples typically perform one-time code transformations and don't need to be deployed as services.

## Contributing

If you'd like to add a new example, please follow the [Contributing Guide](../CONTRIBUTING.md) for instructions.

## License

All examples are licensed under the [Apache 2.0 license](../LICENSE).

Loading
Loading