minifly-api 0.1.2

API server for Minifly - Local Fly.io development simulator
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
use axum::{
    extract::{Path, Query, State},
    http::HeaderMap,
    Json,
};
use chrono::Utc;
use minifly_core::models::{
    Machine, MachineState, MachineEvent, ImageRef,
    CreateMachineRequest, UpdateMachineRequest, StopMachineRequest,
    StartMachineResponse, StopMachineResponse, WaitMachineQuery,
    CreateLeaseRequest, LeaseResponse, Lease,
};
use minifly_core::{SuccessResponse, Error as CoreError};
use serde_json::{json, Value};
use std::collections::HashMap;
use tracing::{info, instrument};
use crate::state::AppState;
use crate::error::{ApiError, Result};
use crate::middleware::region::{log_machine_operation, get_machine_region};
use minifly_network::extract_container_ip;

pub async fn list_machines(
    State(state): State<AppState>,
    Path(app_name): Path<String>,
    Query(params): Query<HashMap<String, String>>,
) -> Result<Json<Vec<Machine>>> {
    let machines = state.machines.read().unwrap();
    
    let mut result: Vec<Machine> = machines.values()
        .filter(|m| m.name.starts_with(&format!("{}-", app_name)))
        .cloned()
        .collect();
    
    // Apply filters
    if let Some(region) = params.get("region") {
        result.retain(|m| &m.region == region);
    }
    
    if let Some(include_deleted) = params.get("include_deleted") {
        if include_deleted != "true" {
            result.retain(|m| m.state != MachineState::Destroyed);
        }
    }
    
    Ok(Json(result))
}

