aegis-orchestrator 0.15.0-pre-alpha

100monkeys.ai AEGIS orchestrator CLI and daemon
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
// Copyright (c) 2026 100monkeys.ai
// SPDX-License-Identifier: AGPL-3.0
//! # AEGIS Agent Host CLI
//!
//! The `aegis` binary is the Agent Host that enables an Agent Node.
//!
//! ## Architecture
//!
//! This CLI follows a **CLI-first** design with daemon capabilities:
//!
//! - **Default mode**: CLI commands delegate to daemon if running, else embed services
//! - **Daemon mode**: `aegis --daemon` runs as background service
//! - **Detection**: Check PID file + HTTP health check
//!
//! ## Commands
//!
//! - `aegis daemon start|stop|status|install|uninstall` - Manage daemon lifecycle
//! - `aegis task deploy|execute|status|logs` - Agent operations
//! - `aegis config show|validate|generate` - Configuration management
//! - `aegis init` - Interactive setup wizard
//! - `aegis up [--yes] [--tag <TAG>]` - Start the stack (runs init
//!   automatically if needed)
//! - `aegis down [--volumes]` - Stop the Docker Compose stack
//! - `aegis restart [--profile <name>]` - Restart the Docker Compose services
//! - `aegis uninstall [-y]` - Stop stack and remove the ~/.aegis directory
//!
//! See the architecture documentation for details.
//!
//! # Architecture
//!
//! - **Layer:** Interface / Presentation Layer
//! - **Purpose:** Implements internal responsibilities for main

use aegis_orchestrator_core::domain::node_config::{LoggingConfig, OtlpProtocol};
use anyhow::{Context, Result};
use clap::{Parser, Subcommand};
use colored::Colorize;
use opentelemetry_otlp::{LogExporter, WithExportConfig, WithHttpConfig, WithTonicConfig};
use opentelemetry_sdk::logs::LoggerProvider;
use std::path::PathBuf;
use std::time::Duration;
use tracing::info;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, Layer};

mod auth;
mod commands;
mod daemon;
mod output;

use commands::auth::AuthCommand;
use commands::{
    AgentCommand, ConfigCommand, CredentialCommand, DaemonCommand, DownArgs, FuseDaemonCommand,
    InitArgs, NodeCommand, RestartArgs, SecretCommand, StatusArgs, TaskCommand, UninstallArgs,
    UpArgs, WorkflowCommand,
};
use output::{structured_output_unsupported, OutputFormat};

/// AEGIS Agent Host - Enable autonomous agent execution
#[derive(Parser)]
#[command(name = "aegis")]
#[command(version, about, long_about = None)]
struct Cli {
    /// Run as background daemon service
    #[arg(long)]
    daemon: bool,

    /// Path to configuration file (overrides discovery)
    #[arg(
        short,
        long,
        global = true,
        env = "AEGIS_CONFIG_PATH",
        value_name = "FILE"
    )]
    config: Option<PathBuf>,

    /// HTTP API port (default: 8088)
    #[arg(long, global = true, env = "AEGIS_PORT", default_value = "8088")]
    port: u16,

    /// HTTP API host (default: 127.0.0.1)
    #[arg(long, global = true, env = "AEGIS_HOST", default_value = "127.0.0.1")]
    host: String,

    /// Log level (trace, debug, info, warn, error)
    #[arg(long, global = true, env = "AEGIS_LOG_LEVEL", default_value = "info")]
    log_level: String,

    /// Output format for supported scriptable commands
    #[arg(long, global = true, value_enum, default_value_t = OutputFormat::Text)]
    output: OutputFormat,

    #[command(subcommand)]
    command: Option<Commands>,
}

#[derive(Subcommand)]
enum Commands {
    /// Manage daemon lifecycle
    #[command(name = "daemon")]
    Daemon {
        #[command(subcommand)]
        command: DaemonCommand,
    },

    /// Agent task operations
    #[command(name = "task")]
    Task {
        #[command(subcommand)]
        command: TaskCommand,
    },

    /// Cluster node operations
    #[command(name = "node")]
    Node {
        #[command(subcommand)]
        command: NodeCommand,
    },

    /// Configuration management
    #[command(name = "config")]
    Config {
        #[command(subcommand)]
        command: ConfigCommand,
    },

    /// Agent management
    #[command(name = "agent")]
    Agent {
        #[command(subcommand)]
        command: AgentCommand,
    },

    /// Workflow management
    #[command(name = "workflow")]
    Workflow {
        #[command(subcommand)]
        command: WorkflowCommand,
    },
    /// Manage secrets in the OpenBao-backed secret store
    #[command(name = "secret")]
    Secret {
        #[command(subcommand)]
        command: SecretCommand,
    },

