Skip to content
This repository has been archived by the owner on Jul 2, 2024. It is now read-only.

Commit

Permalink
Merge pull request #37 from tsirysndr/fix/docker-pull
Browse files Browse the repository at this point in the history
fix(docker): pull image if not present, setup project's network
  • Loading branch information
tsirysndr authored May 13, 2023
2 parents 73410ce + 9c9014f commit f198126
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 12 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ hcl-rs = "0.12.3"
indexmap = {version = "1.9.2", features = ["serde"]}
inquire = "0.6.2"
local-ip-addr = "0.1.1"
md5 = "0.7.0"
mime_guess = "2.0.4"
names = "0.14.0"
nix = "0.26.2"
Expand Down
23 changes: 15 additions & 8 deletions examples/nginx-nodejs-redis/Superfile.hcl
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
project = "nginx-nodejs-redis"

service "nodejs" {
type = "exec"
type = "docker"
command = "npm start"
working_dir = "./web"
description = "Ping Service Example"
depends_on = ["redis"]
wait_for = ["redis"]
env = {
REDIS_HOST = "localhost"
REDIS_HOST = "redis"
}
autostart = true
autorestart = false
Expand All @@ -20,13 +20,15 @@ service "nodejs" {
command = "npm install"
}

use "flox" {
environment = ".#nginx-nodejs-redis"
}
#use "flox" {
# environment = ".#nginx-nodejs-redis"
#}

use "docker" { }
}

service "redis" {
type = "exec"
type = "docker"
command = "redis-server"
stop_command = "redis-cli shutdown"
working_dir = "."
Expand All @@ -40,7 +42,12 @@ service "redis" {
stderr = "/tmp/redis-stderr.log"
port = 6379

use "flox" {
environment = ".#nginx-nodejs-redis"
#use "flox" {
# environment = ".#nginx-nodejs-redis"
#}

use "docker" {
image = "redislabs/redismod:edge"
ports = ["6379:6379"]
}
}
1 change: 1 addition & 0 deletions examples/nginx-nodejs-redis/web/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
2 changes: 1 addition & 1 deletion examples/nginx-nodejs-redis/web/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ FROM node:14.17.3-alpine3.14
WORKDIR /usr/src/app

COPY package.json package-lock.json ./
RUN npm ci
RUN npm install
COPY ./server.js ./

CMD ["npm","start"]
68 changes: 65 additions & 3 deletions src/superviseur/drivers/docker/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ use owo_colors::OwoColorize;
use shiplift::{
rep::ContainerDetails,
tty::{self, TtyChunk},
ContainerConnectionOptions, ContainerOptions, Docker, LogsOptions,
ContainerConnectionOptions, ContainerOptions, Docker, LogsOptions, NetworkCreateOptions,
PullOptions,
};
use tokio::sync::mpsc;
use tokio_stream::StreamExt;
Expand Down Expand Up @@ -105,10 +106,51 @@ impl Driver {
self.docker
.networks()
.get(&network.id)
.connect(&ContainerConnectionOptions::builder(container.id()).build())
.connect(
&ContainerConnectionOptions::builder(container.id())
.aliases(vec![&self.service.name])
.build(),
)
.await?;
}
}
if cfg.networks.clone().unwrap_or(Vec::new()).len() == 0 {
// create a network
let project_hash = format!("{:x}", md5::compute(&self.context));
let network_name = format!("{}_{}", self.project, project_hash);
// verify if network exists
match self.docker.networks().get(&network_name).inspect().await {
Ok(network) => {
// network exists
self.docker
.networks()
.get(&network.id)
.connect(
&ContainerConnectionOptions::builder(container.id())
.aliases(vec![&self.service.name])
.build(),
)
.await?;
}
Err(_) => {
// network does not exist
let network = self
.docker
.networks()
.create(&NetworkCreateOptions::builder(&network_name).build())
.await?;
self.docker
.networks()
.get(&network.id)
.connect(
&ContainerConnectionOptions::builder(container.id())
.aliases(vec![&self.service.name])
.build(),
)
.await?;
}
}
}
}
None => {}
};
Expand Down Expand Up @@ -138,11 +180,31 @@ impl Driver {
}

async fn build_image(&self, project: String) -> anyhow::Result<()> {
if self.config.as_ref().unwrap().image.is_some() {
if let Some(img) = &self.config.as_ref().unwrap().image {
println!(
"-> Skipping {} build, using image from config",
self.service.name.bright_green()
);
let mut stream = self
.docker
.images()
.pull(&PullOptions::builder().image(img).build());

while let Some(pull_result) = stream.next().await {
match pull_result {
Ok(output) => {
print!("\r");
print!(
"{} {} {}",
output["id"], output["status"], output["progress"]
)
}
Err(e) => eprintln!("Error: {}", e),
}
}

println!("");

return Ok(());
}
println!(
Expand Down

0 comments on commit f198126

Please sign in to comment.