failover_vr 0.4.1

VRRP implementation library (and binary installation) in Rust.
Documentation
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
use crate::{error::OptError, general::random_vr_name, OptResult};
use clap::{Parser, Subcommand};
use ipnet::Ipv4Net;
use log::LevelFilter;
use log4rs::{
    append::{console::ConsoleAppender, file::FileAppender},
    config::{Appender, Root},
    Config,
};
use serde::{Deserialize, Serialize};
use std::{
    env,
    ffi::OsStr,
    fs::{create_dir_all, File},
    io::{BufReader, Write},
    path::Path,
};

const DEFAULT_JSON_CONFIG: &[u8; 201] = b"
{
    \"name\": \"VR_1\",
    \"vrid\": 51,
    \"interface_name\": \"wlo1\",
    \"ip_addresses\": [
        \"192.168.100.100/24\"
    ],
    \"priority\": 101,
    \"advert_interval\": 1,
    \"preempt_mode\": true
}
";

fn default_priority() -> u8 {
    100
}
fn default_advert_int() -> u8 {
    1
}
fn default_preempt_mode() -> bool {
    true
}
fn default_action() -> Action {
    Action::Run
}

// for reading JSON config file
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct FileConfig {
    pub vrid: u8,
    pub ip_addresses: Vec<String>,
    pub interface_name: String,

    #[serde(default = "random_vr_name")]
    pub name: String,
    #[serde(default = "default_priority")]
    pub priority: u8,
    #[serde(default = "default_advert_int")]
    pub advert_interval: u8,
    #[serde(default = "default_preempt_mode")]
    pub preempt_mode: bool,
    #[serde(default = "default_action")]
    pub action: Action,
}

#[derive(Debug, Clone)]
pub struct CliConfig {
    pub name: Option<String>,
    pub vrid: u8,
    pub ip_addresses: Vec<String>,
    pub interface_name: String,

    pub priority: u8,
    pub advert_interval: u8,
    pub preempt_mode: bool,
    pub action: Action,
}

#[derive(Debug, Clone)]
pub struct BaseConfig {
    pub name: Option<String>,
    pub vrid: u8,
    pub ip_addresses: Vec<Ipv4Net>,
    pub interface_name: String,
    pub priority: u8,
    pub advert_interval: u8,
    pub preempt_mode: bool,
    pub action: Action,
}

// pub struct

#[derive(Debug, Clone)]
pub enum VrrpConfig {
    File(FileConfig),
    Cli(CliConfig),
}

impl VrrpConfig {
    // for name, if not specified, we will generate a random name (VR-{random-string})
    pub fn name(&self) -> String {
        let name = match self {
            VrrpConfig::File(config) => Some(config.name.clone()),
            VrrpConfig::Cli(config) => config.name.clone(),
            // VrrpConfig::Base(config) => config.name.unwrap()
        };
        match name {
            Some(n) => n,
            None => random_vr_name(),
        }
    }

    pub fn vrid(&self) -> u8 {
        match self {
            VrrpConfig::File(config) => config.vrid,
            VrrpConfig::Cli(config) => config.vrid,
        }
    }

    pub fn ip_addresses(&self) -> Vec<String> {
        match self {
            VrrpConfig::File(config) => config.ip_addresses.clone(),
            VrrpConfig::Cli(config) => config.ip_addresses.clone(),
        }
    }

    // if interface name has not been specified, we will create one with format: ( fover-{random-string} )
    pub fn interface_name(&self) -> String {
        match self {
            VrrpConfig::File(config) => config.interface_name.clone(),
            VrrpConfig::Cli(config) => config.interface_name.clone(),
        }
    }

    pub fn priority(&self) -> u8 {
        match self {
            VrrpConfig::File(config) => config.priority,
            VrrpConfig::Cli(config) => config.priority,
        }
    }

    pub fn advert_interval(&self) -> u8 {
        match self {
            VrrpConfig::File(config) => config.advert_interval,
            VrrpConfig::Cli(config) => config.advert_interval,
        }
    }

    pub fn preempt_mode(&self) -> bool {
        match self {
            VrrpConfig::File(config) => config.preempt_mode,
            VrrpConfig::Cli(config) => config.preempt_mode,
        }
    }

