# ๐Ÿ—๏ธ CloudHost PaaS โ€” System Architecture ## Overview CloudHost is a self-service PaaS platform that enables users to deploy **Node.js**, **Laravel**, and **WordPress** applications onto Kubernetes clusters managed by a super admin. It includes a full billing/wallet system, automated lifecycle management, and Helm-based deployments. --- ## ๐Ÿงฑ High-Level Architecture ``` โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ USERS / ADMINS โ”‚ โ”‚ (Browser / CLI) โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ HTTPS โ–ผ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ FRONTEND (Next.js 14) โ”‚ โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ โ”‚ โ”‚ Auth UI โ”‚ โ”‚ Deploy Wizard โ”‚ โ”‚ Dashboardโ”‚ โ”‚Admin Panelโ”‚ โ”‚ โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ REST API (JSON) โ–ผ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ BACKEND (NestJS 10) โ”‚ โ”‚ โ”‚ โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ โ”‚ โ”‚Auth โ”‚ โ”‚Applications โ”‚ โ”‚Deployments โ”‚ โ”‚Clusters โ”‚ โ”‚ โ”‚ โ”‚Module โ”‚ โ”‚Module โ”‚ โ”‚Module โ”‚ โ”‚Module โ”‚ โ”‚ โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ โ”‚ โ”‚ โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ โ”‚ โ”‚Billing โ”‚ โ”‚Lifecycle โ”‚ โ”‚Snapshots โ”‚ โ”‚Tickets โ”‚ โ”‚ โ”‚ โ”‚Module โ”‚ โ”‚Module โ”‚ โ”‚Module โ”‚ โ”‚Module โ”‚ โ”‚ โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ โ”‚ โ”‚ โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ โ”‚ โ”‚ Kubernetes โ”‚ โ”‚ Helm โ”‚ โ”‚ Build โ”‚ โ”‚ โ”‚ โ”‚ Service โ”‚ โ”‚ Service โ”‚ โ”‚ Service โ”‚ โ”‚ โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ โ”‚ โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ Kubernetes โ”‚ โ”‚ Helm CLI โ”‚ โ”‚ Kaniko โ”‚ โ”‚ Cluster(s) โ”‚ โ”‚ (v3) โ”‚ โ”‚ (in-cluster) โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ ``` --- ## ๐Ÿงฉ Module Overview | Module | Purpose | |--------|---------| | **Auth** | JWT access/refresh tokens, Passport strategies, role guards | | **Users** | User CRUD, admin activate/deactivate, profile management | | **Applications** | App CRUD, code upload (zip), metadata, runtime detection | | **Build** | Kaniko-based image builds via BullMQ queue; auto-detects Node.js/Laravel/WordPress | | **Kubernetes** | K8s API interactions โ€” namespace, scale, delete, pod logs, build pods | | **Helm** | Helm CLI wrapper โ€” install/upgrade, rollback, uninstall, history | | **Deployments** | Deployment lifecycle orchestration, history, stop/restart | | **Clusters** | Multi-cluster management, kubeconfig storage, default cluster selection | | **Billing** | Wallet system (deposit/deduct), transaction ledger, plan cost calculation | | **Lifecycle** | Cron-based scanner: auto-suspend expired apps, auto-delete after grace period | | **Snapshots** | Application snapshot/backup management | | **Tickets** | Support ticket system for users | --- ## ๐Ÿ”ง Tech Stack ### Frontend: Next.js 14 (App Router) + Tailwind CSS | Reason | Detail | |--------|--------| | **SSR & SEO** | Server-side rendering for fast initial loads | | **App Router** | React Server Components, layouts, loading states | | **Tailwind CSS** | Rapid UI development, consistent design system | | **TypeScript** | End-to-end type safety with shared types | | **React Query** | Server state management, caching, polling for live status | | **Zustand** | Lightweight client state management (auth store) | ### Backend: NestJS 10 (Node.js) | Reason | Detail | |--------|--------| | **Modular architecture** | Each domain is a self-contained module | | **TypeScript native** | Full type safety, shared interfaces with frontend | | **@kubernetes/client-node** | Official K8s client for direct API interaction | | **Helm CLI** | Shell-out to helm for chart-based deployments | | **Bull/BullMQ** | Redis-backed job queues for async build pipelines | | **TypeORM** | PostgreSQL ORM with entity-based schema | ### Build System: Kaniko (in-cluster) | Reason | Detail | |--------|--------| | **No Docker daemon** | Builds inside K8s pods โ€” no Docker-in-Docker | | **Runtime detection** | Auto-detects Node.js, Laravel, WordPress from source files | | **WordPress support** | Custom entrypoint script for wp-content merging | | **Registry push** | Native push to insecure or authenticated registries | ### Deployment: Helm v3 Charts | Reason | Detail | |--------|--------| | **Templated manifests** | Single chart handles Node.js, Laravel, WordPress | | **Rollback support** | Built-in revision history and rollback | | **Resource policies** | PVCs and secrets persist across helm uninstall | | **Registry pull secrets** | Auto-created per namespace for insecure registries | --- ## ๐Ÿ” Security Architecture - JWT Authentication (access + refresh tokens) - Role-Based Access Control (User / Admin) - K8s Namespace Isolation per user - K8s RBAC โ€” scoped ServiceAccounts - Network Policies between namespaces - Resource Quotas & Limit Ranges - Secrets encryption (K8s Secrets) - Input validation (class-validator on all DTOs) - Helmet HTTP security headers - Bcrypt password hashing (12 rounds) --- ## ๐Ÿ”„ Deployment Flow ``` User uploads code (zip) โ”‚ โ–ผ API stores file + metadata in PostgreSQL โ”‚ โ–ผ BullMQ build job queued โ”‚ โ–ผ detectRuntime() โ†’ nodejs | laravel | wordpress โ”‚ โ–ผ Generate Dockerfile per runtime โ”‚ โ–ผ Kaniko Pod builds image โ†’ pushes to registry โ”‚ โ–ผ HelmService.installOrUpgrade() with cloudhost-app chart โ”‚ โ–ผ Helm creates: Namespace, Deployment, Service, Ingress, DB, PVC, Secrets, Registry Pull Secret, TLS cert โ”‚ โ–ผ App live at https://.apps.cloudhost.ir ``` --- ## ๐Ÿ’ฐ Billing & Lifecycle Flow ``` ACTIVE โ”€โ”€(expires)โ”€โ”€โ–บ SUSPENDED โ”€โ”€(grace)โ”€โ”€โ–บ PENDING_DELETION โ”€โ”€โ–บ DELETED โ–ฒ โ”‚ โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€ payment โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€ payment (within grace) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ ``` - **Billing Cycles**: HOURLY | MONTHLY | YEARLY - **Hourly plans**: auto-renew from wallet each hour - **Grace periods**: admin-configurable via PlatformSettings table - **Lifecycle Scanner**: runs every 60s (configurable) --- ## Helm Chart: cloudhost-platform Chart at `backend/helm/cloudhost-platform/` deploys the **control plane** (NestJS API, Next.js UI, PostgreSQL, Redis) into a dedicated namespace (default `cloudhost`). | Value | Purpose | |-------|---------| | `ingress.enabled` | Create Ingress (default `true`) | | `ingress.tls.enabled` | cert-manager TLS via `clusterIssuer` | | `ingress.frontend.host` / `ingress.api.host` | Public hostnames | | `postgres.password` / `secrets.jwtSecret` | Credentials (auto-generated if empty on first install) | | `migrations.enabled` | Post-install SQL migration Job | --- ## ๐Ÿš€ Helm Chart: cloudhost-app Single chart at `backend/helm/cloudhost-app/` handles all runtimes: | Template | Purpose | |----------|---------| | `deployment.yaml` | App pod with imagePullSecrets, probes, WordPress volumes | | `service.yaml` | ClusterIP (port 80 โ†’ app port) | | `ingress.yaml` | Nginx ingress with cert-manager TLS | | `secret.yaml` | User env vars as K8s Secret | | `db-deployment.yaml` | PostgreSQL or MySQL with health probes | | `db-service.yaml` | Database ClusterIP service | | `db-pvc.yaml` | Database storage (resource-policy: keep) | | `db-secret.yaml` | Database credentials (resource-policy: keep) | | `wp-pvc.yaml` | WordPress wp-content PVC (resource-policy: keep) | | `registry-pull-secret.yaml` | imagePullSecret for insecure registry | --- ## ๐Ÿ“ Project Structure ``` host/ โ”œโ”€โ”€ ARCHITECTURE.md โ”œโ”€โ”€ README.md โ”œโ”€โ”€ CHANGELOG.md โ”œโ”€โ”€ CONTRIBUTING.md โ”œโ”€โ”€ docker-compose.yml โ”œโ”€โ”€ backend/ โ”‚ โ”œโ”€โ”€ Dockerfile โ”‚ โ”œโ”€โ”€ package.json โ”‚ โ”œโ”€โ”€ helm/cloudhost-platform/ # Helm chart for control plane โ”‚ โ”œโ”€โ”€ helm/cloudhost-app/ # Helm chart for user apps โ”‚ โ”œโ”€โ”€ src/ โ”‚ โ”‚ โ”œโ”€โ”€ main.ts / app.module.ts โ”‚ โ”‚ โ”œโ”€โ”€ auth/ # JWT + Passport โ”‚ โ”‚ โ”œโ”€โ”€ users/ # User management โ”‚ โ”‚ โ”œโ”€โ”€ applications/ # App CRUD + upload โ”‚ โ”‚ โ”œโ”€โ”€ deployments/ # Deploy orchestration โ”‚ โ”‚ โ”œโ”€โ”€ clusters/ # Multi-cluster (admin) โ”‚ โ”‚ โ”œโ”€โ”€ kubernetes/ # K8s client + Helm service โ”‚ โ”‚ โ”œโ”€โ”€ build/ # Kaniko builds (BullMQ) โ”‚ โ”‚ โ”œโ”€โ”€ billing/ # Wallet + transactions โ”‚ โ”‚ โ”œโ”€โ”€ lifecycle/ # Auto-suspend/delete โ”‚ โ”‚ โ”œโ”€โ”€ snapshots/ # App snapshots โ”‚ โ”‚ โ””โ”€โ”€ tickets/ # Support tickets โ”‚ โ””โ”€โ”€ templates/ # Legacy Handlebars (deprecated) โ”œโ”€โ”€ frontend/ โ”‚ โ”œโ”€โ”€ Dockerfile โ”‚ โ”œโ”€โ”€ package.json โ”‚ โ””โ”€โ”€ src/ โ”‚ โ”œโ”€โ”€ app/dashboard/ # Apps, deploy, admin pages โ”‚ โ”œโ”€โ”€ components/ โ”‚ โ”œโ”€โ”€ lib/ # API client, auth store โ”‚ โ””โ”€โ”€ types/ # Shared TS interfaces โ””โ”€โ”€ uploads/ # User-uploaded code archives ``` --- ## ๐Ÿ”ฎ Future Considerations 1. Custom domains with auto TLS via cert-manager 2. Horizontal Pod Autoscaler based on CPU/memory 3. WebSocket/SSE for real-time build log streaming 4. GitOps integration (ArgoCD) 5. Additional runtimes (Python, Go, Rust) 6. App marketplace with pre-built templates 7. Per-app resource consumption dashboards