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
//! Configuration for the runtime's various subcomponents.
use std::env;
use std::fs::read_to_string;
use std::path::PathBuf;

use async_lib::config::Config as AsyncLibConfig;
use location_manager::config::Config as LocationManagerConfig;
use logging::config::Config as LoggingConfig;
use nando_lib::config::Config as NandoLibConfig;
use ownership_tracker::config::Config as OwnershipTrackerConfig;
use scheduling::config::Config as SchedulingConfig;
use serde::{Deserialize, Serialize};
use toml;

#[derive(Serialize, Deserialize, Clone)]
pub struct ClientConfig {
    /// The numerical ID of the current host (index into the [`ClientConfig::legacy_service_hosts`] field).
    pub client_id: u16,
    pub legacy_service_hosts: Vec<String>,
    pub legacy_service_port: u16,
    pub execution_subsystem_port: u16,
}

/// Contains all subcomponent configuration objects.
#[derive(Serialize, Deserialize, Clone)]
pub struct Config {
    pub(crate) hostname: String,
    pub async_lib_config: AsyncLibConfig,
    pub location_manager_config: LocationManagerConfig,
    pub ownership_tracker_config: OwnershipTrackerConfig,
    pub client_config: ClientConfig,
    pub nando_lib_config: NandoLibConfig,
    pub logging_config: LoggingConfig,
    pub scheduling_config: SchedulingConfig,
}

impl Config {
    pub fn init_from_file(config_file: PathBuf) -> Self {
        let config_str = read_to_string(&config_file)
            .expect(&format!("failed to open config file {:#?}", config_file));

        let mut config: Self = toml::from_str(&config_str).unwrap();
        match env::var("CLIENT_ID") {
            // FIXME @hack
            Ok(v) => {
                let client_id: u16 = v
                    .parse()
                    .expect("failed to parse client id from command line");

                config.client_config.client_id = client_id;
                config.location_manager_config.client_id = client_id;
            }
            Err(_e) => (),
        };

        match env::var("MAGPIE_WORKER_THREADS") {
            Ok(v) => {
                let num_worker_threads: u16 =
                    v.parse().expect("failed to parse worker threads env var");

                config.nando_lib_config.executor_config.num_worker_threads = num_worker_threads;
                config.logging_config.num_executor_threads = num_worker_threads;
            }
            Err(_e) => (),
        };

        match config.hostname.is_empty() {
            false => {}
            true => config.hostname = env::var("HOSTNAME").unwrap(),
        }

        // Sanity checks
        let logging_config_threads = config.logging_config.num_executor_threads;
        let executor_config_threads = config.nando_lib_config.executor_config.num_worker_threads;
        if logging_config_threads != executor_config_threads {
            panic!(
                "Mismatch in executor thread configs: executor config specifies {} threads, but logging config specifies {}",
                executor_config_threads,
                logging_config_threads,
            );
        }

        config
    }
}