#[instrument(skip(state), fields(app_name = %app_name, region = tracing::field::Empty))]
pub async fn create_machine(
    State(state): State<AppState>,
    Path(app_name): Path<String>,
    Json(req): Json<CreateMachineRequest>,
) -> Result<Json<Machine>> {
    let machine_id = state.generate_machine_id();
    let instance_id = state.generate_instance_id();
    let machine_index = state.machines.read().unwrap().len() as u32;
    let private_ip = state.generate_private_ip(&app_name, machine_index);
    
    // Get region from request or use default
    let region = get_machine_region(req.region.as_deref());
    tracing::Span::current().record("region", &region);
    
    info!(
        machine_id = %machine_id,
        app_name = %app_name,
        region = %region,
        image = %req.config.image,
        "Creating machine"
    );
    
    // Parse image reference
    let image_parts: Vec<&str> = req.config.image.split('/').collect();
    let (registry, repository, tag_digest) = match image_parts.len() {
        1 => ("registry-1.docker.io", "library", image_parts[0]),
        2 => ("registry-1.docker.io", image_parts[0], image_parts[1]),
        _ => (image_parts[0], image_parts[1], image_parts[2]),
    };
    
    let (tag, digest) = if let Some((t, d)) = tag_digest.split_once('@') {
        (t.to_string(), Some(d.to_string()))
    } else if let Some((_repo, t)) = tag_digest.split_once(':') {
        (t.to_string(), None)
    } else {
        ("latest".to_string(), None)
    };
    
    let machine = Machine {
        id: machine_id.clone(),
        name: req.name.unwrap_or_else(|| format!("{}-{}", app_name, machine_id)),
        state: if req.skip_launch.unwrap_or(false) {
            MachineState::Created
        } else {
            MachineState::Starting
        },
        region: region.clone(),
        image_ref: ImageRef {
            registry: registry.to_string(),
            repository: repository.to_string(),
            tag,
            digest,
        },
        instance_id: instance_id.clone(),
        private_ip,
        created_at: Utc::now(),
        updated_at: Utc::now(),
        config: req.config.clone(),
        events: vec![MachineEvent {
            event_type: "launch".to_string(),
            status: "created".to_string(),
            source: "user".to_string(),
            timestamp: Utc::now().timestamp_millis() as u64,
        }],
    };
    
    // Create container
    if !req.skip_launch.unwrap_or(false) {
        // Start LiteFS if volumes are configured
        let has_volumes = req.config.mounts.as_ref()
            .map(|mounts| !mounts.is_empty())
            .unwrap_or(false);
        
        if has_volumes {
            let is_primary = req.config.env.as_ref()
                .and_then(|env| env.get("FLY_LITEFS_PRIMARY"))
                .map(|v| v == "true")
                .unwrap_or(true);
            
            if let Err(e) = state.litefs.start_for_machine(&machine_id, is_primary).await {
                return Err(CoreError::LiteFSError(format!("Failed to start LiteFS: {}", e)).into());
            }
        }
        
        match state.docker.create_container(&machine_id, &app_name, &req.config).await {
            Ok(container_id) => {
                // Start container
                if let Err(e) = state.docker.start_container(&container_id).await {
                    // Clean up LiteFS if container start failed
                    if has_volumes {
                        let _ = state.litefs.stop_for_machine(&machine_id).await;
                    }
                    return Err(CoreError::DockerError(format!("Failed to start container: {}", e)).into());
                }
                
                // Wait a moment for container to get IP
                tokio::time::sleep(tokio::time::Duration::from_millis(500)).await;
                
                // Get container IP and register with DNS
                if let Ok(container_info) = state.docker.inspect_container(&container_id).await {
                    if let Some(network_settings) = container_info.network_settings {
                        if let Some(networks) = network_settings.networks {
                            let networks_value = serde_json::to_value(&networks).unwrap_or_default();
                            if let Some(ip) = extract_container_ip(&networks_value) {
                                // Register with DNS resolver
                                if let Err(e) = state.dns_resolver.register_machine(&app_name, &machine_id, ip).await {
                                    tracing::warn!("Failed to register machine with DNS: {}", e);
                                }
                            }
                        }
                    }
                }
            }
            Err(e) => {
                // Clean up LiteFS if container creation failed
                if has_volumes {
                    let _ = state.litefs.stop_for_machine(&machine_id).await;
                }
                return Err(CoreError::DockerError(format!("Failed to create container: {}", e)).into());
            }
        }
    }
    
    // Store machine
    state.machines.write().unwrap().insert(machine_id.clone(), machine.clone());
    
    // Handle lease if requested
    if let Some(ttl) = req.lease_ttl {
        let lease = create_machine_lease(&state, &machine_id, ttl, None);
        state.leases.write().unwrap().insert(machine_id.clone(), lease);
    }
    
    // Log successful creation
    log_machine_operation("create", &machine_id, &app_name, &region);
    
    info!(
        machine_id = %machine_id,
        app_name = %app_name,
        region = %region,
        state = ?machine.state,
        "Machine created successfully"
    );
    
    Ok(Json(machine))
}

pub async fn get_machine(
    State(state): State<AppState>,
    Path((_app_name, machine_id)): Path<(String, String)>,
) -> Result<Json<Machine>> {
    let machines = state.machines.read().unwrap();
    
    match machines.get(&machine_id) {
        Some(machine) => Ok(Json(machine.clone())),
        None => Err(CoreError::MachineNotFound(machine_id).into()),
    }
}

pub async fn update_machine(
    State(state): State<AppState>,
    Path((_app_name, machine_id)): Path<(String, String)>,
    headers: HeaderMap,
    Json(req): Json<UpdateMachineRequest>,
) -> Result<Json<Machine>> {
    // Check lease if provided
    if let Some(nonce) = headers.get("fly-machine-lease-nonce") {
        let leases = state.leases.read().unwrap();
        if let Some(lease) = leases.get(&machine_id) {
            if lease.nonce != nonce.to_str().unwrap_or("") {
                return Err(CoreError::InvalidLeaseNonce.into());
            }
        }
    }
    
    let mut machines = state.machines.write().unwrap();
    
    match machines.get_mut(&machine_id) {
        Some(machine) => {
            machine.config = req.config;
            machine.updated_at = Utc::now();
            
            Ok(Json(machine.clone()))
        }
        None => Err(CoreError::MachineNotFound(machine_id).into()),
    }
}