    pub fn action(&self) -> Action {
        match self {
            VrrpConfig::File(config) => config.action.clone(),
            VrrpConfig::Cli(config) => config.action.clone(),
        }
    }
}

#[derive(Parser, Debug)]
#[command(name = "Version")]
#[command(about = "Runs the VRRP protocol", long_about = None)]
pub struct CliArgs {
    #[command(
        subcommand,
        help = "file-mode for if you want the configs loaded from a file. cli-mode if you want your configs taken directly from the CLI."
    )]
    cfg: Cfg,

    #[arg(long, default_value = "run")]
    action: String,
}

#[derive(Subcommand, Debug)]
enum Cfg {
    FileMode {
        #[arg(long, help = "path to the we will get our configs from")]
        filename: Option<String>,

        #[arg(
            long,
            default_value = None,
            help = "Path log file you want to use"
        )]
        log_file_path: Option<String>,
    },
    CliMode {
        #[arg(long, help = "The name of the Virtual Router Instance. e.g `VR_1`")]
        name: Option<String>,

        #[arg(long, help = "Virtual Router ID of the Virtual router instance. ")]
        vrid: u8,

        #[arg(long, num_args=1.., help="The IP Address(es) of that will the Virtual router will be assigned. Can be more than one. ")]
        ip_address: Vec<String>,

        #[arg(
            long,
            help = "name of the network interface where the Virtual Router instance will be attached. "
        )]
        interface_name: String,

        #[arg(
            long,
            default_value = "100",
            help = "The priority of this instance of the Virtual Router, maximum of 255. The higher priority is chosen to be MASTER."
        )]
        priority: u8,

        #[arg(
            long,
            default_value = "1",
            help = "Interval(in seconds) between which the priodic advert updates are sent (when MASTER). Also used to calculate MasterDown interval when in BACKUP state."
        )]
        advert_interval: u8,

        #[arg(
            long,
            action,
            help = "(highly advised to be true). When true, the higher priority will always preempt the lower priority."
        )]
        preempt_mode: bool,

        #[arg(
            long,
            default_value = None,
            help = "Path log file you want to use"
        )]
        log_file_path: Option<String>,
    },
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum Action {
    Run,
    Teardown,
}

pub fn parse_cli_opts(args: CliArgs) -> OptResult<Vec<VrrpConfig>> {
    match args.cfg {
        Cfg::FileMode {
            filename,
            log_file_path,
        } => {
            // configure logging as required
            configure_logging(log_file_path);

            // generate file path if none is given
            let fpath = match filename {
                None => {
                    // get default file path and create new directory if it does not exist
                    match env::var("SNAP_COMMON") {
                        Ok(path) => path + "/vrrp-config.json",
                        Err(_) => "/etc/failover/vrrp-config.json".to_string(),
                    }
                }
                Some(f) => f,
            };

            log::info!("using config file {:#?}", fpath);
            // create the config file (if it does not exist)
            if !Path::new(&fpath).exists() {
                let mut file = match File::create(&fpath) {
                    Ok(f) => f,
                    Err(_) => {
                        let dir_path = match std::path::Path::new(&fpath).parent() {
                            Some(dir) => dir,
                            None => {
                                return Err(OptError(String::from("unable to find config path")))
                            }
                        };
                        let _ = create_dir_all(dir_path);
                        File::create(&fpath).unwrap()
                    }
                };
                let _ = file.write_all(DEFAULT_JSON_CONFIG);
            }

            let mut configs: Vec<VrrpConfig> = vec![];

            match read_json_config(&fpath) {
                Ok(vec_config) => {
                    for mut c in vec_config {
                        c.action = match args.action.to_lowercase().trim() {
                            "teardown" => Action::Teardown,
                            "run" => Action::Run,
                            _ => {
                                log::warn!("{} is not a valid action, therefore resulted to default 'run' action", args.action);
                                Action::Run
                            }
                        };
                        configs.push(VrrpConfig::File(c));
                    }
                }
                Err(_) => return Result::Err(OptError(format!("Problem parsing file {}", &fpath))),
            }
            Ok(configs)
        }

        Cfg::CliMode {
            mut name,
            vrid,
            ip_address,
            interface_name,
            priority,
            advert_interval,
            preempt_mode,
            log_file_path,
        } => {
            configure_logging(log_file_path);
            if name.is_none() {
                name = Some(random_vr_name());
            };

            let config = CliConfig {
                name,
                vrid,
                ip_addresses: ip_address,
                interface_name,
                priority,
                advert_interval,
                preempt_mode,
                action: match args.action.to_lowercase().trim() {
                    "teardown" => Action::Teardown,
                    "run" => Action::Run,
                    _ => {
                        //log::warn!("{} is not a valid action, therefore resulted to default 'run' action", args.action);
                        Action::Run
                    }
                },
            };
            Ok(vec![VrrpConfig::Cli(config)])
        }
    }
}

