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