From ee25ee5fe16a3fd12b6cab3f64837e7c09916f58 Mon Sep 17 00:00:00 2001 From: Fabian Schlenz Date: Tue, 2 Nov 2021 14:38:12 +0100 Subject: [PATCH] More commands. --- _get_container.sh | 13 +++++++++++++ dbash | 5 +++++ dhelp | 9 +++++++++ dlogs | 5 +++++ dlogsl | 5 +++++ dlr | 5 +++++ dsh | 5 +++++ dstats | 3 +++ ofelias.rb | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 9 files changed, 97 insertions(+) create mode 100644 _get_container.sh create mode 100755 dbash create mode 100755 dhelp create mode 100755 dlogs create mode 100755 dlogsl create mode 100755 dlr create mode 100755 dsh create mode 100755 dstats create mode 100755 ofelias.rb diff --git a/_get_container.sh b/_get_container.sh new file mode 100644 index 0000000..b328606 --- /dev/null +++ b/_get_container.sh @@ -0,0 +1,13 @@ +#!/bin/sh + +if [ -z "$1" ]; then + base=`realpath . --relative-to="/data/containers" | cut -d"/" -f1` + if [ "$base" = ".." -o "$base" = "." ]; then + echo "No container given and not within /data/containers" + exit 1 + fi + container="$base" + echo "Working on container $container." +else + container="$1" +fi diff --git a/dbash b/dbash new file mode 100755 index 0000000..3aedb55 --- /dev/null +++ b/dbash @@ -0,0 +1,5 @@ +#!/bin/sh + +. $(dirname $0)/_get_container.sh + +docker exec -it "$container" /bin/bash diff --git a/dhelp b/dhelp new file mode 100755 index 0000000..6e26feb --- /dev/null +++ b/dhelp @@ -0,0 +1,9 @@ +echo "Docker-Tools" +echo +echo "dps - Shows a tabular list of running containers." +echo "dlogs - Show latest log entries of container." +echo "dlogsl - Shows latest log entries using less." +echo "dup - Creates and runs containers from YAML files." +echo "dbash - Starts a bash shell in the container." +echo "dsh - Starts a sh shell in the container." +echo "dstats - Shows stats for all running containers." diff --git a/dlogs b/dlogs new file mode 100755 index 0000000..6cd8b4f --- /dev/null +++ b/dlogs @@ -0,0 +1,5 @@ +#!/bin/sh + +. $(dirname $0)/_get_container.sh + +docker logs --tail=100 -f "$container" diff --git a/dlogsl b/dlogsl new file mode 100755 index 0000000..468dce4 --- /dev/null +++ b/dlogsl @@ -0,0 +1,5 @@ +#!/bin/sh + +. $(dirname $0)/_get_container.sh + +docker logs -f "$container" 2>&1 | less diff --git a/dlr b/dlr new file mode 100755 index 0000000..57418f3 --- /dev/null +++ b/dlr @@ -0,0 +1,5 @@ +#!/bin/sh + +. $(dirname $0)/_get_container.sh + +docker restart "$container" && docker logs --tail=100 -f "$container" diff --git a/dsh b/dsh new file mode 100755 index 0000000..cfa44d6 --- /dev/null +++ b/dsh @@ -0,0 +1,5 @@ +#!/bin/sh + +. $(dirname $0)/_get_container.sh + +docker exec -it "$container" /bin/sh diff --git a/dstats b/dstats new file mode 100755 index 0000000..3fe7e6e --- /dev/null +++ b/dstats @@ -0,0 +1,3 @@ +#!/bin/bash + +echo docker stats $(docker inspect --format "{{.Name}}" $(docker ps -q)) diff --git a/ofelias.rb b/ofelias.rb new file mode 100755 index 0000000..d52939c --- /dev/null +++ b/ofelias.rb @@ -0,0 +1,47 @@ +#!/usr/bin/env ruby + +require 'socket' +require 'json' +require 'terminal-table' + +def fetch_data + socket = UNIXSocket.new("/var/run/docker.sock") + request = "GET /v1.24/containers/json?all=1 HTTP/1.0\r\n\r\n" + socket.write(request) + + response = "" + + loop do + break if socket.eof? + line = socket.gets + break if line=="\r\n" + end + + until socket.eof? + line = socket.gets + response += line + end + + return JSON.parse(response, symbolize_names: true) +end + +rows = [] +data = fetch_data +data.each do |d| + next unless labels = d[:Labels] + name = d[:Names][0][1..-1] + + next unless labels[:"ofelia.enabled"] + tasks = labels.keys.map(&:to_s).map{|s| s.scan(/\Aofelia\.job-([a-z]+)\.([^.]*)\./).first}.uniq.compact + next unless tasks.count>0 + tasks.each do |task| + type, taskname = *task + schedule = labels[:"ofelia.job-#{type}.#{taskname}.schedule"] + command = labels[:"ofelia.job-#{type}.#{taskname}.command"] + rows << [name, type, taskname, schedule, command] + end +end + +exit 0 unless rows.count>0 + +puts Terminal::Table.new(rows: rows, headings: ["Container", "Type", "Task", "Schedule", "Command"]) \ No newline at end of file