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
// Copyright (c) 2019 DDN. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

use crate::{
    agent_error::{ImlAgentError, Result},
    systemd,
};
use serde;
use serde_json;
use std::fs;

static SERVICE_NAME: &str = "lipe_web";

static STRATAGEM_DATA_FILE: &str = "/var/www/lipe/static/lipe_web.json";

#[derive(serde::Serialize, serde::Deserialize)]
pub struct Devices {
    pub path: String,
    pub host_id: String,
    pub groups: Vec<String>,
    pub device_id: String,
}

#[derive(serde::Serialize, serde::Deserialize)]
pub struct Groups {
    pub rules: Vec<Rules>,
    pub name: String,
}

#[derive(serde::Serialize, serde::Deserialize)]
pub struct Rules {
    pub action: String,
    pub expression: String,
    pub argument: String,
}

#[derive(serde::Serialize, serde::Deserialize)]
pub struct SshHosts {
    pub host_id: String,
    pub hostname: String,
    pub ssh_identity_file: String,
}

#[derive(serde::Serialize, serde::Deserialize)]
pub struct StratagemData {
    pub ssh_hosts: Vec<SshHosts>,
    pub groups: Vec<Groups>,
    pub devices: Vec<Devices>,
    pub dump_flist: bool,
    pub dry_run: bool,
    pub only_scan_active: bool,
}

/// Tries to read the the lipe_web.json file and
/// returns a `Result` of the serialized `StratagemData`.
fn read_stratagem_data() -> Result<StratagemData> {
    let contents = fs::read(STRATAGEM_DATA_FILE)?;

    serde_json::from_slice(&contents).map_err(ImlAgentError::Serde)
}

pub fn start_stratagem() -> Result<bool> {
    systemd::systemctl_start(SERVICE_NAME)
}

pub fn stop_stratagem() -> Result<bool> {
    systemd::systemctl_stop(SERVICE_NAME)
}

pub fn status_stratagem() -> Result<bool> {
    systemd::systemctl_status(SERVICE_NAME).map(systemd::did_succeed)
}

pub fn stratagem_groups() -> Result<Vec<Groups>> {
    let data = read_stratagem_data()?;

    Ok(data.groups)
}