lxc.sh 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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. # load environment of the LXC suite
  8. LXC_ENV="${LXC_ENV:-${REPO_ROOT}/utils/lxc-searx.env}"
  9. source "$LXC_ENV"
  10. lxc_set_suite_env
  11. # ----------------------------------------------------------------------------
  12. # config
  13. # ----------------------------------------------------------------------------
  14. #
  15. # read also:
  16. # - https://lxd.readthedocs.io/en/latest/
  17. LXC_HOST_PREFIX="${LXC_HOST_PREFIX:-test}"
  18. # where all folders from HOST are mounted
  19. LXC_SHARE_FOLDER="/share"
  20. LXC_REPO_ROOT="${LXC_SHARE_FOLDER}/$(basename "${REPO_ROOT}")"
  21. ubu1604_boilerplate="
  22. export DEBIAN_FRONTEND=noninteractive
  23. apt-get update -y
  24. apt-get upgrade -y
  25. apt-get install -y git curl wget
  26. "
  27. ubu1804_boilerplate="$ubu1604_boilerplate"
  28. ubu1904_boilerplate="$ubu1804_boilerplate"
  29. ubu1910_boilerplate="$ubu1904_boilerplate"
  30. # shellcheck disable=SC2034
  31. ubu2004_boilerplate="
  32. $ubu1910_boilerplate
  33. echo 'Set disable_coredump false' >> /etc/sudo.conf
  34. "
  35. # shellcheck disable=SC2034
  36. archlinux_boilerplate="
  37. pacman -Syu --noconfirm
  38. pacman -S --noconfirm git curl wget sudo
  39. echo 'Set disable_coredump false' >> /etc/sudo.conf
  40. "
  41. # shellcheck disable=SC2034
  42. fedora31_boilerplate="
  43. dnf update -y
  44. dnf install -y git curl wget hostname
  45. echo 'Set disable_coredump false' >> /etc/sudo.conf
  46. "
  47. REMOTE_IMAGES=()
  48. CONTAINERS=()
  49. LOCAL_IMAGES=()
  50. for ((i=0; i<${#LXC_SUITE[@]}; i+=2)); do
  51. REMOTE_IMAGES=("${REMOTE_IMAGES[@]}" "${LXC_SUITE[i]}")
  52. CONTAINERS=("${CONTAINERS[@]}" "${LXC_HOST_PREFIX}-${LXC_SUITE[i+1]}")
  53. LOCAL_IMAGES=("${LOCAL_IMAGES[@]}" "${LXC_SUITE[i+1]}")
  54. done
  55. HOST_USER="${SUDO_USER:-$USER}"
  56. HOST_USER_ID=$(id -u "${HOST_USER}")
  57. HOST_GROUP_ID=$(id -g "${HOST_USER}")
  58. # ----------------------------------------------------------------------------
  59. usage() {
  60. # ----------------------------------------------------------------------------
  61. _cmd="$(basename "$0")"
  62. cat <<EOF
  63. usage::
  64. $_cmd build [containers]
  65. $_cmd copy [images]
  66. $_cmd remove [containers|<name>|images]
  67. $_cmd [start|stop] [containers|<name>]
  68. $_cmd show [info|config|suite|images]
  69. $_cmd cmd [--|<name>] '...'
  70. $_cmd install [suite]
  71. build
  72. :containers: build & launch all LXC containers of the suite
  73. copy:
  74. :images: copy remote images of the suite into local storage
  75. remove
  76. :containers: delete all 'containers' or only <container-name>
  77. :images: delete local images of the suite
  78. start/stop
  79. :containers: start/stop all 'containers' from the suite
  80. :<name>: start/stop conatiner <name> from suite
  81. show
  82. :info: show info of all the containers from LXC suite
  83. :config: show config of all the containers from the LXC suite
  84. :suite: show services of all the containers from the LXC suite
  85. :images: show information of local images
  86. cmd
  87. use single qoutes to evaluate in container's bash, e.g. 'echo $(hostname)'
  88. -- run command '...' in all containers of the LXC suite
  89. :<name>: run command '...' in container <name>
  90. install
  91. :suite: install LXC suite, includes morty & filtron
  92. EOF
  93. usage_images
  94. echo
  95. usage_containers
  96. echo
  97. [ -n "${1+x}" ] && err_msg "$1"
  98. }
  99. usage_containers() {
  100. cat <<EOF
  101. LXC suite containers:
  102. $(echo " ${CONTAINERS[*]}" | $FMT)
  103. EOF
  104. [ -n "${1+x}" ] && err_msg "$1"
  105. }
  106. usage_images() {
  107. cat <<EOF
  108. LXC suite images:
  109. $(echo " ${LOCAL_IMAGES[*]}" | $FMT)
  110. EOF
  111. }
  112. lxd_info() {
  113. cat <<EOF
  114. LXD is needed, to install run::
  115. snap install lxd
  116. lxd init --auto
  117. EOF
  118. }
  119. main() {
  120. local exit_val
  121. local _usage="unknown or missing $1 command $2"
  122. # don't check prerequisite when in recursion
  123. if [[ ! $1 == __* ]]; then
  124. ! required_commands lxc && lxd_info && exit 42
  125. [[ -z $LXC_SUITE ]] && err_msg "missing LXC_SUITE" && exit 42
  126. fi
  127. case $1 in
  128. --source-only) ;;
  129. -h|--help) usage; exit 0;;
  130. build)
  131. sudo_or_exit
  132. case $2 in
  133. ''|containers) build_instances ;;
  134. *) usage "$_usage"; exit 42;;
  135. esac
  136. ;;
  137. copy)
  138. case $2 in
  139. ''|images) lxc_copy_images_localy;;
  140. *) usage "$_usage"; exit 42;;
  141. esac
  142. ;;
  143. remove)
  144. sudo_or_exit
  145. case $2 in
  146. ''|containers) remove_instances ;;
  147. images) lxc_delete_images_localy ;;
  148. ${LXC_HOST_PREFIX}-*)
  149. ! lxc_exists "$2" && usage_containers "unknown container: $2" && exit 42
  150. if ask_yn "Do you really want to delete conatiner $2"; then
  151. lxc_delete_container "$2"
  152. fi
  153. ;;
  154. *) usage "uknown or missing container <name> $2"; exit 42;;
  155. esac
  156. ;;
  157. start|stop)
  158. sudo_or_exit
  159. case $2 in
  160. ''|containers) lxc_cmd "$1" ;;
  161. ${LXC_HOST_PREFIX}-*)
  162. ! lxc_exists "$2" && usage_containers "unknown container: $2" && exit 42
  163. info_msg "lxc $1 $2"
  164. lxc "$1" "$2" | prefix_stdout "[${_BBlue}${i}${_creset}] "
  165. ;;
  166. *) usage "uknown or missing container <name> $2"; exit 42;;
  167. esac
  168. ;;
  169. show)
  170. sudo_or_exit
  171. case $2 in
  172. suite) show_suite ;;
  173. images) show_images ;;
  174. config)
  175. rst_title "container configurations"
  176. echo
  177. lxc list "$LXC_HOST_PREFIX-"
  178. echo
  179. lxc_cmd config show
  180. ;;
  181. info)
  182. rst_title "container info"
  183. echo
  184. lxc_cmd info
  185. ;;
  186. *) usage "$_usage"; exit 42;;
  187. esac
  188. ;;
  189. __show)
  190. case $2 in
  191. suite) lxc_suite_info ;;
  192. esac
  193. ;;
  194. cmd)
  195. sudo_or_exit
  196. shift
  197. case $1 in
  198. --)
  199. shift
  200. for name in "${CONTAINERS[@]}"; do
  201. lxc_exec_cmd "${name}" "$@"
  202. done
  203. ;;
  204. ${LXC_HOST_PREFIX}-*)
  205. ! lxc_exists "$1" && usage_containers "unknown container: $1" && exit 42
  206. local name=$1
  207. shift
  208. lxc_exec_cmd "${name}" "$@"
  209. ;;
  210. *) usage "uknown or missing container <name> $2"; exit 42;;
  211. esac
  212. ;;
  213. install)
  214. sudo_or_exit
  215. case $2 in
  216. suite) install_suite ;;
  217. *) usage "$_usage"; exit 42 ;;
  218. esac
  219. ;;
  220. __install)
  221. case $2 in
  222. suite) lxc_suite_install ;;
  223. esac
  224. ;;
  225. doc)
  226. echo
  227. echo ".. generic utils/lxc.sh documentation"
  228. ;;
  229. -*) usage "unknown option $1"; exit 42;;
  230. *) usage "unknown or missing command $1"; exit 42;;
  231. esac
  232. }
  233. build_instances() {
  234. rst_title "Build LXC instances"
  235. echo
  236. lxc_copy_images_localy
  237. echo
  238. rst_title "build containers" section
  239. echo
  240. lxc_init_containers
  241. lxc_config_containers
  242. lxc_boilerplate_containers
  243. echo
  244. lxc list "$LXC_HOST_PREFIX"
  245. }
  246. remove_instances() {
  247. rst_title "Remove LXC instances"
  248. rst_para "existing containers matching ${_BGreen}$LXC_HOST_PREFIX-*${_creset}"
  249. echo
  250. lxc list "$LXC_HOST_PREFIX-"
  251. echo -en "\\n${_BRed}LXC containers to delete::${_creset}\\n\\n ${CONTAINERS[*]}\\n" | $FMT
  252. if ask_yn "Do you really want to delete these conatiners"; then
  253. for i in "${CONTAINERS[@]}"; do
  254. lxc_delete_container "$i"
  255. done
  256. fi
  257. echo
  258. lxc list "$LXC_HOST_PREFIX-"
  259. }
  260. # images
  261. # ------
  262. lxc_copy_images_localy() {
  263. rst_title "copy images" section
  264. echo
  265. for ((i=0; i<${#LXC_SUITE[@]}; i+=2)); do
  266. if lxc_image_exists "local:${LXC_SUITE[i+1]}"; then
  267. info_msg "image ${LXC_SUITE[i]} already copied --> ${LXC_SUITE[i+1]}"
  268. else
  269. info_msg "copy image locally ${LXC_SUITE[i]} --> ${LXC_SUITE[i+1]}"
  270. lxc image copy "${LXC_SUITE[i]}" local: \
  271. --alias "${LXC_SUITE[i+1]}" | prefix_stdout
  272. fi
  273. done
  274. # lxc image list local: && wait_key
  275. }
  276. lxc_delete_images_localy() {
  277. rst_title "Delete LXC images"
  278. rst_para "local existing images"
  279. echo
  280. lxc image list local:
  281. echo -en "\\n${_BRed}LXC images to delete::${_creset}\\n\\n ${LOCAL_IMAGES[*]}\\n"
  282. if ask_yn "Do you really want to delete these images"; then
  283. for i in "${LOCAL_IMAGES[@]}"; do
  284. lxc_delete_local_image "$i"
  285. done
  286. fi
  287. echo
  288. lxc image list local:
  289. }
  290. show_images(){
  291. rst_title "local images"
  292. echo
  293. lxc image list local:
  294. echo -en "\\n${_Green}LXC suite images::${_creset}\\n\\n ${LOCAL_IMAGES[*]}\\n"
  295. wait_key
  296. for i in "${LOCAL_IMAGES[@]}"; do
  297. if lxc_image_exists "$i"; then
  298. info_msg "lxc image info ${_BBlue}${i}${_creset}"
  299. lxc image info "$i" | prefix_stdout "[${_BBlue}${i}${_creset}] "
  300. else
  301. warn_msg "image ${_BBlue}$i${_creset} does not yet exists"
  302. fi
  303. done
  304. }
  305. # container
  306. # ---------
  307. show_suite(){
  308. rst_title "LXC suite ($LXC_HOST_PREFIX-*)"
  309. echo
  310. lxc list "$LXC_HOST_PREFIX-"
  311. echo
  312. for i in "${CONTAINERS[@]}"; do
  313. if ! lxc_exists "$i"; then
  314. warn_msg "container ${_BBlue}$i${_creset} does not yet exists"
  315. else
  316. lxc exec -t "${i}" -- "${LXC_REPO_ROOT}/utils/lxc.sh" __show suite \
  317. | prefix_stdout "[${_BBlue}${i}${_creset}] "
  318. echo
  319. fi
  320. done
  321. }
  322. install_suite() {
  323. for i in "${CONTAINERS[@]}"; do
  324. if ! lxc_exists "$i"; then
  325. warn_msg "container ${_BBlue}$i${_creset} does not yet exists"
  326. else
  327. info_msg "[${_BBlue}${i}${_creset}] ${_BGreen}${LXC_REPO_ROOT}/utils/lxc.sh install suite${_creset}"
  328. lxc exec -t "${i}" -- "${LXC_REPO_ROOT}/utils/lxc.sh" __install suite \
  329. | prefix_stdout "[${_BBlue}${i}${_creset}] "
  330. fi
  331. done
  332. }
  333. lxc_cmd() {
  334. for i in "${CONTAINERS[@]}"; do
  335. if ! lxc_exists "$i"; then
  336. warn_msg "container ${_BBlue}$i${_creset} does not yet exists"
  337. else
  338. info_msg "lxc $* $i"
  339. lxc "$@" "$i" | prefix_stdout "[${_BBlue}${i}${_creset}] "
  340. echo
  341. fi
  342. done
  343. }
  344. lxc_exec_cmd() {
  345. local name="$1"
  346. shift
  347. exit_val=
  348. info_msg "[${_BBlue}${name}${_creset}] ${_BGreen}${*}${_creset}"
  349. lxc exec --cwd "${LXC_REPO_ROOT}" "${name}" -- bash -c "$*"
  350. exit_val=$?
  351. if [[ $exit_val -ne 0 ]]; then
  352. warn_msg "[${_BBlue}${name}${_creset}] exit code (${_BRed}${exit_val}${_creset}) from ${_BGreen}${*}${_creset}"
  353. else
  354. info_msg "[${_BBlue}${name}${_creset}] exit code (${exit_val}) from ${_BGreen}${*}${_creset}"
  355. fi
  356. echo
  357. }
  358. lxc_init_containers() {
  359. local image_name
  360. local container_name
  361. for ((i=0; i<${#LXC_SUITE[@]}; i+=2)); do
  362. image_name="${LXC_SUITE[i+1]}"
  363. container_name="${LXC_HOST_PREFIX}-${image_name}"
  364. if lxc info "${container_name}" &>/dev/null; then
  365. info_msg "container '${container_name}' already exists"
  366. else
  367. info_msg "create conatiner instance: ${container_name}"
  368. lxc init "local:${image_name}" "${container_name}"
  369. fi
  370. done
  371. }
  372. lxc_config_containers() {
  373. for i in "${CONTAINERS[@]}"; do
  374. info_msg "[${_BBlue}${i}${_creset}] configure container ..."
  375. info_msg "[${_BBlue}${i}${_creset}] map uid/gid from host to container"
  376. # https://lxd.readthedocs.io/en/latest/userns-idmap/#custom-idmaps
  377. echo -e -n "uid $HOST_USER_ID 0\\ngid $HOST_GROUP_ID 0"\
  378. | lxc config set "$i" raw.idmap -
  379. info_msg "[${_BBlue}${i}${_creset}] share ${REPO_ROOT} (repo_share) from HOST into container"
  380. # https://lxd.readthedocs.io/en/latest/instances/#type-disk
  381. lxc config device add "$i" repo_share disk \
  382. source="${REPO_ROOT}" \
  383. path="${LXC_REPO_ROOT}" &>/dev/null
  384. # lxc config show "$i" && wait_key
  385. done
  386. }
  387. lxc_boilerplate_containers() {
  388. local image_name
  389. local container_name
  390. local boilerplate_script
  391. for ((i=0; i<${#LXC_SUITE[@]}; i+=2)); do
  392. image_name="${LXC_SUITE[i+1]}"
  393. container_name="${LXC_HOST_PREFIX}-${image_name}"
  394. boilerplate_script="${image_name}_boilerplate"
  395. boilerplate_script="${!boilerplate_script}"
  396. info_msg "[${_BBlue}${container_name}${_creset}] install boilerplate"
  397. if lxc start -q "${container_name}" &>/dev/null; then
  398. sleep 5 # guest needs some time to come up and get an IP
  399. fi
  400. if [[ -n "${boilerplate_script}" ]]; then
  401. echo "${boilerplate_script}" \
  402. | lxc exec "${container_name}" -- bash \
  403. | prefix_stdout "[${_BBlue}${container_name}${_creset}] "
  404. else
  405. err_msg "[${_BBlue}${container_name}${_creset}] no boilerplate for image '${image_name}'"
  406. fi
  407. done
  408. }
  409. # ----------------------------------------------------------------------------
  410. main "$@"
  411. # ----------------------------------------------------------------------------