vorpal-sdk 0.1.0

Rust SDK for building Vorpal artifacts.
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
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
use crate::{api, context};
use anyhow::{bail, Result};
use indoc::formatdoc;

pub mod bun;
pub mod cargo;
pub mod clippy;
pub mod crane;
pub mod gh;
pub mod git;
pub mod go;
pub mod goimports;
pub mod gopls;
pub mod grpcurl;
pub mod language;
pub mod linux_debian;
pub mod linux_vorpal;
pub mod linux_vorpal_slim;
pub mod nodejs;
pub mod oci_image;
pub mod pnpm;
pub mod protoc;
pub mod protoc_gen_go;
pub mod protoc_gen_go_grpc;
pub mod rsync;
pub mod rust_analyzer;
pub mod rust_src;
pub mod rust_std;
pub mod rust_toolchain;
pub mod rustc;
pub mod rustfmt;
pub mod staticcheck;
pub mod step;
pub mod system;

pub struct Argument<'a> {
    pub name: &'a str,
    pub require: bool,
}

pub struct ArtifactSource<'a> {
    pub digest: Option<&'a str>,
    pub excludes: Vec<String>,
    pub includes: Vec<String>,
    pub name: &'a str,
    pub path: &'a str,
}

pub struct ArtifactStep<'a> {
    pub arguments: Vec<String>,
    pub artifacts: Vec<String>,
    pub entrypoint: &'a str,
    pub environments: Vec<String>,
    pub secrets: Vec<api::artifact::ArtifactStepSecret>,
    pub script: Option<String>,
}

pub struct Artifact<'a> {
    pub aliases: Vec<String>,
    pub name: &'a str,
    pub sources: Vec<api::artifact::ArtifactSource>,
    pub steps: Vec<api::artifact::ArtifactStep>,
    pub systems: Vec<api::artifact::ArtifactSystem>,
}

pub struct Job<'a> {
    pub artifacts: Vec<String>,
    pub name: &'a str,
    pub secrets: Vec<api::artifact::ArtifactStepSecret>,
    pub script: String,
    pub systems: Vec<api::artifact::ArtifactSystem>,
}

pub struct Process<'a> {
    pub arguments: Vec<String>,
    pub artifacts: Vec<String>,
    pub entrypoint: &'a str,
    pub name: &'a str,
    pub secrets: Vec<api::artifact::ArtifactStepSecret>,
    pub systems: Vec<api::artifact::ArtifactSystem>,
}

pub struct DevelopmentEnvironment<'a> {
    pub artifacts: Vec<String>,
    pub environments: Vec<String>,
    pub name: &'a str,
    pub secrets: Vec<api::artifact::ArtifactStepSecret>,
    pub systems: Vec<api::artifact::ArtifactSystem>,
}

pub struct UserEnvironment<'a> {
    pub artifacts: Vec<String>,
    pub environments: Vec<String>,
    pub name: &'a str,
    pub symlinks: Vec<(String, String)>,
    pub systems: Vec<api::artifact::ArtifactSystem>,
}

impl<'a> Argument<'a> {
    pub fn new(name: &'a str) -> Self {
        Self {
            name,
            require: false,
        }
    }

    pub fn with_require(mut self) -> Self {
        self.require = true;
        self
    }

    pub fn build(self, context: &mut context::ConfigContext) -> Result<Option<String>> {
        let variable = context.get_variable(self.name);

        if self.require && variable.is_none() {
            bail!("variable '{}' is required", self.name)
        }

        Ok(variable)
    }
}

impl<'a> ArtifactSource<'a> {
    pub fn new(name: &'a str, path: &'a str) -> Self {
        Self {
            digest: None,
            excludes: vec![],
            includes: vec![],
            name,
            path,
        }
    }

    pub fn with_digest(mut self, digest: &'a str) -> Self {
        self.digest = Some(digest);
        self
    }

    pub fn with_excludes(mut self, excludes: Vec<String>) -> Self {
        self.excludes = excludes;
        self
    }

    pub fn with_includes(mut self, includes: Vec<String>) -> Self {
        self.includes = includes;
        self
    }

