2021-12-17 21:23:47 +00:00
|
|
|
mod change_password;
|
2021-08-06 17:58:40 +00:00
|
|
|
mod constants;
|
|
|
|
mod error;
|
|
|
|
mod generate_manifest;
|
2022-07-05 13:56:52 +00:00
|
|
|
mod publish_address;
|
2021-12-22 15:02:50 +00:00
|
|
|
mod set_permissions;
|
2021-08-06 17:58:40 +00:00
|
|
|
mod setup_networking;
|
|
|
|
mod setup_peach;
|
|
|
|
mod setup_peach_deb;
|
2022-07-04 12:35:17 +00:00
|
|
|
mod status;
|
2021-08-06 17:58:40 +00:00
|
|
|
mod update;
|
|
|
|
mod utils;
|
2022-07-12 09:51:49 +00:00
|
|
|
mod wait_for_sbot;
|
2021-08-06 17:58:40 +00:00
|
|
|
|
|
|
|
use clap::arg_enum;
|
|
|
|
use log::error;
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use structopt::StructOpt;
|
|
|
|
|
|
|
|
#[derive(StructOpt, Debug)]
|
|
|
|
#[structopt(
|
|
|
|
name = "peach-config",
|
|
|
|
about = "a CLI tool for updating, installing and configuring PeachCloud"
|
|
|
|
)]
|
|
|
|
struct Opt {
|
2021-10-25 14:05:25 +00:00
|
|
|
//#[structopt(short, long)]
|
|
|
|
//verbose: bool,
|
2021-08-06 17:58:40 +00:00
|
|
|
|
|
|
|
// SUBCOMMANDS
|
|
|
|
#[structopt(subcommand)]
|
|
|
|
commands: Option<PeachConfig>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(StructOpt, Debug)]
|
|
|
|
#[structopt(name = "peach-config", about = "about")]
|
|
|
|
enum PeachConfig {
|
|
|
|
/// Prints json manifest of peach configurations
|
|
|
|
#[structopt(name = "manifest")]
|
|
|
|
Manifest,
|
|
|
|
|
|
|
|
/// Idempotent setup of PeachCloud
|
|
|
|
#[structopt(name = "setup")]
|
|
|
|
Setup(SetupOpts),
|
|
|
|
|
|
|
|
/// Updates all PeachCloud microservices
|
|
|
|
#[structopt(name = "update")]
|
|
|
|
Update(UpdateOpts),
|
2021-12-17 21:23:47 +00:00
|
|
|
|
|
|
|
/// Changes the password for the peach-web interface
|
2022-07-07 08:41:36 +00:00
|
|
|
#[structopt(name = "change-password")]
|
2021-12-17 21:23:47 +00:00
|
|
|
ChangePassword(ChangePasswordOpts),
|
2021-12-18 15:00:40 +00:00
|
|
|
|
|
|
|
/// Updates file permissions on PeachCloud device
|
|
|
|
#[structopt(name = "permissions")]
|
|
|
|
SetPermissions,
|
2022-07-04 11:23:55 +00:00
|
|
|
|
|
|
|
/// Returns sbot id if sbot is running
|
|
|
|
#[structopt(name = "whoami")]
|
|
|
|
WhoAmI,
|
2022-07-05 13:56:52 +00:00
|
|
|
|
2022-07-06 10:39:55 +00:00
|
|
|
/// Publish domain and port.
|
|
|
|
/// It takes an address argument of the form host:port
|
2022-07-05 13:56:52 +00:00
|
|
|
#[structopt(name = "publish-address")]
|
|
|
|
PublishAddress(PublishAddressOpts),
|
2022-07-12 09:51:49 +00:00
|
|
|
|
|
|
|
/// Wait for a successful connection to sbot
|
|
|
|
#[structopt(name = "wait-for-sbot")]
|
|
|
|
WaitForSbot,
|
2021-08-06 17:58:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(StructOpt, Debug)]
|
|
|
|
struct SetupOpts {
|
|
|
|
/// Setup i2c configurations
|
|
|
|
#[structopt(short, long)]
|
|
|
|
i2c: bool,
|
|
|
|
/// Optionally select which model of real-time-clock is being used,
|
|
|
|
/// {ds1307, ds3231}
|
|
|
|
#[structopt(short, long)]
|
|
|
|
rtc: Option<RtcOption>,
|
|
|
|
/// Run peach-config in non-interactive mode
|
|
|
|
#[structopt(short, long)]
|
|
|
|
no_input: bool,
|
|
|
|
/// Use the default en_US.UTF-8 locale for compatability
|
|
|
|
#[structopt(short, long)]
|
|
|
|
default_locale: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(StructOpt, Debug)]
|
|
|
|
pub struct UpdateOpts {
|
|
|
|
/// Only update other microservices and not peach-config
|
|
|
|
#[structopt(short, long)]
|
|
|
|
microservices: bool,
|
|
|
|
/// Only update peach-config and not other microservices
|
|
|
|
#[structopt(short, long = "--self")]
|
|
|
|
self_only: bool,
|
|
|
|
/// List microservices which are available for updating
|
|
|
|
#[structopt(short, long)]
|
|
|
|
list: bool,
|
|
|
|
}
|
|
|
|
|
2021-12-17 21:23:47 +00:00
|
|
|
#[derive(StructOpt, Debug)]
|
|
|
|
pub struct ChangePasswordOpts {
|
|
|
|
/// Optional argument to specify password as CLI argument
|
|
|
|
/// if not specified, this command asks for user input for the passwords
|
|
|
|
#[structopt(short, long)]
|
|
|
|
password: Option<String>,
|
|
|
|
}
|
|
|
|
|
2022-07-05 13:56:52 +00:00
|
|
|
#[derive(StructOpt, Debug)]
|
|
|
|
pub struct PublishAddressOpts {
|
|
|
|
/// Specify address in the form domain:port
|
|
|
|
#[structopt(short, long)]
|
|
|
|
address: String,
|
|
|
|
}
|
|
|
|
|
2021-08-06 17:58:40 +00:00
|
|
|
arg_enum! {
|
|
|
|
/// enum options for real-time clock choices
|
|
|
|
#[derive(Debug)]
|
|
|
|
#[allow(non_camel_case_types)]
|
|
|
|
#[allow(clippy::enum_variant_names)]
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub enum RtcOption {
|
|
|
|
DS1307,
|
|
|
|
DS3231
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-04 12:35:17 +00:00
|
|
|
async fn run() {
|
2021-08-06 17:58:40 +00:00
|
|
|
// initialize the logger
|
|
|
|
env_logger::init();
|
|
|
|
|
|
|
|
// parse cli arguments
|
|
|
|
let opt = Opt::from_args();
|
|
|
|
|
|
|
|
// switch based on subcommand
|
|
|
|
if let Some(subcommand) = opt.commands {
|
|
|
|
match subcommand {
|
|
|
|
PeachConfig::Setup(cfg) => {
|
2021-12-22 15:02:50 +00:00
|
|
|
match setup_peach::setup_peach(cfg.no_input, cfg.default_locale, cfg.i2c, cfg.rtc) {
|
2021-08-06 17:58:40 +00:00
|
|
|
Ok(_) => {}
|
|
|
|
Err(err) => {
|
|
|
|
error!("peach-config encountered an error: {}", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-12-22 15:02:50 +00:00
|
|
|
PeachConfig::Manifest => match generate_manifest::generate_manifest() {
|
2021-08-06 17:58:40 +00:00
|
|
|
Ok(_) => {}
|
|
|
|
Err(err) => {
|
|
|
|
error!(
|
2021-12-18 15:00:40 +00:00
|
|
|
"peach-config encountered an error generating manifest: {}",
|
2021-08-06 17:58:40 +00:00
|
|
|
err
|
|
|
|
)
|
|
|
|
}
|
|
|
|
},
|
2021-12-22 15:02:50 +00:00
|
|
|
PeachConfig::Update(opts) => match update::update(opts) {
|
2021-08-06 17:58:40 +00:00
|
|
|
Ok(_) => {}
|
|
|
|
Err(err) => {
|
|
|
|
error!("peach-config encountered an error during update: {}", err)
|
|
|
|
}
|
|
|
|
},
|
2021-12-22 15:02:50 +00:00
|
|
|
PeachConfig::ChangePassword(opts) => {
|
|
|
|
match change_password::set_peach_web_password(opts) {
|
|
|
|
Ok(_) => {}
|
|
|
|
Err(err) => {
|
|
|
|
error!(
|
|
|
|
"peach-config encountered an error during password update: {}",
|
|
|
|
err
|
|
|
|
)
|
|
|
|
}
|
2021-12-17 21:23:47 +00:00
|
|
|
}
|
2021-12-22 15:02:50 +00:00
|
|
|
}
|
|
|
|
PeachConfig::SetPermissions => match set_permissions::set_permissions() {
|
2021-12-18 15:00:40 +00:00
|
|
|
Ok(_) => {}
|
|
|
|
Err(err) => {
|
|
|
|
error!(
|
|
|
|
"peach-config ecountered an error updating file permissions: {}",
|
|
|
|
err
|
|
|
|
)
|
|
|
|
}
|
2021-12-22 15:02:50 +00:00
|
|
|
},
|
2022-07-04 12:35:17 +00:00
|
|
|
PeachConfig::WhoAmI => match status::whoami().await {
|
|
|
|
Ok(sbot_id) => {
|
|
|
|
println!("{:?}", sbot_id);
|
|
|
|
{}
|
|
|
|
}
|
2022-07-04 11:23:55 +00:00
|
|
|
Err(err) => {
|
2022-07-04 12:35:17 +00:00
|
|
|
error!("sbot whoami encountered an error: {}", err)
|
2022-07-04 11:23:55 +00:00
|
|
|
}
|
|
|
|
},
|
2022-07-05 13:56:52 +00:00
|
|
|
PeachConfig::PublishAddress(opts) => {
|
|
|
|
match publish_address::publish_address(opts.address).await {
|
|
|
|
Ok(_) => {}
|
|
|
|
Err(err) => {
|
|
|
|
error!(
|
|
|
|
"peach-config encountered an error during publish address: {}",
|
|
|
|
err
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2022-07-12 09:51:49 +00:00
|
|
|
},
|
|
|
|
PeachConfig::WaitForSbot => {
|
|
|
|
match wait_for_sbot::wait_for_sbot().await {
|
|
|
|
Ok(_) => {}
|
|
|
|
Err(err) => {
|
|
|
|
error!(
|
|
|
|
"peach-config did not successfully connect to sbot: {}",
|
|
|
|
err
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2022-07-05 13:56:52 +00:00
|
|
|
}
|
2021-08-06 17:58:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-07-04 12:35:17 +00:00
|
|
|
|
|
|
|
// Enable an async main function and execute the `run()` function,
|
|
|
|
// catching any errors and printing them to `stderr` before exiting the
|
|
|
|
// process.
|
|
|
|
#[async_std::main]
|
|
|
|
async fn main() {
|
|
|
|
run().await;
|
|
|
|
}
|