UNPKG

@codai/cbd

Version:

Codai Better Database - High-Performance Vector Memory System with HPKV-inspired architecture and MCP server

312 lines 10 kB
/** * Multi-Cloud Configuration Interface * Defines configuration patterns for AWS, Azure, and GCP integration */ /** * Default multi-cloud configuration */ export const defaultMultiCloudConfig = { strategy: 'performance', primaryCloud: 'local', fallbackClouds: ['aws', 'azure', 'gcp'], aws: { region: 'us-east-1', credentials: { // Will be loaded from environment or IAM role }, services: { dynamodb: { region: 'us-east-1', consistentReads: false, billingMode: 'PAY_PER_REQUEST' }, opensearch: { endpoint: '', region: 'us-east-1', version: '2.3', instanceType: 't3.small.search', instanceCount: 1 }, rds: { engine: 'postgres', instanceClass: 'db.t3.micro', allocatedStorage: 20, multiAZ: false, backupRetentionPeriod: 7 }, s3: { bucket: 'cbd-universal-storage', region: 'us-east-1', storageClass: 'STANDARD', versioning: true, encryption: true }, lambda: { runtime: 'nodejs18.x', timeout: 30, memorySize: 512, environment: {} } } }, azure: { tenantId: '', clientId: '', subscriptionId: '', resourceGroup: 'cbd-resources', services: { cosmosDb: { endpoint: '', primaryKey: '', databaseName: 'cbd-universal', consistencyLevel: 'Session', requestUnits: 400, multiRegion: false }, cognitiveSearch: { endpoint: '', apiKey: '', indexName: 'cbd-vectors', tier: 'basic' }, sqlDatabase: { server: '', database: 'cbd-relational', username: '', password: '', tier: 'Basic', maxSizeMB: 250 }, blobStorage: { accountName: '', accountKey: '', containerName: 'cbd-files', tier: 'Hot', redundancy: 'LRS' }, functions: { appName: 'cbd-functions', resourceGroup: 'cbd-resources', runtime: 'node', version: '18', consumptionPlan: true } } }, gcp: { projectId: '', region: 'us-central1', services: { firestore: { projectId: '', databaseId: '(default)', location: 'us-central1', type: 'firestore-native' }, spanner: { instanceId: 'cbd-instance', databaseId: 'cbd-database', nodeCount: 1, processingUnits: 1000 }, bigquery: { datasetId: 'cbd_analytics', location: 'US' }, cloudStorage: { bucketName: 'cbd-gcp-storage', location: 'US', storageClass: 'STANDARD', uniformBucketLevelAccess: true }, cloudFunctions: { runtime: 'nodejs18', region: 'us-central1', memory: 512, timeout: 60, environmentVariables: {} } } }, local: { dataPath: './data', backupPath: './backup', maxMemoryMB: 2048, enablePersistence: true, compressionEnabled: true }, monitoring: { enabled: true, metricsInterval: 60000, // 1 minute cloudWatch: false, azureMonitor: false, gcpOperations: false }, cost: { enabled: true, maxMonthlyCost: 100, // $100/month costAlerts: true, autoScaling: true, spotInstances: false } }; /** * Configuration builder for multi-cloud setup */ export class MultiCloudConfigBuilder { config = {}; static create() { return new MultiCloudConfigBuilder(); } withStrategy(strategy) { this.config.strategy = strategy; return this; } withPrimaryCloud(cloud) { this.config.primaryCloud = cloud; return this; } withFallbackClouds(clouds) { this.config.fallbackClouds = clouds; return this; } withAWS(config) { this.config.aws = { ...defaultMultiCloudConfig.aws, ...config }; return this; } withAzure(config) { this.config.azure = { ...defaultMultiCloudConfig.azure, ...config }; return this; } withGCP(config) { this.config.gcp = { ...defaultMultiCloudConfig.gcp, ...config }; return this; } withLocal(config) { this.config.local = { ...defaultMultiCloudConfig.local, ...config }; return this; } withMonitoring(config) { this.config.monitoring = { ...defaultMultiCloudConfig.monitoring, ...config }; return this; } withCostOptimization(config) { this.config.cost = { ...defaultMultiCloudConfig.cost, ...config }; return this; } build() { return { ...defaultMultiCloudConfig, ...this.config }; } /** * Load configuration from environment variables */ fromEnvironment() { // AWS Configuration if (process.env.AWS_REGION && process.env.AWS_ACCESS_KEY_ID && process.env.AWS_SECRET_ACCESS_KEY) { const credentials = { accessKeyId: process.env.AWS_ACCESS_KEY_ID, secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY }; if (process.env.AWS_SESSION_TOKEN) { credentials.sessionToken = process.env.AWS_SESSION_TOKEN; } this.withAWS({ region: process.env.AWS_REGION, credentials }); } // Azure Configuration if (process.env.AZURE_TENANT_ID && process.env.AZURE_CLIENT_ID && process.env.AZURE_SUBSCRIPTION_ID) { const azureConfig = { tenantId: process.env.AZURE_TENANT_ID, clientId: process.env.AZURE_CLIENT_ID, subscriptionId: process.env.AZURE_SUBSCRIPTION_ID }; if (process.env.AZURE_CLIENT_SECRET) { azureConfig.clientSecret = process.env.AZURE_CLIENT_SECRET; } this.withAzure(azureConfig); } // GCP Configuration if (process.env.GOOGLE_CLOUD_PROJECT) { const gcpConfig = { projectId: process.env.GOOGLE_CLOUD_PROJECT, region: process.env.GOOGLE_CLOUD_REGION || 'us-central1' }; if (process.env.GOOGLE_APPLICATION_CREDENTIALS) { gcpConfig.keyFilename = process.env.GOOGLE_APPLICATION_CREDENTIALS; } this.withGCP(gcpConfig); } // Strategy from environment if (process.env.CBD_CLOUD_STRATEGY) { const strategy = process.env.CBD_CLOUD_STRATEGY; if (['performance', 'cost', 'latency', 'availability'].includes(strategy)) { this.withStrategy(strategy); } } return this; } /** * Load configuration from file */ static fromFile(filePath) { try { const fs = require('fs'); const configData = JSON.parse(fs.readFileSync(filePath, 'utf8')); return { ...defaultMultiCloudConfig, ...configData }; } catch (error) { console.warn(`Failed to load config from ${filePath}, using defaults:`, error); return defaultMultiCloudConfig; } } /** * Validate configuration */ static validate(config) { const errors = []; // Validate strategy if (!['performance', 'cost', 'latency', 'availability'].includes(config.strategy)) { errors.push(`Invalid strategy: ${config.strategy}`); } // Validate primary cloud if (!['aws', 'azure', 'gcp', 'local'].includes(config.primaryCloud)) { errors.push(`Invalid primary cloud: ${config.primaryCloud}`); } // Validate fallback clouds if (!Array.isArray(config.fallbackClouds)) { errors.push('Fallback clouds must be an array'); } else { for (const cloud of config.fallbackClouds) { if (!['aws', 'azure', 'gcp', 'local'].includes(cloud)) { errors.push(`Invalid fallback cloud: ${cloud}`); } } } // AWS specific validation if (config.primaryCloud === 'aws' || config.fallbackClouds.includes('aws')) { if (!config.aws.region) { errors.push('AWS region is required when using AWS'); } } // Azure specific validation if (config.primaryCloud === 'azure' || config.fallbackClouds.includes('azure')) { if (!config.azure.tenantId || !config.azure.subscriptionId) { errors.push('Azure tenant ID and subscription ID are required when using Azure'); } } // GCP specific validation if (config.primaryCloud === 'gcp' || config.fallbackClouds.includes('gcp')) { if (!config.gcp.projectId) { errors.push('GCP project ID is required when using GCP'); } } return { valid: errors.length === 0, errors }; } } //# sourceMappingURL=MultiCloudConfiguration.js.map