summaryrefslogtreecommitdiffstats
path: root/crates/core/tedge
diff options
context:
space:
mode:
authorPradeepKiruvale <pradeepkumar.kj@softwareag.com>2022-03-24 13:35:38 +0530
committerGitHub <noreply@github.com>2022-03-24 13:35:38 +0530
commit59142ef60484ddc7b103c841d34b308b9444f316 (patch)
treed7b8f89179d2244a1e116081b11a28e7c3a141fa /crates/core/tedge
parentd9ca6aedfad3de7931964200f26e40fdd577b48a (diff)
[#761] tedge init (#993)
* tedge init #Closes 761 * add file/directory creation errors * add the clear session to az and collectd mappers * refactor tedgecomponent * remove redundant code * subscribe to az/collectd topics in init_session * remove duplicate init_sessions
Diffstat (limited to 'crates/core/tedge')
-rw-r--r--crates/core/tedge/src/cli/mod.rs12
-rw-r--r--crates/core/tedge/src/main.rs38
2 files changed, 36 insertions, 14 deletions
diff --git a/crates/core/tedge/src/cli/mod.rs b/crates/core/tedge/src/cli/mod.rs
index 37870b30..6c0e3811 100644
--- a/crates/core/tedge/src/cli/mod.rs
+++ b/crates/core/tedge/src/cli/mod.rs
@@ -13,14 +13,20 @@ mod mqtt;
#[clap(
name = clap::crate_name!(),
version = clap::crate_version!(),
- about = clap::crate_description!()
+ about = clap::crate_description!(),
+ arg_required_else_help(true)
)]
+
pub struct Opt {
- #[clap(subcommand)]
- pub tedge: TEdgeOpt,
+ /// Initialize the tedge
+ #[clap(long)]
+ pub init: bool,
#[clap(long = "config-dir", default_value = DEFAULT_TEDGE_CONFIG_PATH)]
pub config_dir: PathBuf,
+
+ #[clap(subcommand)]
+ pub tedge: Option<TEdgeOpt>,
}
#[derive(clap::Subcommand, Debug)]
diff --git a/crates/core/tedge/src/main.rs b/crates/core/tedge/src/main.rs
index 672947fb..5b384e35 100644
--- a/crates/core/tedge/src/main.rs
+++ b/crates/core/tedge/src/main.rs
@@ -4,24 +4,27 @@
use anyhow::Context;
use clap::Parser;
use tedge_users::UserManager;
-
mod cli;
mod command;
mod error;
mod system_services;
+use tedge_utils::file::create_directory_with_user_group;
type ConfigError = crate::error::TEdgeError;
use command::{BuildCommand, BuildContext};
fn main() -> anyhow::Result<()> {
- let user_manager = UserManager::new();
-
- let _user_guard = user_manager.become_user(tedge_users::TEDGE_USER)?;
-
let opt = cli::Opt::parse();
+ if opt.init {
+ initialize_tedge()?;
+ return Ok(());
+ }
+
+ let user_manager = UserManager::new();
let tedge_config_location = tedge_config::TEdgeConfigLocation::from_custom_root(opt.config_dir);
+ let _user_guard = user_manager.become_user(tedge_users::TEDGE_USER)?;
let config_repository = tedge_config::TEdgeConfigRepository::new(tedge_config_location.clone());
let build_context = BuildContext {
@@ -30,11 +33,24 @@ fn main() -> anyhow::Result<()> {
user_manager,
};
- let cmd = opt
- .tedge
- .build_command(build_context)
- .with_context(|| "missing configuration parameter")?;
+ if let Some(tedge_opt) = opt.tedge {
+ let cmd = tedge_opt
+ .build_command(build_context)
+ .with_context(|| "missing configuration parameter")?;
+
+ cmd.execute()
+ .with_context(|| format!("failed to {}", cmd.description()))
+ } else {
+ Ok(())
+ }
+}
- cmd.execute()
- .with_context(|| format!("failed to {}", cmd.description()))
+fn initialize_tedge() -> anyhow::Result<()> {
+ create_directory_with_user_group("/etc/tedge", "tedge", "tedge", 0o775)?;
+ create_directory_with_user_group("/var/log/tedge", "tedge", "tedge", 0o775)?;
+ create_directory_with_user_group("/etc/tedge/mosquitto-conf", "tedge", "tedge", 0o775)?;
+ create_directory_with_user_group("/etc/tedge/operations", "tedge", "tedge", 0o775)?;
+ create_directory_with_user_group("/etc/tedge/plugins", "tedge", "tedge", 0o775)?;
+ create_directory_with_user_group("/etc/tedge/device-certs", "mosquitto", "mosquitto", 0o775)?;
+ Ok(())
}