Browse Source

Simpler Resource

master
Adrian Heine 8 years ago
parent
commit
7b0575da1b
  1. 36
      src/resources/mod.rs
  2. 6
      src/symbols/acme/cert.rs
  3. 7
      src/symbols/file.rs
  4. 2
      src/symbols/hook.rs
  5. 1
      src/symbols/list.rs
  6. 2
      src/symbols/mod.rs
  7. 2
      src/symbols/nginx/server.rs
  8. 6
      src/symbols/systemd/node_js_user_service.rs
  9. 6
      src/symbols/tls/csr.rs
  10. 6
      src/symbols/tls/self_signed_cert.rs

36
src/resources/mod.rs

@ -1,33 +1,15 @@
use std::borrow::Cow;
#[derive(PartialEq, Eq, Hash, Clone)]
pub struct Resource(pub String, pub String);
pub trait Resource {
fn get_type(&self) -> &str;
fn get_value(&self) -> &str;
impl<'a> Resource {
pub fn new<A: Into<String>, B: Into<String>>(rtype: A, value: B) -> Self {
Resource(rtype.into(), value.into())
} }
pub struct UserResource<'a> {
pub name: &'a str
pub fn get_type(&self) -> &str {
&self.0
} }
impl<'a> Resource for UserResource<'a> {
fn get_type(&self) -> &str { "user" }
fn get_value(&self) -> &str { self.name }
}
pub struct DirResource<'a> {
pub path: Cow<'a, str>
}
impl<'a> Resource for DirResource<'a> {
fn get_type(&self) -> &str { "dir" }
fn get_value(&self) -> &str { &*self.path }
}
pub struct FileResource<'a> {
pub path: Cow<'a, str>
pub fn get_value(&self) -> &str {
&self.1
} }
impl<'a> Resource for FileResource<'a> {
fn get_type(&self) -> &str { "file" }
fn get_value(&self) -> &str { &*self.path }
} }

6
src/symbols/acme/cert.rs

@ -6,7 +6,7 @@ use std::io::{self, Write};
use command_runner::CommandRunner; use command_runner::CommandRunner;
use symbols::Symbol; use symbols::Symbol;
use resources::{FileResource, Resource};
use resources::Resource;
pub struct AcmeCert<'a> { pub struct AcmeCert<'a> {
domain: Cow<'a, str>, domain: Cow<'a, str>,
@ -72,8 +72,8 @@ impl<'a> Symbol for AcmeCert<'a> {
Ok(()) Ok(())
} }
fn get_prerequisites(&self) -> Vec<Box<Resource>> {
vec![Box::new(FileResource { path: self.get_csr_path().into() })]
fn get_prerequisites(&self) -> Vec<Resource> {
vec![ Resource::new("file", self.get_csr_path()) ]
} }
} }

7
src/symbols/file.rs

@ -7,7 +7,7 @@ use std::ops::Deref;
use std::path::Path; use std::path::Path;
use symbols::Symbol; use symbols::Symbol;
use resources::{DirResource, Resource};
use resources::Resource;
pub struct File<C, D> where C: Deref<Target=str>, D: AsRef<str> + fmt::Display { pub struct File<C, D> where C: Deref<Target=str>, D: AsRef<str> + fmt::Display {
path: D, path: D,
@ -53,9 +53,8 @@ impl<C, D> Symbol for File<C, D> where C: Deref<Target=str>, D: AsRef<str> + fmt
Ok(()) Ok(())
} }
fn get_prerequisites(&self) -> Vec<Box<Resource>> {
vec![
Box::new(DirResource { path: String::from(Path::new(self.path.as_ref()).parent().unwrap().to_string_lossy()).into() }) ]
fn get_prerequisites(&self) -> Vec<Resource> {
vec![ Resource::new("dir", Path::new(self.path.as_ref()).parent().unwrap().to_string_lossy() ) ]
} }
} }

2
src/symbols/hook.rs

