A library for writing host-specific, single-binary configuration management and deployment tools
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
2.1 KiB

use resources::Resource;
use std::borrow::{Borrow, Cow};
use std::error::Error;
use std::fmt;
use std::ops::Deref;
use std::path::PathBuf;
use command_runner::CommandRunner;
use symbols::acme::AcmeAccountKey;
use symbols::dir::Dir;
use symbols::file::File;
use symbols::list::List;
use symbols::owner::Owner;
use symbols::{Action, OwnedSymbolAction, Symbol, SymbolAction, SymbolRunner};
pub struct AcmeUser<'a>(Cow<'a, str>);
impl<'a> AcmeUser<'a> {
pub fn new<S: Into<Cow<'a, str>>>(user_name: S) -> Self {
AcmeUser(user_name.into())
}
}
impl<'a> Symbol for AcmeUser<'a> {
fn target_reached(&self) -> Result<bool, Box<dyn Error>> {
Ok(false)
}
fn execute(&self) -> Result<(), Box<dyn Error>> {
Ok(())
}
fn get_prerequisites(&self) -> Vec<Resource> {
vec![]
}
fn provides(&self) -> Option<Vec<Resource>> {
None
}
fn as_action<'b>(&'b self, runner: &'b dyn SymbolRunner) -> Box<dyn Action + 'b> {
Box::new(SymbolAction::new(runner, self))
}
fn into_action<'b>(self: Box<Self>, runner: &'b dyn SymbolRunner) -> Box<dyn Action + 'b>
where
Self: 'b,
{
Box::new(OwnedSymbolAction::new(runner, *self))
}
}
impl<'a> fmt::Display for AcmeUser<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "AcmeUser {}", self.0)
}
}
pub fn new<'a, S: Into<Cow<'a, str>>, C: CommandRunner, P: 'a + Deref<Target = str>>(
command_runner: &'a C,
cert: P,
user_name: S,
) -> impl Symbol + 'a {
// impl trait
let user_name_cow = user_name.into();
let account_key_file: PathBuf = ["/home", user_name_cow.borrow(), "account.key"]
.iter()
.collect();
List::from((
AcmeAccountKey::new(account_key_file.clone().into(), command_runner),
Owner::new(
account_key_file.to_string_lossy().into_owned(),
user_name_cow.clone(),
command_runner,
),
Dir::new("/home/acme/challenges"),
Owner::new(
"/home/acme/challenges",
user_name_cow.clone(),
command_runner,
),
Dir::new("/etc/ssl/local_certs"),
Owner::new("/etc/ssl/local_certs", user_name_cow, command_runner),
File::new("/home/acme/lets_encrypt_x3_cross_signed.pem", cert),
))
}