pub async fn delete_machine(
    State(state): State<AppState>,
    Path((app_name, machine_id)): Path<(String, String)>,
    Query(params): Query<HashMap<String, String>>,
) -> Result<Json<SuccessResponse>> {
    let force = params.get("force").map(|v| v == "true").unwrap_or(false);
    
    // Check if machine exists and needs container operations
    let needs_stop = {
        let machines = state.machines.read().unwrap();
        match machines.get(&machine_id) {
            Some(machine) => machine.state == MachineState::Started || force,
            None => return Err(CoreError::MachineNotFound(machine_id.clone()).into()),
        }
    };
    
    // Stop and remove container if needed
    if needs_stop {
        let container_name = format!("minifly-{}-{}", app_name, machine_id);
        if let Err(e) = state.docker.stop_container(&container_name, Some(30)).await {
            if !force {
                return Err(CoreError::DockerError(format!("Failed to stop container: {}", e)).into());
            }
        }
        
        if let Err(e) = state.docker.remove_container(&container_name).await {
            return Err(CoreError::DockerError(format!("Failed to remove container: {}", e)).into());
        }
        
        // Stop LiteFS if running
        if state.litefs.is_running(&machine_id).await {
            if let Err(e) = state.litefs.stop_for_machine(&machine_id).await {
                if !force {
                    return Err(CoreError::LiteFSError(format!("Failed to stop LiteFS: {}", e)).into());
                }
            }
        }
    }
    
    // Update state and remove machine
    {
        let mut machines = state.machines.write().unwrap();
        if let Some(machine) = machines.get_mut(&machine_id) {
            machine.state = MachineState::Destroyed;
        }
        machines.remove(&machine_id);
    }
    
    // Unregister from DNS
    if let Err(e) = state.dns_resolver.unregister_machine(&app_name, &machine_id).await {
        tracing::warn!("Failed to unregister machine from DNS: {}", e);
    }
    
    Ok(Json(SuccessResponse { ok: true }))
}

#[instrument(skip(state), fields(app_name = %app_name, machine_id = %machine_id, region = tracing::field::Empty))]
pub async fn start_machine(
    State(state): State<AppState>,
    Path((app_name, machine_id)): Path<(String, String)>,
) -> Result<Json<StartMachineResponse>> {
    // Get previous state and region without holding the lock
    let (previous_state, region) = {
        let machines = state.machines.read().unwrap();
        match machines.get(&machine_id) {
            Some(machine) => (
                format!("{:?}", machine.state).to_lowercase(),
                machine.region.clone()
            ),
            None => return Err(CoreError::MachineNotFound(machine_id.clone()).into()),
        }
    };
    
    // Record region in tracing span
    tracing::Span::current().record("region", &region);
    
    info!(
        machine_id = %machine_id,
        app_name = %app_name,
        region = %region,
        previous_state = %previous_state,
        "Starting machine"
    );
    
    // Start container
    let container_name = format!("minifly-{}-{}", app_name, machine_id);
    if let Err(e) = state.docker.start_container(&container_name).await {
        return Err(CoreError::DockerError(format!("Failed to start container: {}", e)).into());
    }
    
    // Re-register with DNS after starting
    tokio::time::sleep(tokio::time::Duration::from_millis(500)).await;
    if let Ok(container_info) = state.docker.inspect_container(&container_name).await {
        if let Some(network_settings) = container_info.network_settings {
            if let Some(networks) = network_settings.networks {
                let networks_value = serde_json::to_value(&networks).unwrap_or_default();
                if let Some(ip) = extract_container_ip(&networks_value) {
                    // Register with DNS resolver
                    if let Err(e) = state.dns_resolver.register_machine(&app_name, &machine_id, ip).await {
                        tracing::warn!("Failed to register machine with DNS: {}", e);
                    }
                }
            }
        }
    }
    
    // Update machine state
    {
        let mut machines = state.machines.write().unwrap();
        if let Some(machine) = machines.get_mut(&machine_id) {
            machine.state = MachineState::Started;
            machine.updated_at = Utc::now();
            machine.events.push(MachineEvent {
                event_type: "start".to_string(),
                status: "started".to_string(),
                source: "user".to_string(),
                timestamp: Utc::now().timestamp_millis() as u64,
            });
        }
    }
    
    // Log successful start
    log_machine_operation("start", &machine_id, &app_name, &region);
    
    info!(
        machine_id = %machine_id,
        app_name = %app_name,
        region = %region,
        "Machine started successfully"
    );
    
    Ok(Json(StartMachineResponse {
        previous_state,
        migrated: false,
        new_host: String::new(),
    }))
}

