greentic-start-dev 1.1.27190108346

Greentic lifecycle runner for start/restart/stop orchestration
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
use std::collections::BTreeMap;
use std::io::Read;
use std::path::{Path, PathBuf};

use anyhow::Context;
use greentic_types::decode_pack_manifest;
use hyper::http::Uri;
use serde::{Deserialize, Serialize};
use zip::ZipArchive;

use crate::domains;

pub const EXT_STATIC_ROUTES_V1: &str = "greentic.static-routes.v1";

#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct BundleStaticRoutesInspection {
    pub pack_paths: Vec<PathBuf>,
}

impl BundleStaticRoutesInspection {
    pub fn bundle_has_static_routes(&self) -> bool {
        !self.pack_paths.is_empty()
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct StartupContractInput {
    pub bundle_has_static_routes: bool,
    pub http_listener_enabled: bool,
    pub asset_serving_enabled: bool,
    pub public_base_url: Option<String>,
    pub runtime_config: Option<RuntimeConfig>,
}

#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq, Eq)]
pub struct RuntimeConfig {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub public_base_url: Option<RuntimePublicBaseUrl>,
}

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub struct RuntimePublicBaseUrl {
    pub value: String,
    pub source: RuntimePublicBaseUrlSource,
}

#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum RuntimePublicBaseUrlSource {
    Configured,
    EnvStore,
    Tunnel,
    Derived,
}

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub struct StartupContract {
    pub bundle_has_static_routes: bool,
    pub public_http_enabled: bool,
    pub static_routes_enabled: bool,
    pub asset_serving_enabled: bool,
    pub public_base_url: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub runtime_config: Option<RuntimeConfig>,
}

impl StartupContract {
    pub fn apply_env(&self, env: &mut BTreeMap<String, String>) {
        env.insert(
            "PUBLIC_HTTP_ENABLED".to_string(),
            bool_str(self.public_http_enabled).to_string(),
        );
        env.insert(
            "STATIC_ROUTES_ENABLED".to_string(),
            bool_str(self.static_routes_enabled).to_string(),
        );
        env.insert(
            "ASSET_SERVING_ENABLED".to_string(),
            bool_str(self.asset_serving_enabled).to_string(),
        );
        env.insert(
            "BUNDLE_HAS_STATIC_ROUTES".to_string(),
            bool_str(self.bundle_has_static_routes).to_string(),
        );
        if let Some(url) = self.public_base_url.as_ref() {
            env.insert("PUBLIC_BASE_URL".to_string(), url.clone());
        }
    }
}

pub fn inspect_bundle(root: &Path) -> anyhow::Result<BundleStaticRoutesInspection> {
    let mut pack_paths = Vec::new();
    for pack_path in collect_bundle_packs(root)? {
        if pack_declares_static_routes(&pack_path)? {
            pack_paths.push(pack_path);
        }
    }
    pack_paths.sort();
    Ok(BundleStaticRoutesInspection { pack_paths })
}

pub fn resolve(input: StartupContractInput) -> anyhow::Result<StartupContract> {
    let runtime_config = input.runtime_config.or_else(|| {
        input.public_base_url.clone().map(|value| RuntimeConfig {
            public_base_url: Some(RuntimePublicBaseUrl {
                value,
                source: RuntimePublicBaseUrlSource::Configured,
            }),
        })
    });
    let effective_public_base_url = runtime_config
        .as_ref()
        .and_then(|config| config.public_base_url.as_ref())
        .map(|public_base_url| public_base_url.value.clone())
        .or(input.public_base_url);

    if input.bundle_has_static_routes {
        if !input.http_listener_enabled {
            anyhow::bail!(
                "bundle declares static routes but this launch mode does not expose public HTTP"
            );
        }
        if !input.asset_serving_enabled {
            anyhow::bail!(
                "bundle declares static routes but asset serving is not supported in this launch mode"
            );
        }
    }

    let public_http_enabled = input.http_listener_enabled;
    let static_routes_enabled = input.bundle_has_static_routes
        && input.asset_serving_enabled
        && input.http_listener_enabled;

    Ok(StartupContract {
        bundle_has_static_routes: input.bundle_has_static_routes,
        public_http_enabled,
        static_routes_enabled,
        asset_serving_enabled: input.asset_serving_enabled,
        public_base_url: effective_public_base_url,
        runtime_config,
    })
}

pub fn configured_public_base_url_from_env() -> anyhow::Result<Option<String>> {
    let Ok(raw) = std::env::var("PUBLIC_BASE_URL") else {
        return Ok(None);
    };
    normalize_public_base_url(&raw).map(Some)
}

/// Reads the persisted `public_base_url` from the deployer-managed env store.
///
/// Returns `Ok(None)` when the env directory is missing (cold start before
/// `gtc op env init`), the environment has no persisted URL, or the home dir
/// cannot be resolved (headless CI). Returns `Err` when an env directory
/// exists but is malformed (`environment.json` unreadable or corrupt).
///
/// The deploy-spec validator (`validate_public_base_url`) already gates
/// writes, so no second normalization is needed here.
pub fn configured_public_base_url_from_env_store(env_id: &str) -> anyhow::Result<Option<String>> {
    use greentic_deployer::environment::{EnvironmentStore, LocalFsStore};
    use greentic_types::EnvId;

    let Some(root) = LocalFsStore::default_root() else {
        return Ok(None);
    };
    if !root.join(env_id).is_dir() {
        return Ok(None);
    }
    let env_typed =
        EnvId::new(env_id).with_context(|| format!("invalid environment id `{env_id}`"))?;
    let store = LocalFsStore::new(root);
    let environment = EnvironmentStore::load(&store, &env_typed)
        .with_context(|| format!("loading environment `{env_id}` for public_base_url"))?;
    Ok(environment.host_config.public_base_url)
}

/// Single point of truth for the env-derived precedence:
/// `host_config.public_base_url > PUBLIC_BASE_URL env var`.
///
/// Returns `Err` only when the env var is set but malformed; the caller picks
/// the error policy. The boot-time bundle-less path propagates with `?`; the
/// reload hook in `revision_webhook_register` discards with `.ok().flatten()`
/// because the watcher is intentionally fail-soft.
pub fn resolve_public_base_url(
    env: &greentic_deploy_spec::Environment,
) -> anyhow::Result<Option<String>> {
    if let Some(url) = env.host_config.public_base_url.as_deref() {
        return Ok(Some(url.to_string()));
    }
    configured_public_base_url_from_env()
}

fn collect_bundle_packs(root: &Path) -> anyhow::Result<Vec<PathBuf>> {
    let mut packs = Vec::new();
    for dir in [root.join("providers"), root.join("packs")] {
        if !dir.exists() {
            continue;
        }
        collect_gtpacks(&dir, &mut packs)?;
    }
    packs.retain(|path| domains::supports_runtime_pack_loading(path));
    packs.sort();
    packs.dedup();
    Ok(packs)
}

fn collect_gtpacks(root: &Path, out: &mut Vec<PathBuf>) -> anyhow::Result<()> {
    let mut stack = vec![root.to_path_buf()];
    while let Some(dir) = stack.pop() {
        for entry in std::fs::read_dir(&dir)? {
            let entry = entry?;
            let path = entry.path();
            if entry.file_type()?.is_dir() {
                stack.push(path);
                continue;
            }
            if path.extension().and_then(|ext| ext.to_str()) == Some("gtpack") {
                out.push(path);
            }
        }
    }
    Ok(())
}

fn pack_declares_static_routes(path: &Path) -> anyhow::Result<bool> {
    let file = std::fs::File::open(path)?;
    let mut archive = ZipArchive::new(file)?;
    let mut manifest_entry = archive.by_name("manifest.cbor").map_err(|err| {
        anyhow::anyhow!("failed to open manifest.cbor in {}: {err}", path.display())
    })?;
    let mut bytes = Vec::new();
    manifest_entry.read_to_end(&mut bytes)?;
    let manifest = decode_pack_manifest(&bytes)
        .with_context(|| format!("failed to decode pack manifest in {}", path.display()))?;
    Ok(manifest
        .extensions
        .as_ref()
        .is_some_and(|extensions| extensions.contains_key(EXT_STATIC_ROUTES_V1)))
}

fn normalize_public_base_url(value: &str) -> anyhow::Result<String> {
    let trimmed = value.trim();
    if trimmed.is_empty() {
        anyhow::bail!("PUBLIC_BASE_URL cannot be empty");
    }
    if trimmed.contains(char::is_whitespace) {
        anyhow::bail!("PUBLIC_BASE_URL must not contain whitespace");
    }
    let uri: Uri = trimmed
        .parse()
        .with_context(|| format!("PUBLIC_BASE_URL is not a valid URI: {trimmed}"))?;
    match uri.scheme_str() {
        Some("http") | Some("https") => {}
        _ => anyhow::bail!("PUBLIC_BASE_URL must start with http:// or https://"),
    }
    if uri.authority().is_none() {
        anyhow::bail!("PUBLIC_BASE_URL must include a host");
    }
    if let Some(path_and_query) = uri.path_and_query() {
        if path_and_query.query().is_some() {
            anyhow::bail!("PUBLIC_BASE_URL must not include a query string");
        }
        let path = path_and_query.path();
        if path != "/" && !path.is_empty() {
            anyhow::bail!("PUBLIC_BASE_URL must be an origin without a path");
        }
    }
    Ok(trimmed.trim_end_matches('/').to_string())
}

const fn bool_str(value: bool) -> &'static str {
    if value { "true" } else { "false" }
}