fn configure_logging(log_file_path: Option<String>) {
    let log_console_stderr = ConsoleAppender::builder().build();
    let mut log_builder = Config::builder()
        .appender(Appender::builder().build("stderr", Box::new(log_console_stderr)));
    let mut root_builder = Root::builder();

    // set file path logging
    if let Some(file_path) = log_file_path {
        // Logging to log file.
        let log_file = FileAppender::builder()
            // Pattern: https://docs.rs/log4rs/*/log4rs/encode/pattern/index.html
            //.encoder(Box::new(PatternEncoder::new("{l} - {m}\n")))
            .build(file_path)
            .unwrap();
        log_builder =
            log_builder.appender(Appender::builder().build("logfile", Box::new(log_file)));
        root_builder = root_builder.appender("logfile");
    }
    root_builder = root_builder.appender("stderr"); // .appender("stdout");
                                                    //.build(LevelFilter::Trace);

    let log_config = log_builder
        .build(root_builder.build(LevelFilter::Trace))
        .unwrap();
    let _handler = log4rs::init_config(log_config);
}

fn read_json_config<P: AsRef<Path>>(path: P) -> OptResult<Vec<FileConfig>> {
    let path_str = path.as_ref().as_os_str();

    //log::info!("Reading from config file {:?}", path_str);
    let file = match File::open(path_str) {
        Ok(f) => f,
        Err(_) => {
            //log::error!("Unable to open file {:?}", path.as_ref().as_os_str());
            return Err(OptError(format!(
                "unable to open file `{:?}`",
                path.as_ref().as_os_str()
            )));
        }
    };

    let reader = BufReader::new(file);
    let mut result: Vec<FileConfig> = Vec::new();

    let list_file_configs: Vec<FileConfig> = match serde_json::from_reader(reader) {
        Ok(config) => config,
        Err(_) => match single_file_config(path_str) {
            Ok(conf) => conf,
            Err(err) => return Err(err),
        },
    };

    for file_config in list_file_configs {
        // check if the name of Virtual Router being entered is unique
        if let Some(_con) = result
            .iter()
            .find(|r: &&FileConfig| r.name == file_config.name)
        {
            //log::warn!("Configs for Virtual Router with name {:?} already exist. Will be ignored", con.name);
            continue;
        };

        // check if VRID of the Virtual Router being entered is unique
        if let Some(_con) = result
            .iter()
            .find(|r: &&FileConfig| r.vrid == file_config.vrid)
        {
            //log::warn!("Configs for Virtual Router with VRID {:?} already exist. Will be ignored", con.vrid);
            continue;
        };

        result.push(file_config);
    }

    Ok(result)
}

fn single_file_config(path: &OsStr) -> OptResult<Vec<FileConfig>> {
    // this single file config method is called only after
    // the normal config fails, which it does after reading the file.
    // thus unwrap()'ing here is safe.
    let file = File::open(path).unwrap();

    let reader = BufReader::new(file);
    let _: FileConfig = match serde_json::from_reader(reader) {
        Ok(config) => return Ok(vec![config]),
        Err(_) => {
            //log::error!("Wrong configurations for file {:?}", path);
            return Err(OptError(format!(
                "Wrong config formatting in file {:?}",
                path
            )));
        }
    };
}