    pub fn build(self) -> api::artifact::ArtifactSource {
        api::artifact::ArtifactSource {
            digest: self.digest.map(|v| v.to_string()),
            includes: self.includes,
            excludes: self.excludes,
            name: self.name.to_string(),
            path: self.path.to_string(),
        }
    }
}

impl<'a> ArtifactStep<'a> {
    pub fn new(entrypoint: &'a str) -> Self {
        Self {
            arguments: vec![],
            artifacts: vec![],
            entrypoint,
            environments: vec![],
            secrets: vec![],
            script: None,
        }
    }

    pub fn with_arguments(mut self, arguments: Vec<&str>) -> Self {
        self.arguments = arguments.iter().map(|v| v.to_string()).collect();
        self
    }

    pub fn with_artifacts(mut self, artifacts: Vec<String>) -> Self {
        self.artifacts = artifacts;
        self
    }

    pub fn with_environments(mut self, environments: Vec<String>) -> Self {
        self.environments = environments;
        self
    }

    pub fn with_secrets(mut self, secrets: Vec<api::artifact::ArtifactStepSecret>) -> Self {
        for secret in secrets {
            if !self.secrets.iter().any(|s| s.name == secret.name) {
                self.secrets.push(secret);
            }
        }
        self
    }

    pub fn with_script(mut self, script: String) -> Self {
        self.script = Some(script);
        self
    }

    pub fn build(self) -> api::artifact::ArtifactStep {
        api::artifact::ArtifactStep {
            arguments: self.arguments,
            artifacts: self.artifacts,
            entrypoint: Some(self.entrypoint.to_string()),
            environments: self.environments,
            secrets: self.secrets,
            script: self.script,
        }
    }
}

impl<'a> Artifact<'a> {
    pub fn new(
        name: &'a str,
        steps: Vec<api::artifact::ArtifactStep>,
        systems: Vec<api::artifact::ArtifactSystem>,
    ) -> Self {
        Self {
            aliases: vec![],
            name,
            sources: vec![],
            steps,
            systems,
        }
    }

    pub fn with_aliases(mut self, aliases: Vec<String>) -> Self {
        for alias in aliases {
            if !self.aliases.contains(&alias) {
                self.aliases.push(alias);
            }
        }
        self
    }

    pub fn with_sources(mut self, sources: Vec<api::artifact::ArtifactSource>) -> Self {
        for source in sources {
            if !self.sources.iter().any(|s| s.name == source.name) {
                self.sources.push(source);
            }
        }

        self
    }

    pub async fn build(self, context: &mut context::ConfigContext) -> Result<String> {
        let artifact = api::artifact::Artifact {
            aliases: self.aliases,
            name: self.name.to_string(),
            sources: self.sources,
            steps: self.steps,
            systems: self.systems.into_iter().map(|v| v.into()).collect(),
            target: context.get_system().into(),
        };

        context.add_artifact(&artifact).await
    }
}

impl<'a> Job<'a> {
    pub fn new(name: &'a str, script: String, systems: Vec<api::artifact::ArtifactSystem>) -> Self {
        Self {
            artifacts: vec![],
            name,
            secrets: vec![],
            script,
            systems,
        }
    }

    pub fn with_artifacts(mut self, artifacts: Vec<String>) -> Self {
        self.artifacts = artifacts;
        self
    }

    pub fn with_secrets(mut self, secrets: Vec<(&str, &str)>) -> Self {
        for (name, value) in secrets {
            if !self.secrets.iter().any(|s| s.name == name) {
                self.secrets.push(api::artifact::ArtifactStepSecret {
                    name: name.to_string(),
                    value: value.to_string(),
                });
            }
        }

        self
    }

    pub async fn build(mut self, context: &mut context::ConfigContext) -> Result<String> {
        // Sort for deterministic output
        self.secrets.sort_by(|a, b| a.name.cmp(&b.name));

        let step = step::shell(context, self.artifacts, vec![], self.script, self.secrets).await?;

        Artifact::new(self.name, vec![step], self.systems)
            .build(context)
            .await
    }
}

impl<'a> DevelopmentEnvironment<'a> {
    pub fn new(name: &'a str, systems: Vec<api::artifact::ArtifactSystem>) -> Self {
        Self {
            artifacts: vec![],
            environments: vec![],
            name,
            secrets: vec![],
            systems,
        }
    }

