kotoba_deploy_core/
lib.rs

1//! # Kotoba Deploy Core
2//!
3//! Core types and configuration structures for the Kotoba deployment system.
4//! This crate provides the fundamental building blocks for deployment management.
5
6use kotoba_core::types::{Result, Value, ContentHash, KotobaError};
7use serde::{Deserialize, Serialize};
8use std::collections::HashMap;
9use chrono::{DateTime, Utc};
10
11/// ランタイムタイプの列挙
12#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
13pub enum RuntimeType {
14    /// Denoランタイム
15    Deno,
16    /// Node.jsランタイム
17    NodeJs,
18    /// Pythonランタイム
19    Python,
20    /// Rustランタイム
21    Rust,
22    /// Goランタイム
23    Go,
24}
25
26/// デプロイメントのステータス
27#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
28pub enum DeploymentStatus {
29    /// 作成中
30    Creating,
31    /// 準備中
32    Preparing,
33    /// 実行中
34    Running,
35    /// 停止中
36    Stopping,
37    /// 停止済み
38    Stopped,
39    /// エラー
40    Error(String),
41}
42
43/// デプロイメントの優先度
44#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
45pub enum DeploymentPriority {
46    /// 低優先度
47    Low,
48    /// 通常優先度
49    Normal,
50    /// 高優先度
51    High,
52    /// 緊急
53    Critical,
54}
55
56/// リソース使用量
57#[derive(Debug, Clone, Serialize, Deserialize)]
58pub struct ResourceUsage {
59    /// CPU使用率 (0.0 - 1.0)
60    pub cpu_usage: f64,
61    /// メモリ使用量 (MB)
62    pub memory_usage: f64,
63    /// ネットワーク使用量 (MB/s)
64    pub network_usage: f64,
65    /// ディスク使用量 (MB)
66    pub disk_usage: f64,
67    /// リクエスト数/秒
68    pub request_rate: f64,
69}
70
71/// デプロイメントメタデータ
72#[derive(Debug, Clone, Serialize, Deserialize)]
73pub struct DeployMetadata {
74    /// デプロイメント名
75    pub name: String,
76    /// バージョン
77    pub version: String,
78    /// 説明
79    pub description: Option<String>,
80    /// 作者
81    pub author: Option<String>,
82    /// 作成日時
83    pub created_at: DateTime<Utc>,
84    /// 最終更新日時
85    pub updated_at: Option<DateTime<Utc>>,
86    /// 設定ファイルのハッシュ
87    pub config_hash: Option<ContentHash>,
88}
89
90/// アプリケーション設定
91#[derive(Debug, Clone, Serialize, Deserialize)]
92pub struct ApplicationConfig {
93    /// エントリーポイント
94    pub entry_point: String,
95    /// ランタイムタイプ
96    pub runtime: RuntimeType,
97    /// 環境変数
98    pub environment: HashMap<String, String>,
99    /// ビルドコマンド
100    pub build_command: Option<String>,
101    /// スタートコマンド
102    pub start_command: Option<String>,
103}
104
105/// スケーリング設定
106#[derive(Debug, Clone, Serialize, Deserialize)]
107pub struct ScalingConfig {
108    /// 最小インスタンス数
109    pub min_instances: u32,
110    /// 最大インスタンス数
111    pub max_instances: u32,
112    /// CPU使用率の閾値 (0.0 - 1.0)
113    pub cpu_threshold: f64,
114    /// メモリ使用率の閾値 (0.0 - 1.0)
115    pub memory_threshold: f64,
116    /// 自動スケーリング有効化
117    pub auto_scaling_enabled: bool,
118}
119
120/// ネットワーク設定
121#[derive(Debug, Clone, Serialize, Deserialize)]
122pub struct NetworkConfig {
123    /// ドメイン設定
124    pub domains: Vec<String>,
125    /// SSL設定
126    pub ssl: Option<SslConfig>,
127    /// CORS設定
128    pub cors: Option<CorsConfig>,
129    /// CDN設定
130    pub cdn: Option<CdnConfig>,
131}
132
133/// SSL設定
134#[derive(Debug, Clone, Serialize, Deserialize)]
135pub struct SslConfig {
136    /// 証明書タイプ
137    pub cert_type: CertType,
138    /// 証明書ファイルのパス
139    pub cert_path: Option<String>,
140    /// キーファイルのパス
141    pub key_path: Option<String>,
142}
143
144/// 証明書タイプ
145#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
146pub enum CertType {
147    /// Let's Encrypt
148    LetsEncrypt,
149    /// カスタム証明書
150    Custom,
151    /// 自己署名証明書
152    SelfSigned,
153}
154
155/// CORS設定
156#[derive(Debug, Clone, Serialize, Deserialize)]
157pub struct CorsConfig {
158    /// 許可されたオリジン
159    pub allowed_origins: Vec<String>,
160    /// 許可されたメソッド
161    pub allowed_methods: Vec<String>,
162    /// 許可されたヘッダー
163    pub allowed_headers: Vec<String>,
164    /// クレデンシャル許可
165    pub allow_credentials: bool,
166}
167
168/// CDN設定
169#[derive(Debug, Clone, Serialize, Deserialize)]
170pub struct CdnConfig {
171    /// CDNプロバイダー
172    pub provider: CdnProvider,
173    /// CDNエッジロケーション
174    pub edge_locations: Vec<String>,
175    /// キャッシュ設定
176    pub cache_config: CacheConfig,
177}
178
179/// CDNプロバイダー
180#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
181pub enum CdnProvider {
182    /// Cloudflare
183    Cloudflare,
184    /// Fastly
185    Fastly,
186    /// Akamai
187    Akamai,
188    /// AWS CloudFront
189    CloudFront,
190}
191
192/// キャッシュ設定
193#[derive(Debug, Clone, Serialize, Deserialize)]
194pub struct CacheConfig {
195    /// キャッシュ有効期間 (秒)
196    pub ttl: u32,
197    /// キャッシュ無効化パターン
198    pub invalidation_patterns: Vec<String>,
199}
200
201/// デプロイ設定のメイン構造体
202#[derive(Debug, Clone, Serialize, Deserialize)]
203pub struct DeployConfig {
204    /// デプロイメントのメタデータ
205    pub metadata: DeployMetadata,
206    /// アプリケーション設定
207    pub application: ApplicationConfig,
208    /// スケーリング設定
209    pub scaling: ScalingConfig,
210    /// ネットワーク設定
211    pub network: NetworkConfig,
212    /// カスタム設定
213    pub custom: Value,
214}
215
216impl Default for DeployConfig {
217    fn default() -> Self {
218        Self {
219            metadata: DeployMetadata {
220                name: "default".to_string(),
221                version: "1.0.0".to_string(),
222                description: None,
223                author: None,
224                created_at: Utc::now(),
225                updated_at: None,
226                config_hash: None,
227            },
228            application: ApplicationConfig {
229                entry_point: "index.js".to_string(),
230                runtime: RuntimeType::Deno,
231                environment: HashMap::new(),
232                build_command: None,
233                start_command: None,
234            },
235            scaling: ScalingConfig {
236                min_instances: 1,
237                max_instances: 10,
238                cpu_threshold: 0.8,
239                memory_threshold: 0.8,
240                auto_scaling_enabled: true,
241            },
242            network: NetworkConfig {
243                domains: vec!["localhost".to_string()],
244                ssl: None,
245                cors: None,
246                cdn: None,
247            },
248            custom: Value::Null,
249        }
250    }
251}
252
253/// デプロイ設定ビルダー
254#[derive(Debug, Clone)]
255pub struct DeployConfigBuilder {
256    config: DeployConfig,
257}
258
259impl DeployConfigBuilder {
260    pub fn new(name: String) -> Self {
261        let mut config = DeployConfig::default();
262        config.metadata.name = name;
263        Self { config }
264    }
265
266    pub fn version(mut self, version: String) -> Self {
267        self.config.metadata.version = version;
268        self
269    }
270
271    pub fn description(mut self, description: String) -> Self {
272        self.config.metadata.description = Some(description);
273        self
274    }
275
276    pub fn author(mut self, author: String) -> Self {
277        self.config.metadata.author = Some(author);
278        self
279    }
280
281    pub fn entry_point(mut self, entry_point: String) -> Self {
282        self.config.application.entry_point = entry_point;
283        self
284    }
285
286    pub fn runtime(mut self, runtime: RuntimeType) -> Self {
287        self.config.application.runtime = runtime;
288        self
289    }
290
291    pub fn environment(mut self, key: String, value: String) -> Self {
292        self.config.application.environment.insert(key, value);
293        self
294    }
295
296    pub fn build_command(mut self, command: String) -> Self {
297        self.config.application.build_command = Some(command);
298        self
299    }
300
301    pub fn start_command(mut self, command: String) -> Self {
302        self.config.application.start_command = Some(command);
303        self
304    }
305
306    pub fn min_instances(mut self, min: u32) -> Self {
307        self.config.scaling.min_instances = min;
308        self
309    }
310
311    pub fn max_instances(mut self, max: u32) -> Self {
312        self.config.scaling.max_instances = max;
313        self
314    }
315
316    pub fn domains(mut self, domains: Vec<String>) -> Self {
317        self.config.network.domains = domains;
318        self
319    }
320
321    pub fn build(self) -> DeployConfig {
322        self.config
323    }
324}
325
326// Re-export commonly used types
327pub use DeployConfig as Config;
328pub use DeployMetadata as Metadata;
329pub use ApplicationConfig as AppConfig;
330pub use ScalingConfig as Scaling;
331pub use NetworkConfig as Network;