diff --git a/README.md b/README.md index 74e28f8..4d33a48 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,67 @@ # dnuke -Ruby script for conveniently nuking Docker containers and volumes by project name +Ruby script for conveniently nuking Docker containers and volumes by name + +### Usage +Pass the name(s) of your containers / volumes to dnuke: + +`dnuke project1` + +Your output should look something like this: + +```bash +$ dnuke project1 + +I, the mighty dnuke, was passed: project1 + +removing containers that match: project1 +container a0123456789b was removed +container b9876543210a was removed + +removing volumes that match: project1 +volume project1_postgres was removed + +-------------------------------------------------------------------- +``` + +You can pass multiple container / volume names as arguments: + +```bash +$ dnuke project1 project2 + +I, the mighty dnuke, was passed: project1 + +removing containers that match: project1 +container a0123456789b was removed +container b9876543210a was removed + +removing volumes that match: project1 +volume project1_postgres was removed + +-------------------------------------------------------------------- + +I, the mighty dnuke, was passed: project2 + +removing containers that match: project2 +container b9876543210a was removed +container a0123456789b was removed + +removing volumes that match: project2 +volume project2_postgres was removed + +-------------------------------------------------------------------- +``` + +dnuke uses fuzzy matching when searching your local Docker containers and volumes: + +```bash +$ dnuke cat + +I, the mighty dnuke, was passed: cat + +... + +removing volumes that match: cat +volume caterpillar_postgres was removed + +-------------------------------------------------------------------- +``` diff --git a/dnuke b/dnuke new file mode 100755 index 0000000..8c5501e --- /dev/null +++ b/dnuke @@ -0,0 +1,55 @@ +#!/usr/bin/env ruby + +# TODO: make sure no matching containers are +# running before attempting removal +def remove_containers(args) + puts "\n" + containers = `docker ps -aqf name=#{args}` # String + containers = containers.split(/\n/) # Array + unless containers.empty? + puts "removing containers that match: #{args}" + # TODO: remove false message of "container x was removed" + # when container x wasn't actually removed + # e.g. if the container is still running + containers.map do |c| + `docker container rm #{c}` + puts "container #{c} was removed" + end + else + puts "no containers removed: no containers match #{args}" + end +end + +def remove_volumes(args) + puts "\n" + volumes = `docker volume ls` # String + volumes = volumes.squeeze(' ').chop + volumes = volumes.split(/\nlocal /) # Array + volumes = volumes.map do |v| + if v.match(args) + v + end + end + volumes = volumes.compact # remove any lingering nil values + unless volumes.empty? + puts "removing volumes that match: #{args}" + volumes.map do |v| + `docker volume rm #{v}` + puts "volume #{v} was removed" + end + else + puts "no volumes removed: no volumes match #{args}" + end +end + +unless ARGV.length == 0 + ARGV.each do |value| + puts "\n" + puts "I, the mighty " + File.basename(__FILE__) + ", was passed: " + value + remove_containers(value) + remove_volumes(value) + puts "\n--------------------------------------------------------------------" + end +else + puts "Please pass an argument" +end