lxc.sh 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #!/usr/bin/env bash
  2. # -*- coding: utf-8; mode: sh indent-tabs-mode: nil -*-
  3. # SPDX-License-Identifier: AGPL-3.0-or-later
  4. # shellcheck source=utils/lib.sh
  5. source "$(dirname "${BASH_SOURCE[0]}")/lib.sh"
  6. source_dot_config
  7. # ----------------------------------------------------------------------------
  8. # config
  9. # ----------------------------------------------------------------------------
  10. # name of https://images.linuxcontainers.org
  11. LINUXCONTAINERS_ORG_NAME="${LINUXCONTAINERS_ORG_NAME:-images}"
  12. HOST_PREFIX="${HOST_PREFIX:-searx}"
  13. TEST_IMAGES=(
  14. "$LINUXCONTAINERS_ORG_NAME:ubuntu/18.04" "ubu1804"
  15. "$LINUXCONTAINERS_ORG_NAME:ubuntu/19.04" "ubu1904"
  16. "$LINUXCONTAINERS_ORG_NAME:archlinux" "archlinux"
  17. #"$LINUXCONTAINERS_ORG_NAME:fedora/31" "fedora31"
  18. #"ubuntu-minimal:18.04" "ubu1804"
  19. #"ubuntu-minimal:19.10" "ubu1910"
  20. )
  21. REMOTE_IMAGES=()
  22. LOCAL_IMAGES=()
  23. for ((i=0; i<${#TEST_IMAGES[@]}; i+=2)); do
  24. REMOTE_IMAGES=("${REMOTE_IMAGES[@]}" "${TEST_IMAGES[i]}")
  25. LOCAL_IMAGES=("${LOCAL_IMAGES[@]}" "${TEST_IMAGES[i+1]}")
  26. done
  27. # ----------------------------------------------------------------------------
  28. usage() {
  29. # ----------------------------------------------------------------------------
  30. cat <<EOF
  31. usage::
  32. $(basename "$0") build [hosts]
  33. $(basename "$0") delete [hosts]
  34. build / delete
  35. build and/or delete all LXC hosts
  36. all LXC hosts:
  37. ${LOCAL_IMAGES[@]}
  38. EOF
  39. [ ! -z "${1+x}" ] && err_msg "$1"
  40. }
  41. main() {
  42. rst_title "LXC tooling box" part
  43. required_commands lxc || exit
  44. local _usage="unknown or missing $1 command $2"
  45. case $1 in
  46. --source-only) ;;
  47. -h|--help) usage; exit 0;;
  48. build)
  49. sudo_or_exit
  50. case $2 in
  51. hosts) build_instances ;;
  52. *) usage "$_usage"; exit 42;;
  53. esac ;;
  54. delete)
  55. sudo_or_exit
  56. case $2 in
  57. hosts) delete_instances ;;
  58. *) usage "$_usage"; exit 42;;
  59. esac ;;
  60. *)
  61. usage "unknown or missing command $1"; exit 42;;
  62. esac
  63. }
  64. build_instances() {
  65. rst_title "Build LXC instances"
  66. lxc_copy_images_localy
  67. lxc_init_containers
  68. err_msg "WIP / sorry, not implemented yet :o"
  69. }
  70. delete_instances() {
  71. rst_title "Delete LXC instances"
  72. echo -en "\\nLXC hosts(s)::\\n\\n ${LOCAL_IMAGES[*]}\\n" | $FMT
  73. if ask_yn "Do you really want to delete all images"; then
  74. lxc_delete_containers
  75. fi
  76. }
  77. lxc_copy_images_localy() {
  78. echo
  79. for ((i=0; i<${#TEST_IMAGES[@]}; i+=2)); do
  80. if lxc image info "local:${TEST_IMAGES[i+1]}" &>/dev/null; then
  81. info_msg "image ${TEST_IMAGES[i]} already copied --> ${TEST_IMAGES[i+1]}"
  82. else
  83. info_msg "copy image locally ${TEST_IMAGES[i]} --> ${TEST_IMAGES[i+1]}"
  84. lxc image copy "${TEST_IMAGES[i]}" local: \
  85. --alias "${TEST_IMAGES[i+1]}" prefix_stdout
  86. fi
  87. done
  88. #lxc image list local:
  89. }
  90. lxc_delete_images_localy() {
  91. echo
  92. for i in "${LOCAL_IMAGES[@]}"; do
  93. info_msg "delete image 'local:$i'"
  94. lxc image delete "local:$i"
  95. done
  96. #lxc image list local:
  97. }
  98. lxc_init_containers() {
  99. echo
  100. for i in "${LOCAL_IMAGES[@]}"; do
  101. if lxc info "$HOST_PREFIX-$i" &>/dev/null; then
  102. info_msg "conatiner '$HOST_PREFIX-$i' already exists"
  103. else
  104. info_msg "create conatiner instance: $HOST_PREFIX-$i"
  105. lxc init "local:$i" "$HOST_PREFIX-$i"
  106. fi
  107. done
  108. #lxc list "$HOST_PREFIX"
  109. }
  110. lxc_delete_containers() {
  111. echo
  112. for i in "${LOCAL_IMAGES[@]}"; do
  113. if lxc info "$HOST_PREFIX-$i" &>/dev/null; then
  114. info_msg "stop & delete instance '$HOST_PREFIX-$i'"
  115. lxc stop "$HOST_PREFIX-$i" &>/dev/null
  116. lxc delete "$HOST_PREFIX-$i" | prefix_stdout
  117. else
  118. warn_msg "instance '$HOST_PREFIX-$i' does not exist / can't delete :o"
  119. fi
  120. done
  121. #lxc list "$HOST_PREFIX"
  122. }
  123. # ----------------------------------------------------------------------------
  124. main "$@"
  125. # ----------------------------------------------------------------------------