Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support NixOS containers as targets #112

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/nix/hive/options.nix
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,14 @@ with builtins; rec {
type = types.nullOr types.str;
default = "root";
};
targetContainer = lib.mkOption {
description = ''
If set to a string, Colmena will update a container on the
target host instead of updating the target host itself.
'';
type = types.nullOr types.str;
default = null;
};
allowLocalDeployment = lib.mkOption {
description = ''
Allow the configuration to be applied locally on the host running
Expand Down
18 changes: 17 additions & 1 deletion src/nix/host/ssh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ pub struct Ssh {
/// The port to connect to.
port: Option<u16>,

/// The container to target on the host.
container: Option<String>,

/// Local path to a ssh_config file.
ssh_config: Option<PathBuf>,

Expand Down Expand Up @@ -162,11 +165,12 @@ impl Host for Ssh {
}

impl Ssh {
pub fn new(user: Option<String>, host: String) -> Self {
pub fn new(user: Option<String>, host: String, container: Option<String>) -> Self {
Self {
user,
host,
port: None,
container,
ssh_config: None,
privilege_escalation_command: Vec::new(),
job: None,
Expand Down Expand Up @@ -199,13 +203,25 @@ impl Ssh {
&[]
};

// This scopes the command to a named container on the NixOS host, if requested.
let container_scope_command = match &self.container {
Some(container) => vec![
"nixos-container",
"run",
container.as_ref(),
"--"
],
None => vec![]
};

let mut cmd = Command::new("ssh");

cmd
.arg(self.ssh_target())
.args(&options)
.arg("--")
.args(privilege_escalation_command)
.args(container_scope_command.as_slice())
.args(command)
.env("NIX_SSHOPTS", options_str);

Expand Down
9 changes: 8 additions & 1 deletion src/nix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ pub struct NodeConfig {
#[serde(rename = "targetPort")]
target_port: Option<u16>,

#[serde(rename = "targetContainer")]
target_container: Option<String>,

#[serde(rename = "allowLocalDeployment")]
allow_local_deployment: bool,

Expand Down Expand Up @@ -174,7 +177,11 @@ impl NodeConfig {

pub fn to_ssh_host(&self) -> Option<Ssh> {
self.target_host.as_ref().map(|target_host| {
let mut host = Ssh::new(self.target_user.clone(), target_host.clone());
let mut host = Ssh::new(
self.target_user.clone(),
target_host.clone(),
self.target_container.clone()
);
host.set_privilege_escalation_command(self.privilege_escalation_command.clone());

if let Some(target_port) = self.target_port {
Expand Down
1 change: 1 addition & 0 deletions src/nix/node_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ mod tests {
target_host: None,
target_user: None,
target_port: None,
target_container: None,
allow_local_deployment: false,
build_on_target: false,
replace_unknown_profiles: false,
Expand Down