container 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #!/bin/bash
  2. set -eu
  3. STATE_DIR="${FAKE_CONTAINER_STATE:?FAKE_CONTAINER_STATE is required}"
  4. mkdir -p \
  5. "${STATE_DIR}/containers" \
  6. "${STATE_DIR}/running" \
  7. "${STATE_DIR}/networks" \
  8. "${STATE_DIR}/network-subnets" \
  9. "${STATE_DIR}/volumes" \
  10. "${STATE_DIR}/unowned/container" \
  11. "${STATE_DIR}/unowned/network" \
  12. "${STATE_DIR}/unowned/volume"
  13. list_names() {
  14. local directory=$1
  15. local path
  16. for path in "${directory}"/*; do
  17. [[ -e "${path}" ]] || continue
  18. basename "${path}"
  19. done
  20. }
  21. last_argument() {
  22. local value=""
  23. for value in "$@"; do :; done
  24. printf '%s\n' "${value}"
  25. }
  26. inspect_resource() {
  27. local resource_type=$1
  28. local resource_name=$2
  29. local label_value="apple-container"
  30. local address="192.168.65.4/24"
  31. local subnet="192.168.65.0/24"
  32. if [[ -e "${STATE_DIR}/unowned/${resource_type}/${resource_name}" ]]; then
  33. label_value="other"
  34. fi
  35. if [[ "${resource_type}" == "network" ]]; then
  36. if [[ -s "${STATE_DIR}/network-subnets/${resource_name}" ]]; then
  37. subnet="$(<"${STATE_DIR}/network-subnets/${resource_name}")"
  38. fi
  39. printf '[{"configuration":{"ipv4Subnet":"%s","labels":{"org.sub2api.stack":"%s"}}}]\n' \
  40. "${subnet}" "${label_value}"
  41. return
  42. fi
  43. case "${resource_name}" in
  44. sub2api-apple-postgres) address="192.168.65.2/24" ;;
  45. sub2api-apple-redis) address="192.168.65.3/24" ;;
  46. esac
  47. printf '[{"configuration":{"labels":{"org.sub2api.stack":"%s"}},"status":{"networks":[{"ipv4Address":"%s"}]}}]\n' \
  48. "${label_value}" "${address}"
  49. }
  50. command=${1-}
  51. if [[ $# -gt 0 ]]; then shift; fi
  52. case "${command}" in
  53. --version)
  54. echo "container CLI version 1.1.0 (build: release, commit: fake)"
  55. ;;
  56. system)
  57. subcommand=${1-}
  58. case "${subcommand}" in
  59. status) [[ -e "${STATE_DIR}/system-running" ]] ;;
  60. start) touch "${STATE_DIR}/system-running" ;;
  61. stop) rm -f "${STATE_DIR}/system-running" "${STATE_DIR}/running"/* ;;
  62. *) exit 1 ;;
  63. esac
  64. ;;
  65. list)
  66. include_all=false
  67. for argument in "$@"; do
  68. [[ "${argument}" == "--all" || "${argument}" == "-a" ]] && include_all=true
  69. done
  70. if [[ "${include_all}" == true ]]; then
  71. list_names "${STATE_DIR}/containers"
  72. else
  73. list_names "${STATE_DIR}/running"
  74. fi
  75. ;;
  76. network)
  77. subcommand=${1-}
  78. shift || true
  79. case "${subcommand}" in
  80. list)
  81. echo default
  82. list_names "${STATE_DIR}/networks"
  83. ;;
  84. create)
  85. network_name="$(last_argument "$@")"
  86. subnet=""
  87. while [[ $# -gt 0 ]]; do
  88. case "$1" in
  89. --subnet)
  90. subnet=$2
  91. shift 2
  92. ;;
  93. *) shift ;;
  94. esac
  95. done
  96. touch "${STATE_DIR}/networks/${network_name}"
  97. printf '%s' "${subnet}" >"${STATE_DIR}/network-subnets/${network_name}"
  98. ;;
  99. inspect) inspect_resource network "${1}" ;;
  100. delete)
  101. rm -f "${STATE_DIR}/networks/${1}"
  102. rm -f "${STATE_DIR}/network-subnets/${1}"
  103. ;;
  104. *) exit 1 ;;
  105. esac
  106. ;;
  107. volume)
  108. subcommand=${1-}
  109. shift || true
  110. case "${subcommand}" in
  111. list) list_names "${STATE_DIR}/volumes" ;;
  112. create) touch "${STATE_DIR}/volumes/$(last_argument "$@")" ;;
  113. inspect) inspect_resource volume "${1}" ;;
  114. delete) rm -f "${STATE_DIR}/volumes/${1}" ;;
  115. *) exit 1 ;;
  116. esac
  117. ;;
  118. image)
  119. subcommand=${1-}
  120. case "${subcommand}" in
  121. inspect|pull) exit 0 ;;
  122. *) exit 1 ;;
  123. esac
  124. ;;
  125. create)
  126. name=""
  127. while [[ $# -gt 0 ]]; do
  128. case "$1" in
  129. --name)
  130. name=$2
  131. shift 2
  132. ;;
  133. --label|--network|--platform|--ulimit|--env-file|--volume|--entrypoint|--publish)
  134. shift 2
  135. ;;
  136. *)
  137. shift
  138. ;;
  139. esac
  140. done
  141. [[ -n "${name}" ]]
  142. touch "${STATE_DIR}/containers/${name}"
  143. ;;
  144. inspect)
  145. inspect_resource container "${1}"
  146. ;;
  147. start)
  148. touch "${STATE_DIR}/running/${1}"
  149. ;;
  150. stop)
  151. for argument in "$@"; do
  152. case "${argument}" in
  153. --time|--signal) skip_next=true ;;
  154. [0-9]*|SIG*) ;;
  155. *) rm -f "${STATE_DIR}/running/${argument}" ;;
  156. esac
  157. done
  158. ;;
  159. delete)
  160. for argument in "$@"; do
  161. case "${argument}" in
  162. --force|-f) ;;
  163. *)
  164. rm -f "${STATE_DIR}/running/${argument}"
  165. rm -f "${STATE_DIR}/containers/${argument}"
  166. ;;
  167. esac
  168. done
  169. ;;
  170. exec)
  171. echo 1
  172. ;;
  173. logs|copy)
  174. exit 0
  175. ;;
  176. *)
  177. echo "Unsupported fake container command: ${command} $*" >&2
  178. exit 1
  179. ;;
  180. esac