    pub fn with_artifacts(mut self, artifacts: Vec<String>) -> Self {
        self.artifacts = artifacts;
        self
    }

    pub fn with_environments(mut self, environments: Vec<String>) -> Self {
        self.environments = environments;
        self
    }

    pub fn with_secrets(mut self, secrets: Vec<(&str, &str)>) -> Self {
        for (name, value) in secrets.into_iter() {
            if !self.secrets.iter().any(|s| s.name == name) {
                self.secrets.push(api::artifact::ArtifactStepSecret {
                    name: name.to_string(),
                    value: value.to_string(),
                });
            }
        }

        self
    }

    pub async fn build(mut self, context: &mut context::ConfigContext) -> Result<String> {
        // Sort for deterministic output
        self.secrets.sort_by(|a, b| a.name.cmp(&b.name));

        let mut envs_backup = vec![
            "export VORPAL_SHELL_BACKUP_PATH=\"$PATH\"".to_string(),
            "export VORPAL_SHELL_BACKUP_PS1=\"$PS1\"".to_string(),
            "export VORPAL_SHELL_BACKUP_VORPAL_SHELL=\"$VORPAL_SHELL\"".to_string(),
        ];

        let mut envs_export = vec![
            format!("export PS1=\"({}) $PS1\"", self.name),
            "export VORPAL_SHELL=\"1\"".to_string(),
        ];

        let mut envs_restore = vec![
            "export PATH=\"$VORPAL_SHELL_BACKUP_PATH\"".to_string(),
            "export PS1=\"$VORPAL_SHELL_BACKUP_PS1\"".to_string(),
            "export VORPAL_SHELL=\"$VORPAL_SHELL_BACKUP_VORPAL_SHELL\"".to_string(),
        ];

        let mut envs_unset = vec![
            "unset VORPAL_SHELL_BACKUP_PATH".to_string(),
            "unset VORPAL_SHELL_BACKUP_PS1".to_string(),
            "unset VORPAL_SHELL_BACKUP_VORPAL_SHELL".to_string(),
        ];

        for env in self.environments.clone().into_iter() {
            let key = env.split("=").next().unwrap();

            if key == "PATH" {
                continue;
            }

            envs_backup.push(format!("export VORPAL_SHELL_BACKUP_{key}=\"${key}\""));
            envs_export.push(format!("export {env}"));
            envs_restore.push(format!("export {key}=\"$VORPAL_SHELL_BACKUP_{key}\""));
            envs_unset.push(format!("unset VORPAL_SHELL_BACKUP_{key}"));
        }

        // Setup path

        let step_path_artifacts = self
            .artifacts
            .iter()
            .map(|artifact| format!("{}/bin", get_env_key(artifact)))
            .collect::<Vec<String>>()
            .join(":");

        let mut step_path = step_path_artifacts;

        if let Some(path) = self.environments.iter().find(|x| x.starts_with("PATH=")) {
            if let Some(path_value) = path.split('=').nth(1) {
                step_path = format!("{path_value}:{step_path}");
            }
        }

        envs_export.push(format!("export PATH={step_path}:$PATH"));

        // Setup script

        let step_script = formatdoc! {"
            mkdir -p $VORPAL_WORKSPACE/bin

            cat > bin/activate << \"EOF\"
            #!/bin/bash

            {backups}
            {exports}

            deactivate(){{
            {restores}
            {unsets}
            }}

            exec \"$@\"
            EOF

            chmod +x $VORPAL_WORKSPACE/bin/activate

            mkdir -p $VORPAL_OUTPUT/bin

            cp -pr bin \"$VORPAL_OUTPUT\"",
            backups = envs_backup.join("\n"),
            exports = envs_export.join("\n"),
            restores = envs_restore.join("\n"),
            unsets = envs_unset.join("\n"),
        };

        let steps =
            vec![step::shell(context, self.artifacts, vec![], step_script, self.secrets).await?];

        Artifact::new(self.name, steps, self.systems)
            .build(context)
            .await
    }
}

impl<'a> Process<'a> {
    pub fn new(
        name: &'a str,
        entrypoint: &'a str,
        systems: Vec<api::artifact::ArtifactSystem>,
    ) -> Self {
        Self {
            arguments: vec![],
            artifacts: vec![],
            entrypoint,
            name,
            secrets: vec![],
            systems,
        }
    }

