1
0
Fork 0
mirror of https://codeberg.org/icewind/haze.git synced 2026-06-03 17:14:08 +02:00

allow specifying extra mappings in config

This commit is contained in:
Robin Appelman 2021-04-21 20:14:03 +02:00
commit b4aec684c6
4 changed files with 56 additions and 6 deletions

View file

@ -109,4 +109,14 @@ work_dir = "/path/to/temp/dir" # path to temporary directory. optional, defaults
enabled = false # whether or not to automatically install nextcloud on `haze start`. optional, defaults to false
username = "foo" # username for admin user during auto setup. optional, defaults to "admin"
password = "bar" # password for admin user during auto setup. optional, defaults to "admin"
[[volume]]
source = "/tmp/haze-shared"
target = "/shared"
create = true
[[volume]]
source = "/home/me/Downloads"
target = "/Downloads"
read_only = true
```

View file

@ -1,7 +1,7 @@
use crate::config::HazeConfig;
use crate::database::Database;
use crate::exec::{exec, exec_tty};
use crate::mapping::default_mappings;
use crate::mapping::{default_mappings, Mapping};
use crate::php::PhpVersion;
use crate::service::Service;
use bollard::container::{ListContainersOptions, RemoveContainerOptions};
@ -130,7 +130,12 @@ impl Cloud {
let id = format!("haze-{}", petname(2, "-"));
let workdir = config.work_dir.join(&id);
let mappings = default_mappings();
let mappings = config
.volume
.iter()
.map(Mapping::from)
.chain(default_mappings())
.collect::<Vec<_>>();
for mapping in &mappings {
mapping
.create(&id, config)

View file

@ -11,6 +11,8 @@ pub struct HazeConfig {
pub work_dir: Utf8PathBuf,
#[serde(default)]
pub auto_setup: HazeAutoSetupConfig,
#[serde(default)]
pub volume: Vec<HazeVolumeConfig>,
}
#[derive(Debug, Deserialize)]
@ -44,6 +46,16 @@ fn default_auto_setup_password() -> String {
"admin".to_string()
}
#[derive(Debug, Deserialize)]
pub struct HazeVolumeConfig {
pub source: Utf8PathBuf,
pub target: Utf8PathBuf,
#[serde(default)]
pub read_only: bool,
#[serde(default)]
pub create: bool,
}
impl HazeConfig {
pub fn load() -> Result<Self> {
let dirs = ProjectDirs::from("nl", "icewind", "haze").unwrap();

View file

@ -1,4 +1,4 @@
use crate::config::HazeConfig;
use crate::config::{HazeConfig, HazeVolumeConfig};
use camino::Utf8Path;
use color_eyre::Result;
use tokio::fs::{create_dir_all, write};
@ -68,6 +68,7 @@ impl<'a> Mapping<'a> {
MappingSourceType::WorkDir => config.work_dir.join(id).join(self.source),
MappingSourceType::GlobalWorkDir => config.work_dir.join(self.source),
MappingSourceType::Sources => return Ok(()),
MappingSourceType::Absolute => self.source.into(),
};
match self.mapping_type {
MappingType::Folder => create_dir_all(source).await?,
@ -85,6 +86,7 @@ impl<'a> Mapping<'a> {
MappingSourceType::WorkDir => config.work_dir.join(id).join(self.source),
MappingSourceType::GlobalWorkDir => config.work_dir.join(self.source),
MappingSourceType::Sources => config.sources_root.join(self.source),
MappingSourceType::Absolute => self.source.into(),
};
Some(if self.read_only {
format!("{}:{}:ro", source, self.target)
@ -94,10 +96,10 @@ impl<'a> Mapping<'a> {
}
}
pub fn default_mappings() -> Vec<Mapping<'static>> {
pub fn default_mappings<'a>() -> impl IntoIterator<Item = Mapping<'a>> {
use MappingSourceType::*;
vec![
let mappings = [
Mapping::new(Sources, "", "/var/www/html"),
Mapping::new(WorkDir, "data", "/var/www/html/data"),
Mapping::new(WorkDir, "config", "/var/www/html/config"),
@ -149,7 +151,8 @@ pub fn default_mappings() -> Vec<Mapping<'static>> {
Mapping::new(Sources, ".htaccess", "/var/www/html/.htaccess")
.file()
.read_only(),
]
];
std::array::IntoIter::new(mappings)
}
#[derive(Debug, Copy, Clone)]
@ -157,6 +160,7 @@ pub enum MappingSourceType {
Sources,
WorkDir,
GlobalWorkDir,
Absolute,
}
#[derive(Debug, Copy, Clone)]
@ -164,3 +168,22 @@ pub enum MappingType {
Folder,
File,
}
impl<'a> From<&'a HazeVolumeConfig> for Mapping<'a> {
fn from(config: &'a HazeVolumeConfig) -> Self {
let ty = if config.source.is_dir() {
MappingType::Folder
} else {
MappingType::File
};
Mapping {
source_type: MappingSourceType::Absolute,
source: config.source.as_path(),
target: config.target.as_path(),
mapping_type: ty,
read_only: config.read_only,
map: true,
create: config.create,
}
}
}