    /// Manage provider credential bindings (API keys and OAuth tokens)
    #[command(name = "credential")]
    Credential {
        #[command(subcommand)]
        command: CredentialCommand,
    },

    /// Authenticate with an AEGIS environment.
    #[command(name = "auth")]
    Auth {
        #[command(subcommand)]
        command: AuthCommand,
    },

    /// Host-side FUSE daemon for out-of-process volume mounts (ADR-107)
    #[command(name = "fuse-daemon")]
    FuseDaemon {
        #[command(subcommand)]
        command: FuseDaemonCommand,
    },

    /// Update AEGIS database
    #[command(name = "update")]
    Update {
        #[command(flatten)]
        command: commands::UpdateCommand,
    },

    /// Interactive setup wizard — install and configure AEGIS from scratch
    #[command(name = "init")]
    Init {
        #[command(flatten)]
        args: InitArgs,
    },

    /// Stop the local AEGIS Docker Compose stack
    #[command(name = "down")]
    Down {
        #[command(flatten)]
        args: DownArgs,
    },

    /// Start the AEGIS stack (runs `aegis init` automatically if not set up)
    #[command(name = "up")]
    Up {
        #[command(flatten)]
        args: UpArgs,
    },

    /// Restart local AEGIS Docker Compose services
    #[command(name = "restart")]
    Restart {
        #[command(flatten)]
        args: RestartArgs,
    },

    /// Check AEGIS service health
    #[command(name = "status")]
    Status {
        #[command(flatten)]
        args: StatusArgs,
    },

    /// Stop the stack and permanently remove the AEGIS data directory
    #[command(name = "uninstall")]
    Uninstall {
        #[command(flatten)]
        args: UninstallArgs,
    },
}

#[tokio::main]
async fn main() -> Result<()> {
    let cli = Cli::parse();

    // Load config first to initialize logging properly
    let config = aegis_orchestrator_core::domain::node_config::NodeConfigManifest::load_or_default(
        cli.config.clone(),
    )
    .unwrap_or_default();

    // Initialize logging
    let log_provider = init_logging(
        &cli.log_level,
        config
            .spec
            .observability
            .as_ref()
            .and_then(|o| o.logging.as_ref()),
    )?;

    // Handle daemon mode (background service)
    if cli.daemon {
        info!("Starting AEGIS Agent Host in daemon mode");
        return daemon::start_daemon(cli.config, cli.port).await;
    }

    // Handle commands in CLI mode
    let res = match cli.command {
        Some(Commands::Daemon { command }) => {
            commands::daemon::handle_command(command, cli.config, &cli.host, cli.port, cli.output)
                .await
        }
        Some(Commands::Task { command }) => {
            commands::task::handle_command(command, &cli.host, cli.port, cli.output).await
        }
        Some(Commands::Node { command }) => {
            commands::node::handle_command(command, cli.config, &cli.host, cli.port, cli.output)
                .await
        }
        Some(Commands::Config { command }) => {
            commands::config::handle_command(command, cli.config, cli.output).await
        }
        Some(Commands::Agent { command }) => {
            commands::agent::handle_command(command, cli.config, &cli.host, cli.port, cli.output)
                .await
        }
        Some(Commands::Workflow { command }) => {
            commands::workflow::handle_command(command, cli.config, &cli.host, cli.port, cli.output)
                .await
        }
        Some(Commands::Secret { command }) => {
            commands::secret::handle_command(command, cli.config, &cli.host, cli.port, cli.output)
                .await
        }
        Some(Commands::Credential { command }) => {
            commands::credential::handle_command(
                command, cli.config, &cli.host, cli.port, cli.output,
            )
            .await
        }
        Some(Commands::Auth { command }) => {
            commands::auth::handle_command(command, cli.output).await
        }
        Some(Commands::FuseDaemon { command }) => {
            commands::fuse_daemon::handle_command(command, cli.output).await
        }
        Some(Commands::Update { command }) => {
            commands::update::execute(command, cli.config, cli.output).await
        }
        Some(Commands::Init { args }) => {
            if cli.output.is_structured() {
                structured_output_unsupported("aegis init", cli.output)
            } else {
                commands::init::run(args).await
            }
        }
        Some(Commands::Down { args }) => {
            if cli.output.is_structured() {
                structured_output_unsupported("aegis down", cli.output)
            } else {
                commands::down::run(args).await
            }
        }
        Some(Commands::Up { args }) => {
            if cli.output.is_structured() {
                structured_output_unsupported("aegis up", cli.output)
            } else {
                commands::up::run(args).await
            }
        }
        Some(Commands::Restart { args }) => {
            if cli.output.is_structured() {
                structured_output_unsupported("aegis restart", cli.output)
            } else {
                commands::restart::run(args).await
            }
        }
        Some(Commands::Status { args }) => {
            commands::status::run(args, cli.config, &cli.host, cli.port, cli.output).await
        }
        Some(Commands::Uninstall { args }) => {
            if cli.output.is_structured() {
                structured_output_unsupported("aegis uninstall", cli.output)
            } else {
                commands::uninstall::run(args).await
            }
        }
        None => {
            // No command provided - show help
            eprintln!("{}", "No command specified. Use --help for usage.".yellow());
            std::process::exit(1);
        }
    };

    if let Some(provider) = log_provider {
        let _ = provider.shutdown();
    }

    res
}