    pub fn with_arguments(mut self, arguments: Vec<&str>) -> Self {
        self.arguments = arguments.iter().map(|v| v.to_string()).collect();
        self
    }

    pub fn with_artifacts(mut self, artifacts: Vec<String>) -> Self {
        for artifact in artifacts {
            if !self.artifacts.contains(&artifact) {
                self.artifacts.push(artifact);
            }
        }
        self
    }

    pub fn with_secrets(mut self, secrets: Vec<(&str, &str)>) -> Self {
        for (name, value) in secrets {
            if !self.secrets.iter().any(|s| s.name == name) {
                self.secrets.push(api::artifact::ArtifactStepSecret {
                    name: name.to_string(),
                    value: value.to_string(),
                });
            }
        }

        self
    }

    pub async fn build(mut self, context: &mut context::ConfigContext) -> Result<String> {
        // Sort for deterministic output
        self.secrets.sort_by(|a, b| a.name.cmp(&b.name));

        let script = formatdoc! {r#"
            mkdir -p $VORPAL_OUTPUT/bin

            cat > $VORPAL_OUTPUT/bin/{name}-logs << "EOF"
            #!/bin/bash
            set -euo pipefail

            if [ -f $VORPAL_OUTPUT/logs.txt ]; then
                tail -f $VORPAL_OUTPUT/logs.txt
            else
                echo "No logs found"
            fi
            EOF

            chmod +x $VORPAL_OUTPUT/bin/{name}-logs

            cat > $VORPAL_OUTPUT/bin/{name}-stop << "EOF"
            #!/bin/bash
            set -euo pipefail

            if [ -f $VORPAL_OUTPUT/pid ]; then
                kill $(cat $VORPAL_OUTPUT/pid)
                rm -rf $VORPAL_OUTPUT/pid
            fi
            EOF

            chmod +x $VORPAL_OUTPUT/bin/{name}-stop

            cat > $VORPAL_OUTPUT/bin/{name}-start << "EOF"
            #!/bin/bash
            set -euo pipefail

            export PATH={artifacts}:$PATH

            $VORPAL_OUTPUT/bin/{name}-stop

            echo "Process: {entrypoint} {arguments}"

            nohup {entrypoint} {arguments} > $VORPAL_OUTPUT/logs.txt 2>&1 &

            PROCESS_PID=$!

            echo "Process ID: $PROCESS_PID"

            echo $PROCESS_PID > $VORPAL_OUTPUT/pid

            echo "Process commands:"
            echo "- {name}-logs (tail logs)"
            echo "- {name}-stop (stop process)"
            echo "- {name}-start (start process)"
            EOF

            chmod +x $VORPAL_OUTPUT/bin/{name}-start"#,
            arguments = self
                .arguments
                .iter()
                .map(|v| v.to_string())
                .collect::<Vec<String>>()
                .join(" "),
            artifacts = self
                .artifacts
                .iter()
                .map(|v| format!("$VORPAL_ARTIFACT_{v}/bin"))
                .collect::<Vec<String>>()
                .join(":"),
            entrypoint = self.entrypoint,
            name = self.name,
        };

        let step = step::shell(context, self.artifacts, vec![], script, self.secrets).await?;

        Artifact::new(self.name, vec![step], self.systems)
            .build(context)
            .await
    }
}

impl<'a> UserEnvironment<'a> {
    pub fn new(name: &'a str, systems: Vec<api::artifact::ArtifactSystem>) -> Self {
        Self {
            artifacts: vec![],
            environments: vec![],
            name,
            symlinks: vec![],
            systems,
        }
    }

    pub fn with_artifacts(mut self, artifacts: Vec<String>) -> Self {
        self.artifacts = artifacts;
        self
    }

    pub fn with_environments(mut self, environments: Vec<String>) -> Self {
        self.environments = environments;
        self
    }

    pub fn with_symlinks(mut self, symlinks: Vec<(&str, &str)>) -> Self {
        for (source, target) in symlinks.into_iter() {
            self.symlinks.push((source.to_string(), target.to_string()));
        }
        self
    }

