starlane 0.3.18

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
#![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());

pub mod err;
pub mod properties;
pub mod template;

pub mod env;

pub mod platform;

#[cfg(test)]
pub mod test;

//#[cfg(feature="space")]
//pub extern crate starlane_space as starlane;
#[cfg(feature = "space")]
pub mod space {
    pub use starlane_space::space::*;
}

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

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

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

pub mod executor;
pub mod host;

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

pub mod driver;

#[cfg(feature = "server")]
mod server;

#[cfg(feature = "server")]
pub use server::*;

use crate::cli::{Cli, Commands};
use crate::platform::Platform;
use anyhow::anyhow;
use clap::Parser;
use once_cell::sync::Lazy;
use starlane::space::loc::ToBaseKind;
use std::fs::File;
use std::io::{Read, Seek, Write};
use std::ops::{Add, Mul};
use std::path::{Path, PathBuf};
use std::process;
use std::process::Stdio;
use std::str::FromStr;
use std::time::Duration;
use atty::Stream;
use cliclack::{clear_screen, confirm, intro, multi_progress, outro, progress_bar, select, spinner, ProgressBar};
use colored::{Colorize, CustomColor};
use lerp::Lerp;
use text_to_ascii_art::fonts::get_font;
use text_to_ascii_art::to_art;
use tokio::{fs, join, signal};
use tokio::fs::DirEntry;
use tokio::runtime::Builder;
use zip::write::FileOptions;
use starlane_primitive_macros::ToBase;
use starlane_space::space::util::log;
use crate::env::STARLANE_HOME;

#[cfg(feature = "server")]
async fn config() -> StarlaneConfig {

    let file = format!("{}/config.yaml", STARLANE_HOME.to_string()).to_string();
    let config = match fs::try_exists(file.clone()).await {
        Ok(true) => {
            match fs::read_to_string(file.clone()).await {
                Ok(config) => {
                    match serde_yaml::from_str(&config).map_err(|e| anyhow!(e)) {
                        Ok(config) => config,
                        Err(err) => {
                            println!("starlane config file '{}' failed to parse: '{}'", file, err.to_string());
                            Default::default()
                        }
                    }
                }
                Err(err) => {
                    println!("starlane config file '{}' error when attempting to read to string: '{}'", file, err.to_string());
                    Default::default()
                }
            }
        }
        Ok(false) => {
           let config = Default::default();
            if let Ok(ser) = serde_yaml::to_string(&config) {
                let file: PathBuf = file.into();
                match file.parent() {
                    None => {}
                    Some(dir) => {
                        fs::create_dir_all(dir).await.unwrap_or_default();
                        fs::write(file,ser).await.unwrap_or_default();
                    }
                }
            }
           config
        }
        Err(err) => {
            println!("starlane encountered problem when attempting to load config file: '{}' with error: '{}'", file, err.to_string());
            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 || process::exit(1)).unwrap();

    init();

    let cli = Cli::parse();
    match cli.command {
        Commands::Demo=> demo(),
        Commands::Run => run(),
        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(())
        }
    }
}

