Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(settings): reset launchpad #370

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
1 change: 1 addition & 0 deletions libs/protocol/src/launchpad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ pub enum LaunchpadAction {
Connect,
ChangeSession(LaunchpadSession),
SaveSettings(Box<PersistentSettings>),
ResetSettings,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
Expand Down
20 changes: 20 additions & 0 deletions libs/sdm-launchpad/src/bus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,10 @@ impl LaunchpadWorker {
LaunchpadAction::SaveSettings(settings) => {
self.save_settings(*settings).await?;
},
LaunchpadAction::ResetSettings => {
self.reset_settings().await?;
self.scope.reset_docker().await?;
},
}
Ok(())
}
Expand Down Expand Up @@ -244,6 +248,22 @@ impl LaunchpadWorker {
Ok(())
}

async fn reset_settings(&mut self) -> Result<(), Error> {
debug!("Remove the settings file");

let mut path = self
.state
.config
.settings
.as_ref()
.map(|s| s.data_directory.clone())
.ok_or_else(|| Error::msg("Can't reset the settings: no settings are attached to the config"))?;
path.push("config");
path.push("settings.toml");
tokio::fs::remove_file(path).await?;
Ok(())
}

async fn process_report(&mut self, report: ReportEnvelope<LaunchpadProtocol>) -> Result<(), Error> {
// TODO: Convert to the `LaunchpadDelta` and apply
match report.details {
Expand Down
4 changes: 4 additions & 0 deletions libs/sdm/src/image/task/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ impl<C: ManagedProtocol> RunnableContext<ImageTask<C>> for TaskContext<ImageTask
async fn update(&mut self) -> Result<(), Error> {
self.process_update_impl().await
}

async fn reset(&mut self) -> Result<(), Error> {
self.reset().await
}
}

impl<C: ManagedProtocol> TaskContext<ImageTask<C>> {
Expand Down
8 changes: 8 additions & 0 deletions libs/sdm/src/image/task/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ impl<C: ManagedProtocol> TaskContext<ImageTask<C>> {
}
}

pub async fn reset(&mut self) -> Result<(), Error> {
if self.image_exists().await {
drop(self.do_drop_image().await);
}

self.try_remove_container().await
}

async fn do_initial_state(&mut self) -> Result<(), Error> {
log::trace!("[Update event: Image] `do_initial_state` {}", self.inner.image_name);
self.update_task_status(TaskStatus::Inactive)?;
Expand Down
4 changes: 4 additions & 0 deletions libs/sdm/src/network/task/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ impl<C: ManagedProtocol> RunnableContext<NetworkTask<C>> for TaskContext<Network
async fn update(&mut self) -> Result<(), Error> {
self.process_update_impl().await
}

async fn reset(&mut self) -> Result<(), Error> {
self.reset().await
}
}

#[derive(Debug)]
Expand Down
4 changes: 4 additions & 0 deletions libs/sdm/src/network/task/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ impl<C: ManagedProtocol> TaskContext<NetworkTask<C>> {
}
}

pub async fn reset(&mut self) -> Result<(), Error> {
self.do_cleanup().await
}

async fn do_initial_state(&mut self) -> Result<(), Error> {
log::trace!("[Update event: Network] `do_initial_state`");
self.update_task_status(TaskStatus::Inactive)?;
Expand Down
6 changes: 6 additions & 0 deletions libs/sdm/src/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ pub enum ControlEvent<C: ManagedProtocol> {
task_id: TaskId,
},
InnerEvent(C::Inner),
Reset,
}

impl<C: ManagedProtocol> Clone for ControlEvent<C> {
Expand All @@ -84,6 +85,7 @@ impl<C: ManagedProtocol> Clone for ControlEvent<C> {
task_id: task_id.clone(),
},
Self::InnerEvent(inner) => Self::InnerEvent(inner.clone()),
Self::Reset => Self::Reset,
}
}
}
Expand Down Expand Up @@ -172,4 +174,8 @@ impl<C: ManagedProtocol> SdmScope<C> {
}

pub fn stop(&self) {}

pub async fn reset_docker(&mut self) -> Result<(), Error> {
self.send(ControlEvent::Reset)
}
}
12 changes: 10 additions & 2 deletions libs/sdm/src/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ pub trait RunnableTask: Sized + Send + 'static {
pub trait RunnableContext<T: RunnableTask> {
/// Subscribe to events here
async fn initialize(&mut self);
async fn reset(&mut self) -> Result<(), Error>;
fn reconfigure(&mut self, config: Option<&<T::Protocol as ManagedProtocol>::Config>) -> bool;
fn process_inner_event(&mut self, event: <T::Protocol as ManagedProtocol>::Inner);
fn process_event(&mut self, event: T::Event) -> Result<(), Error>;
Expand Down Expand Up @@ -298,7 +299,7 @@ where
self.context.status.get()
);
if let Some(Ok(req)) = req {
self.process_request(req);
self.process_request(req).await;
} else {
log::info!("Requests stream closed");
break;
Expand Down Expand Up @@ -338,7 +339,7 @@ where
}
}

