starlane 0.3.21

Starlane -- An Orchestration and Infrastructure Framework for WebAssembly Components (https://starlane.io) This packaged manages `HyperSpace` which provides infrastructure for `Space` Apis (WebAssembly & external programs meant to provide custom behaviors in Starlane), This package references the `starlane-space` package and reuses of it to run the infrastructure and it also contains mechanisms (Drivers) for extending the Starlane Type system
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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
#![allow(warnings)]
#[macro_use]
extern crate async_trait;
#[macro_use]
extern crate clap;
#[macro_use]
extern crate lazy_static;
#[macro_use]
extern crate starlane_macros;

pub static VERSION: Lazy<semver::Version> =
    Lazy::new(|| semver::Version::from_str(env!("CARGO_PKG_VERSION").trim()).unwrap());

#[cfg(test)]
pub mod test;

pub mod install;
//#[cfg(feature="space")]


#[cfg(feature = "cli")]
pub mod cli;

pub mod env;
#[cfg(feature = "server")]
pub mod server;
pub mod space;
pub mod hyperspace;



use crate::hyperspace::foundation::Foundation;
use crate::hyperspace::foundation::StandAloneFoundation;
pub use crate::hyperspace::platform::Platform;
use crate::hyperspace::shutdown::shutdown;
use crate::cli::{Cli, Commands, ContextCmd};
use crate::env::{
    config_exists, context, context_dir, ensure_global_settings, save_global_settings, set_context,
    STARLANE_HOME,
};
use crate::install::{Console, StarlaneTheme};
use crate::server::Starlane;
use anyhow::{anyhow, ensure};
use clap::Parser;
use cliclack::log::{error, success};
use cliclack::{intro, outro, spinner};
use colored::Colorize;
use crossterm::execute;
use crossterm::style::{Color, Print, ResetColor, SetBackgroundColor, SetForegroundColor, Stylize};
use lerp::Lerp;
use nom::{InputIter, InputTake, Slice};
use once_cell::sync::Lazy;
use starlane_primitive_macros::{create_mark, ToBase};
use crate::space::loc::ToBaseKind;
use crate::space::err::PrintErr;
use crate::space::log::push_scope;
use crate::space::parse::SkewerCase;
use crate::space::particle::Status;
use std::any::Any;
use std::fmt::Display;
use std::fs::File;
use std::io::{Read, Seek, Write};
use std::ops::{Add, Index, Mul};
use std::path::Path;
use std::str::FromStr;
use std::time::Duration;
use std::{io, process};
use tokio::fs::DirEntry;
use tokio::runtime::Builder;
use tracing::instrument::WithSubscriber;
use tracing::Instrument;
use zip::write::FileOptions;
/*
let config = Default::default();

config

 */

pub fn init() {
    #[cfg(feature = "cli")]
    {
        use rustls::crypto::aws_lc_rs::default_provider;
        default_provider()
            .install_default()
            .expect("crypto provider could not be installed");
    }
}

#[cfg(feature = "cli")]
pub fn main() -> Result<(), anyhow::Error> {
    ctrlc::set_handler(move || shutdown(1)).unwrap();

    init();

    let cli = Cli::parse();
    match cli.command {
        Commands::Splash => {
            let console = Console::new();
            console.splash2();
            Ok(())
        }
        Commands::Install { edit, nuke } => {
            if nuke {
                crate::nuke(false);
            }
            install::install(edit)
        }
        Commands::Run => {
            let runtime = Builder::new_multi_thread().enable_all().build()?;
            runtime.block_on(async move { push_scope(run, create_mark!()).await });
            Ok(())
        }
        Commands::Term(args) => {
            let runtime = Builder::new_multi_thread().enable_all().build()?;

            match runtime.block_on(async move { cli::term(args).await }) {
                Ok(_) => Ok(()),
                Err(err) => {
                    println!("err! {}", err.to_string());
                    Err(err.into())
                }
            }
        }
        Commands::Version => {
            println!("{}", VERSION.to_string());
            Ok(())
        }
        Commands::Scorch => {
            scorch();
            Ok(())
        }
        Commands::Nuke { all } => {
            nuke(all);
            Ok(())
        }
        Commands::Context(args) => {
            match args.command {
                ContextCmd::Create { context_name } => {
                    let context_name =
                        SkewerCase::from_str(context_name.as_str()).map_err(|e| {
                            e.print();
                            anyhow!("illegal context name")
                        })?;
                    set_context(context_name.as_str())?;
                    if config_exists(context_name.to_string()) {
                        Err(anyhow!("context '{}' already exists", context_name))?;
                    }

                    println!(
                        "Context '{}' created.  Next you may want to run '{}'",
                        context_name.truecolor(COOL.0, COOL.1, COOL.2),
                        "starlane install"
                            .to_string()
                            .truecolor(COOL.0, COOL.1, COOL.2)
                    );
                }
                ContextCmd::Switch { context_name } => {
                    let context_name =
                        SkewerCase::from_str(context_name.as_str()).map_err(|e| {
                            e.print();
                            anyhow!("illegal context name")
                        })?;
                    set_context(context_name.as_str());
                }
                ContextCmd::Default => {
                    set_context("default").unwrap_or_default();
                }
                ContextCmd::Which => {
                    println!("{}", context());
                }
                ContextCmd::List => {
                    let context = context();
                    let dir = std::fs::read_dir(STARLANE_HOME.to_string())?;
                    for dir in dir.into_iter() {
                        let dir = dir?;
                        if dir.metadata()?.is_dir() {
                            let dir = dir
                                .path()
                                .iter()
                                .last()
                                .expect("expected a last directory")
                                .to_str()
                                .unwrap_or_default()
                                .to_string();
                            if context == dir {
                                println!("{}{}", "*", dir.truecolor(0xff, 0xff, 0xff));
                            } else {
                                println!(" {}", dir.truecolor(COOL.0, COOL.1, COOL.2));
                            }
                        }
                    }
                }
            }
            Ok(())
        }
    }
}

#[cfg(not(feature = "server"))]
fn run() -> Result<(), anyhow::Error> {
    println!("'' feature is not enabled in this starlane installation");
    Err(anyhow!(
        "'machine' feature is not enabled in this starlane installation"
    ))
}
#[cfg(feature = "server")]
async fn run() -> Result<(), anyhow::Error> {
    let console = Console::new();
    console.info("starlane started.")?;

    console.intro("RUN STARLANE").unwrap_or_default();

    async fn runner(console: &Console) -> Result<(), anyhow::Error> {
        console.spinner().start("initializing");
        console.info("initialization complete.")?;

        console.long_delay();
        let mut spinner = console.spinner();
        spinner.start("loading configuration");

        let config = match env::config() {
            Ok(Some(config)) => config,
            Ok(None) => {
                console.long_delay();
                spinner.error("Starlane configuration not found.");

                error(format!(
                    "Starlane looked for a configuration here: '{}' But none was found.",
                    env::config_path()
                ))?;

                console.remark(format!("if '{}' isn't the config file you wanted, please set environment variable `export STARLANE_HOME=\"/config/parent/dir\"", env::config_path()))?;

                console.note(
                    "install",
                    "please run `starlane install` to configure a new Starlane runner",
                )?;

                outro("Good Luck!")?;
                console.newlines(3);
                shutdown(1);
                panic!();
            }
            Err(err) => {
                spinner.error("invalid configuration");
                console.error(format!("{}", err.to_string()))?;
                console.note("wrong config?", format!("if '{}' isn't the config file you wanted, please set environment variable `export STARLANE_HOME=\"/config/parent/dir\"", env::config_path()))?;
                console.note(
                    "fresh install",
                    "To create a fresh configuration please run: `starlane install`",
                )?;
                outro("Good Luck!")?;
                console.newlines(3);
                shutdown(1);
                panic!();
            }
        };

        console.long_delay();
        console.success("starlane configured.")?;
        spinner.next(
            "configuration loaded.",
            "launching registry [this may take a while]",
        );

        console.long_delay();
        let starlane = Starlane::new(config, StandAloneFoundation())
            .await
            .map_err(|e| {
                println!("{}", e.to_string());
                e
            })
            .unwrap();

        spinner.next("registry status: [Ready]", "acquiring machine API");

        let machine_api = starlane.machine();

        spinner.next(
            "machine API acquired",
            "waiting for Starlane [Ready] status",
        );

        let api = tokio::time::timeout(Duration::from_secs(30), machine_api).await??;

        spinner.clear();

        console.status("Starlane status:", Status::Ready)?;

        console.newlines(3);

        console.splash_with_params(1, 2, 25);

        console.note(
            "what's next?",
            "You can connect to this starlane runner with a control terminal: `starlane term`",
        )?;

        console.outro("Starlane is running.")?;
        console.newlines(3);

        // this is a dirty hack which is good enough for a 0.3.0 release...
        loop {
            tokio::time::sleep(Duration::from_secs(60)).await;
        }

        Ok(())
    };

    match runner(&console).await {
        Ok(_) => {}
        Err(err) => {
            error(format!(
                "starlane halted due to an error: {}",
                err.to_string()
            ))
            .unwrap_or_default();

            console.outro("runner failed").unwrap();
            console.newlines(3);
        }
    }

    Ok(())
}
/*
#[cfg(feature = "server")]
fn run() -> Result<(), anyhow::Error> {
    let console = Console::new();
    console.info("starlane started.")?;
    let runtime = tokio::runtime::Builder::new_multi_thread()
        .enable_all()
        .build()?;

    runtime.block_on(async move {

        log_entry_point()
        console.intro("RUN STARLANE").unwrap_or_default();

        async fn runner(console: &Console) -> Result<(),anyhow::Error> {
            console.spinner().start("initializing");
            console.info("initialization complete.")?;

            console.long_delay();
            let mut spinner = console.spinner();
            spinner.start("loading configuration");

            let config = match env::config() {
                Ok(Some(config)) => config,
                Ok(None) => {
                    console.long_delay();
                    spinner.error("Starlane configuration not found.");

                    error(format!("Starlane looked for a configuration here: '{}' But none was found.", env::config_path()))?;

                    console.remark(format!("if '{}' isn't the config file you wanted, please set environment variable `export STARLANE_HOME=\"/config/parent/dir\"", env::config_path()))?;

                    console.note("install", "please run `starlane install` to configure a new Starlane runner")?;

                    outro("Good Luck!")?;
                    console.newlines(3);
                    shutdown(1);
                    panic!();
                }
                Err(err) => {
                    spinner.error("invalid configuration");
                    console.error(format!("{}", err.to_string()))?;
                    console.note("wrong config?", format!("if '{}' isn't the config file you wanted, please set environment variable `export STARLANE_HOME=\"/config/parent/dir\"", env::config_path()))?;
                    console.note("fresh install", "To create a fresh configuration please run: `starlane install`")?;
                    outro("Good Luck!")?;
                    console.newlines(3);
                    shutdown(1);
                    panic!();
                }
            };

            console.long_delay();
            console.success("starlane configured.")?;
            spinner.next("configuration loaded.","launching registry [this may take a while]");

            console.long_delay();
            let starlane = Starlane::new(config,StandAloneFoundation()).await.map_err(|e|{println!("{}",e.to_string()); e}).unwrap();

            spinner.next("registry status: [Ready]","acquiring machine API");

            let machine_api = starlane.machine();


            spinner.next("machine API acquired","waiting for Starlane [Ready] status");

            let api = tokio::time::timeout(Duration::from_secs(30), machine_api).await??;

            spinner.clear();

            console.status( "Starlane status:", Status::Ready)?;

            console.newlines(3);

            console.splash_with_params(1, 2, 25);

            console.note("what's next?", "You can connect to this starlane runner with a control terminal: `starlane term`" )?;

            console.outro("Starlane is running.")?;
            console.newlines(3);

            // this is a dirty hack which is good enough for a 0.3.0 release...
            loop {
                tokio::time::sleep(Duration::from_secs(60)).await;
            }

            Ok(())
        };

        match runner(&console).await {
            Ok(_) => {}
            Err(err) =>
                {
                    error(format!("starlane halted due to an error: {}", err.to_string())).unwrap_or_default();

                    console.outro("runner failed").unwrap();
                    console.newlines(3);
                }
        }
    });

    Ok(())
}

 */

/*
#[no_mangle]
pub extern "C" fn starlane_uuid() -> loc::Uuid {
loc::Uuid::from(uuid::Uuid::new_v4()).unwrap()
}

#[no_mangle]
pub extern "C" fn starlane_timestamp() -> Timestamp {
Timestamp { millis: Utc::now().timestamp_millis() }
}

*/
/*
#[cfg(feature = "cli")]
async fn cli() -> Result<(), SpaceErr> {
    let home_dir: String = match dirs::home_dir() {
        None => ".".to_string(),
        Some(dir) => dir.display().to_string(),
    };
    let matches = ClapCommand::new("cosmic-cli")
        .arg(
            Arg::new("host")
                .short('h')
                .long("host")
                .takes_value(true)
                .value_name("host")
                .required(false)
                .default_value("localhost"),
        )
        .arg(
            Arg::new("certs")
                .short('c')
                .long("certs")
                .takes_value(true)
                .value_name("certs")
                .required(false)
                .default_value(format!("{}/.old/localhost/certs", home_dir).as_str()),
        )
        .subcommand(ClapCommand::new("script"))
        .allow_external_subcommands(true)
        .get_matches();

    let host = matches.get_one::<String>("host").unwrap().clone();
    let certs = matches.get_one::<String>("certs").unwrap().clone();
    let session = Session::new(host, certs).await?;

    if matches.subcommand_name().is_some() {
        session.command(matches.subcommand_name().unwrap()).await
    } else {
        loop {
            let line: String = text_io::try_read!("{};").map_err(|e| SpaceErr::new(500, "err"))?;

            let line_str = line.trim();

            if "exit" == line_str {
                return Ok(());
            }
            println!("> {}", line_str);
            session.command(line.as_str()).await?;
        }
        Ok(())
    }
}

 */

pub fn zip_dir<T>(
    it: impl Iterator<Item = DirEntry>,
    prefix: &str,
    writer: T,
    method: zip::CompressionMethod,
) -> zip::result::ZipResult<T>
where
    T: Write + Seek,
{
    let mut zip = zip::ZipWriter::new(writer);
    let options = FileOptions::default()
        .compression_method(method)
        .unix_permissions(0o755);

    let mut buffer = Vec::new();
    for entry in it {
        let path = entry.path();
        let name = path.strip_prefix(Path::new(prefix)).unwrap();

        // Write file or directory explicitly
        // Some unzip tools unzip files with directory paths correctly, some do not!
        if path.is_file() {
            zip.start_file(name.to_str().unwrap(), options)?;
            let mut f = File::open(path)?;

            f.read_to_end(&mut buffer)?;
            zip.write_all(&*buffer)?;
            buffer.clear();
        } else if !name.as_os_str().is_empty() {
            // Only if not root! Avoids path spec / warning
            // and mapname conversion failed error on unzip
            zip.add_directory(name.to_str().unwrap(), options)?;
        }
    }
    let result = zip.finish()?;
    Result::Ok(result)
}

/*

#[derive(Lerp,Clone)]
struct Color {
   pub r: Nu,
   pub g: Nu,
   pub b: Nu,
}

#[derive(Lerp,Clone)]
pub struct Nu {
    value: u8
}

impl Nu {
    pub fn new(value: u8) -> Nu {
        Nu { value }
    }
}


impl Mul for Nu {
    type Output = Self;

    fn mul(self, rhs: Self) -> Self::Output {
        Nu::new((self.value as f32 * rhs.value as f32) as u8)
    }
}

impl Add for Nu {
    type Output = Self;

    fn add(self, rhs: Self) -> Self::Output {
        Nu::new(self.value + rhs.value  )
    }
}

impl Color {
    pub fn new( r: u8, g: u8, b: u8 ) -> Self {
        let r = Nu::new(r);
        let g = Nu::new(g);
        let b = Nu::new(b);
        Self { r, g, b }
    }

    pub fn custom(&self) -> CustomColor {
        CustomColor::new(self.r.value.clone() , self.g.value.0.clone() , self.b.value.0.clone() )
    }
}

 */

static COOL: (u8, u8, u8) = (0x6D, 0xD7, 0xFD);
static UNDERSTATED: (u8, u8, u8) = (0x66, 0x66, 0xFD);

static IMPORTANT: (u8, u8, u8) = (0xFF, 0xFF, 0xFF);
static ERR: (u8, u8, u8) = (0xcc, 0x00, 0x00);

static OK: (u8, u8, u8) = (0x00, 0xcc, 0x00);
#[derive(ToBase)]
pub enum StartSequence {
    Starting(String),
}

/*
splash().await;

println!(
    "{}",
    "Let Starlane manage your infrastructure with WebAssembly & More!"
        .truecolor(COLORS.0, COLORS.1, crate::COLORS.2)
);
println!();
println!("{}", "Welcome to Starlane!".white());
println!();
println!();


 */

#[tokio::main]
async fn scorch() {
    if let Ok(Some(config)) = env::config() {
        if !config.can_scorch {
            panic!(
                "in config: '{}' can_scorch flag is set to false.",
                env::config_path()
            );
        }
    }
}

fn nuke(all: bool) {
    if all {
        let global = ensure_global_settings();
        if global.nuke {
            std::fs::remove_dir_all(STARLANE_HOME.as_str()).unwrap();
            println!("all starlane contexts deleted");
            // saving the global.conf again
            save_global_settings(global).unwrap();
        }
    }

    if let Ok(Some(config)) = env::config() {
        if !config.can_nuke {
            panic!(
                "in config: '{}' can_nuke flag is set to false.",
                env::config_path()
            );
        } else {
            std::fs::remove_dir_all(context_dir()).unwrap();
        }
    }
}

/*
fn list_contexts() -> Result<Vec<String>,anyhow::Error> {
    let mut rtn = vec![];
    let dir = std::fs::read_dir(STARLANE_HOME.to_string())?;
    for dir in dir.into_iter() {
        let dir = dir?;
        if dir.metadata()?.is_dir() {
            let dir : String= dir.path().into();
            rtn.push(dir);
        }
    }
    Ok(rtn)
}

 */