pub async fn stop_machine(
    State(state): State<AppState>,
    Path((app_name, machine_id)): Path<(String, String)>,
    req: Option<Json<StopMachineRequest>>,
) -> Result<Json<StopMachineResponse>> {
    // Check if machine exists
    {
        let machines = state.machines.read().unwrap();
        if !machines.contains_key(&machine_id) {
            return Err(CoreError::MachineNotFound(machine_id.clone()).into());
        }
    }
    
    let timeout = req.as_ref()
        .and_then(|Json(r)| r.timeout.as_ref())
        .and_then(|t| t.parse::<i64>().ok())
        .unwrap_or(30);
    
    // Stop container
    let container_name = format!("minifly-{}-{}", app_name, machine_id);
    if let Err(e) = state.docker.stop_container(&container_name, Some(timeout)).await {
        return Err(CoreError::DockerError(format!("Failed to stop container: {}", e)).into());
    }
    
    // Update machine state
    {
        let mut machines = state.machines.write().unwrap();
        if let Some(machine) = machines.get_mut(&machine_id) {
            machine.state = MachineState::Stopped;
            machine.updated_at = Utc::now();
            machine.events.push(MachineEvent {
                event_type: "stop".to_string(),
                status: "stopped".to_string(),
                source: "user".to_string(),
                timestamp: Utc::now().timestamp_millis() as u64,
            });
        }
    }
    
    // Unregister from DNS when stopped
    if let Err(e) = state.dns_resolver.unregister_machine(&app_name, &machine_id).await {
        tracing::warn!("Failed to unregister machine from DNS: {}", e);
    }
    
    Ok(Json(StopMachineResponse { ok: true }))
}

pub async fn suspend_machine(
    State(state): State<AppState>,
    Path((app_name, machine_id)): Path<(String, String)>,
) -> Result<Json<SuccessResponse>> {
    // Check if machine exists
    {
        let machines = state.machines.read().unwrap();
        if !machines.contains_key(&machine_id) {
            return Err(CoreError::MachineNotFound(machine_id.clone()).into());
        }
    }
    
    // Note: Docker doesn't support true suspend, so we'll just stop the container
    let container_name = format!("minifly-{}-{}", app_name, machine_id);
    if let Err(e) = state.docker.stop_container(&container_name, Some(30)).await {
        return Err(CoreError::DockerError(format!("Failed to suspend container: {}", e)).into());
    }
    
    // Update machine state
    {
        let mut machines = state.machines.write().unwrap();
        if let Some(machine) = machines.get_mut(&machine_id) {
            machine.state = MachineState::Suspended;
            machine.updated_at = Utc::now();
        }
    }
    
    Ok(Json(SuccessResponse { ok: true }))
}

pub async fn wait_machine(
    State(state): State<AppState>,
    Path((_app_name, machine_id)): Path<(String, String)>,
    Query(_query): Query<WaitMachineQuery>,
) -> Result<Json<Value>> {
    let machines = state.machines.read().unwrap();
    
    match machines.get(&machine_id) {
        Some(machine) => {
            // In a real implementation, this would wait for state changes
            // For now, we'll just return the current state
            Ok(Json(json!({
                "ok": true,
                "state": format!("{:?}", machine.state).to_lowercase(),
                "instance_id": machine.instance_id,
            })))
        }
        None => Err(CoreError::MachineNotFound(machine_id).into()),
    }
}

