Skip to content

Commit 67d323c

Browse files
authored
Merge pull request #141 from Zeeeepa/codegen-bot/modal-deployment-updates
2 parents 4feb18b + a25be0a commit 67d323c

File tree

8 files changed

+576
-155
lines changed

8 files changed

+576
-155
lines changed

codegen-examples/examples/Deployer.sh

Lines changed: 141 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,52 @@ set -e
77
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
88
cd "$SCRIPT_DIR"
99

10+
# Colors for output
11+
GREEN='\033[0;32m'
12+
RED='\033[0;31m'
13+
YELLOW='\033[0;33m'
14+
BLUE='\033[0;34m'
15+
NC='\033[0m' # No Color
16+
17+
# Print banner
18+
echo -e "${BLUE}╔════════════════════════════════════════════════════════════╗${NC}"
19+
echo -e "${BLUE}║ Codegen Modal Deployer ║${NC}"
20+
echo -e "${BLUE}╚════════════════════════════════════════════════════════════╝${NC}"
21+
echo ""
22+
1023
# Check if Python is installed
1124
if ! command -v python3 &> /dev/null; then
12-
echo "Python 3 is required but not installed. Please install Python 3 and try again."
25+
echo -e "${RED}Python 3 is required but not installed. Please install Python 3 and try again.${NC}"
1326
exit 1
1427
fi
1528

29+
# Check Python version
30+
PYTHON_VERSION=$(python3 -c 'import sys; print(f"{sys.version_info.major}.{sys.version_info.minor}")')
31+
PYTHON_VERSION_MAJOR=$(echo $PYTHON_VERSION | cut -d. -f1)
32+
PYTHON_VERSION_MINOR=$(echo $PYTHON_VERSION | cut -d. -f2)
33+
34+
if [ "$PYTHON_VERSION_MAJOR" -lt 3 ] || ([ "$PYTHON_VERSION_MAJOR" -eq 3 ] && [ "$PYTHON_VERSION_MINOR" -lt 9 ]); then
35+
echo -e "${YELLOW}Warning: Python 3.9+ is recommended. You are using Python $PYTHON_VERSION.${NC}"
36+
read -p "Continue anyway? (y/n): " continue_anyway
37+
if [ "$continue_anyway" != "y" ] && [ "$continue_anyway" != "Y" ]; then
38+
echo "Exiting."
39+
exit 0
40+
fi
41+
fi
42+
1643
# Check if Modal is installed
1744
if ! python3 -c "import modal" &> /dev/null; then
18-
echo "Modal is not installed. Installing now..."
19-
pip install modal
45+
echo -e "${YELLOW}Modal is not installed. Installing now...${NC}"
46+
pip install modal==1.0.0
2047
fi
2148

49+
# Check Modal version
50+
MODAL_VERSION=$(python3 -c "import modal; print(modal.__version__)")
51+
echo -e "${GREEN}Using Modal version: $MODAL_VERSION${NC}"
52+
2253
# Check if Modal token is set up
2354
if ! modal token list &> /dev/null; then
24-
echo "Modal token not set up. Please run 'modal token new' to set up your Modal token."
55+
echo -e "${RED}Modal token not set up. Please run 'modal token new' to set up your Modal token.${NC}"
2556
exit 1
2657
fi
2758

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

