summaryrefslogtreecommitdiffstats
path: root/plugins/log_request_plugin/src/main.rs
blob: d448124b7bf7604d658b54f5e21f38e73b380aac (plain)
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
mod smartrest;

use c8y_api::http_proxy::{C8YHttpProxy, JwtAuthHttpProxy};
use c8y_smartrest::{smartrest_deserializer::SmartRestLogRequest, topic::C8yTopic};
use tedge_config::{get_tedge_config, ConfigSettingAccessor, MqttPortSetting};

use c8y_smartrest::smartrest_deserializer::SmartRestRequestGeneric;
use futures::SinkExt;

use smartrest::{
    get_log_file_request_done_message, get_log_file_request_executing, read_tedge_logs,
};

const AGENT_LOG_DIR: &str = "/var/log/tedge/agent";

/// creates an mqtt client
pub async fn create_mqtt_client() -> Result<mqtt_channel::Connection, anyhow::Error> {
    let tedge_config = get_tedge_config()?;
    let mqtt_port = tedge_config.query(MqttPortSetting)?.into();
    let mqtt_config = mqtt_channel::Config::default()
        .with_port(mqtt_port)
        .with_subscriptions(mqtt_channel::TopicFilter::new_unchecked(
            C8yTopic::SmartRestResponse.as_str(),
        ));

    let mqtt_client = mqtt_channel::Connection::new(&mqtt_config).await?;
    Ok(mqtt_client)
}

/// creates an http client
pub async fn create_http_client() -> Result<JwtAuthHttpProxy, anyhow::Error> {
    let config = get_tedge_config()?;
    let mut http_proxy = JwtAuthHttpProxy::try_new(&config).await?;
    let () = http_proxy.init().await?;
    Ok(http_proxy)
}

#[tokio::main]
async fn main() -> Result<(), anyhow::Error> {
    // reading payload from command line arguments
    let payload = std::env::args().nth(1).expect("no payload given");

    // creating required clients
    let mut mqtt_client = create_mqtt_client().await?;
    let mut http_client = create_http_client().await?;

    // retrieve smartrest object from payload
    let smartrest_obj = SmartRestLogRequest::from_smartrest(&payload)?;

    // 1. set log file request to executing
    let msg = get_log_file_request_executing().await?;
    let () = mqtt_client.published.send(msg).await?;
    // 2. read logs
    let log_content = read_tedge_logs(&smartrest_obj, AGENT_LOG_DIR)?;

    // 3. upload log file
    let upload_event_url = http_client.upload_log_binary(&log_content).await?;

    // 4. set log file request to done
    let msg = get_log_file_request_done_message(&upload_event_url).await?;
    let () = mqtt_client.published.send(msg).await?;

    mqtt_client.close().await;

    Ok(())
}