Skip to main content

updated_example/
lib.rs

1// SPDX-License-Identifier: MIT
2// SPDX-FileCopyrightText: <text>
3// Copyright(c) 2026 Liebherr-Digital Development Center GmbH
4// Written by Thomas Witte <thomas.witte@liebherr.com>
5// </text>
6
7use std::{collections::HashMap, path::Path, time::Duration};
8
9use libupdated::{
10    checks::SystemdIsSystemRunning,
11    consent_handler::AutoConsent,
12    directory_installer::DirectoryInstaller,
13    directory_source::DirectorySource,
14    file_store::FileStore,
15    log_reporter::LogReporter,
16    trigger::Rate,
17    update_workflow::{
18        UpdateOptions, WorkflowError, get_config, monitor_update, update_with_consent_flow,
19    },
20    workflow,
21};
22use tracing::{error, info};
23
24// This is an example for a simple update workflow that periodically checks a
25// directory for updates, always gives consent for the update, and copies the
26// update files to a target directory.
27// On the next start, it checks whether the system is still running correctly.
28#[workflow]
29pub async fn example_workflow(config: HashMap<String, String>) -> Result<(), WorkflowError> {
30    // Create a Trigger that checks for updates every `update_interval_sec`
31    // seconds.
32    let update_interval = get_config(&config, "update_interval_sec")?
33        .parse::<u64>()
34        .map(Duration::from_secs)
35        .map_err(|e| WorkflowError::InvalidConfiguration(e.to_string()))?;
36    let mut trigger = Rate::new(
37        update_interval,
38        UpdateOptions {
39            ignore_previously_declined: true,
40            consent_timeout: update_interval,
41        },
42    );
43
44    // Create an UpdateSource for the directory `source_dir` in the config.
45    let mut source = DirectorySource::new(Path::new(get_config(&config, "source_dir")?.as_str()));
46
47    // If consent is required for the update, always accept it.
48    let mut consent_handler = AutoConsent::new(true);
49
50    // Create an Installer that copies files to the `target_dir` in the config.
51    let installer = DirectoryInstaller::new(get_config(&config, "target_dir")?.as_str());
52
53    // Create a store file to persist the state of an update across restarts.
54    let mut store = FileStore::new(get_config(&config, "sota_state_file")?.as_str());
55
56    // Report the progress of the update to the (tracing) log.
57    let mut progress = LogReporter::new("example_workflow");
58
59    // Configure checks to be run after an update.
60    let mut check = SystemdIsSystemRunning {};
61
62    info!(target: "example_workflow", "Workflow configuration finished, starting update loop. Configuration: {:?}", config);
63
64    // Check whether a previous update left the system in a bad state
65    if let Err(e) = monitor_update(&mut source, &mut store, &mut progress, &mut check).await {
66        error!(target: "example_workflow", "Previous update failed: {}", e);
67    }
68
69    loop {
70        // Run the update logic and print the result.
71        if let Err(e) = update_with_consent_flow(
72            &mut trigger,
73            &mut source,
74            &mut consent_handler,
75            &installer,
76            &mut store,
77            &mut progress,
78        )
79        .await
80        {
81            error!(target: "example_workflow", "Update result: {}", e);
82        } else {
83            info!(target: "example_workflow", "Update completed successfully.");
84        }
85    }
86}