    pub async fn build(mut self, context: &mut context::ConfigContext) -> Result<String> {
        // Sort for deterministic output
        self.symlinks.sort_by(|a, b| a.0.cmp(&b.0));

        // Setup path

        let step_path_artifacts = self
            .artifacts
            .iter()
            .map(|artifact| format!("{}/bin", get_env_key(artifact)))
            .collect::<Vec<String>>()
            .join(":");

        let mut step_path = step_path_artifacts;

        if let Some(path) = self.environments.iter().find(|x| x.starts_with("PATH=")) {
            if let Some(path_value) = path.split('=').nth(1) {
                step_path = format!("{path_value}:{step_path}");
            }
        }

        // Setup script

        let step_script = formatdoc! {r#"
            mkdir -p $VORPAL_OUTPUT/bin

            cat > $VORPAL_OUTPUT/bin/vorpal-activate-shell << "EOF"
            {environments}
            export PATH="$VORPAL_OUTPUT/bin:{step_path}:$PATH"
            EOF

            cat > $VORPAL_OUTPUT/bin/vorpal-deactivate-symlinks << "EOF"
            #!/bin/bash
            set -euo pipefail
            {symlinks_deactivate}
            EOF

            cat > $VORPAL_OUTPUT/bin/vorpal-activate-symlinks << "EOF"
            #!/bin/bash
            set -euo pipefail
            {symlinks_check}
            {symlinks_activate}
            EOF

            cat > $VORPAL_OUTPUT/bin/vorpal-activate << "EOF"
            #!/bin/bash
            set -euo pipefail

            echo "Deactivating previous symlinks..."

            if [ -f $HOME/.vorpal/bin/vorpal-deactivate-symlinks ]; then
                $HOME/.vorpal/bin/vorpal-deactivate-symlinks
            fi

            echo "Activating symlinks..."

            $VORPAL_OUTPUT/bin/vorpal-activate-symlinks

            echo "Vorpal userenv installed. Run 'source vorpal-activate-shell' to activate."

            ln -sf $VORPAL_OUTPUT/bin/vorpal-activate-shell $HOME/.vorpal/bin/vorpal-activate-shell
            ln -sf $VORPAL_OUTPUT/bin/vorpal-activate-symlinks $HOME/.vorpal/bin/vorpal-activate-symlinks
            ln -sf $VORPAL_OUTPUT/bin/vorpal-deactivate-symlinks $HOME/.vorpal/bin/vorpal-deactivate-symlinks
            EOF


            chmod +x $VORPAL_OUTPUT/bin/vorpal-activate-shell
            chmod +x $VORPAL_OUTPUT/bin/vorpal-deactivate-symlinks
            chmod +x $VORPAL_OUTPUT/bin/vorpal-activate-symlinks
            chmod +x $VORPAL_OUTPUT/bin/vorpal-activate"#,
            environments = self.environments
                .iter()
                .filter(|e| !e.starts_with("PATH="))
                .map(|e| format!("export {e}"))
                .collect::<Vec<String>>()
                .join("\n"),
            symlinks_deactivate = self.symlinks
                .iter()
                .map(|(_, target)| format!("rm -f {target}"))
                .collect::<Vec<String>>()
                .join("\n"),
            symlinks_check = self.symlinks
                .iter()
                .map(|(_, target)| format!("if [ -f {target} ]; then echo \"ERROR: Symlink target exists -> {target}\" && exit 1; fi"))
                .collect::<Vec<String>>()
                .join("\n"),
            symlinks_activate = self.symlinks
                .iter()
                .map(|(source, target)| format!("ln -s {source} {target}"))
                .collect::<Vec<String>>()
                .join("\n"),
        };

        let steps = vec![step::shell(context, self.artifacts, vec![], step_script, vec![]).await?];

        Artifact::new(self.name, steps, self.systems)
            .build(context)
            .await
    }
}

pub fn get_default_address() -> String {
    if let Ok(path) = std::env::var("VORPAL_SOCKET_PATH") {
        if !path.is_empty() {
            return format!("unix://{}", path);
        }
    }
    "unix:///var/lib/vorpal/vorpal.sock".to_string()
}

pub fn get_env_key(digest: &String) -> String {
    format!("$VORPAL_ARTIFACT_{digest}")
}