mirror of
https://github.com/nomadkaraoke/karaoke-gen.git
synced 2026-12-05 17:38:25 +09:00
## Summary
- Refactor monolithic `infrastructure/__main__.py` from **2,602 lines to
339 lines** (87% reduction)
- Split resources into organized, purpose-specific modules following
SOLID principles
- Extract VM startup scripts to separate `.sh` files for easier
maintenance
- Extract embedded Python encoding worker app to proper package
## Changes
### New Module Structure
```
infrastructure/
├── __main__.py # Entry point (339 lines, was 2,602)
├── config.py # Centralized configuration
├── modules/ # Core GCP resources
│ ├── database.py # Firestore + indexes
│ ├── storage.py # GCS bucket + lifecycle
│ ├── artifact_registry.py # Docker repository
│ ├── secrets.py # Secret Manager secrets
│ ├── cloud_tasks.py # Task queues
│ ├── cloud_run.py # Domain mapping, Cloud Run Jobs
│ ├── monitoring.py # Alert policies
│ └── iam/ # Service accounts by purpose
├── compute/ # VM-based resources
│ ├── flacfetch_vm.py
│ ├── encoding_worker_vm.py
│ ├── github_runners.py
│ └── startup_scripts/ # External .sh files
└── docs/
└── PHASE5-PACKER-IMAGE-PLAN.md # Future optimization plan
```
### Extracted Code
- **Startup scripts**: Moved from embedded strings to separate `.sh`
files
- **GCE encoding service**: Extracted ~600-line Python app to
`backend/services/gce_encoding/`
### Documentation
- Updated `infrastructure/README.md` with new structure
- Added `docs/PHASE5-PACKER-IMAGE-PLAN.md` for future Packer image
optimization
## Verification
```bash
pulumi preview --diff
# Result: 120 resources unchanged
# Only 10 GitHub runner replacements (expected - startup script improvements from main)
```
## Test Plan
- [x] `pulumi preview` shows no unintended infrastructure changes
- [x] All module imports resolve correctly
- [x] Startup scripts produce identical output when read from files
## Notes
- Phase 5 (Packer custom images) documented but not implemented -
reduces encoding worker startup from ~10min to ~30sec
- `deploy.sh` for gdrive_validator kept - handles source upload to GCS
which Pulumi doesn't do automatically
@coderabbitai ignore
---
🤖 Generated with [Claude Code](https://claude.com/claude-code)
---------
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
89 lines
2.8 KiB
Python
89 lines
2.8 KiB
Python
"""
|
|
Cloud Run resources.
|
|
|
|
Manages Cloud Run domain mapping and Cloud Run Jobs.
|
|
"""
|
|
|
|
import pulumi
|
|
import pulumi_gcp as gcp
|
|
from pulumi_gcp import cloudrun, cloudrunv2
|
|
|
|
from config import PROJECT_ID, REGION
|
|
|
|
|
|
def create_domain_mapping() -> cloudrun.DomainMapping:
|
|
"""
|
|
Create the Cloud Run domain mapping for api.nomadkaraoke.com.
|
|
|
|
Returns:
|
|
cloudrun.DomainMapping: The domain mapping resource.
|
|
"""
|
|
domain_mapping = cloudrun.DomainMapping(
|
|
"karaoke-backend-domain",
|
|
location=REGION,
|
|
name="api.nomadkaraoke.com",
|
|
metadata=cloudrun.DomainMappingMetadataArgs(
|
|
namespace=PROJECT_ID,
|
|
),
|
|
spec=cloudrun.DomainMappingSpecArgs(
|
|
route_name="karaoke-backend",
|
|
),
|
|
)
|
|
|
|
return domain_mapping
|
|
|
|
|
|
def create_video_encoding_job(
|
|
bucket: gcp.storage.Bucket,
|
|
service_account: gcp.serviceaccount.Account,
|
|
) -> cloudrunv2.Job:
|
|
"""
|
|
Create the Cloud Run Job for video encoding.
|
|
|
|
Cloud Run Jobs provide up to 24-hour execution time for long-running video encoding.
|
|
This is used when video encoding might exceed the 30-minute Cloud Tasks timeout.
|
|
|
|
Args:
|
|
bucket: The GCS bucket for job artifacts.
|
|
service_account: The service account to run the job.
|
|
|
|
Returns:
|
|
cloudrunv2.Job: The Cloud Run Job resource.
|
|
"""
|
|
video_encoding_job = cloudrunv2.Job(
|
|
"video-encoding-job",
|
|
name="video-encoding-job",
|
|
location=REGION,
|
|
template=cloudrunv2.JobTemplateArgs(
|
|
template=cloudrunv2.JobTemplateTemplateArgs(
|
|
containers=[
|
|
cloudrunv2.JobTemplateTemplateContainerArgs(
|
|
image=f"{REGION}-docker.pkg.dev/{PROJECT_ID}/karaoke-repo/karaoke-backend:latest",
|
|
args=["python", "-m", "backend.workers.video_worker"],
|
|
resources=cloudrunv2.JobTemplateTemplateContainerResourcesArgs(
|
|
limits={
|
|
"cpu": "4",
|
|
"memory": "8Gi",
|
|
},
|
|
),
|
|
envs=[
|
|
cloudrunv2.JobTemplateTemplateContainerEnvArgs(
|
|
name="GOOGLE_CLOUD_PROJECT",
|
|
value=PROJECT_ID,
|
|
),
|
|
cloudrunv2.JobTemplateTemplateContainerEnvArgs(
|
|
name="GCS_BUCKET_NAME",
|
|
value=bucket.name,
|
|
),
|
|
],
|
|
)
|
|
],
|
|
service_account=service_account.email,
|
|
timeout="3600s", # 1 hour max per task
|
|
max_retries=2,
|
|
),
|
|
),
|
|
)
|
|
|
|
return video_encoding_job
|