Skip to content

Commit

Permalink
feat(watcher): update config impl with alert channels
Browse files Browse the repository at this point in the history
  • Loading branch information
therealdannzor committed Aug 7, 2024
1 parent 5673049 commit 5efa294
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 47 deletions.
2 changes: 2 additions & 0 deletions applications/tari_watcher/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ license.workspace = true
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
#tari_common = { workspace = true }

clap = { workspace = true, features = ["derive"] }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
Expand Down
36 changes: 0 additions & 36 deletions applications/tari_watcher/data/watcher/config.toml

This file was deleted.

18 changes: 15 additions & 3 deletions applications/tari_watcher/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use std::path::PathBuf;

use clap::Parser;

use crate::config::Config;

#[derive(Clone, Debug, Parser)]
pub struct Cli {
#[clap(flatten)]
Expand Down Expand Up @@ -34,15 +36,25 @@ impl Cli {
pub struct CommonCli {
#[clap(short = 'b', long, parse(from_os_str))]
pub base_dir: Option<PathBuf>,
#[clap(short, long, parse(from_os_str), default_value = "./data/watcher/config.toml")]
#[clap(short = 'c', long, parse(from_os_str), default_value = "./data/watcher/config.toml")]
pub config_path: PathBuf,
}

#[derive(Clone, Debug, clap::Subcommand)]
pub enum Commands {
Init,
Init(InitArgs),
Start,
}

#[derive(Clone, Debug, clap::Args)]
pub struct Overrides {}
pub struct InitArgs {
#[clap(long)]
/// Disable initial and auto registration of the validator node
pub no_auto_register: bool,
}

impl InitArgs {
pub fn apply(&self, config: &mut Config) {
config.auto_register = !self.no_auto_register;
}
}
34 changes: 30 additions & 4 deletions applications/tari_watcher/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
// Copyright 2024 The Tari Project
// SPDX-License-Identifier: BSD-3-Clause

use crate::Cli;
use std::collections::HashMap;
use std::fmt::{self, Display};
use std::path::PathBuf;
use std::{
collections::HashMap,
fmt::{self, Display},
path::PathBuf,
};

use tokio::io::{self, AsyncWriteExt};

use crate::Cli;

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct Config {
/// Allow watcher to submit a new validator node registration transaction initially and before
Expand All @@ -31,6 +35,9 @@ pub struct Config {

/// The process specific configuration for the executables
pub executable_config: Vec<ExecutableConfig>,

/// The channel configuration for alerting and monitoring
pub channel_config: Vec<ChannelConfig>,
}

impl Config {
Expand All @@ -53,6 +60,13 @@ impl Display for InstanceType {
}
}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct ChannelConfig {
pub name: String,
pub enabled: bool,
pub credentials: String,
}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct ExecutableConfig {
pub instance_type: InstanceType,
Expand Down Expand Up @@ -150,5 +164,17 @@ pub fn get_base_config(cli: &Cli) -> anyhow::Result<Config> {
vn_registration_file: base_dir.join("vn_registration.toml"),
instance_config: instances.to_vec(),
executable_config: executables,
channel_config: vec![
ChannelConfig {
name: "mattermost".to_string(),
enabled: true,
credentials: "".to_string(),
},
ChannelConfig {
name: "telegram".to_string(),
enabled: true,
credentials: "".to_string(),
},
],
})
}
15 changes: 11 additions & 4 deletions applications/tari_watcher/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// Copyright 2024 The Tari Project
// SPDX-License-Identifier: BSD-3-Clause

use crate::cli::{Cli, Commands};
use anyhow::{anyhow, Context};
use tokio::fs;

use crate::config::get_base_config;
use crate::{
cli::{Cli, Commands},
config::get_base_config,
};

mod cli;
mod config;
Expand All @@ -16,12 +18,14 @@ async fn main() -> anyhow::Result<()> {
let config_path = cli.get_config_path();

match cli.command {
Commands::Init => {
Commands::Init(ref args) => {
// set by default in CommonCli
let parent = config_path.parent().unwrap();
fs::create_dir_all(parent).await?;

let config = get_base_config(&cli)?;
let mut config = get_base_config(&cli)?;
// optionally disables auto register
args.apply(&mut config);

let file = fs::File::create(&config_path)
.await
Expand All @@ -31,6 +35,9 @@ async fn main() -> anyhow::Result<()> {
let config_path = config_path
.canonicalize()
.context("Failed to canonicalize config path")?;

// TODO: use standardised logging
// if let Err(e) = initialize_logging(..)
log::info!("Config file created at {}", config_path.display());
},
Commands::Start => {
Expand Down

0 comments on commit 5efa294

Please sign in to comment.