|
| 1 | +//! Create-from-JSON-file example. |
| 2 | +//! |
| 3 | +//! Demonstrates how to use `create_environment_from_file()` — the SDK method |
| 4 | +//! that mirrors the CLI's `create environment --env-file <path>` flag. |
| 5 | +//! |
| 6 | +//! Users migrating from the CLI will already have JSON config files in their |
| 7 | +//! `envs/` directory. This example shows the typical workflow: |
| 8 | +//! |
| 9 | +//! 1. **Validate** — check the config file is valid before creating |
| 10 | +//! 2. **Create** — load the file and create the environment in one call |
| 11 | +//! 3. **Show** — inspect the resulting environment |
| 12 | +//! 4. **Purge** — clean up |
| 13 | +//! |
| 14 | +//! The example also shows how to handle `CreateEnvironmentFromFileError` to |
| 15 | +//! distinguish load/parse failures from creation failures. |
| 16 | +//! |
| 17 | +//! # Running |
| 18 | +//! |
| 19 | +//! ```bash |
| 20 | +//! cargo run --example sdk_create_from_json_file |
| 21 | +//! ``` |
| 22 | +
|
| 23 | +use std::io::Write as _; |
| 24 | +use std::path::PathBuf; |
| 25 | + |
| 26 | +use torrust_tracker_deployer_lib::presentation::sdk::{ |
| 27 | + ConfigLoadError, CreateEnvironmentFromFileError, Deployer, |
| 28 | +}; |
| 29 | + |
| 30 | +fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 31 | + let workspace = PathBuf::from(env!("CARGO_MANIFEST_DIR")); |
| 32 | + |
| 33 | + println!("=== Torrust Tracker Deployer SDK — Create from JSON File ===\n"); |
| 34 | + |
| 35 | + let deployer = Deployer::builder().working_dir(&workspace).build()?; |
| 36 | + println!( |
| 37 | + "[OK] Deployer initialized (workspace: {})\n", |
| 38 | + workspace.display() |
| 39 | + ); |
| 40 | + |
| 41 | + let private_key = workspace.join("fixtures/testing_rsa"); |
| 42 | + let public_key = workspace.join("fixtures/testing_rsa.pub"); |
| 43 | + |
| 44 | + // ------------------------------------------------------------------ |
| 45 | + // Write an environment config JSON file to a temp location. |
| 46 | + // |
| 47 | + // In real usage you would already have this file in your envs/ dir; |
| 48 | + // here we generate it at runtime so the example is self-contained and |
| 49 | + // the SSH key paths are always correct for the current workspace. |
| 50 | + // ------------------------------------------------------------------ |
| 51 | + let json = format!( |
| 52 | + r#"{{ |
| 53 | + "environment": {{ "name": "sdk-from-json-file" }}, |
| 54 | + "ssh_credentials": {{ |
| 55 | + "private_key_path": "{private_key}", |
| 56 | + "public_key_path": "{public_key}" |
| 57 | + }}, |
| 58 | + "provider": {{ |
| 59 | + "provider": "lxd", |
| 60 | + "profile_name": "torrust-sdk-from-json-file" |
| 61 | + }}, |
| 62 | + "tracker": {{ |
| 63 | + "core": {{ |
| 64 | + "database": {{ "driver": "sqlite3", "database_name": "tracker.db" }}, |
| 65 | + "private": false |
| 66 | + }}, |
| 67 | + "udp_trackers": [{{ "bind_address": "0.0.0.0:6969" }}], |
| 68 | + "http_trackers": [{{ "bind_address": "0.0.0.0:7070" }}], |
| 69 | + "http_api": {{ "bind_address": "0.0.0.0:1212", "admin_token": "MyAccessToken" }}, |
| 70 | + "health_check_api": {{ "bind_address": "0.0.0.0:1313" }} |
| 71 | + }} |
| 72 | +}}"#, |
| 73 | + private_key = private_key.display(), |
| 74 | + public_key = public_key.display(), |
| 75 | + ); |
| 76 | + |
| 77 | + let mut tmp = tempfile::NamedTempFile::new()?; |
| 78 | + tmp.write_all(json.as_bytes())?; |
| 79 | + let config_path = tmp.path(); |
| 80 | + println!("Config file written to: {}\n", config_path.display()); |
| 81 | + |
| 82 | + // ------------------------------------------------------------------ |
| 83 | + // Step 1: Validate the config file before creating |
| 84 | + // ------------------------------------------------------------------ |
| 85 | + println!("--- Step 1: Validate config ---"); |
| 86 | + let validation = deployer.validate(config_path)?; |
| 87 | + println!(" Config is valid."); |
| 88 | + println!(" Environment name : {}", validation.environment_name); |
| 89 | + println!(" Provider : {}\n", validation.provider); |
| 90 | + |
| 91 | + // ------------------------------------------------------------------ |
| 92 | + // Step 2: Create environment from the JSON file |
| 93 | + // ------------------------------------------------------------------ |
| 94 | + println!("--- Step 2: Create from file ---"); |
| 95 | + let env_name = deployer.create_environment_from_file(config_path)?; |
| 96 | + println!(" Created: {env_name}\n"); |
| 97 | + |
| 98 | + // ------------------------------------------------------------------ |
| 99 | + // Step 3: Show environment details |
| 100 | + // ------------------------------------------------------------------ |
| 101 | + println!("--- Step 3: Show environment ---"); |
| 102 | + let info = deployer.show(&env_name)?; |
| 103 | + println!(" Name: {}", info.name); |
| 104 | + println!(" State: {}", info.state); |
| 105 | + println!(" Provider: {}", info.provider); |
| 106 | + println!(" Created at: {}\n", info.created_at); |
| 107 | + |
| 108 | + // ------------------------------------------------------------------ |
| 109 | + // Step 4: Demonstrate error handling for a missing file |
| 110 | + // ------------------------------------------------------------------ |
| 111 | + println!("--- Step 4: Error — file not found ---"); |
| 112 | + let bad_path = workspace.join("envs/non-existent-config.json"); |
| 113 | + match deployer.create_environment_from_file(&bad_path) { |
| 114 | + Ok(_) => println!(" Unexpected success."), |
| 115 | + Err(CreateEnvironmentFromFileError::Load(ConfigLoadError::FileNotFound { path })) => { |
| 116 | + println!(" Caught FileNotFound: {}", path.display()); |
| 117 | + println!(" (Expected — file does not exist.)\n"); |
| 118 | + } |
| 119 | + Err(e) => println!(" Unexpected error: {e}\n"), |
| 120 | + } |
| 121 | + |
| 122 | + // ------------------------------------------------------------------ |
| 123 | + // Cleanup |
| 124 | + // ------------------------------------------------------------------ |
| 125 | + println!("--- Cleanup ---"); |
| 126 | + deployer.purge(&env_name)?; |
| 127 | + println!(" Environment '{env_name}' purged."); |
| 128 | + |
| 129 | + println!("\n=== Example complete ==="); |
| 130 | + Ok(()) |
| 131 | +} |
0 commit comments