-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Factor out the enterprise init service setup into its own module
- Loading branch information
Showing
4 changed files
with
223 additions
and
73 deletions.
There are no files selected for viewing
188 changes: 188 additions & 0 deletions
188
src/action/common/configure_enterprise_edition_init_service.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
use std::path::PathBuf; | ||
|
||
#[cfg(target_os = "macos")] | ||
use serde::{Deserialize, Serialize}; | ||
#[cfg(target_os = "macos")] | ||
use tokio::io::AsyncWriteExt; | ||
use tokio::process::Command; | ||
use tracing::{span, Span}; | ||
|
||
use crate::action::{ActionError, ActionErrorKind, ActionTag, StatefulAction}; | ||
use crate::execute_command; | ||
|
||
use crate::action::{Action, ActionDescription}; | ||
|
||
#[cfg(target_os = "macos")] | ||
const DARWIN_ENTERPRISE_EDITION_DAEMON_DEST: &str = | ||
"/Library/LaunchDaemons/systems.determinate.nix-daemon.plist"; | ||
/** | ||
Configure the init to run the Nix daemon | ||
*/ | ||
#[derive(Debug, serde::Deserialize, serde::Serialize, Clone)] | ||
pub struct ConfigureEnterpriseEditionInitService { | ||
start_daemon: bool, | ||
} | ||
|
||
impl ConfigureEnterpriseEditionInitService { | ||
#[tracing::instrument(level = "debug", skip_all)] | ||
pub async fn plan(start_daemon: bool) -> Result<StatefulAction<Self>, ActionError> { | ||
Ok(Self { start_daemon }.into()) | ||
} | ||
} | ||
|
||
#[async_trait::async_trait] | ||
#[typetag::serde(name = "configure_enterprise_edition_init_service")] | ||
impl Action for ConfigureEnterpriseEditionInitService { | ||
fn action_tag() -> ActionTag { | ||
ActionTag("configure_enterprise_edition_init_service") | ||
} | ||
fn tracing_synopsis(&self) -> String { | ||
"Configure the Determinate Nix Enterprise Edition daemon related settings with launchctl" | ||
.to_string() | ||
} | ||
|
||
fn tracing_span(&self) -> Span { | ||
span!( | ||
tracing::Level::DEBUG, | ||
"configure_enterprise_edition_init_service" | ||
) | ||
} | ||
|
||
fn execute_description(&self) -> Vec<ActionDescription> { | ||
let mut explanation = vec![format!("Create `{DARWIN_ENTERPRISE_EDITION_DAEMON_DEST}`")]; | ||
if self.start_daemon { | ||
explanation.push(format!( | ||
"Run `launchctl load {DARWIN_ENTERPRISE_EDITION_DAEMON_DEST}`" | ||
)); | ||
} | ||
|
||
vec![ActionDescription::new(self.tracing_synopsis(), explanation)] | ||
} | ||
|
||
#[tracing::instrument(level = "debug", skip_all)] | ||
async fn execute(&mut self) -> Result<(), ActionError> { | ||
let Self { start_daemon } = self; | ||
|
||
let daemon_file = DARWIN_ENTERPRISE_EDITION_DAEMON_DEST; | ||
let domain = "system"; | ||
let service = "systems.determinate.nix-daemon"; | ||
|
||
let generated_plist = generate_plist(); | ||
|
||
let mut options = tokio::fs::OpenOptions::new(); | ||
options.create(true).write(true).read(true); | ||
|
||
let mut file = options | ||
.open(&daemon_file) | ||
.await | ||
.map_err(|e| Self::error(ActionErrorKind::Open(PathBuf::from(daemon_file), e)))?; | ||
|
||
let mut buf = Vec::new(); | ||
plist::to_writer_xml(&mut buf, &generated_plist).map_err(Self::error)?; | ||
file.write_all(&buf) | ||
.await | ||
.map_err(|e| Self::error(ActionErrorKind::Write(PathBuf::from(daemon_file), e)))?; | ||
|
||
execute_command( | ||
Command::new("launchctl") | ||
.process_group(0) | ||
.args(["load", "-w"]) | ||
.arg(daemon_file) | ||
.stdin(std::process::Stdio::null()), | ||
) | ||
.await | ||
.map_err(Self::error)?; | ||
|
||
let is_disabled = crate::action::macos::service_is_disabled(domain, service) | ||
.await | ||
.map_err(Self::error)?; | ||
if is_disabled { | ||
execute_command( | ||
Command::new("launchctl") | ||
.process_group(0) | ||
.arg("enable") | ||
.arg(&format!("{domain}/{service}")) | ||
.stdin(std::process::Stdio::null()), | ||
) | ||
.await | ||
.map_err(Self::error)?; | ||
} | ||
|
||
if *start_daemon { | ||
execute_command( | ||
Command::new("launchctl") | ||
.process_group(0) | ||
.arg("kickstart") | ||
.arg("-k") | ||
.arg(&format!("{domain}/{service}")) | ||
.stdin(std::process::Stdio::null()), | ||
) | ||
.await | ||
.map_err(Self::error)?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
fn revert_description(&self) -> Vec<ActionDescription> { | ||
vec![ActionDescription::new( | ||
"Unconfigure Nix daemon related settings with launchctl".to_string(), | ||
vec![format!( | ||
"Run `launchctl unload {DARWIN_ENTERPRISE_EDITION_DAEMON_DEST}`" | ||
)], | ||
)] | ||
} | ||
|
||
#[tracing::instrument(level = "debug", skip_all)] | ||
async fn revert(&mut self) -> Result<(), ActionError> { | ||
execute_command( | ||
Command::new("launchctl") | ||
.process_group(0) | ||
.arg("unload") | ||
.arg(DARWIN_ENTERPRISE_EDITION_DAEMON_DEST), | ||
) | ||
.await | ||
.map_err(Self::error)?; | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
#[non_exhaustive] | ||
#[derive(Debug, thiserror::Error)] | ||
pub enum ConfigureEnterpriseEditionNixDaemonServiceError {} | ||
|
||
#[cfg(target_os = "macos")] | ||
#[derive(Deserialize, Clone, Debug, Serialize, PartialEq)] | ||
#[serde(rename_all = "PascalCase")] | ||
pub struct DeterminateNixDaemonPlist { | ||
label: String, | ||
program: String, | ||
keep_alive: bool, | ||
run_at_load: bool, | ||
standard_error_path: String, | ||
standard_out_path: String, | ||
soft_resource_limits: ResourceLimits, | ||
} | ||
|
||
#[cfg(target_os = "macos")] | ||
#[derive(Deserialize, Clone, Debug, Serialize, PartialEq)] | ||
#[serde(rename_all = "PascalCase")] | ||
pub struct ResourceLimits { | ||
number_of_files: usize, | ||
} | ||
|
||
#[cfg(target_os = "macos")] | ||
fn generate_plist() -> DeterminateNixDaemonPlist { | ||
DeterminateNixDaemonPlist { | ||
keep_alive: true, | ||
run_at_load: true, | ||
label: "systems.determinate.nix-daemon".into(), | ||
program: "/usr/local/bin/determinate-nix-ee".into(), | ||
standard_error_path: "/var/log/determinate-nix-daemon.log".into(), | ||
standard_out_path: "/var/log/determinate-nix-daemon.log".into(), | ||
soft_resource_limits: ResourceLimits { | ||
number_of_files: 1048576, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters