Skip to content

Commit 1e9fdef

Browse files
committed
fixes to localnet purge
1 parent 23ad51c commit 1e9fdef

File tree

5 files changed

+61
-36
lines changed

5 files changed

+61
-36
lines changed

tools/internal/localnet-orchestrator/src/cli/purge.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use crate::cli::CommonArgs;
55
use crate::orchestrator::LocalnetOrchestrator;
66
use crate::orchestrator::setup::purge;
7+
use clap::ArgAction;
78
use std::path::PathBuf;
89
use tracing::debug;
910

@@ -13,11 +14,11 @@ pub(crate) struct Args {
1314
common: CommonArgs,
1415

1516
/// Remove any built docker and container images
16-
#[clap(long, default_value_t = true)]
17+
#[clap(long, action = ArgAction::Set, default_value_t = true)]
1718
remove_images: bool,
1819

1920
/// Remove any cached build data
20-
#[clap(long, default_value_t = true)]
21+
#[clap(long, action = ArgAction::Set, default_value_t = true)]
2122
remove_cache: bool,
2223

2324
/// Custom path to root of the monorepo in case this binary has been executed from a different location.

tools/internal/localnet-orchestrator/src/orchestrator/setup/purge.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
// Copyright 2025 - Nym Technologies SA <[email protected]>
22
// SPDX-License-Identifier: Apache-2.0
33

4-
use crate::constants::LOCALNET_NYXD_IMAGE_NAME;
54
use crate::helpers::monorepo_root_path;
65
use crate::orchestrator::LocalnetOrchestrator;
76
use crate::orchestrator::container_helpers::{
87
default_nym_binaries_image_tag, remove_container_image,
98
};
109
use crate::orchestrator::context::LocalnetContext;
10+
use anyhow::Context;
1111
use std::path::PathBuf;
1212

1313
struct LocalnetPurge {
@@ -41,12 +41,16 @@ impl LocalnetOrchestrator {
4141
) -> anyhow::Result<()> {
4242
ctx.begin_next_step("removing all built images", "🔥");
4343
if !ctx.data.remove_images {
44-
ctx.println("\t NOT ENABLED - SKIPPING")
44+
ctx.println("\t NOT ENABLED - SKIPPING");
45+
return Ok(());
4546
}
4647

4748
let nym_binaries_tag = default_nym_binaries_image_tag(&ctx.data.monorepo_root)?;
4849

49-
for tag in [nym_binaries_tag, LOCALNET_NYXD_IMAGE_NAME.to_string()] {
50+
// TODO: we need to dynamically determine tag for this
51+
// LOCALNET_NYXD_IMAGE_NAME.to_string()
52+
53+
for tag in [nym_binaries_tag] {
5054
ctx.execute_cmd_with_stdout("docker", ["image", "rm", &tag])
5155
.await?;
5256
remove_container_image(ctx, &tag).await?;
@@ -58,7 +62,8 @@ impl LocalnetOrchestrator {
5862
fn remove_build_cache(&self, ctx: &mut LocalnetContext<LocalnetPurge>) -> anyhow::Result<()> {
5963
ctx.begin_next_step("removing build cache", "🔥");
6064
if !ctx.data.remove_cache {
61-
ctx.println("\t NOT ENABLED - SKIPPING")
65+
ctx.println("\t NOT ENABLED - SKIPPING");
66+
return Ok(());
6267
}
6368

6469
self.storage.data_cache().clear()
@@ -70,17 +75,18 @@ impl LocalnetOrchestrator {
7075
) -> anyhow::Result<()> {
7176
ctx.begin_next_step("removing storage data", "🔥");
7277

73-
std::fs::remove_dir_all(self.storage.localnet_directory())?;
78+
std::fs::remove_dir_all(self.storage.localnet_directory())
79+
.context("missing main storage directory")?;
7480
let storage_db = self.storage.into_orchestrator_storage();
7581
let db_path = storage_db.stop().await?;
76-
std::fs::remove_dir_all(db_path)?;
82+
std::fs::remove_file(db_path).context("missing database path")?;
7783

7884
Ok(())
7985
}
8086

8187
pub(crate) async fn purge_localnet(self, config: Config) -> anyhow::Result<()> {
8288
let purge = LocalnetPurge::new(config)?;
83-
let mut ctx = LocalnetContext::new(purge, 4, "\npurging localnet");
89+
let mut ctx = LocalnetContext::new(purge, 3, "\npurging localnet");
8490

8591
// 1. stop the localnet
8692
self.stop_localnet().await?;

tools/internal/localnet-orchestrator/src/serde_helpers/linux.rs

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright 2025 - Nym Technologies SA <[email protected]>
22
// SPDX-License-Identifier: Apache-2.0
33

4-
use crate::serde_helpers::PlaceholderCommonContainerInformation;
54
use anyhow::Context;
65
use anyhow::bail;
76
use serde::{Deserialize, Serialize};
@@ -84,6 +83,7 @@ impl TryFrom<ContainersList> for super::ContainersList {
8483
containers: value
8584
.0
8685
.into_iter()
86+
.filter(|c| c.names.contains("localnet"))
8787
.map(TryInto::try_into)
8888
.collect::<anyhow::Result<Vec<_>>>()?,
8989
})
@@ -103,11 +103,11 @@ pub struct ContainerListContainer {
103103
pub status: String,
104104
}
105105

106-
impl TryFrom<ContainerListContainer> for PlaceholderCommonContainerInformation {
106+
impl TryFrom<ContainerListContainer> for super::CommonContainerInformation {
107107
type Error = anyhow::Error;
108108

109109
fn try_from(value: ContainerListContainer) -> Result<Self, Self::Error> {
110-
Ok(PlaceholderCommonContainerInformation {
110+
Ok(super::CommonContainerInformation {
111111
name: value.names,
112112
ip_address: None,
113113
status: value.status.to_lowercase(),
@@ -159,30 +159,37 @@ pub struct ContainerNetworkInformation {
159159
pub mac_address: String,
160160
}
161161

162-
impl TryFrom<ContainerInformation> for PlaceholderCommonContainerInformation {
162+
impl TryFrom<ContainerInformation> for super::CommonContainerInformation {
163163
type Error = anyhow::Error;
164164

165165
fn try_from(value: ContainerInformation) -> Result<Self, Self::Error> {
166-
if value.network_settings.networks.is_empty() {
167-
bail!("no attached networks")
168-
}
166+
let status = value.state.status.to_lowercase();
167+
168+
let ip_address = if status != "running" && status != "up" {
169+
if value.network_settings.networks.is_empty() {
170+
bail!("no attached networks")
171+
}
169172

170-
// find first network with non-empty ip address
171-
let Some(network) = value
172-
.network_settings
173-
.networks
174-
.iter()
175-
.find(|n| !n.1.ip_address.is_empty())
176-
.map(|n| n.1)
177-
else {
178-
bail!("no valid network")
173+
// find first network with non-empty ip address
174+
let Some(network) = value
175+
.network_settings
176+
.networks
177+
.iter()
178+
.find(|n| !n.1.ip_address.is_empty())
179+
.map(|n| n.1)
180+
else {
181+
bail!("no valid network")
182+
};
183+
Some(network.ip_address.parse().context("invalid ip address")?)
184+
} else {
185+
None
179186
};
180187

181-
Ok(PlaceholderCommonContainerInformation {
188+
Ok(super::CommonContainerInformation {
182189
name: value.name,
183190
image: value.image,
184-
ip_address: Some(network.ip_address.parse().context("invalid ip address")?),
185-
status: value.state.status.to_lowercase(),
191+
ip_address,
192+
status,
186193
})
187194
}
188195
}

tools/internal/localnet-orchestrator/src/serde_helpers/macos.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
use crate::constants::CONTAINER_NETWORK_NAME;
5-
use crate::serde_helpers::PlaceholderCommonContainerInformation;
65
use anyhow::{Context, bail};
76
use serde::{Deserialize, Serialize};
87
use std::net::IpAddr;
@@ -18,6 +17,7 @@ impl TryFrom<ContainersList> for super::ContainersList {
1817
containers: value
1918
.0
2019
.into_iter()
20+
.filter(|c| c.configuration.id.contains("localnet"))
2121
.map(TryInto::try_into)
2222
.collect::<anyhow::Result<Vec<_>>>()?,
2323
})
@@ -135,15 +135,26 @@ pub struct ContainerNetwork {
135135
pub address: String, // represented in cidr location
136136
}
137137

138-
impl TryFrom<ContainerInformation> for PlaceholderCommonContainerInformation {
138+
impl TryFrom<ContainerInformation> for super::CommonContainerInformation {
139139
type Error = anyhow::Error;
140140

141+
#[track_caller]
141142
fn try_from(value: ContainerInformation) -> Result<Self, Self::Error> {
142-
Ok(PlaceholderCommonContainerInformation {
143-
ip_address: Some(value.container_ip()?),
143+
let status = value.status.to_lowercase();
144+
let ip_address = if status != "running" && status != "up" {
145+
Some(value.container_ip().context(format!(
146+
"invalid container {} ({})",
147+
value.configuration.id, value.configuration.image.reference
148+
))?)
149+
} else {
150+
None
151+
};
152+
153+
Ok(super::CommonContainerInformation {
154+
ip_address,
144155
name: value.configuration.id,
145156
image: value.configuration.image.reference,
146-
status: value.status.to_lowercase(),
157+
status,
147158
})
148159
}
149160
}

tools/internal/localnet-orchestrator/src/serde_helpers/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub(crate) mod linux;
1212

1313
#[derive(Debug)]
1414
pub struct ContainersList {
15-
pub containers: Vec<PlaceholderCommonContainerInformation>,
15+
pub containers: Vec<CommonContainerInformation>,
1616
}
1717

1818
impl ContainersList {
@@ -25,7 +25,7 @@ impl ContainersList {
2525

2626
#[derive(Debug)]
2727
pub struct ContainerInspect {
28-
pub info: Option<PlaceholderCommonContainerInformation>,
28+
pub info: Option<CommonContainerInformation>,
2929
}
3030

3131
impl ContainerInspect {
@@ -50,7 +50,7 @@ impl ContainerInspect {
5050
}
5151

5252
#[derive(Debug)]
53-
pub struct PlaceholderCommonContainerInformation {
53+
pub struct CommonContainerInformation {
5454
pub name: String,
5555
pub ip_address: Option<IpAddr>,
5656
pub status: String,

0 commit comments

Comments
 (0)