Skip to content

Commit

Permalink
Merge branch 'refactor_use_cache_image'
Browse files Browse the repository at this point in the history
  • Loading branch information
eZanmoto committed Aug 20, 2022
2 parents f3aadfd + ccbe17a commit 69a1dcb
Show file tree
Hide file tree
Showing 11 changed files with 244 additions and 143 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ license = "MIT"
name = "dock"
readme = "README.md"
repository = "https://github.com/ezanmoto/dock"
version = "0.35.5"
version = "0.35.6"
edition = "2021"

[dependencies]
Expand Down
33 changes: 25 additions & 8 deletions scripts/clean_images.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
# Use of this source code is governed by an MIT
# licence that can be found in the LICENCE file.

# `$0 <prefix>` removes all Docker images whose name starts with `prefix`.
# `$0 <prefix>` removes all Docker images whose name starts with `prefix`,
# and removes any containers created from them.

set -o errexit

Expand All @@ -15,15 +16,31 @@ fi

prefix="$1"

images=$(
tagged_images="$(
docker images \
| grep \
"$prefix" \
| grep "$prefix" \
| sed 's/ \+/ /g' \
| cut \
--delimiter=' ' \
--field=1
)
--field=1-2 \
| sed 's/ /:/'
)"

if [ ! -z "$images" ] ; then
docker rmi $images
if [ ! -z "$tagged_images" ] ; then
for img in $tagged_images ; do
cont_ids="$(
docker ps \
--all \
--filter=ancestor="$img" \
--quiet
)"

if [ ! -z "$cont_ids" ] ; then
docker rm \
--force \
$cont_ids
fi
done

docker rmi $tagged_images
fi
26 changes: 0 additions & 26 deletions src/docker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use std::ffi::OsStr;
use std::fmt::Debug;
use std::io::BufRead;
use std::io::Error as IoError;
use std::process::Command;
use std::process::ExitStatus;
Expand Down Expand Up @@ -70,28 +69,3 @@ pub enum StreamRunError {
#[snafu(display("Couldn't wait for the `docker` process: {}", source))]
WaitFailed{source: IoError},
}

pub fn get_image_ids(repository: &str)
-> Result<Vec<String>, GetImageIdsError>
{
let output = assert_run(&["images", "--quiet", repository])
.context(ViewImagesFailed)?;

let mut ids = vec![];
for line in output.stdout.lines() {
let id = line
.context(ReadStdoutLineFailed)?;

ids.push(id);
}

Ok(ids)
}

#[derive(Debug, Snafu)]
pub enum GetImageIdsError {
#[snafu(display("Couldn't get Docker image IDs: {}", source))]
ViewImagesFailed{source: AssertRunError},
#[snafu(display("Couldn't read line of `docker` STDOUT: {}", source))]
ReadStdoutLineFailed{source: IoError},
}
69 changes: 63 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,15 @@ use init::FileActionLogger;
use init::InitError;
use run_in::Args;
use run_in::CmdLoggers;
use run_in::Rebuild;
use run_in::RebuildAction;
use run_in::RebuildForRunInError;
use run_in::RunInError;
use run_in::SwitchingCmdLogger;

const DEFAULT_TEMPLATES_SOURCE: &str = env!("DOCK_DEFAULT_TEMPLATES_SOURCE");

const CACHE_TAG_FLAG: &str = "cache-tag";
const TAGGED_IMG_FLAG: &str = "tagged-image";
const COMMAND_ARGS_FLAG: &str = "docker-args";
const ENV_FLAG: &str = "env";
Expand All @@ -57,6 +59,9 @@ const SKIP_REBUILD_FLAG: &str = "skip-rebuild";
const SOURCE_FLAG: &str = "source";
const TEMPLATE_FLAG: &str = "template";

const DEFAULT_CACHE_TAG: &str = "cached";

