Skip to content

Commit

Permalink
feat: config option for url to connect to docker with
Browse files Browse the repository at this point in the history
  • Loading branch information
VirtCode committed May 7, 2024
1 parent c3a2249 commit be8e04c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
4 changes: 4 additions & 0 deletions server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ RUNNER_PREFIX=serene-aur-runner-
# do not require authentication for the read-only parts of the api
ALLOW_READS=false

# DEBUG the unix or tcp url to docker with a prefix (e.g. tcp://127.0.0.1:2375)
# the runner containers will be spun up on this docker instance
DOCKER_URL=unix:///var/run/docker.sock

# DEBUG the port the server binds to in the container
PORT=80

Expand Down
8 changes: 7 additions & 1 deletion server/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@ pub struct Config {
pub container_prefix: String,
/// runner docker image
pub runner_image: String,
/// custom url for docker instance to use
pub docker_url: Option<String>,
/// port to bind to
pub port: u16,
/// build the cli by default
pub build_cli: bool,
/// url to reach itself to pull dependencies from itself
/// url for runners to reach the server to pull dependencies from its repo
pub own_repository_url: Option<String>,
}

Expand All @@ -50,6 +52,8 @@ impl Default for Config {

container_prefix: "serene-aur-runner-".to_string(),
runner_image: "ghcr.io/virtcode/serene-aur-runner:main".to_string(),

docker_url: None,

port: 80,
build_cli: true,
Expand Down Expand Up @@ -77,6 +81,8 @@ impl Config {

container_prefix: env::var("RUNNER_PREFIX").unwrap_or(default.container_prefix),
runner_image: env::var("RUNNER_IMAGE").unwrap_or(default.runner_image),

docker_url: env::var("DOCKER_URL").ok().or(default.docker_url),

port: env::var("PORT").ok()
.and_then(|s| u16::from_str(&s).map_err(|_| warn!("failed to parse PORT, using default")).ok())
Expand Down
24 changes: 21 additions & 3 deletions server/src/runner/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ use std::io::Read;
use anyhow::{Context};
use async_tar::Archive;
use bollard::container::{Config, CreateContainerOptions, DownloadFromContainerOptions, ListContainersOptions, LogsOptions, StartContainerOptions, UploadToContainerOptions, WaitContainerOptions};
use bollard::Docker;
use bollard::{API_DEFAULT_VERSION, Docker};
use bollard::image::CreateImageOptions;
use chrono::{DateTime, Utc};
use futures_util::{AsyncRead, StreamExt, TryStreamExt};
use hyper::body::HttpBody;
use log::{info, warn};
use serde::{Deserialize, Serialize};
use tokio_util::io::StreamReader;
use tokio_util::compat::{TokioAsyncReadCompatExt};
Expand Down Expand Up @@ -40,9 +41,26 @@ impl Runner {

/// creates a new runner by taking the docker from the default socket
pub fn new() -> anyhow::Result<Self> {
let docker = if let Some(url) = &CONFIG.docker_url {

if url.starts_with("tcp://") || url.starts_with("http://") {

info!("using docker via tcp at '{url}'");
Docker::connect_with_http(url, 120, API_DEFAULT_VERSION)

} else {
if !url.starts_with("unix://") { warn!("missing docker url scheme, assuming path to unix socket"); }

info!("using docker via unix socket at '{url}'");
Docker::connect_with_unix(url, 120, API_DEFAULT_VERSION)
}
} else {
info!("using docker via the default unix socket");
Docker::connect_with_unix_defaults()
};

Ok(Self {
docker: Docker::connect_with_socket_defaults()
.context("failed to connect to docker via default socket")?
docker: docker.context("failed to initialize docker")?
})
}

Expand Down

0 comments on commit be8e04c

Please sign in to comment.