fn process_request(&mut self, req: ControlEvent<R::Protocol>) {
async fn process_request(&mut self, req: ControlEvent<R::Protocol>) {
match req {
ControlEvent::SetConfig(config) => {
let config = config.as_deref();
Expand All @@ -363,6 +364,9 @@ where
ControlEvent::InnerEvent(inner) => {
self.process_inner_event(inner);
},
ControlEvent::Reset => {
drop(self.reset().await);
},
}
}

Expand Down Expand Up @@ -398,6 +402,10 @@ where
self.context.should_start = is_active;
}

pub async fn reset(&mut self) {
drop(self.context.reset().await);
}

pub fn process_inner_event(&mut self, event: <R::Protocol as ManagedProtocol>::Inner) {
self.context.process_inner_event(event);
}
Expand Down
12 changes: 6 additions & 6 deletions libs/sdm/src/volume/task/docker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use anyhow::Error;
use bollard::{
models::{EventMessage, EventMessageTypeEnum},
system::EventsOptions,
volume::CreateVolumeOptions,
volume::{CreateVolumeOptions, RemoveVolumeOptions},
};
use futures::TryStreamExt;

Expand Down Expand Up @@ -73,11 +73,11 @@ impl<C: ManagedProtocol> TaskContext<VolumeTask<C>> {
Ok(())
}

// pub async fn try_remove_volume(&mut self) -> Result<(), Error> {
// let opts = RemoveVolumeOptions { force: true };
// self.driver.remove_volume(&self.inner.volume_name, Some(opts)).await?;
// Ok(())
// }
pub async fn try_remove_volume(&mut self) -> Result<(), Error> {
let opts: RemoveVolumeOptions = RemoveVolumeOptions { force: true };
self.driver.remove_volume(&self.inner.volume_name, Some(opts)).await?;
Ok(())
}
}

struct EventConv {
Expand Down
4 changes: 4 additions & 0 deletions libs/sdm/src/volume/task/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ impl<C: ManagedProtocol> RunnableContext<VolumeTask<C>> for TaskContext<VolumeTa
async fn update(&mut self) -> Result<(), Error> {
self.process_update_impl().await
}

async fn reset(&mut self) -> Result<(), Error> {
self.reset().await
}
}

#[derive(Debug)]
Expand Down
4 changes: 4 additions & 0 deletions libs/sdm/src/volume/task/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ impl<C: ManagedProtocol> TaskContext<VolumeTask<C>> {
}
}

pub async fn reset(&mut self) -> Result<(), Error> {
self.try_remove_volume().await
}

async fn do_initial_state(&mut self) -> Result<(), Error> {
log::trace!("[Update event: Volume] `do_initial_state`");
self.update_task_status(TaskStatus::Inactive)?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
import { SettingsBox, SettingsContainer } from '../styles';
import t from '../../../locales';

function ResetSettings() {
function ResetSettings({ handleReset }: { handleReset: VoidFunction }) {
const [confirmReset, setConfirmReset] = useState(false);
return (
<SettingsContainer>
Expand Down Expand Up @@ -38,8 +38,8 @@ function ResetSettings() {
<Button variant="outlined" onClick={() => setConfirmReset(false)}>
{t.reset.settings.keepEditing}
</Button>
<Button variant="contained" onClick={() => console.log('Reset')}>
{t.reset.settings.resetAndExit}
<Button variant="contained" onClick={handleReset}>
{t.reset.settings.reset}
</Button>
</HorisontalButtons>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ function SettingsDialog() {
}));
};

const handleResetSettings = () => {
emit('tari://actions', {
Action: { type: 'ResetSettings' },
});
}

const menuItems = [
{
label: 'Tari Mining',
Expand Down Expand Up @@ -146,7 +152,7 @@ function SettingsDialog() {
},
{
label: 'Reset',
component: <ResetSettings />,
component: <ResetSettings handleReset={handleResetSettings} />,
},
];

Expand Down
2 changes: 1 addition & 1 deletion ui/frontend/src/locales/reset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ export default {
'Clean all settings and start from scratch (requires restarting)',
resetButton: 'Reset',
keepEditing: 'Keep editing',
resetAndExit: 'Reset & Exit',
reset: 'Reset',
},
};
5 changes: 5 additions & 0 deletions ui/frontend/src/store/appStateStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,11 @@ const useAppStateStore = create<AppStateStore>((set, get) => ({
Action: { type: 'SaveSettings', payload: settings },
});
},
reset_settings: async () => {
emit('tari://actions', {
Action: { type: 'ResetSettings' },
})
}
}));

export default useAppStateStore;