fix: something
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
use std::collections::HashMap;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::pin::Pin;
|
||||
use std::sync::Arc;
|
||||
|
||||
@@ -10,23 +11,37 @@ use tokio::sync::{Mutex, RwLock};
|
||||
use tokio::task::JoinHandle;
|
||||
use tracing::info;
|
||||
|
||||
use crate::config::DockerConfig;
|
||||
use crate::config::DaemonConfig;
|
||||
|
||||
type AttachedInput = Pin<Box<dyn AsyncWrite + Send>>;
|
||||
|
||||
pub(crate) struct CommandStreamHandle {
|
||||
/// Docker id of the container this stdin stream was opened against. A
|
||||
/// container that gets recreated (or restarted) keeps the same name but
|
||||
/// gets a new id, and writes to the old hijacked socket are silently
|
||||
/// swallowed — so the id is what makes a cached stream reusable.
|
||||
container_id: String,
|
||||
input: Mutex<AttachedInput>,
|
||||
drain_task: JoinHandle<()>,
|
||||
}
|
||||
|
||||
impl CommandStreamHandle {
|
||||
pub(crate) fn new(input: AttachedInput, drain_task: JoinHandle<()>) -> Self {
|
||||
pub(crate) fn new(
|
||||
container_id: String,
|
||||
input: AttachedInput,
|
||||
drain_task: JoinHandle<()>,
|
||||
) -> Self {
|
||||
Self {
|
||||
container_id,
|
||||
input: Mutex::new(input),
|
||||
drain_task,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn container_id(&self) -> &str {
|
||||
&self.container_id
|
||||
}
|
||||
|
||||
pub(crate) async fn write_all(&self, bytes: &[u8]) -> Result<()> {
|
||||
let mut input = self.input.lock().await;
|
||||
input.write_all(bytes).await?;
|
||||
@@ -44,13 +59,15 @@ impl CommandStreamHandle {
|
||||
pub struct DockerManager {
|
||||
client: Docker,
|
||||
network_name: String,
|
||||
data_root: PathBuf,
|
||||
host_data_root: PathBuf,
|
||||
command_streams: Arc<RwLock<HashMap<String, Arc<CommandStreamHandle>>>>,
|
||||
}
|
||||
|
||||
impl DockerManager {
|
||||
pub async fn new(config: &DockerConfig) -> Result<Self> {
|
||||
pub async fn new(config: &DaemonConfig) -> Result<Self> {
|
||||
let client = Docker::connect_with_socket(
|
||||
&config.socket,
|
||||
&config.docker.socket,
|
||||
120, // timeout
|
||||
bollard::API_DEFAULT_VERSION,
|
||||
)?;
|
||||
@@ -62,13 +79,25 @@ impl DockerManager {
|
||||
"Connected to Docker"
|
||||
);
|
||||
|
||||
let data_root = config.data_path.clone();
|
||||
let host_data_root = config.host_data_path();
|
||||
if data_root != host_data_root {
|
||||
info!(
|
||||
data_root = %data_root.display(),
|
||||
host_data_root = %host_data_root.display(),
|
||||
"Server data directories are bind-mounted from a different host path",
|
||||
);
|
||||
}
|
||||
|
||||
let manager = Self {
|
||||
client,
|
||||
network_name: config.network.clone(),
|
||||
network_name: config.docker.network.clone(),
|
||||
data_root,
|
||||
host_data_root,
|
||||
command_streams: Arc::new(RwLock::new(HashMap::new())),
|
||||
};
|
||||
|
||||
manager.ensure_network(&config.network_subnet).await?;
|
||||
manager.ensure_network(&config.docker.network_subnet).await?;
|
||||
|
||||
Ok(manager)
|
||||
}
|
||||
@@ -81,6 +110,32 @@ impl DockerManager {
|
||||
&self.network_name
|
||||
}
|
||||
|
||||
/// Translate a daemon-local server data directory into the path the Docker
|
||||
/// host must bind-mount. These differ when the daemon runs in a container.
|
||||
pub fn host_bind_source(&self, data_path: &Path) -> PathBuf {
|
||||
if self.data_root == self.host_data_root {
|
||||
return data_path.to_path_buf();
|
||||
}
|
||||
|
||||
match data_path.strip_prefix(&self.data_root) {
|
||||
Ok(relative) => self.host_data_root.join(relative),
|
||||
Err(_) => data_path.to_path_buf(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Inverse of [`Self::host_bind_source`]: turn a bind-mount source reported
|
||||
/// by Docker back into a path the daemon can read and write itself.
|
||||
pub fn daemon_data_path(&self, host_path: &Path) -> PathBuf {
|
||||
if self.data_root == self.host_data_root {
|
||||
return host_path.to_path_buf();
|
||||
}
|
||||
|
||||
match host_path.strip_prefix(&self.host_data_root) {
|
||||
Ok(relative) => self.data_root.join(relative),
|
||||
Err(_) => host_path.to_path_buf(),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn command_streams(&self) -> &Arc<RwLock<HashMap<String, Arc<CommandStreamHandle>>>> {
|
||||
&self.command_streams
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user