#[cfg(test)]
mod tests {
    use super::*;
    use greentic_types::{
        ExtensionInline, ExtensionRef, PackId, PackKind, PackManifest, PackSignatures,
    };
    use semver::Version;
    use serde_json::json;
    use std::collections::BTreeMap;
    use std::fs::File;
    use std::io::Write;
    use tempfile::tempdir;
    use zip::ZipWriter;
    use zip::write::FileOptions;

    #[test]
    fn inspect_bundle_detects_static_route_extension() -> anyhow::Result<()> {
        let dir = tempdir()?;
        let pack_path = dir.path().join("packs").join("default.gtpack");
        write_pack(&pack_path, true)?;
        let inspection = inspect_bundle(dir.path())?;
        assert!(inspection.bundle_has_static_routes());
        assert_eq!(inspection.pack_paths, vec![pack_path]);
        Ok(())
    }

    #[test]
    fn inspect_bundle_ignores_non_runtime_packs() -> anyhow::Result<()> {
        let dir = tempdir()?;
        let pack_path = dir.path().join("packs").join("default.gtpack");
        write_pack(&pack_path, true)?;
        std::fs::write(
            dir.path().join("packs").join("terraform.gtpack"),
            b"not-a-zip",
        )?;
        let inspection = inspect_bundle(dir.path())?;
        assert!(inspection.bundle_has_static_routes());
        assert_eq!(inspection.pack_paths, vec![pack_path]);
        Ok(())
    }