/// Initialize tracing subscriber for logging
fn init_logging(level: &str, config: Option<&LoggingConfig>) -> Result<Option<LoggerProvider>> {
    let filter = tracing_subscriber::EnvFilter::try_from_default_env()
        .or_else(|_| tracing_subscriber::EnvFilter::try_new(level))
        .context("Failed to create log filter")?;

    let fmt_layer = tracing_subscriber::fmt::layer()
        .with_target(false)
        .with_thread_ids(false)
        .with_file(false)
        .with_line_number(false)
        .compact();

    let subscriber = tracing_subscriber::registry().with(filter).with(fmt_layer);

    if let Some(cfg) = config {
        if let Some(endpoint) = &cfg.otlp_endpoint {
            let exporter = match cfg.otlp_protocol {
                OtlpProtocol::Grpc => {
                    let mut exporter_builder = LogExporter::builder()
                        .with_tonic()
                        .with_endpoint(endpoint)
                        .with_timeout(Duration::from_millis(cfg.batch.export_timeout_ms));

                    let mut metadata = tonic_12::metadata::MetadataMap::new();
                    for (k, v) in &cfg.otlp_headers {
                        use std::str::FromStr;
                        if let (Ok(key), Ok(val)) = (
                            tonic_12::metadata::MetadataKey::from_str(k),
                            tonic_12::metadata::MetadataValue::from_str(v),
                        ) {
                            metadata.insert(key, val);
                        }
                    }
                    if !metadata.is_empty() {
                        exporter_builder = exporter_builder.with_metadata(metadata);
                    }

                    if let Some(ca) = &cfg.tls.ca_cert_path {
                        if let Ok(pem) = std::fs::read(ca) {
                            let cert = tonic_12::transport::Certificate::from_pem(pem);
                            let tls_config =
                                tonic_12::transport::ClientTlsConfig::new().ca_certificate(cert);
                            exporter_builder = exporter_builder.with_tls_config(tls_config);
                        }
                    }

                    exporter_builder
                        .build()
                        .context("Failed to build OTLP gRPC log exporter")?
                }
                OtlpProtocol::Http => {
                    let mut exporter_builder = LogExporter::builder()
                        .with_http()
                        .with_endpoint(endpoint)
                        .with_timeout(Duration::from_millis(cfg.batch.export_timeout_ms));

                    let mut headers = std::collections::HashMap::new();
                    for (k, v) in &cfg.otlp_headers {
                        headers.insert(k.clone(), v.clone());
                    }
                    if !headers.is_empty() {
                        exporter_builder = exporter_builder.with_headers(headers);
                    }

                    exporter_builder
                        .build()
                        .context("Failed to build OTLP HTTP log exporter")?
                }
            };

            let batch_config = opentelemetry_sdk::logs::BatchConfigBuilder::default()
                .with_max_queue_size(cfg.batch.max_queue_size)
                .with_scheduled_delay(Duration::from_millis(cfg.batch.scheduled_delay_ms))
                .with_max_export_batch_size(cfg.batch.max_export_batch_size)
                .with_max_export_timeout(Duration::from_millis(cfg.batch.export_timeout_ms))
                .build();

            let processor = opentelemetry_sdk::logs::BatchLogProcessor::builder(
                exporter,
                opentelemetry_sdk::runtime::Tokio,
            )
            .with_batch_config(batch_config)
            .build();

            let provider = opentelemetry_sdk::logs::LoggerProvider::builder()
                .with_log_processor(processor)
                .with_resource(opentelemetry_sdk::Resource::new(vec![
                    opentelemetry::KeyValue::new(
                        "service.name",
                        cfg.service_name
                            .clone()
                            .unwrap_or_else(|| "aegis-orchestrator".to_string()),
                    ),
                ]))
                .build();

            let otlp_layer =
                opentelemetry_appender_tracing::layer::OpenTelemetryTracingBridge::new(&provider);
            let min_level_filter = tracing_subscriber::EnvFilter::new(&cfg.min_level);

            subscriber
                .with(otlp_layer.with_filter(min_level_filter))
                .init();

            return Ok(Some(provider));
        }
    }

    subscriber.init();
    Ok(None)
}