#[allow(clippy::too_many_lines)]
fn main() {
let dock_file_name = "dock.yaml";

Expand All @@ -72,6 +77,14 @@ fn main() {
);
let init_about: &str =
"Initialise the current directory with a Dock environment";
let cache_tag_long_help: &str = &format!(
"The tag to use for the image that will be replaced by the rebuild. \
If an image with the tagged name `{tagged_img_flag}` exists then its \
tag will be replaced by `{cache_tag_flag}` for the duration of the \
rebuild.",
tagged_img_flag = TAGGED_IMG_FLAG,
cache_tag_flag = CACHE_TAG_FLAG,
);

let args =
Command::new("dock")
Expand All @@ -83,6 +96,11 @@ fn main() {
.trailing_var_arg(true)
.about(rebuild_about)
.args(&[
Arg::new(CACHE_TAG_FLAG)
.long(CACHE_TAG_FLAG)
.default_value(DEFAULT_CACHE_TAG)
.help("The tag for the cache image")
.long_help(cache_tag_long_help),
Arg::new(TAGGED_IMG_FLAG)
.required(true)
.help("The tagged name for the new image")
Expand All @@ -98,6 +116,11 @@ fn main() {
.trailing_var_arg(true)
.about(run_about)
.args(&[
Arg::new(CACHE_TAG_FLAG)
.long(CACHE_TAG_FLAG)
.default_value(DEFAULT_CACHE_TAG)
.help("The tag for the cache image")
.long_help(cache_tag_long_help),
Arg::new(DEBUG_FLAG)
.short('D')
.long(DEBUG_FLAG)
Expand All @@ -120,6 +143,7 @@ fn main() {
Command::new("shell")
.about(shell_about)
.args(&[
// TODO Add support for `cache-tag` flag.
Arg::new(DEBUG_FLAG)
.short('D')
.long(DEBUG_FLAG)
Expand Down Expand Up @@ -163,9 +187,12 @@ fn handle_arg_matches(args: &ArgMatches, dock_file_name: &str) {
Some(vs) => vs.collect(),
None => vec![],
};
let target_img = sub_args.value_of(TAGGED_IMG_FLAG).unwrap();

let exit_code = rebuild(target_img, &docker_args);
let exit_code = rebuild(
sub_args.value_of(TAGGED_IMG_FLAG).unwrap(),
sub_args.value_of(CACHE_TAG_FLAG).unwrap(),
&docker_args,
);
process::exit(exit_code);
},
Some(("run-in", sub_args)) => {
Expand Down Expand Up @@ -196,13 +223,34 @@ fn handle_arg_matches(args: &ArgMatches, dock_file_name: &str) {
}
}

fn rebuild(target_img: &str, docker_args: &[&str]) -> i32 {
fn rebuild(target_img: &str, cache_tag: &str, docker_args: &[&str]) -> i32 {
if let Some(i) = index_of_first_unsupported_flag(docker_args) {
eprintln!("unsupported argument: `{}`", docker_args[i]);
return 1;
}

match rebuild::rebuild_with_streaming_output(target_img, docker_args) {
let target_img_parts =
target_img.split(':').collect::<Vec<&str>>();

let img_name =
if let [name, _tag] = target_img_parts.as_slice() {
name
} else {
eprintln!(
"`{}` must contain exactly one `:`",
TAGGED_IMG_FLAG,
);
return 1;
};

let cache_img = new_tagged_img_name(img_name, cache_tag);

let rebuild_result = rebuild::rebuild_with_streaming_output(
target_img,
&cache_img,
docker_args,
);
match rebuild_result {
Ok(exit_status) => {
exit_code_from_exit_status(exit_status)
},
Expand All @@ -214,6 +262,10 @@ fn rebuild(target_img: &str, docker_args: &[&str]) -> i32 {
}
}

fn new_tagged_img_name(img_name: &str, tag: &str) -> String {
format!("{}:{}", img_name, tag)
}

fn exit_code_from_exit_status(status: ExitStatus) -> i32 {
if let Some(code) = status.code() {
code
Expand Down Expand Up @@ -258,16 +310,19 @@ fn run_in(dock_file_name: &str, arg_matches: &ArgMatches) -> i32 {
docker_args.push("--tty");
}

let cache_tag = arg_matches.value_of(CACHE_TAG_FLAG).unwrap();

let args = &Args{docker: &docker_args, command: &cmd_args};

handle_run_in(dock_file_name, Some(arg_matches), args, None)
handle_run_in(dock_file_name, Some(arg_matches), args, None, cache_tag)
}

fn handle_run_in(
dock_file_name: &str,
arg_matches: Option<&ArgMatches>,
args: &Args,
shell: Option<PathBuf>,
cache_tag: &str,
) -> i32 {
let mut debug = false;
let mut env_name = None;
Expand Down Expand Up @@ -315,6 +370,7 @@ fn handle_run_in(
};

let mut logger = SwitchingCmdLogger::new(loggers);

let result = run_in::run_in(
&mut logger,
// TODO We would ideally lock and pass the `stdio` for the current
Expand All @@ -323,7 +379,7 @@ fn handle_run_in(
Stdio::inherit(),
dock_file_name,
env_name,
&rebuild_action,
&Rebuild{action: rebuild_action, cache_tag: cache_tag.to_string()},
args,
shell,
);
Expand Down Expand Up @@ -393,6 +449,7 @@ fn shell(dock_file_name: &str, args: Option<&ArgMatches>) -> i32 {
command: &[],
},
Some(Path::new("/bin/sh").to_path_buf()),
DEFAULT_CACHE_TAG,
)
}

Expand Down

0 comments on commit 69a1dcb

Please sign in to comment.