@ -25,7 +25,7 @@ impl<A, B> Symbol for Hook<A, B> where A: Symbol, B: Symbol {
self.b.execute() self.b.execute()
} }
fn get_prerequisites(&self) -> Vec<Box<Resource>> {
fn get_prerequisites(&self) -> Vec<Resource> {
let mut r = vec![]; let mut r = vec![];
r.extend(self.a.get_prerequisites().into_iter()); r.extend(self.a.get_prerequisites().into_iter());
r.extend(self.b.get_prerequisites().into_iter()); r.extend(self.b.get_prerequisites().into_iter());

1
src/symbols/list.rs

@ -1,6 +1,7 @@
use std::error::Error; use std::error::Error;
use std::fmt; use std::fmt;
use resources::Resource;
use symbols::Symbol; use symbols::Symbol;
pub struct List<'a> { pub struct List<'a> {

2
src/symbols/mod.rs

@ -5,7 +5,7 @@ use resources::Resource;
pub trait Symbol: Display { pub trait Symbol: Display {
fn target_reached(&self) -> Result<bool, Box<Error>>; fn target_reached(&self) -> Result<bool, Box<Error>>;
fn execute(&self) -> Result<(), Box<Error>>; fn execute(&self) -> Result<(), Box<Error>>;
fn get_prerequisites(&self) -> Vec<Box<Resource>> {
fn get_prerequisites(&self) -> Vec<Resource> {
vec![] vec![]
} }
} }

2
src/symbols/nginx/server.rs

@ -144,7 +144,7 @@ impl<'a, C> Symbol for NginxServer<'a, C> where C: Deref<Target=str> {
Ok(()) Ok(())
} }
fn get_prerequisites(&self) -> Vec<Box<Resource>> {
fn get_prerequisites(&self) -> Vec<Resource> {
self.file.get_prerequisites() self.file.get_prerequisites()
} }
} }

6
src/symbols/systemd/node_js_user_service.rs

@ -9,7 +9,7 @@ use std::time::Duration;
use std::ops::Deref; use std::ops::Deref;
use command_runner::CommandRunner; use command_runner::CommandRunner;
use resources::{ FileResource, Resource };
use resources::Resource;
use symbols::Symbol; use symbols::Symbol;
use symbols::file::File as FileSymbol; use symbols::file::File as FileSymbol;
@ -156,8 +156,8 @@ impl<'a, C> Symbol for NodeJsSystemdUserService<'a, C> where C: Deref<Target=str
Ok(()) Ok(())
} }
fn get_prerequisites(&self) -> Vec<Box<Resource>> {
let mut r: Vec<Box<Resource>> = vec![ Box::new(FileResource { path: ("/var/lib/systemd/linger/".to_string() + self.user_name).into() }) ];
fn get_prerequisites(&self) -> Vec<Resource> {
let mut r = vec![ Resource::new("file", format!("/var/lib/systemd/linger/{}", self.user_name)) ];
r.extend(self.file.get_prerequisites().into_iter()); r.extend(self.file.get_prerequisites().into_iter());
r r
} }

6
src/symbols/tls/csr.rs

@ -3,7 +3,7 @@ use std::error::Error;
use std::fmt; use std::fmt;
use command_runner::CommandRunner; use command_runner::CommandRunner;
use resources::{Resource, FileResource};
use resources::Resource;
use symbols::Symbol; use symbols::Symbol;
pub struct TlsCsr<'a> { pub struct TlsCsr<'a> {
@ -52,8 +52,8 @@ impl<'a> Symbol for TlsCsr<'a> {
Ok(()) Ok(())
} }
fn get_prerequisites(&self) -> Vec<Box<Resource>> {
vec![Box::new(FileResource { path: self.get_key_path().into() })]
fn get_prerequisites(&self) -> Vec<Resource> {
vec![Resource::new("file", self.get_key_path())]
} }
} }

6
src/symbols/tls/self_signed_cert.rs

@ -3,7 +3,7 @@ use std::error::Error;
use std::fmt; use std::fmt;
use command_runner::CommandRunner; use command_runner::CommandRunner;
use resources::{Resource, FileResource};
use resources::Resource;
use symbols::Symbol; use symbols::Symbol;
pub struct SelfSignedTlsCert<'a> { pub struct SelfSignedTlsCert<'a> {
@ -57,8 +57,8 @@ impl<'a> Symbol for SelfSignedTlsCert<'a> {
} }
} }
fn get_prerequisites(&self) -> Vec<Box<Resource>> {
vec![Box::new(FileResource { path: self.get_key_path().into() })]
fn get_prerequisites(&self) -> Vec<Resource> {
vec![Resource::new("file", self.get_key_path())]
} }
} }

Loading…
Cancel
Save