Skip to content

Commit

Permalink
service reconfiguration on khost update
Browse files Browse the repository at this point in the history
  • Loading branch information
aspect committed Aug 3, 2024
1 parent 8dca4e2 commit 178222d
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 12 deletions.
7 changes: 4 additions & 3 deletions src/kaspad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ pub fn purge_data_folder(config: &Config) -> Result<()> {
)
}

pub fn check_for_updates(ctx: &Context) -> Result<()> {
pub fn check_for_updates(ctx: &Context) -> Result<bool> {
let mut updates = Vec::new();
for origin in unique_origins(ctx) {
let path = folder(&origin);
Expand All @@ -532,9 +532,10 @@ pub fn check_for_updates(ctx: &Context) -> Result<()> {
.interact()?
{
update(ctx)?;
Ok(true)
} else {
Ok(false)
}

Ok(())
}

pub fn find_config_by_network<'a>(
Expand Down
27 changes: 27 additions & 0 deletions src/khost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use crate::imports::*;

pub const VERSION: &str = env!("CARGO_PKG_VERSION");

const CONFIGURATION_REBUILD_REQUIRED: bool = true;

pub fn binary() -> Result<PathBuf> {
Ok(std::env::current_exe()?)
}
Expand Down Expand Up @@ -38,6 +40,31 @@ pub fn update() -> Result<()> {
Ok(())
}

pub fn reconfigure_if_needed(ctx: &mut Context, _services_updated: bool) -> Result<()> {
let version_file = data_folder().join("version");
let rebuild = if !version_file.exists() {
fs::write(version_file, VERSION)?;
CONFIGURATION_REBUILD_REQUIRED
} else {
let current_version = fs::read_to_string(&version_file)?;
if current_version != VERSION {
fs::write(version_file, VERSION)?;
CONFIGURATION_REBUILD_REQUIRED
} else {
false
}
};

if rebuild {
log::warning("Service configuration rebuild is required...")?;
kaspad::reconfigure(ctx, true)?;
resolver::reconfigure(ctx, true)?;
nginx::reconfigure(ctx)?;
}

Ok(())
}

pub fn surrender() {
let _ = duct::cmd!("khost").run();
std::process::exit(0);
Expand Down
12 changes: 9 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,20 @@ fn main() {

init_user_interaction();

if first_run {
let services_updated = if first_run {
if let Err(err) = actions::Bootstrap::select(&mut ctx) {
log::error(err).ok();
log::info("You can attempt another full install from 'Advanced' menu").ok();
}
false
} else {
kaspad::check_for_updates(&ctx).ok();
resolver::check_for_updates(&ctx).ok();
let kaspad_update = kaspad::check_for_updates(&ctx).unwrap_or_default();
let resolver_update = resolver::check_for_updates(&ctx).unwrap_or_default();
kaspad_update || resolver_update
};

if let Err(err) = khost::reconfigure_if_needed(&mut ctx, services_updated) {
log::error(err).ok();
}

actions::Main::run(&mut ctx).ok();
Expand Down
18 changes: 12 additions & 6 deletions src/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,10 +286,10 @@ pub fn status(config: &Config) -> Result<String> {
systemd::status(config)
}

pub fn check_for_updates(ctx: &Context) -> Result<()> {
pub fn check_for_updates(ctx: &Context) -> Result<bool> {
let config = &ctx.config.resolver;
if !config.enabled() {
return Ok(());
return Ok(false);
}
let mut updates = Vec::new();
let origin = &config.origin;
Expand All @@ -310,9 +310,10 @@ pub fn check_for_updates(ctx: &Context) -> Result<()> {
.interact()?
{
update(ctx)?;
Ok(true)
} else {
Ok(false)
}

Ok(())
}

pub fn reconfigure(ctx: &mut Context, _force: bool) -> Result<()> {
Expand All @@ -328,7 +329,13 @@ pub fn reconfigure(ctx: &mut Context, _force: bool) -> Result<()> {
&& systemd::is_enabled(config.service_name())?
&& systemd::is_active(config.service_name())?
{
restart(ctx)?;
step("Configuring 'kaspa-resolver'", || {
systemd::stop(config)?;
create_systemd_unit(ctx, &ctx.config.resolver)?;
systemd::daemon_reload()?;
systemd::start(config)?;
Ok(())
})?;
} else {
step("Configuring 'kaspa-resolver'", || {
if config.enabled() {
Expand All @@ -345,7 +352,6 @@ pub fn reconfigure(ctx: &mut Context, _force: bool) -> Result<()> {
Ok(())
})?;
}
// restart(ctx)?;
Ok(())
}

Expand Down

0 comments on commit 178222d

Please sign in to comment.