Browse Source

Some small fixes

master
Adrian Heine 4 years ago
parent
commit
42690f1d50
  1. 3
      Cargo.toml
  2. 23
      src/loggers.rs
  3. 9
      src/setup/core.rs
  4. 12
      src/setup/setup.rs
  5. 13
      src/symbols/npm.rs
  6. 8
      src/symbols/user.rs
  7. 2
      src/templates/systemd.rs

3
Cargo.toml

@ -10,7 +10,8 @@ users = "0.10.0"
regex = "1.0.1" regex = "1.0.1"
futures = "0.3" futures = "0.3"
async-trait = "0.1" async-trait = "0.1"
tokio = { version = "0.2", features = ["process", "io-util", "rt-core", "macros"] }
tokio = { version = "0.2", features = ["process", "io-util", "rt-core", "macros", "sync"] }
once_cell = "1.4"
[dev-dependencies] [dev-dependencies]
tempdir = "0.3" tempdir = "0.3"

23
src/loggers.rs

@ -100,7 +100,7 @@ impl<'a, L: Logger> Logger for FilteringLogger<'a, L> {
#[derive(Debug, Default)] #[derive(Debug, Default)]
pub struct StoringLogger { pub struct StoringLogger {
log: RefCell<Vec<Entry<String>>>,
log: RefCell<(bool, Vec<Entry<String>>)>,
} }
impl StoringLogger { impl StoringLogger {
@ -109,21 +109,28 @@ impl StoringLogger {
} }
pub fn release(self) -> Vec<Entry<String>> { pub fn release(self) -> Vec<Entry<String>> {
self.log.into_inner()
self.log.into_inner().1
} }
} }
impl Logger for StoringLogger { impl Logger for StoringLogger {
fn write<S: AsRef<str> + Into<String>>(&self, level: Level, line: S) { fn write<S: AsRef<str> + Into<String>>(&self, level: Level, line: S) {
let mut log = self.log.borrow_mut(); let mut log = self.log.borrow_mut();
let entry = log
.pop()
.map(|e| Entry(min(e.0, level), e.1 + line.as_ref()))
.unwrap_or_else(|| Entry(level, line.into()));
log.push(entry);
let entry = if log.0 {
log
.1
.pop()
.map(|e| Entry(min(e.0, level), e.1 + line.as_ref()))
} else {
None
}
.unwrap_or_else(|| Entry(level, line.into()));
log.1.push(entry);
} }
fn writeln<S: AsRef<str> + Into<String>>(&self, level: Level, line: S) { fn writeln<S: AsRef<str> + Into<String>>(&self, level: Level, line: S) {
self.log.borrow_mut().push(Entry(level, line.into()));
let mut log = self.log.borrow_mut();
log.0 = false;
log.1.push(Entry(level, line.into()));
} }
} }

9
src/setup/core.rs