#[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")]
fn run() -> Result<(), anyhow::Error> {



    let runtime = tokio::runtime::Builder::new_multi_thread()
        .enable_all()
        .build()?;
    runtime.block_on(async move {

        splash().await;



        let config = config().await;
        let starlane = Starlane::new(config.registry).await.unwrap();
        let machine_api = starlane.machine();

        let api = tokio::time::timeout(Duration::from_secs(30), machine_api)
            .await
            .unwrap()
            .unwrap();
        // this is a dirty hack which is good enough for a 0.3.0 release...
        tokio::time::sleep(Duration::from_secs(2)).await;
        outro("starlane is running.");
        loop {
            tokio::time::sleep(Duration::from_secs(60)).await;
        }
    });



    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 COLORS: (u8,u8,u8) = (0x6D, 0xD7, 0xFD);


async fn splash( ) {
    match to_art("*STARLANE*".to_string(), "default", 0, 0, 0) {
        Ok(string) => {

            let string = format!("\n\n\n\n\n\n{}\n\n\n\n\n\n",string).to_string();

            let begin= (0xFF, 0xFF, 0xFF);
            let end= (0xEE, 0xAA, 0x5A);
            let end= COLORS;

            //let begin = (0x00, 0x00, 0x00);
            // this is bad code however I couldn't find out how to get lines().len() withou
            // giving up ownership (therefor the clone)
            let size = string.clone().lines().count();
            let mut index = 0;
            for line in  string.lines(){
                let progress =  if index < 6 {
                    0.0f32
                } else if index > 6 && index < size-6 {
                    (index-6) as f32 / (size-6) as f32
                } else {
                    1.0f32
                };


                let r = (begin.0 as f32).lerp(end.0 as f32, progress) as u8;
                let g = (begin.1 as f32).lerp(end.1 as f32, progress) as u8;
                let b = (begin.2 as f32).lerp(end.2 as f32, progress) as u8;
                println!("{}", line.truecolor(r, g, b));
                tokio::time::sleep(Duration::from_millis(50)).await;

                index = index + 1;
            }


            //            println!("{}", string.truecolor(0xEE, 0xAA, 0x5A));
        }
        Err(err) =>  {
            eprintln!("err! {}", err.to_string());
        }
    }
}




#[derive(ToBase)]
pub enum StartSequence{
    Starting(String)
}



#[tokio::main]
async fn demo() -> Result<(), anyhow::Error> {


    intro("STARLANE DEMO");


            async fn wait(t: u64) {
                tokio::time::sleep(Duration::from_millis(t)).await;
            }
            wait(1000).await;
            {
                let spinner = spinner();
                spinner.start("starting...");
                wait(5000).await;
                spinner.stop("start successful!");
                wait(250).await;
                clear_screen();
                wait(1000).await;
                splash().await;
                wait(1000).await;
                spinner.stop("Config not found");
                intro("Install?");
            }

            let selected = select(
                r#"This Starlane instance has not configured.
This program (the Starlane Runner) must either be a stand alone cluster or can connect to a remote cluster.
\n
Would you like to install a cluster on this machine or provide a configuration for a remote machine?
"#
            )
                .item("this", "Install stand alone on this machine", "")
                .item("remote", "Configure to access a remote machine", "")
                .interact().unwrap_or_default();

            wait(1000).await;
            let postgres = select(
                r#"Starlane needs a Postgres instance as it's registry when in stand alone mode.\n
Do you have an existing postgres instance that you would like Starlane to connect to or
would your prefer Starlane to install and manage its own Postgres instance?"#
            )
                .item("remote", "Connect to an existing Postgres Cluster Instance", "")
                .item("this", "Let Starlane manage its own Postgres instance", "")
                .interact().unwrap_or_default();

            wait(1000).await;


            let multi = multi_progress("Downloading Service Extensions");
            let postgres = multi.add(progress_bar(100));
            let filestore = multi.add(progress_bar(100).with_download_template());
            let artifacts = multi.add(progress_bar(100).with_download_template());
            let spinner = multi.add(spinner());

            async fn go(bar: ProgressBar, name: &'static str, size: u64) {
                bar.start(format!("looking up: '{}'", name));
                tokio::time::sleep(Duration::from_millis(size)).await;
                bar.set_message(format!("downloading {}...", name));
                tokio::time::sleep(Duration::from_millis(size)).await;
                for _ in 0..100 {
                    bar.inc(1);
                    tokio::time::sleep(Duration::from_millis(size / 100)).await;
                }
                tokio::time::sleep(Duration::from_millis(size)).await;
                bar.set_message(format!("{} download complete", name));

                tokio::time::sleep(Duration::from_millis(size * 2)).await;

                bar.set_message(format!("installing {}", name));
                tokio::time::sleep(Duration::from_millis(500)).await;
                for _ in 0..100 {
                    bar.inc(1);
                    tokio::time::sleep(Duration::from_millis(3 * (size / 100))).await;
                }
                tokio::time::sleep(Duration::from_millis(500)).await;
                bar.stop(format!("{} installation complete", name));
            }

            let postgres = go(postgres, "postgres", 500);
            let filestore = go(filestore, "local filestore", 1500);
            let artifacts = go(artifacts, "artifact repository", 700);

            println!();

            join!(postgres, filestore, artifacts);

            println!();

            spinner.stop("Service extension installation complete.");

            multi.stop();

            println!();
            println!();
            println!();
            println!();
            println!("{}", "Installation complete! To run your local starlane instance: `starlane run` ".to_string().truecolor(COLORS.0, COLORS.1, COLORS.2));
            println!();
            println!();

          outro("DEMO COMPLETED");
            process::exit(0);


            Ok(())
        }