3364
if [ -f "$example_dir/deploy.sh" ]; then
34-
echo "Deploying $example_name..."
65+
echo -e "${BLUE}Deploying $example_name...${NC}"
3566
(cd "$example_dir" && bash deploy.sh)
36-
return $?
67+
local status=$?
68+
if [ $status -eq 0 ]; then
69+
echo -e "${GREEN}$example_name deployed successfully.${NC}"
70+
else
71+
echo -e "${RED}$example_name deployment failed with status $status.${NC}"
72+
fi
73+
return $status
3774
else
38-
echo "No deploy.sh script found for $example_name. Skipping."
75+
echo -e "${YELLOW}No deploy.sh script found for $example_name. Skipping.${NC}"
76+
return 1
77+
fi
78+
}
79+
80+
# Function to verify deployment
81+
verify_deployment() {
82+
local example_name="$1"
83+
local app_name="$2"
84+
85+
echo -e "${BLUE}Verifying deployment of $example_name...${NC}"
86+
if modal app status "$app_name" | grep -q "RUNNING"; then
87+
echo -e "${GREEN}$example_name is running.${NC}"
88+
return 0
89+
else
90+
echo -e "${YELLOW}! $example_name is not running. It may still be starting up or may have failed to deploy.${NC}"
3991
return 1
4092
fi
4193
}
@@ -44,26 +96,30 @@ deploy_example() {
4496
examples=()
4597
for dir in "$SCRIPT_DIR"/*/; do
4698
if [ -f "${dir}deploy.sh" ]; then
47-
examples+=("$(basename "$dir")")
99+
examples+=($(basename "$dir"))
48100
fi
49101
done
50102

51103
if [ ${#examples[@]} -eq 0 ]; then
52-
echo "No deployable examples found."
104+
echo -e "${RED}No deployable examples found.${NC}"
53105
exit 1
54106
fi
55107

108+
# Sort examples alphabetically
109+
IFS=$'\n' examples=($(sort <<<"${examples[*]}"))
110+
unset IFS
111+
56112
# Display menu
57-
echo "Available examples for deployment:"
113+
echo -e "${GREEN}Available examples for deployment:${NC}"
58114
echo ""
59115

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

64120
echo ""
65-
echo "[a] Deploy all examples"
66-
echo "[q] Quit"
121+
echo -e "${BLUE}[a] Deploy all examples${NC}"
122+
echo -e "${BLUE}[q] Quit${NC}"
67123
echo ""
68124

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

74130
if [ "$selection" == "q" ]; then
75-
echo "Exiting without deployment."
131+
echo -e "${YELLOW}Exiting without deployment.${NC}"
76132
exit 0
77133
elif [ "$selection" == "a" ]; then
78134
for i in "${!examples[@]}"; do
@@ -81,7 +137,7 @@ while true; do
81137
break
82138
elif [ "$selection" == "d" ]; then
83139
if [ ${#selected_indices[@]} -eq 0 ]; then
84-
echo "No examples selected. Please select at least one example."
140+
echo -e "${YELLOW}No examples selected. Please select at least one example.${NC}"
85141
else
86142
break
87143
fi
@@ -93,34 +149,34 @@ while true; do
93149
# Check if already selected
94150
if [[ ! " ${selected_indices[@]} " =~ " ${idx} " ]]; then
95151
selected_indices+=($idx)
96-
echo "Added ${examples[$idx]} to deployment list."
152+
echo -e "${GREEN}Added ${examples[$idx]} to deployment list.${NC}"
97153
else
98-
echo "${examples[$idx]} is already selected."
154+
echo -e "${YELLOW}${examples[$idx]} is already selected.${NC}"
99155
fi
100156
else
101-
echo "Invalid selection: $num. Please enter numbers between 1 and ${#examples[@]}."
157+
echo -e "${RED}Invalid selection: $num. Please enter numbers between 1 and ${#examples[@]}.${NC}"
102158
fi
103159
done
104160
fi
105161
done
106162

107163
# Show selected examples
108164
echo ""
109-
echo "Selected examples for deployment:"
165+
echo -e "${GREEN}Selected examples for deployment:${NC}"
110166
for idx in "${selected_indices[@]}"; do
111-
echo "- ${examples[$idx]}"
167+
echo -e "${BLUE}- ${examples[$idx]}${NC}"
112168
done
113169
echo ""
114170

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

122178
# Deploy selected examples concurrently
123-
echo "Starting deployment of selected examples..."
179+
echo -e "${GREEN}Starting deployment of selected examples...${NC}"
124180
pids=()
125181
results=()
126182

@@ -142,8 +198,8 @@ done
142198

143199
# Print summary
144200
echo ""
145-
echo "Deployment Summary:"
146-
echo "=================="
201+
echo -e "${BLUE}Deployment Summary:${NC}"
202+
echo -e "${BLUE}==================${NC}"
147203
success_count=0
148204
failure_count=0
149205

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

155211
if [ "$result" -eq 0 ]; then
156-
echo " ${example}: SUCCESS"
212+
echo -e "${GREEN} ${example}: SUCCESS${NC}"
157213
((success_count++))
158214
else
159-
echo " ${example}: FAILED"
215+
echo -e "${RED} ${example}: FAILED${NC}"
160216
((failure_count++))
161217
fi
162218
done
163219

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

167223
if [ "$failure_count" -gt 0 ]; then
168224
echo ""
169-
echo "Some deployments failed. Check the logs above for details."
225+
echo -e "${RED}Some deployments failed. Check the logs above for details.${NC}"
170226
exit 1
171227
fi
172228

173229
echo ""
174-
echo "All deployments completed successfully!"
230+
echo -e "${GREEN}All deployments completed successfully!${NC}"
231+
232+
# Offer to view logs
233+
echo ""
234+
echo -e "${BLUE}Options:${NC}"
235+
echo -e "${BLUE}[l] View logs for a deployed example${NC}"
236+
echo -e "${BLUE}[s] View status of all deployed examples${NC}"
237+
echo -e "${BLUE}[q] Quit${NC}"
238+
echo ""
239+
240+
read -p "Select an option: " option
241+
if [ "$option" == "l" ]; then
242+
echo ""
243+
echo -e "${BLUE}Select an example to view logs:${NC}"
244+
for i in "${!selected_indices[@]}"; do
245+
idx="${selected_indices[$i]}"
246+
echo -e "${BLUE}[$((i+1))] ${examples[$idx]}${NC}"
247+
done
248+
echo ""
249+
250+
read -p "Enter number: " log_selection
251+
if [[ "$log_selection" =~ ^[0-9]+$ ]] && [ "$log_selection" -ge 1 ] && [ "$log_selection" -le ${#selected_indices[@]} ]; then
252+
log_idx=$((log_selection-1))
253+
selected_idx="${selected_indices[$log_idx]}"
254+
example="${examples[$selected_idx]}"
255+
256+
# Extract app name from deploy.sh
257+
app_name=$(grep -o "modal app [a-zA-Z0-9_-]*" "$SCRIPT_DIR/$example/deploy.sh" | head -1 | awk '{print $3}')
258+
if [ -z "$app_name" ]; then
259+
# Try to guess app name from example name
260+
app_name=$(echo "$example" | tr '_' '-')
261+
fi
262+
263+
echo -e "${BLUE}Viewing logs for $example (app: $app_name)...${NC}"
264+
modal app logs "$app_name"
265+
else
266+
echo -e "${RED}Invalid selection.${NC}"
267+
fi
268+
elif [ "$option" == "s" ]; then
269+
echo ""
270+
echo -e "${BLUE}Status of deployed examples:${NC}"
271+
for i in "${!selected_indices[@]}"; do
272+
idx="${selected_indices[$i]}"
273+
example="${examples[$idx]}"
274+
275+
# Extract app name from deploy.sh
276+
app_name=$(grep -o "modal app [a-zA-Z0-9_-]*" "$SCRIPT_DIR/$example/deploy.sh" | head -1 | awk '{print $3}')
277+
if [ -z "$app_name" ]; then
278+
# Try to guess app name from example name
279+
app_name=$(echo "$example" | tr '_' '-')
280+
fi
281+
282+
echo -e "${BLUE}$example (app: $app_name):${NC}"
283+
modal app status "$app_name" | grep -E "RUNNING|STOPPED|FAILED"
284+
done
285+
fi
175286

287+
echo -e "${GREEN}Thank you for using the Codegen Modal Deployer!${NC}"

codegen-examples/examples/README.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# Codegen Examples
2+
3+
This directory contains a collection of examples demonstrating various use cases of the [Codegen](https://codegen.com) SDK.
4+
5+
## Types of Examples
6+
7+
The examples in this directory fall into two main categories:
8+
9+
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.)
10+
11+
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.
12+
13+
## Using the Modal Deployer
14+
15+
For Modal-based examples, we provide a convenient deployment tool called `Deployer.sh` that allows you to interactively select and deploy multiple examples concurrently.
16+
17+
### Prerequisites
18+
19+
- Python 3.9 or higher
20+
- [Modal](https://modal.com/) account and CLI
21+
- Git
22+
23+
### Running the Deployer
24+
25+
To use the deployer:
26+
27+
```bash
28+
# Navigate to the examples directory
29+
cd examples
30+
31+
# Run the deployer script
32+
bash Deployer.sh
33+
```
34+
35+
The deployer will:
36+
37+
1. Check for required dependencies (Python, Modal)
38+
2. Display a list of deployable examples
39+
3. Allow you to select which examples to deploy
40+
4. Deploy the selected examples concurrently
41+
5. Provide a summary of deployment results
42+
6. Offer options to view logs or status of deployed examples
43+
44+
### Available Modal Examples
45+
46+
The following examples can be deployed using the Deployer.sh script:
47+
48+
- `ai_impact_analysis`: Analyze the impact of AI on codebases
49+
- `codegen-mcp-server`: MCP server implementation
50+
- `codegen_app`: Codegen web application
51+
- `cyclomatic_complexity`: Calculate cyclomatic complexity of code
52+
- `deep_code_research`: Deep research on code repositories
53+
- `delete_dead_code`: Identify and remove dead code
54+
- `document_functions`: Automatically document functions
55+
- `github_checks`: GitHub checks integration
56+
- `linear_webhooks`: Linear webhooks handler
57+
- `modal_repo_analytics`: Repository analytics using Modal
58+
- `pr_review_bot`: PR review automation
59+
- `repo_analytics`: Repository analytics tools
60+
- `slack_chatbot`: Slack chatbot integration
61+
- `snapshot_event_handler`: Event handler for snapshots
62+
- `swebench_agent_run`: SWE benchmark agent
63+
- `ticket-to-pr`: Convert tickets to PRs
64+
65+
Each of these examples has its own `deploy.sh` script and README with specific deployment instructions.
66+
67+
## Running Non-Modal Examples
68+
69+
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.
70+
71+
## Contributing
72+
73+
If you'd like to add a new example, please follow the [Contributing Guide](../CONTRIBUTING.md) for instructions.
74+
75+
## License
76+
77+
All examples are licensed under the [Apache 2.0 license](../LICENSE).
78+

0 commit comments

Comments
 (0)