    #[test]
    fn resolve_rejects_missing_public_http() {
        let err = resolve(StartupContractInput {
            bundle_has_static_routes: true,
            http_listener_enabled: false,
            asset_serving_enabled: true,
            public_base_url: Some("https://example.com".to_string()),
            runtime_config: None,
        })
        .expect_err("expected launch gating failure");
        assert!(err.to_string().contains("does not expose public HTTP"));
    }

    #[test]
    fn resolve_allows_missing_public_base_url_when_http_and_assets_are_available() {
        let contract = resolve(StartupContractInput {
            bundle_has_static_routes: true,
            http_listener_enabled: true,
            asset_serving_enabled: true,
            public_base_url: None,
            runtime_config: None,
        })
        .expect("expected static routes startup contract");
        assert!(contract.public_http_enabled);
        assert!(contract.static_routes_enabled);
        assert!(contract.public_base_url.is_none());
    }

    #[test]
    fn resolve_enables_static_routes_when_requirements_are_met() -> anyhow::Result<()> {
        let contract = resolve(StartupContractInput {
            bundle_has_static_routes: true,
            http_listener_enabled: true,
            asset_serving_enabled: true,
            public_base_url: Some("https://example.com".to_string()),
            runtime_config: None,
        })?;
        assert!(contract.public_http_enabled);
        assert!(contract.static_routes_enabled);
        Ok(())
    }

    #[test]
    fn resolve_prefers_runtime_config_public_base_url() -> anyhow::Result<()> {
        let contract = resolve(StartupContractInput {
            bundle_has_static_routes: true,
            http_listener_enabled: true,
            asset_serving_enabled: true,
            public_base_url: Some("https://configured.example.com".to_string()),
            runtime_config: Some(RuntimeConfig {
                public_base_url: Some(RuntimePublicBaseUrl {
                    value: "https://tunnel.example.com".to_string(),
                    source: RuntimePublicBaseUrlSource::Tunnel,
                }),
            }),
        })?;
        assert_eq!(
            contract.public_base_url.as_deref(),
            Some("https://tunnel.example.com")
        );
        assert_eq!(
            contract
                .runtime_config
                .as_ref()
                .and_then(|config| config.public_base_url.as_ref())
                .map(|entry| entry.source),
            Some(RuntimePublicBaseUrlSource::Tunnel)
        );
        Ok(())
    }

