Skip to content

Commit

Permalink
Merge pull request #2920 from reubenmiller/feat-device-profile-operation
Browse files Browse the repository at this point in the history
feat: support device profile operation
  • Loading branch information
albinsuresh committed Aug 20, 2024
2 parents be7ef4c + e6697cd commit 059dd8c
Show file tree
Hide file tree
Showing 20 changed files with 1,485 additions and 5 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ define_tedge_config! {
firmware_update: bool,

/// Enable device_profile feature
#[tedge_config(example = "true", default(value = false))]
#[tedge_config(example = "true", default(value = true))]
device_profile: bool,
},

Expand Down
3 changes: 3 additions & 0 deletions crates/core/tedge_agent/src/agent.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::device_profile_manager::DeviceProfileManagerBuilder;
use crate::file_transfer_server::actor::FileTransferServerBuilder;
use crate::file_transfer_server::actor::FileTransferServerConfig;
use crate::operation_file_cache::FileCacheActorBuilder;
Expand Down Expand Up @@ -251,6 +252,8 @@ impl Agent {
// Software update actor
let mut software_update_builder = SoftwareManagerBuilder::new(self.config.sw_update_config);

DeviceProfileManagerBuilder::try_new(&self.config.operations_dir)?;

// Converter actor
let mut converter_actor_builder = WorkflowActorBuilder::new(
self.config.operation_config,
Expand Down
17 changes: 17 additions & 0 deletions crates/core/tedge_agent/src/device_profile_manager/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use camino::Utf8PathBuf;
use tedge_utils::file::create_file_with_defaults;
use tedge_utils::file::FileError;

pub struct DeviceProfileManagerBuilder {}

impl DeviceProfileManagerBuilder {
pub fn try_new(ops_dir: &Utf8PathBuf) -> Result<Self, FileError> {
let workflow_file = ops_dir.join("device_profile.toml");
if !workflow_file.exists() {
let workflow_definition = include_str!("../resources/device_profile.toml");

create_file_with_defaults(workflow_file, Some(workflow_definition))?;
}
Ok(Self {})
}
}
1 change: 1 addition & 0 deletions crates/core/tedge_agent/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use tedge_config::DEFAULT_TEDGE_CONFIG_PATH;
use tracing::log::warn;

mod agent;
mod device_profile_manager;
mod file_transfer_server;
mod operation_file_cache;
mod operation_workflows;
Expand Down
19 changes: 19 additions & 0 deletions crates/core/tedge_agent/src/operation_workflows/actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,25 @@ impl WorkflowActor {

Ok(())
}
OperationAction::Iterate(target_json_path, handlers) => {
match OperationAction::process_iterate(
state.clone(),
&target_json_path,
handlers.clone(),
) {
Ok(next_state) => {
self.publish_command_state(next_state, &mut log_file)
.await?
}
Err(err) => {
error!("Iteration failed due to: {err}");
let new_state = state
.update(handlers.on_error.expect("on_error target can not be none"));
self.publish_command_state(new_state, &mut log_file).await?;
}
}
Ok(())
}
}
}

Expand Down
40 changes: 40 additions & 0 deletions crates/core/tedge_agent/src/resources/device_profile.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
operation = "device_profile"

[init]
action = "proceed"
on_success = "scheduled"

[scheduled]
action = "proceed"
on_success = "executing"

[executing]
action = "proceed"
on_success = "next_operation"

[next_operation]
iterate = "${.payload.operations}"
on_next = "apply_operation"
on_success = "successful"
on_error = "rollback"

[apply_operation]
operation = "${.payload.@next.item.operation}"
input = "${.payload.@next.item.payload}"
on_exec = "awaiting_operation"

[awaiting_operation]
action = "await-operation-completion"
on_success = "next_operation"
on_error = "rollback"

[rollback]
action="proceed"
on_success = { status = "failed", reason = "Device profile application failed" }
on_error = { status = "failed", reason = "Rollback failed" }

[successful]
action = "cleanup"

[failed]
action = "cleanup"
1 change: 1 addition & 0 deletions crates/core/tedge_api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ tokio = { workspace = true, features = ["fs", "process"] }

[dev-dependencies]
anyhow = { workspace = true }
assert-json-diff = { workspace = true }
assert_matches = { workspace = true }
clock = { workspace = true }
maplit = { workspace = true }
Expand Down
3 changes: 3 additions & 0 deletions crates/core/tedge_api/src/workflow/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ pub enum WorkflowDefinitionError {

#[error("Unknown action: {action}")]
UnknownAction { action: String },

#[error("The provided target {0} is not a valid path expression")]
InvalidPathExpression(String),
}

/// Error related to a script definition
Expand Down
Loading

0 comments on commit 059dd8c

Please sign in to comment.