|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +# Return metadata expected by the Docker CLI plugin framework: https://github.com/docker/cli/pull/1564 |
| 5 | +# TODO: update ShortDescription and Version as needed. |
| 6 | +if [ "${1:-}" = "docker-cli-plugin-metadata" ]; then |
| 7 | + cat <<EOF |
| 8 | +{ |
| 9 | + "SchemaVersion": "0.1.0", |
| 10 | + "Vendor": "https://github.com/psviderski", |
| 11 | + "Version": "0.1.0", |
| 12 | + "ShortDescription": "Push Docker images to remote daemons via SSH using unregistry" |
| 13 | +} |
| 14 | +EOF |
| 15 | + exit 0 |
| 16 | +fi |
| 17 | + |
| 18 | +# Colors for output. |
| 19 | +RED='\033[0;31m' |
| 20 | +GREEN='\033[0;32m' |
| 21 | +YELLOW='\033[0;33m' |
| 22 | +NC='\033[0m' # no color |
| 23 | + |
| 24 | +info() { |
| 25 | + echo -e "${GREEN}[INFO]${NC} $1" |
| 26 | +} |
| 27 | + |
| 28 | +warning() { |
| 29 | + echo -e "${YELLOW}[WARNING]${NC} $1" |
| 30 | +} |
| 31 | + |
| 32 | +error() { |
| 33 | + echo -e "${RED}[ERROR]${NC} $1" >&2 |
| 34 | + exit 1 |
| 35 | +} |
| 36 | + |
| 37 | +usage() { |
| 38 | + echo "Usage: docker pussh [OPTIONS] IMAGE[:TAG] [USER@]HOST[:PORT]" |
| 39 | + echo "" |
| 40 | + echo "Upload a Docker image to a remote Docker daemon via SSH." |
| 41 | + echo "" |
| 42 | + echo "Options:" |
| 43 | + echo " -h, --help Show this help message." |
| 44 | + echo " -i, --ssh-key path Path to SSH private key for remote login (if not already added to SSH agent)." |
| 45 | + echo " --platform string Push a specific platform for a multi-platform image (e.g., linux/amd64, linux/arm64)." |
| 46 | + echo " Local Docker has to use containerd image store to support multi-platform images." |
| 47 | + echo "" |
| 48 | + echo "Examples:" |
| 49 | + echo " docker pussh myimage:latest user@host" |
| 50 | + echo " docker pussh --platform linux/amd64 myimage:latest host" |
| 51 | + echo " docker pussh myimage:latest user@host:2222 -i ~/.ssh/id_ed25519" |
| 52 | +} |
| 53 | + |
| 54 | +DOCKER_PLATFORM="" |
| 55 | +SSH_KEY="" |
| 56 | +IMAGE="" |
| 57 | +SSH_ADDRESS="" |
| 58 | + |
| 59 | +# Skip 'pussh' if called as Docker CLI plugin. |
| 60 | +if [ "${1:-}" = "pussh" ]; then |
| 61 | + shift |
| 62 | +fi |
| 63 | + |
| 64 | +# Parse options and arguments. |
| 65 | +help_command="Run 'docker pussh --help' for usage information." |
| 66 | +while [ $# -gt 0 ]; do |
| 67 | + case "$1" in |
| 68 | + -i|--ssh-key) |
| 69 | + if [ -z "${2:-}" ]; then |
| 70 | + error "-i/--ssh-key option requires an argument.\n${help_command}" |
| 71 | + fi |
| 72 | + SSH_KEY="$2" |
| 73 | + shift 2 |
| 74 | + ;; |
| 75 | + --platform) |
| 76 | + if [ -z "${2:-}" ]; then |
| 77 | + error "--platform option requires an argument.\n${help_command}" |
| 78 | + fi |
| 79 | + DOCKER_PLATFORM="$2" |
| 80 | + shift 2 |
| 81 | + ;; |
| 82 | + -h|--help) |
| 83 | + usage |
| 84 | + exit 0 |
| 85 | + ;; |
| 86 | + -*) |
| 87 | + error "unknown option: $1\n${help_command}" |
| 88 | + ;; |
| 89 | + *) |
| 90 | + # First non-option argument is the image. |
| 91 | + if [ -z "$IMAGE" ]; then |
| 92 | + IMAGE="$1" |
| 93 | + # Second non-option argument is the SSH address. |
| 94 | + elif [ -z "$SSH_ADDRESS" ]; then |
| 95 | + SSH_ADDRESS="$1" |
| 96 | + else |
| 97 | + error "too many arguments.\n${help_command}" |
| 98 | + fi |
| 99 | + shift |
| 100 | + ;; |
| 101 | + esac |
| 102 | +done |
| 103 | + |
| 104 | +# Validate required arguments. |
| 105 | +if [ -z "$IMAGE" ] || [ -z "$SSH_ADDRESS" ]; then |
| 106 | + error "IMAGE and HOST are required.\n${help_command}" |
| 107 | +fi |
| 108 | +# Validate SSH key file exists if provided. |
| 109 | +if [ -n "$SSH_KEY" ] && [ ! -f "$SSH_KEY" ]; then |
| 110 | + error "SSH key file not found: $SSH_KEY" |
| 111 | +fi |
0 commit comments