Skip to content

Commit

Permalink
Add iotedge system reprovision (#4660)
Browse files Browse the repository at this point in the history
Adds a command to reprovision device with IoT Hub. Also restarts edged so that the new provisioning info is used.
  • Loading branch information
gordonwang0 authored Mar 23, 2021
1 parent 51ad827 commit 98c9168
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 1 deletion.
2 changes: 2 additions & 0 deletions edgelet/Cargo.lock

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

2 changes: 1 addition & 1 deletion edgelet/identity-client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl IdentityClient {
let res = build_request_uri(&self.host, &uri)
.into_future()
.and_then(move |uri| {
request::<_, _, ()>(&client, hyper::Method::POST, &uri, Some(&body))
request_no_content::<_, _>(&client, hyper::Method::POST, &uri, Some(&body))
});

Box::new(res)
Expand Down
2 changes: 2 additions & 0 deletions edgelet/iotedge/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ zip = "0.5.3"

aziot-certd-config = { git = "https://github.com/Azure/iot-identity-service", branch = "main" }
aziot-identity-common = { git = "https://github.com/Azure/iot-identity-service", branch = "main" }
aziot-identity-common-http = { git = "https://github.com/Azure/iot-identity-service", branch = "main" }
aziot-identityd-config = { git = "https://github.com/Azure/iot-identity-service", branch = "main" }
aziot-keyd-config = { git = "https://github.com/Azure/iot-identity-service", branch = "main" }
aziot-keys-common = { git = "https://github.com/Azure/iot-identity-service", branch = "main" }
Expand All @@ -51,6 +52,7 @@ edgelet-docker = { path = "../edgelet-docker" }
edgelet-http = { path = "../edgelet-http" }
edgelet-http-mgmt = { path = "../edgelet-http-mgmt" }
edgelet-utils = { path = "../edgelet-utils" }
identity-client = { path = "../identity-client" }
management = { path = "../management" }
support-bundle = { path = "../support-bundle" }

Expand Down
5 changes: 5 additions & 0 deletions edgelet/iotedge/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,10 @@ fn run() -> Result<(), Error> {
.required(true),
)
)
.subcommand(
SubCommand::with_name("reprovision")
.about("Reprovision device with IoT Hub.")
)
)
.subcommand(
SubCommand::with_name("support-bundle")
Expand Down Expand Up @@ -519,6 +523,7 @@ fn run() -> Result<(), Error> {
log::Level::from_str(args.value_of("log_level").expect("Value is required"))
.expect("Value is restricted to parsable fields"),
),
("reprovision", Some(_args)) => System::reprovision(&mut tokio_runtime),

(command, _) => {
eprintln!("Unknown system subcommand {:?}", command);
Expand Down
25 changes: 25 additions & 0 deletions edgelet/iotedge/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ use aziotctl_common::{
SERVICE_DEFINITIONS as IS_SERVICES,
};

use aziot_identity_common_http::ApiVersion;
use identity_client::IdentityClient;

use crate::error::{Error, ErrorKind};

lazy_static! {
Expand Down Expand Up @@ -71,4 +74,26 @@ impl System {
Error::from(ErrorKind::System)
})
}

pub fn reprovision(runtime: &mut tokio::runtime::Runtime) -> Result<(), Error> {
let uri = url::Url::parse("unix:///run/aziot/identityd.sock")
.expect("hard-coded URI should parse");
let client = IdentityClient::new(ApiVersion::V2020_09_01, &uri);

runtime
.block_on(client.reprovision_device())
.map_err(|err| {
eprintln!("Failed to reprovision: {}", err);
Error::from(ErrorKind::System)
})?;

println!("Successfully reprovisioned with IoT Hub.");

restart(&[&IOTEDGED]).map_err(|err| {
eprintln!("{:#?}", err);
Error::from(ErrorKind::System)
})?;

Ok(())
}
}

0 comments on commit 98c9168

Please sign in to comment.