pub async fn create_lease(
    State(state): State<AppState>,
    Path((_app_name, machine_id)): Path<(String, String)>,
    Json(req): Json<CreateLeaseRequest>,
) -> Result<Json<LeaseResponse>> {
    let machines = state.machines.read().unwrap();
    
    if !machines.contains_key(&machine_id) {
        return Err(CoreError::MachineNotFound(machine_id.clone()).into());
    }
    
    let mut leases = state.leases.write().unwrap();
    
    // Check if lease already exists
    if leases.contains_key(&machine_id) {
        return Err(CoreError::LeaseConflict.into());
    }
    
    let lease = create_machine_lease(&state, &machine_id, req.ttl.unwrap_or(300), req.description);
    leases.insert(machine_id.clone(), lease.clone());
    
    Ok(Json(LeaseResponse {
        status: "success".to_string(),
        data: lease,
    }))
}

pub async fn get_lease(
    State(state): State<AppState>,
    Path((_app_name, machine_id)): Path<(String, String)>,
) -> Result<Json<LeaseResponse>> {
    let leases = state.leases.read().unwrap();
    
    match leases.get(&machine_id) {
        Some(lease) => Ok(Json(LeaseResponse {
            status: "success".to_string(),
            data: lease.clone(),
        })),
        None => Err(CoreError::NotFound.into()),
    }
}

pub async fn release_lease(
    State(state): State<AppState>,
    Path((_app_name, machine_id)): Path<(String, String)>,
    headers: HeaderMap,
) -> Result<Json<SuccessResponse>> {
    let nonce = headers.get("fly-machine-lease-nonce")
        .and_then(|v| v.to_str().ok())
        .ok_or_else(|| ApiError::from(CoreError::BadRequest("Missing lease nonce".to_string())))?;
    
    let mut leases = state.leases.write().unwrap();
    
    match leases.get(&machine_id) {
        Some(lease) => {
            if lease.nonce != nonce {
                return Err(CoreError::InvalidLeaseNonce.into());
            }
            
            leases.remove(&machine_id);
            Ok(Json(SuccessResponse { ok: true }))
        }
        None => Err(CoreError::NotFound.into()),
    }
}

pub async fn get_metadata(
    State(_state): State<AppState>,
    Path((_app_name, _machine_id)): Path<(String, String)>,
) -> Result<Json<Value>> {
    // TODO: Implement metadata storage
    Ok(Json(json!({})))
}

pub async fn set_metadata(
    State(_state): State<AppState>,
    Path((_app_name, _machine_id, _key)): Path<(String, String, String)>,
    Json(_value): Json<Value>,
) -> Result<Json<SuccessResponse>> {
    // TODO: Implement metadata storage
    Ok(Json(SuccessResponse { ok: true }))
}

pub async fn delete_metadata(
    State(_state): State<AppState>,
    Path((_app_name, _machine_id, _key)): Path<(String, String, String)>,
) -> Result<Json<SuccessResponse>> {
    // TODO: Implement metadata storage
    Ok(Json(SuccessResponse { ok: true }))
}

fn create_machine_lease(_state: &AppState, _machine_id: &str, ttl: u32, description: Option<String>) -> Lease {
    use rand::Rng;
    use uuid::Uuid;
    let mut rng = rand::thread_rng();
    let nonce: String = (0..12)
        .map(|_| format!("{:x}", rng.gen::<u8>()))
        .collect();
    
    Lease {
        nonce,
        expires_at: Utc::now().timestamp() + ttl as i64,
        owner: "minifly@local".to_string(),
        description: description.unwrap_or_default(),
        version: format!("01{}", Uuid::new_v4().simple()),
    }
}