    #[test]
    fn normalize_public_base_url_rejects_paths() {
        let err = normalize_public_base_url("https://example.com/path")
            .expect_err("expected invalid path");
        assert!(err.to_string().contains("without a path"));
    }

    /// Round-trips an EnvStore-source URL through `resolve` so the new enum
    /// variant survives serde + the same precedence-preserving identity that
    /// `Tunnel` already has. Pure runtime-config flow — no disk I/O.
    #[test]
    fn resolve_surfaces_env_store_source_from_runtime_config() -> anyhow::Result<()> {
        let contract = resolve(StartupContractInput {
            bundle_has_static_routes: true,
            http_listener_enabled: true,
            asset_serving_enabled: true,
            public_base_url: Some("https://from-env-var.example.com".to_string()),
            runtime_config: Some(RuntimeConfig {
                public_base_url: Some(RuntimePublicBaseUrl {
                    value: "https://persisted.example.com".to_string(),
                    source: RuntimePublicBaseUrlSource::EnvStore,
                }),
            }),
        })?;
        assert_eq!(
            contract.public_base_url.as_deref(),
            Some("https://persisted.example.com")
        );
        assert_eq!(
            contract
                .runtime_config
                .as_ref()
                .and_then(|config| config.public_base_url.as_ref())
                .map(|entry| entry.source),
            Some(RuntimePublicBaseUrlSource::EnvStore)
        );
        let json = serde_json::to_string(&contract)?;
        assert!(
            json.contains("\"env_store\""),
            "expected snake_case env_store in: {json}"
        );
        Ok(())
    }

    /// Cold-start guard: with no env directory on disk, the reader returns
    /// `Ok(None)` rather than failing. Mirrors the helper's contract for the
    /// pre-init case (no `gtc op env init` yet, no `~/.greentic/environments/`).
    #[test]
    fn env_store_reader_returns_none_for_missing_env_dir() -> anyhow::Result<()> {
        let dir = tempdir()?;
        // Point HOME at an empty temp dir so `LocalFsStore::default_root` resolves
        // to a real path but no env subdirectory exists. `set_var` is `unsafe` on
        // Rust 2024; safe here because the test does not spawn child threads
        // that read HOME concurrently.
        let prev = std::env::var("HOME").ok();
        unsafe {
            std::env::set_var("HOME", dir.path());
        }
        let result = configured_public_base_url_from_env_store("local");
        unsafe {
            match prev {
                Some(v) => std::env::set_var("HOME", v),
                None => std::env::remove_var("HOME"),
            }
        }
        assert_eq!(result?, None);
        Ok(())
    }

    fn write_pack(path: &Path, with_static_routes: bool) -> anyhow::Result<()> {
        if let Some(parent) = path.parent() {
            std::fs::create_dir_all(parent)?;
        }
        let mut extensions = BTreeMap::new();
        if with_static_routes {
            extensions.insert(
                EXT_STATIC_ROUTES_V1.to_string(),
                ExtensionRef {
                    kind: EXT_STATIC_ROUTES_V1.to_string(),
                    version: "1.0.0".to_string(),
                    digest: None,
                    location: None,
                    inline: Some(ExtensionInline::Other(json!({
                        "schema_version": 1,
                        "routes": [{"path": "/"}]
                    }))),
                },
            );
        }
        let manifest = PackManifest {
            schema_version: "pack-v1".to_string(),
            pack_id: PackId::new("demo.static").expect("pack id"),
            name: None,
            version: Version::parse("0.1.0").expect("version"),
            kind: PackKind::Provider,
            publisher: "demo".to_string(),
            components: Vec::new(),
            flows: Vec::new(),
            dependencies: Vec::new(),
            capabilities: Vec::new(),
            secret_requirements: Vec::new(),
            signatures: PackSignatures::default(),
            bootstrap: None,
            extensions: if extensions.is_empty() {
                None
            } else {
                Some(extensions)
            },
        };
        let bytes = greentic_types::encode_pack_manifest(&manifest)?;
        let file = File::create(path)?;
        let mut zip = ZipWriter::new(file);
        zip.start_file("manifest.cbor", FileOptions::<()>::default())?;
        zip.write_all(&bytes)?;
        zip.finish()?;
        Ok(())
    }
}