1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Copyright (c) 2023, MaidSafe.
// All rights reserved.
//
// This SAFE Network Software is licensed under the BSD-3-Clause license.
// Please see the LICENSE file for more details.
use super::*;
use clap::Subcommand;
use color_eyre::Result;
use sn_testnet_deploy::{
inventory::DeploymentInventoryService, CloudProvider, TestnetDeployBuilder,
};
#[derive(Subcommand, Debug)]
pub enum LogCommands {
/// Removes all the rotated log files from the the node VMs.
Cleanup {
/// The name of the environment
#[arg(short = 'n', long)]
name: String,
/// The cloud provider that was used.
#[clap(long, default_value_t = CloudProvider::DigitalOcean, value_parser = parse_provider, verbatim_doc_comment)]
provider: CloudProvider,
/// Setup a cron job to perform the cleanup periodically.
#[clap(long)]
setup_cron: bool,
},
/// Retrieve the logs for a given environment by copying them from all the VMs.
///
/// This will write the logs to 'logs/<name>', relative to the current directory.
Copy {
/// The name of the environment
#[arg(short = 'n', long)]
name: String,
/// The cloud provider that was used.
#[clap(long, default_value_t = CloudProvider::DigitalOcean, value_parser = parse_provider, verbatim_doc_comment)]
provider: CloudProvider,
/// Should we copy the resource-usage.logs only
#[arg(short = 'r', long)]
resources_only: bool,
},
/// Retrieve the logs for a given environment from S3.
///
/// This will write the logs to 'logs/<name>', relative to the current directory.
Get {
/// The name of the environment
#[arg(short = 'n', long)]
name: String,
},
/// Reassemble retrieved logs from their parts.
///
/// The logs must have already been retrieved using the 'get' command and be present at
/// 'logs/<name>'.
///
/// This will write the logs to 'logs/<name>-reassembled', relative to the current directory.
///
/// The original logs are left intact so you can sync again if need be.
Reassemble {
/// The name of the environment for which logs have already been retrieved
#[arg(short = 'n', long)]
name: String,
},
/// Run a ripgrep query through all the logs from all the VMs and copy the results.
///
/// The results will be written to `logs/<name>/<vm>/rg-timestamp.log`
Rg {
/// The ripgrep arguments that are directly passed to ripgrep. The text to search for should be put inside
/// single quotes. The dir to search for is set automatically, so do not provide one.
///
/// Example command: `cargo run --release -- logs rg --name <name> --args "'ValidSpendRecordPutFromNetwork' -z -a"`
#[arg(short = 'a', long, allow_hyphen_values(true))]
args: String,
/// The name of the environment
#[arg(short = 'n', long)]
name: String,
/// The cloud provider that was used.
#[clap(long, default_value_t = CloudProvider::DigitalOcean, value_parser = parse_provider, verbatim_doc_comment)]
provider: CloudProvider,
},
/// Remove the logs from a given environment from the bucket on S3.
Rm {
/// The name of the environment for which logs have already been retrieved
#[arg(short = 'n', long)]
name: String,
},
/// Rsync the logs from all the VMs for a given environment.
/// Rerunning the same command will sync only the changed log files without copying everything from the beginning.
///
/// This will write the logs to 'logs/<name>', relative to the current directory.
Rsync {
/// Do not sync the client logs.
#[arg(long, default_value = "false")]
disable_client_logs: bool,
/// The name of the environment
#[arg(short = 'n', long)]
name: String,
/// The cloud provider that was used.
#[clap(long, default_value_t = CloudProvider::DigitalOcean, value_parser = parse_provider, verbatim_doc_comment)]
provider: CloudProvider,
/// Optionally only sync the logs for the VMs that contain the following string.
#[arg(long)]
vm_filter: Option<String>,
},
}
pub async fn handle_logs_command(log_cmd: LogCommands) -> Result<()> {
match log_cmd {
LogCommands::Cleanup {
name,
provider,
setup_cron,
} => {
let testnet_deployer = TestnetDeployBuilder::default()
.environment_name(&name)
.provider(provider)
.build()?;
testnet_deployer.init().await?;
let inventory_service = DeploymentInventoryService::from(&testnet_deployer);
inventory_service.setup_environment_inventory(&name)?;
testnet_deployer.cleanup_node_logs(setup_cron)?;
Ok(())
}
LogCommands::Copy {
name,
provider,
resources_only,
} => {
let testnet_deployer = TestnetDeployBuilder::default()
.environment_name(&name)
.provider(provider)
.build()?;
testnet_deployer.init().await?;
let inventory_service = DeploymentInventoryService::from(&testnet_deployer);
inventory_service.setup_environment_inventory(&name)?;
testnet_deployer.copy_logs(&name, resources_only)?;
Ok(())
}
LogCommands::Get { name } => {
sn_testnet_deploy::logs::get_logs(&name).await?;
Ok(())
}
LogCommands::Reassemble { name } => {
sn_testnet_deploy::logs::reassemble_logs(&name)?;
Ok(())
}
LogCommands::Rg {
args,
name,
provider,
} => {
let testnet_deployer = TestnetDeployBuilder::default()
.environment_name(&name)
.provider(provider)
.build()?;
testnet_deployer.init().await?;
let inventory_service = DeploymentInventoryService::from(&testnet_deployer);
inventory_service.setup_environment_inventory(&name)?;
testnet_deployer.ripgrep_logs(&name, &args)?;
Ok(())
}
LogCommands::Rm { name } => {
sn_testnet_deploy::logs::rm_logs(&name).await?;
Ok(())
}
LogCommands::Rsync {
disable_client_logs,
name,
provider,
vm_filter,
} => {
let testnet_deployer = TestnetDeployBuilder::default()
.environment_name(&name)
.provider(provider)
.build()?;
testnet_deployer.init().await?;
let inventory_service = DeploymentInventoryService::from(&testnet_deployer);
inventory_service.setup_environment_inventory(&name)?;
testnet_deployer.rsync_logs(&name, vm_filter, disable_client_logs)?;
Ok(())
}
}
}