|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# docker_info_json.sh - 시놀로지 환경에서 sudo docker를 사용하여 |
| 4 | +# 컨테이너/이미지 정보를 JSON 형태로 ONLY 출력하는 스크립트 |
| 5 | +# |
| 6 | +# chkconfig: - 60 30 |
| 7 | +# description: Docker info as JSON |
| 8 | +# |
| 9 | + |
| 10 | +set -euo pipefail |
| 11 | +IFS=$'\n\t' |
| 12 | + |
| 13 | +DOCKER_CMD="sudo docker" |
| 14 | + |
| 15 | +# jq가 설치되어 있는지 확인 |
| 16 | +if ! command -v jq &> /dev/null; then |
| 17 | + echo '{"error": "jq is not installed. Please install jq to use this script."}' |
| 18 | + exit 1 |
| 19 | +fi |
| 20 | + |
| 21 | +# Helper Functions |
| 22 | + |
| 23 | +# 에러 처리 함수 |
| 24 | +error_exit() { |
| 25 | + local message="$1" |
| 26 | + echo "{\"error\": \"${message}\"}" |
| 27 | + exit 1 |
| 28 | +} |
| 29 | + |
| 30 | +# 컨테이너 존재 여부 확인 |
| 31 | +validate_container() { |
| 32 | + local container_name="$1" |
| 33 | + if ! ${DOCKER_CMD} ps -a --format '{{.Names}}' | grep -wq "${container_name}"; then |
| 34 | + error_exit "Container '${container_name}' does not exist." |
| 35 | + fi |
| 36 | +} |
| 37 | + |
| 38 | +# 이미지 존재 여부 확인 |
| 39 | +validate_image() { |
| 40 | + local image_name="$1" |
| 41 | + if ! ${DOCKER_CMD} images --format '{{.Repository}}:{{.Tag}}' | grep -wq "${image_name}"; then |
| 42 | + error_exit "Image '${image_name}' does not exist." |
| 43 | + fi |
| 44 | +} |
| 45 | + |
| 46 | +# 컨테이너 정보 (docker inspect <container_name> [OPTIONS]) |
| 47 | +show_container_info() { |
| 48 | + local container_name="$1" |
| 49 | + shift 1 |
| 50 | + local inspect_options=("$@") |
| 51 | + |
| 52 | + if [ -z "${container_name}" ]; then |
| 53 | + error_exit "No container name specified." |
| 54 | + fi |
| 55 | + |
| 56 | + validate_container "${container_name}" |
| 57 | + |
| 58 | + ${DOCKER_CMD} inspect "${inspect_options[@]}" "${container_name}" | jq '.[0]' |
| 59 | +} |
| 60 | + |
| 61 | +# 이미지 정보 (docker inspect <image_name> [OPTIONS]) |
| 62 | +show_image_info() { |
| 63 | + local image_name="$1" |
| 64 | + shift 1 |
| 65 | + local inspect_options=("$@") |
| 66 | + |
| 67 | + if [ -z "${image_name}" ]; then |
| 68 | + error_exit "No image name specified." |
| 69 | + fi |
| 70 | + |
| 71 | + validate_image "${image_name}" |
| 72 | + |
| 73 | + ${DOCKER_CMD} inspect "${inspect_options[@]}" "${image_name}" | jq '.[0]' |
| 74 | +} |
| 75 | + |
| 76 | +# 모든 컨테이너 목록 (docker ps [OPTIONS]) |
| 77 | +list_containers() { |
| 78 | + local ps_options=("$@") |
| 79 | + ${DOCKER_CMD} ps "${ps_options[@]}" --format '{{json .}}' | jq -s . |
| 80 | +} |
| 81 | + |
| 82 | +# 모든 이미지 목록 (docker images [OPTIONS]) |
| 83 | +list_images() { |
| 84 | + local images_options=("$@") |
| 85 | + ${DOCKER_CMD} images "${images_options[@]}" --format '{{json .}}' | jq -s . |
| 86 | +} |
| 87 | + |
| 88 | +# 컨테이너 로그 (docker logs <container_name> [OPTIONS]) |
| 89 | +show_container_logs() { |
| 90 | + local container_name="$1" |
| 91 | + shift 1 |
| 92 | + local log_options=("$@") |
| 93 | + |
| 94 | + if [ -z "${container_name}" ]; then |
| 95 | + error_exit "No container name specified for logs." |
| 96 | + fi |
| 97 | + |
| 98 | + validate_container "${container_name}" |
| 99 | + |
| 100 | + # 로그 명령어 실행 |
| 101 | + logs=$(${DOCKER_CMD} logs "${log_options[@]}" "${container_name}" 2>&1 || true) |
| 102 | + |
| 103 | + # 로그를 JSON 배열로 변환 |
| 104 | + echo "$logs" | jq -R -s -c 'split("\n") | map(select(length > 0))' |
| 105 | +} |
| 106 | + |
| 107 | +# 시스템 정보 (docker info [OPTIONS]) |
| 108 | +show_system_info() { |
| 109 | + local info_options=("$@") |
| 110 | + ${DOCKER_CMD} info "${info_options[@]}" --format '{{json .}}' | jq '.' |
| 111 | +} |
| 112 | + |
| 113 | +# 메인 로직 |
| 114 | +main() { |
| 115 | + if [ $# -lt 1 ]; then |
| 116 | + error_exit "No command specified. Use one of [container, image, ps, images, logs, system]." |
| 117 | + fi |
| 118 | + |
| 119 | + case "$1" in |
| 120 | + container) |
| 121 | + if [ $# -lt 2 ]; then |
| 122 | + error_exit "Usage: $0 container <container_name> [OPTIONS]" |
| 123 | + fi |
| 124 | + local container_name="$2" |
| 125 | + shift 2 |
| 126 | + show_container_info "${container_name}" "$@" |
| 127 | + ;; |
| 128 | + image) |
| 129 | + if [ $# -lt 2 ]; then |
| 130 | + error_exit "Usage: $0 image <image_name> [OPTIONS]" |
| 131 | + fi |
| 132 | + local image_name="$2" |
| 133 | + shift 2 |
| 134 | + show_image_info "${image_name}" "$@" |
| 135 | + ;; |
| 136 | + ps) |
| 137 | + # 'ps'는 추가 옵션 없이도 사용 가능 |
| 138 | + shift 1 |
| 139 | + list_containers "$@" |
| 140 | + ;; |
| 141 | + images) |
| 142 | + # 'images'는 추가 옵션 없이도 사용 가능 |
| 143 | + shift 1 |
| 144 | + list_images "$@" |
| 145 | + ;; |
| 146 | + logs) |
| 147 | + if [ $# -lt 2 ]; then |
| 148 | + error_exit "Usage: $0 logs <container_name> [OPTIONS]" |
| 149 | + fi |
| 150 | + local container_name="$2" |
| 151 | + shift 2 |
| 152 | + show_container_logs "${container_name}" "$@" |
| 153 | + ;; |
| 154 | + system) |
| 155 | + # 'system'은 추가 옵션 없이도 사용 가능 |
| 156 | + shift 1 |
| 157 | + show_system_info "$@" |
| 158 | + ;; |
| 159 | + help|-h|--help) |
| 160 | + cat <<EOF |
| 161 | +Usage: $0 COMMAND [OPTIONS] |
| 162 | +
|
| 163 | +Commands: |
| 164 | + container <container_name> [OPTIONS] Show detailed information of a specific container. |
| 165 | + image <image_name> [OPTIONS] Show detailed information of a specific image. |
| 166 | + ps [OPTIONS] List containers with optional flags. |
| 167 | + images [OPTIONS] List images with optional flags. |
| 168 | + logs <container_name> [OPTIONS] Show logs of a specific container with optional flags. |
| 169 | + system [OPTIONS] Show Docker system-wide information with optional flags. |
| 170 | + help Show this help message. |
| 171 | +
|
| 172 | +Examples: |
| 173 | + $0 ps |
| 174 | + $0 ps -a |
| 175 | + $0 container my_container --format '{{.Name}}: {{.Status}}' |
| 176 | + $0 logs my_container --tail 50 |
| 177 | + $0 logs my_container -f |
| 178 | + $0 system |
| 179 | +EOF |
| 180 | + ;; |
| 181 | + *) |
| 182 | + error_exit "Invalid command '${1}'. Use one of [container, image, ps, images, logs, system]." |
| 183 | + ;; |
| 184 | + esac |
| 185 | +} |
| 186 | + |
| 187 | +main "$@" |
| 188 | + |
| 189 | +exit 0 |
0 commit comments