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(debug): export zipped logs #372

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 63 additions & 9 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions cli/src/dashboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ impl Do<Reaction> for Dashboard {
state.state.apply(delta);
}
},
_ => {},
}
// Reporting about the state has changed - this gets triggered for every event, so isn't very efficient.
if let Some(state) = self.state.as_mut() {
Expand Down
2 changes: 2 additions & 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>),
ExportLogs,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
Expand Down Expand Up @@ -83,6 +84,7 @@ impl Default for LaunchpadState {
pub enum Reaction {
State(LaunchpadState),
Delta(LaunchpadDelta),
LogsZipped,
}

impl LaunchpadState {
Expand Down
2 changes: 2 additions & 0 deletions libs/sdm-launchpad/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,5 @@ tokio = "1.29.1"
toml = "0.8.8"
tonic = { version = "0.8.3", features = ["tls"] }
tor-hash-passwd = "1.0.1"
walkdir = "2.3.2"
zip = "0.5.13"
1 change: 1 addition & 0 deletions libs/sdm-launchpad/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ impl SdmWorker {
Reaction::Delta(delta) => {
state.apply(delta);
},
_ => {},
}
}
}
Expand Down
34 changes: 32 additions & 2 deletions libs/sdm-launchpad/src/bus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//

use std::path::PathBuf;

use anyhow::Error;
use log::*;
use std::env;
use std::path::PathBuf;
use tari_launchpad_protocol::{
container::{TaskDelta, TaskId, TaskProgress, TaskState, TaskStatus},
launchpad::{Action, LaunchpadAction, LaunchpadDelta, LaunchpadState, Reaction},
Expand All @@ -40,6 +40,7 @@ use crate::{
config::{LaunchpadProtocol, LaunchpadSettings},
images, networks, volumes,
},
utils::zip_and_export,
};

pub type BusTx = mpsc::UnboundedSender<Action>;
Expand Down Expand Up @@ -193,6 +194,10 @@ impl LaunchpadWorker {
LaunchpadAction::SaveSettings(settings) => {
self.save_settings(*settings).await?;
},
LaunchpadAction::ExportLogs => {
self.export_logs().await?;
self.send(Reaction::LogsZipped);
},
}
Ok(())
}
Expand Down Expand Up @@ -244,6 +249,31 @@ impl LaunchpadWorker {
Ok(())
}

async fn export_logs(&mut self) -> Result<(), Error> {
let network = self
.state
.config
.settings
.as_ref()
.map(|s| s.saved_settings.clone().tari_network.lower_case());
let current_dir = env::current_dir().expect("Failed to get the current directory");
let logs_path = format!(
"{}/{}/log",
current_dir.display(),
network.expect("Network not defined")
);
let logs_path2 = format!("{}/log", current_dir.display());

zip_and_export(logs_path.to_string(), "launchpad_logs.zip".to_string(), None)?;
zip_and_export(
logs_path2.to_string(),
"launchpad_core_logs.zip".to_string(),
Some("core".to_string()),
)?;

Ok(())
}

async fn process_report(&mut self, report: ReportEnvelope<LaunchpadProtocol>) -> Result<(), Error> {
// TODO: Convert to the `LaunchpadDelta` and apply
match report.details {
Expand Down
1 change: 1 addition & 0 deletions libs/sdm-launchpad/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ pub mod resources;
#[cfg(feature = "tauri")]
pub mod tauri;
pub use bus::LaunchpadBus;
pub mod utils;
59 changes: 59 additions & 0 deletions libs/sdm-launchpad/src/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright 2022. The Tari Project
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
// following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
// disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
// following disclaimer in the documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
// products derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//

use anyhow::Error;
use std::fs::File;
use std::io::Read;
use std::io::Write;
use tauri::api::path::download_dir;
use walkdir::WalkDir;
use zip::{write::FileOptions, ZipWriter};

pub fn zip_and_export(in_path: String, zip_name: String, root_dir_name: Option<String>) -> Result<(), Error> {
let zip_path = download_dir().unwrap().join(zip_name);
let mut zip_writer = ZipWriter::new(File::create(zip_path)?);

for entry in WalkDir::new(in_path.clone()) {
let entry = entry?;
let path = entry.path();
let options = FileOptions::default();

if path.is_file() {
let file_path = match root_dir_name {
Some(ref dir) => format!("{}/{}", dir, path.file_name().unwrap().to_string_lossy().into_owned()),
None => path.strip_prefix(in_path.clone())?.to_string_lossy().into_owned(),
};
zip_writer.start_file(file_path, options)?;

let mut file = File::open(path)?;
let mut buffer: Vec<u8> = Vec::new();
file.read_to_end(&mut buffer)?;
zip_writer.write_all(&buffer)?;
} else {
let dir_path = path.strip_prefix(in_path.clone())?.to_str().unwrap();
zip_writer.add_directory(dir_path, options)?;
}
}

Ok(())
}
1 change: 1 addition & 0 deletions libs/sdm-launchpad/tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ impl TestStateInner {
state.apply(delta);
}
},
_ => {},
}
Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"scripts": {
"start": "cd ui/frontend && npm run build && cargo run --bin tari_bus_tauri",
"start": "cd ui/frontend && npm run build && cargo run --bin tari_launchpad",
"dev": "cd ui/frontend && npm run dev",
"tauri": "tauri"
},
Expand Down
5 changes: 5 additions & 0 deletions ui/frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ function App() {
openSchedule,
startBaseNode,
startMining,
setIsZippingLogs
} = useAppStateStore(
useShallow((state) => ({
appState: state.appState,
Expand All @@ -67,6 +68,7 @@ function App() {
openSchedule: state.openSchedule,
startBaseNode: state.startBaseNode,
startMining: state.startMining,
setIsZippingLogs: state.setIsZippingLogs,
}))
);
const { startupConfig } = useConfigStore();
Expand Down Expand Up @@ -184,6 +186,9 @@ function App() {
}
}
}
if (payload === "LogsZipped") {
setIsZippingLogs(false);
}
}))();

return () => {
Expand Down
Loading
Loading