@ -137,7 +137,7 @@ where
let resource = resource.as_ref(); let resource = resource.as_ref();
let logger = StoringLogger::new(); let logger = StoringLogger::new();
logger.write(4, format!("Adding {:?} ... ", resource)); logger.write(4, format!("Adding {:?} ... ", resource));
let result = {
let result = async {
logger.trace(format!(" (force_run is {})", force_run)); logger.trace(format!(" (force_run is {})", force_run));
let (location, location_prereqs) = L::locate(resource); let (location, location_prereqs) = L::locate(resource);
logger.trace(format!("Adding location prereqs for {:?}", resource)); logger.trace(format!("Adding location prereqs for {:?}", resource));
@ -162,7 +162,8 @@ where
) )
.await?; .await?;
Ok((location, did_run)) Ok((location, did_run))
};
}
.await;
logger.write(4, "done."); logger.write(4, "done.");
let max_level = if result.is_err() { 5 } else { 3 }; let max_level = if result.is_err() { 5 } else { 3 };
if parent_logger.put(logger.release().into_iter().filter(|e| e.0 <= max_level)) == 0 { if parent_logger.put(logger.release().into_iter().filter(|e| e.0 <= max_level)) == 0 {
@ -181,9 +182,9 @@ impl<SR: SymbolRunner, L, B> SymbolRunner for RegularSetupCore<SR, L, B> {
force: bool, force: bool,
) -> Result<bool, Box<dyn Error>> { ) -> Result<bool, Box<dyn Error>> {
let logger = StoringLogger::new(); let logger = StoringLogger::new();
logger.debug(format!("Directly running {:?} ...", symbol));
logger.write(4, format!("Directly running {:?} ...", symbol));
let result = self.symbol_runner.run_symbol(symbol, &logger, force).await; let result = self.symbol_runner.run_symbol(symbol, &logger, force).await;
logger.debug("done.");
logger.write(4, "done.");
let max_level = if result.is_err() { 5 } else { 3 }; let max_level = if result.is_err() { 5 } else { 3 };
parent_logger.put(logger.release().into_iter().filter(|e| e.0 <= max_level)); parent_logger.put(logger.release().into_iter().filter(|e| e.0 <= max_level));
result result

12
src/setup/setup.rs

@ -64,11 +64,7 @@ impl<
self.0.resources.borrow_mut() self.0.resources.borrow_mut()
} }
pub async fn add_force<R: AddableResource>(
&self,
resource: R,
force_run: bool,
) -> AddResult<R>
pub async fn add_force<R: AddableResource>(&self, resource: R, force_run: bool) -> AddResult<R>
where where
Rs: FromResource<R>, Rs: FromResource<R>,
As: FromArtifact<R> + Clone, As: FromArtifact<R> + Clone,
@ -126,7 +122,11 @@ impl<
self.add_force(resource, false).await self.add_force(resource, false).await
} }
pub async fn run_symbol<S: Runnable>(&self, symbol: S, force: bool) -> Result<bool, Box<dyn Error>>
pub async fn run_symbol<S: Runnable>(
&self,
symbol: S,
force: bool,
) -> Result<bool, Box<dyn Error>>
where where
RegularSetupCore<SR, L, B>: SymbolRunner, RegularSetupCore<SR, L, B>: SymbolRunner,
{ {

13
src/symbols/npm.rs

@ -13,7 +13,7 @@ pub struct Install<'a, T: AsRef<Path>, C: CommandRunner> {
impl<'a, T: AsRef<Path>, C: CommandRunner> Install<'a, T, C> { impl<'a, T: AsRef<Path>, C: CommandRunner> Install<'a, T, C> {
pub fn new(target: T, command_runner: &'a C) -> Self { pub fn new(target: T, command_runner: &'a C) -> Self {
Install {
Self {
target, target,
command_runner, command_runner,
} }
@ -36,9 +36,9 @@ impl<T: AsRef<Path>, C: CommandRunner> Symbol for Install<'_, T, C> {
if !self.target.as_ref().exists() { if !self.target.as_ref().exists() {
return Ok(false); return Ok(false);
} }
let output = self
let result = self
.command_runner .command_runner
.get_output(
.run_with_args(
"sh", "sh",
args![ args![
"-c", "-c",
@ -46,7 +46,12 @@ impl<T: AsRef<Path>, C: CommandRunner> Symbol for Install<'_, T, C> {
], ],
) )
.await?; .await?;
Ok(!String::from_utf8(output).unwrap().contains("(empty)"))
Ok(
result.status.success()
&& !String::from_utf8(result.stdout)
.unwrap()
.contains("(empty)"),
)
} }
async fn execute(&self) -> Result<(), Box<dyn Error>> { async fn execute(&self) -> Result<(), Box<dyn Error>> {

8
src/symbols/user.rs

@ -1,7 +1,12 @@
use crate::command_runner::CommandRunner; use crate::command_runner::CommandRunner;
use crate::symbols::Symbol; use crate::symbols::Symbol;
use async_trait::async_trait; use async_trait::async_trait;
use once_cell::sync::Lazy;
use std::error::Error; use std::error::Error;
use tokio::sync::Semaphore;
pub type Wait = Lazy<Semaphore>;
static WAIT: Wait = Lazy::new(|| Semaphore::new(1));
#[derive(Debug)] #[derive(Debug)]
pub struct User<U, C> { pub struct User<U, C> {
@ -33,6 +38,9 @@ impl<U: AsRef<str>, C: CommandRunner> Symbol for User<U, C> {
} }
async fn execute(&self) -> Result<(), Box<dyn Error>> { async fn execute(&self) -> Result<(), Box<dyn Error>> {
// adduser is not reentrant because finding the next uid
// and creating the account is not an atomic operation
let wait = WAIT.acquire().await;
self self
.command_runner .command_runner
.run_successfully( .run_successfully(

2
src/templates/systemd.rs

@ -36,7 +36,7 @@ pub fn nodejs_service<N: AsRef<Path>, S: AsRef<Path>>(
&format!( &format!(
"Environment=NODE_ENV=production "Environment=NODE_ENV=production
Environment=PORT={0} Environment=PORT={0}
ExecStartPost=/bin/sh -c 'sleep 1 && chmod 666 {0}'
ExecStartPost=/bin/sh -c 'for i in 1 2 3 4 5 6 7 8 9 10; do sleep 0.5; chmod 666 {0} && break; done'
#RuntimeDirectory=service #RuntimeDirectory=service
#RuntimeDirectoryMode=766", #RuntimeDirectoryMode=766",
socket_path.as_ref().to_str().unwrap() socket_path.as_ref().to_str().unwrap()

Loading…
Cancel
Save