make nameserver setup work

This commit is contained in:
Jorge Aparicio 2024-02-02 14:48:26 +01:00
parent bc10cda9cc
commit 6026caf25d
2 changed files with 44 additions and 12 deletions

View File

@ -67,14 +67,16 @@ impl Container {
let id = core::str::from_utf8(&output.stdout)?.trim().to_string(); let id = core::str::from_utf8(&output.stdout)?.trim().to_string();
dbg!(&id); dbg!(&id);
let container = Self {
Ok(Self {
id, id,
name: container_name, name: container_name,
}) };
dbg!(container.ip_addr()?);
Ok(container)
} }
pub fn cp(&self, path_in_container: &str, file_contents: &str) -> Result<()> { pub fn cp(&self, path_in_container: &str, file_contents: &str, chmod: &str) -> Result<()> {
let mut temp_file = NamedTempFile::new()?; let mut temp_file = NamedTempFile::new()?;
fs::write(&mut temp_file, file_contents)?; fs::write(&mut temp_file, file_contents)?;
@ -89,6 +91,12 @@ impl Container {
return Err(format!("`{command:?}` failed").into()); return Err(format!("`{command:?}` failed").into());
} }
let command = &["chmod", chmod, path_in_container];
let output = self.exec(command)?;
if !output.status.success() {
return Err(format!("`{command:?}` failed").into());
}
Ok(()) Ok(())
} }
@ -195,7 +203,7 @@ mod tests {
let path = "/tmp/somefile"; let path = "/tmp/somefile";
let contents = "hello"; let contents = "hello";
container.cp(path, contents)?; container.cp(path, contents, CHMOD_RW_EVERYONE)?;
let output = container.exec(&["cat", path])?; let output = container.exec(&["cat", path])?;
dbg!(&output); dbg!(&output);
@ -211,6 +219,7 @@ mod tests {
fn tld_zone(domain: &str) -> String { fn tld_zone(domain: &str) -> String {
assert!(domain.ends_with(".")); assert!(domain.ends_with("."));
assert!(!domain.starts_with("."));
let mut env = Environment::new(); let mut env = Environment::new();
let name = "main.zone"; let name = "main.zone";
@ -220,6 +229,17 @@ mod tests {
template.render(context! { tld => domain }).unwrap() template.render(context! { tld => domain }).unwrap()
} }
fn nsd_conf(domain: &str) -> String {
assert!(domain.ends_with("."));
let mut env = Environment::new();
let name = "nsd.conf";
env.add_template(name, include_str!("templates/nsd.conf.jinja"))
.unwrap();
let template = env.get_template(name).unwrap();
template.render(context! { domain => domain }).unwrap()
}
fn root_zone() -> String { fn root_zone() -> String {
let mut env = Environment::new(); let mut env = Environment::new();
let name = "main.zone"; let name = "main.zone";
@ -229,29 +249,39 @@ mod tests {
template.render(context! {}).unwrap() template.render(context! {}).unwrap()
} }
const CHMOD_RW_EVERYONE: &str = "666";
// TODO create `nsd.conf` file at runtime // TODO create `nsd.conf` file at runtime
#[test] #[test]
fn tld_setup() -> Result<()> { fn tld_setup() -> Result<()> {
let tld_ns = Container::run(Image::Nsd)?; let tld_ns = Container::run(Image::Nsd)?;
tld_ns.exec(&["mkdir", "-p", "/etc/nsd/zones"])?; tld_ns.exec(&["mkdir", "-p", "/etc/nsd/zones"])?;
tld_ns.cp("/etc/nsd/zones/main.zone", &tld_zone("."))?; tld_ns.cp(
"/etc/nsd/zones/main.zone",
&tld_zone("com."),
CHMOD_RW_EVERYONE,
)?;
tld_ns.cp("/etc/nsd/nsd.conf", &nsd_conf("com."), CHMOD_RW_EVERYONE)?;
tld_ns.exec(&["nsd", "-d"])?; let status = tld_ns.exec2(&["nsd", "-d"])?;
// println!("stdout: {}", core::str::from_utf8(&output.stdout).unwrap());
// println!("stderr: {}", core::str::from_utf8(&output.stderr).unwrap());
assert!(status.success());
Ok(()) Ok(())
} }
#[test] #[test]
fn root_setup() -> Result<()> { fn root_setup() -> Result<()> {
let tld_ns = Container::run(Image::Nsd)?; let root_ns = Container::run(Image::Nsd)?;
tld_ns.exec(&["mkdir", "-p", "/etc/nsd/zones"])?; root_ns.exec(&["mkdir", "-p", "/etc/nsd/zones"])?;
let zone_path = "/etc/nsd/zones/main.zone"; let zone_path = "/etc/nsd/zones/main.zone";
tld_ns.cp(zone_path, &root_zone())?; root_ns.cp("/etc/nsd/nsd.conf", &nsd_conf("."), CHMOD_RW_EVERYONE)?;
tld_ns.exec(&["chmod", "666", zone_path])?; root_ns.cp(zone_path, &root_zone(), CHMOD_RW_EVERYONE)?;
tld_ns.exec2(&["nsd", "-d"])?; root_ns.exec2(&["nsd", "-d"])?;
Ok(()) Ok(())
} }

View File

@ -8,3 +8,5 @@ $TTL 1800
86400 ; Minimum (1 day) 86400 ; Minimum (1 day)
) )
@ IN NS primary.tld-server.{{ tld }} @ IN NS primary.tld-server.{{ tld }}
; intentionally blank