lxc.sh 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  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. # Location in the container where all folders from HOST are mounted
  19. LXC_SHARE_FOLDER="/share"
  20. LXC_REPO_ROOT="${LXC_SHARE_FOLDER}/$(basename "${REPO_ROOT}")"
  21. ubu1804_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. ubu1904_boilerplate="$ubu1804_boilerplate"
  28. # shellcheck disable=SC2034
  29. ubu2004_boilerplate="
  30. $ubu1904_boilerplate
  31. echo 'Set disable_coredump false' >> /etc/sudo.conf
  32. "
  33. # shellcheck disable=SC2034
  34. ubu2010_boilerplate="$ubu1904_boilerplate"
  35. # shellcheck disable=SC2034
  36. archlinux_boilerplate="
  37. pacman -Syu --noconfirm
  38. pacman -S --noconfirm inetutils git curl wget sudo
  39. echo 'Set disable_coredump false' >> /etc/sudo.conf
  40. "
  41. # shellcheck disable=SC2034
  42. fedora33_boilerplate="
  43. dnf update -y
  44. dnf install -y git curl wget hostname
  45. echo 'Set disable_coredump false' >> /etc/sudo.conf
  46. "
  47. # shellcheck disable=SC2034
  48. centos7_boilerplate="
  49. yum update -y
  50. yum install -y git curl wget hostname sudo which
  51. echo 'Set disable_coredump false' >> /etc/sudo.conf
  52. "
  53. REMOTE_IMAGES=()
  54. CONTAINERS=()
  55. LOCAL_IMAGES=()
  56. for ((i=0; i<${#LXC_SUITE[@]}; i+=2)); do
  57. REMOTE_IMAGES=("${REMOTE_IMAGES[@]}" "${LXC_SUITE[i]}")
  58. CONTAINERS=("${CONTAINERS[@]}" "${LXC_HOST_PREFIX}-${LXC_SUITE[i+1]}")
  59. LOCAL_IMAGES=("${LOCAL_IMAGES[@]}" "${LXC_SUITE[i+1]}")
  60. done
  61. HOST_USER="${SUDO_USER:-$USER}"
  62. HOST_USER_ID=$(id -u "${HOST_USER}")
  63. HOST_GROUP_ID=$(id -g "${HOST_USER}")
  64. # ----------------------------------------------------------------------------
  65. usage() {
  66. # ----------------------------------------------------------------------------
  67. _cmd="$(basename "$0")"
  68. cat <<EOF
  69. usage::
  70. $_cmd build [containers|<name>]
  71. $_cmd copy [images]
  72. $_cmd remove [containers|<name>|images]
  73. $_cmd [start|stop] [containers|<name>]
  74. $_cmd show [images|suite|info|config [<name>]]
  75. $_cmd cmd [--|<name>] '...'
  76. $_cmd install [suite|base [<name>]]
  77. build
  78. :containers: build, launch all containers and 'install base' packages
  79. :<name>: build, launch container <name> and 'install base' packages
  80. copy:
  81. :images: copy remote images of the suite into local storage
  82. remove
  83. :containers: delete all 'containers' or only <container-name>
  84. :images: delete local images of the suite
  85. start/stop
  86. :containers: start/stop all 'containers' from the suite
  87. :<name>: start/stop container <name> from suite
  88. show
  89. :info: show info of all (or <name>) containers from LXC suite
  90. :config: show config of all (or <name>) containers from the LXC suite
  91. :suite: show services of all (or <name>) containers from the LXC suite
  92. :images: show information of local images
  93. cmd
  94. use single quotes to evaluate in container's bash, e.g.: 'echo \$(hostname)'
  95. -- run command '...' in all containers of the LXC suite
  96. :<name>: run command '...' in container <name>
  97. install
  98. :base: prepare LXC; install basic packages
  99. :suite: install LXC ${LXC_SUITE_NAME} suite into all (or <name>) containers
  100. EOF
  101. usage_containers
  102. [ -n "${1+x}" ] && err_msg "$1"
  103. }
  104. usage_containers() {
  105. lxc_suite_install_info
  106. [ -n "${1+x}" ] && err_msg "$1"
  107. }
  108. lxd_info() {
  109. cat <<EOF
  110. LXD is needed, to install run::
  111. snap install lxd
  112. lxd init --auto
  113. EOF
  114. }
  115. main() {
  116. local exit_val
  117. local _usage="unknown or missing $1 command $2"
  118. # don't check prerequisite when in recursion
  119. if [[ ! $1 == __* ]] && [[ ! $1 == --help ]]; then
  120. if ! in_container; then
  121. ! required_commands lxc && lxd_info && exit 42
  122. fi
  123. [[ -z $LXC_SUITE ]] && err_msg "missing LXC_SUITE" && exit 42
  124. fi
  125. case $1 in
  126. --getenv) var="$2"; echo "${!var}"; exit 0;;
  127. -h|--help) usage; exit 0;;
  128. build)
  129. sudo_or_exit
  130. case $2 in
  131. ${LXC_HOST_PREFIX}-*) build_container "$2" ;;
  132. ''|--|containers) build_all_containers ;;
  133. *) usage "$_usage"; exit 42;;
  134. esac
  135. ;;
  136. copy)
  137. case $2 in
  138. ''|images) lxc_copy_images_localy;;
  139. *) usage "$_usage"; exit 42;;
  140. esac
  141. ;;
  142. remove)
  143. sudo_or_exit
  144. case $2 in
  145. ''|--|containers) remove_containers ;;
  146. images) lxc_delete_images_localy ;;
  147. ${LXC_HOST_PREFIX}-*)
  148. ! lxc_exists "$2" && warn_msg "container not yet exists: $2" && exit 0
  149. if ask_yn "Do you really want to delete container $2"; then
  150. lxc_delete_container "$2"
  151. fi
  152. ;;
  153. *) usage "unknown or missing container <name> $2"; exit 42;;
  154. esac
  155. ;;
  156. start|stop)
  157. sudo_or_exit
  158. case $2 in
  159. ''|--|containers) lxc_cmd "$1" ;;
  160. ${LXC_HOST_PREFIX}-*)
  161. ! lxc_exists "$2" && usage_containers "unknown container: $2" && exit 42
  162. info_msg "lxc $1 $2"
  163. lxc "$1" "$2" | prefix_stdout "[${_BBlue}${i}${_creset}] "
  164. ;;
  165. *) usage "unknown or missing container <name> $2"; exit 42;;
  166. esac
  167. ;;
  168. show)
  169. sudo_or_exit
  170. case $2 in
  171. suite)
  172. case $3 in
  173. ${LXC_HOST_PREFIX}-*)
  174. lxc exec -t "$3" -- "${LXC_REPO_ROOT}/utils/lxc.sh" __show suite \
  175. | prefix_stdout "[${_BBlue}$3${_creset}] "
  176. ;;
  177. *) show_suite;;
  178. esac
  179. ;;
  180. images) show_images ;;
  181. config)
  182. case $3 in
  183. ${LXC_HOST_PREFIX}-*)
  184. ! lxc_exists "$3" && usage_containers "unknown container: $3" && exit 42
  185. lxc config show "$3" | prefix_stdout "[${_BBlue}${3}${_creset}] "
  186. ;;
  187. *)
  188. rst_title "container configurations"
  189. echo
  190. lxc list "$LXC_HOST_PREFIX-"
  191. echo
  192. lxc_cmd config show
  193. ;;
  194. esac
  195. ;;
  196. info)
  197. case $3 in
  198. ${LXC_HOST_PREFIX}-*)
  199. ! lxc_exists "$3" && usage_containers "unknown container: $3" && exit 42
  200. lxc info "$3" | prefix_stdout "[${_BBlue}${3}${_creset}] "
  201. ;;
  202. *)
  203. rst_title "container info"
  204. echo
  205. lxc_cmd info
  206. ;;
  207. esac
  208. ;;
  209. *) usage "$_usage"; exit 42;;
  210. esac
  211. ;;
  212. __show)
  213. # wrapped show commands, called once in each container
  214. case $2 in
  215. suite) lxc_suite_info ;;
  216. esac
  217. ;;
  218. cmd)
  219. sudo_or_exit
  220. shift
  221. case $1 in
  222. --) shift; lxc_exec "$@" ;;
  223. ${LXC_HOST_PREFIX}-*)
  224. ! lxc_exists "$1" && usage_containers "unknown container: $1" && exit 42
  225. local name=$1
  226. shift
  227. lxc_exec_cmd "${name}" "$@"
  228. ;;
  229. *) usage_containers "unknown container: $1" && exit 42
  230. esac
  231. ;;
  232. install)
  233. sudo_or_exit
  234. case $2 in
  235. suite|base)
  236. case $3 in
  237. ${LXC_HOST_PREFIX}-*)
  238. ! lxc_exists "$3" && usage_containers "unknown container: $3" && exit 42
  239. lxc_exec_cmd "$3" "${LXC_REPO_ROOT}/utils/lxc.sh" __install "$2"
  240. ;;
  241. ''|--) lxc_exec "${LXC_REPO_ROOT}/utils/lxc.sh" __install "$2" ;;
  242. *) usage_containers "unknown container: $3" && exit 42
  243. esac
  244. ;;
  245. *) usage "$_usage"; exit 42 ;;
  246. esac
  247. ;;
  248. __install)
  249. # wrapped install commands, called once in each container
  250. # shellcheck disable=SC2119
  251. case $2 in
  252. suite) lxc_suite_install ;;
  253. base) FORCE_TIMEOUT=0 lxc_install_base_packages ;;
  254. esac
  255. ;;
  256. doc)
  257. echo
  258. echo ".. generic utils/lxc.sh documentation"
  259. ;;
  260. -*) usage "unknown option $1"; exit 42;;
  261. *) usage "unknown or missing command $1"; exit 42;;
  262. esac
  263. }
  264. build_all_containers() {
  265. rst_title "Build all LXC containers of suite"
  266. echo
  267. usage_containers
  268. lxc_copy_images_localy
  269. lxc_init_all_containers
  270. lxc_config_all_containers
  271. lxc_boilerplate_all_containers
  272. rst_title "install LXC base packages" section
  273. echo
  274. lxc_exec "${LXC_REPO_ROOT}/utils/lxc.sh" __install base
  275. echo
  276. lxc list "$LXC_HOST_PREFIX"
  277. }
  278. build_container() {
  279. rst_title "Build container $1"
  280. local remote_image
  281. local container
  282. local image
  283. local boilerplate_script
  284. for ((i=0; i<${#LXC_SUITE[@]}; i+=2)); do
  285. if [ "${LXC_HOST_PREFIX}-${LXC_SUITE[i+1]}" = "$1" ]; then
  286. remote_image="${LXC_SUITE[i]}"
  287. container="${LXC_HOST_PREFIX}-${LXC_SUITE[i+1]}"
  288. image="${LXC_SUITE[i+1]}"
  289. boilerplate_script="${image}_boilerplate"
  290. boilerplate_script="${!boilerplate_script}"
  291. break
  292. fi
  293. done
  294. echo
  295. if [ -z "$container" ]; then
  296. err_msg "container $1 unknown"
  297. usage_containers
  298. return 42
  299. fi
  300. lxc_image_copy "${remote_image}" "${image}"
  301. rst_title "init container" section
  302. lxc_init_container "${image}" "${container}"
  303. rst_title "configure container" section
  304. lxc_config_container "${container}"
  305. rst_title "run LXC boilerplate scripts" section
  306. lxc_install_boilerplate "${container}" "$boilerplate_script"
  307. echo
  308. rst_title "install LXC base packages" section
  309. lxc_exec_cmd "${container}" "${LXC_REPO_ROOT}/utils/lxc.sh" __install base \
  310. | prefix_stdout "[${_BBlue}${container}${_creset}] "
  311. echo
  312. lxc list "$container"
  313. }
  314. remove_containers() {
  315. rst_title "Remove all LXC containers of suite"
  316. rst_para "existing containers matching ${_BGreen}$LXC_HOST_PREFIX-*${_creset}"
  317. echo
  318. lxc list "$LXC_HOST_PREFIX-"
  319. echo -en "\\n${_BRed}LXC containers to delete::${_creset}\\n\\n ${CONTAINERS[*]}\\n" | $FMT
  320. local default=Ny
  321. [[ $FORCE_TIMEOUT = 0 ]] && default=Yn
  322. if ask_yn "Do you really want to delete these containers" $default; then
  323. for i in "${CONTAINERS[@]}"; do
  324. lxc_delete_container "$i"
  325. done
  326. fi
  327. echo
  328. lxc list "$LXC_HOST_PREFIX-"
  329. }
  330. # images
  331. # ------
  332. lxc_copy_images_localy() {
  333. rst_title "copy images" section
  334. for ((i=0; i<${#LXC_SUITE[@]}; i+=2)); do
  335. lxc_image_copy "${LXC_SUITE[i]}" "${LXC_SUITE[i+1]}"
  336. done
  337. # lxc image list local: && wait_key
  338. }
  339. lxc_delete_images_localy() {
  340. rst_title "Delete LXC images"
  341. rst_para "local existing images"
  342. echo
  343. lxc image list local:
  344. echo -en "\\n${_BRed}LXC images to delete::${_creset}\\n\\n ${LOCAL_IMAGES[*]}\\n"
  345. if ask_yn "Do you really want to delete these images"; then
  346. for i in "${LOCAL_IMAGES[@]}"; do
  347. lxc_delete_local_image "$i"
  348. done
  349. fi
  350. for i in $(lxc image list --format csv | grep '^,' | sed 's/,\([^,]*\).*$/\1/'); do
  351. if ask_yn "Image $i has no alias, do you want to delete the image?" Yn; then
  352. lxc_delete_local_image "$i"
  353. fi
  354. done
  355. echo
  356. lxc image list local:
  357. }
  358. show_images(){
  359. rst_title "local images"
  360. echo
  361. lxc image list local:
  362. echo -en "\\n${_Green}LXC suite images::${_creset}\\n\\n ${LOCAL_IMAGES[*]}\\n"
  363. wait_key
  364. for i in "${LOCAL_IMAGES[@]}"; do
  365. if lxc_image_exists "$i"; then
  366. info_msg "lxc image info ${_BBlue}${i}${_creset}"
  367. lxc image info "$i" | prefix_stdout "[${_BBlue}${i}${_creset}] "
  368. else
  369. warn_msg "image ${_BBlue}$i${_creset} does not yet exists"
  370. fi
  371. done
  372. }
  373. # container
  374. # ---------
  375. show_suite(){
  376. rst_title "LXC suite ($LXC_HOST_PREFIX-*)"
  377. echo
  378. lxc list "$LXC_HOST_PREFIX-"
  379. echo
  380. for i in "${CONTAINERS[@]}"; do
  381. if ! lxc_exists "$i"; then
  382. warn_msg "container ${_BBlue}$i${_creset} does not yet exists"
  383. else
  384. lxc exec -t "${i}" -- "${LXC_REPO_ROOT}/utils/lxc.sh" __show suite \
  385. | prefix_stdout "[${_BBlue}${i}${_creset}] "
  386. echo
  387. fi
  388. done
  389. }
  390. lxc_cmd() {
  391. for i in "${CONTAINERS[@]}"; do
  392. if ! lxc_exists "$i"; then
  393. warn_msg "container ${_BBlue}$i${_creset} does not yet exists"
  394. else
  395. info_msg "lxc $* $i"
  396. lxc "$@" "$i" | prefix_stdout "[${_BBlue}${i}${_creset}] "
  397. fi
  398. done
  399. }
  400. lxc_exec_cmd() {
  401. local name="$1"
  402. shift
  403. exit_val=
  404. info_msg "[${_BBlue}${name}${_creset}] ${_BGreen}${*}${_creset}"
  405. lxc exec -t --cwd "${LXC_REPO_ROOT}" "${name}" -- bash -c "$*"
  406. exit_val=$?
  407. if [[ $exit_val -ne 0 ]]; then
  408. warn_msg "[${_BBlue}${name}${_creset}] exit code (${_BRed}${exit_val}${_creset}) from ${_BGreen}${*}${_creset}"
  409. else
  410. info_msg "[${_BBlue}${name}${_creset}] exit code (${exit_val}) from ${_BGreen}${*}${_creset}"
  411. fi
  412. }
  413. lxc_exec() {
  414. for i in "${CONTAINERS[@]}"; do
  415. if ! lxc_exists "$i"; then
  416. warn_msg "container ${_BBlue}$i${_creset} does not yet exists"
  417. else
  418. lxc_exec_cmd "${i}" "$@" | prefix_stdout "[${_BBlue}${i}${_creset}] "
  419. fi
  420. done
  421. }
  422. lxc_init_all_containers() {
  423. rst_title "init all containers" section
  424. local image_name
  425. local container_name
  426. for ((i=0; i<${#LXC_SUITE[@]}; i+=2)); do
  427. lxc_init_container "${LXC_SUITE[i+1]}" "${LXC_HOST_PREFIX}-${LXC_SUITE[i+1]}"
  428. done
  429. }
  430. lxc_config_all_containers() {
  431. rst_title "configure all containers" section
  432. for i in "${CONTAINERS[@]}"; do
  433. lxc_config_container "${i}"
  434. done
  435. }
  436. lxc_config_container() {
  437. info_msg "[${_BBlue}$1${_creset}] configure container ..."
  438. info_msg "[${_BBlue}$1${_creset}] map uid/gid from host to container"
  439. # https://lxd.readthedocs.io/en/latest/userns-idmap/#custom-idmaps
  440. echo -e -n "uid $HOST_USER_ID 0\\ngid $HOST_GROUP_ID 0"\
  441. | lxc config set "$1" raw.idmap -
  442. info_msg "[${_BBlue}$1${_creset}] share ${REPO_ROOT} (repo_share) from HOST into container"
  443. # https://lxd.readthedocs.io/en/latest/instances/#type-disk
  444. lxc config device add "$1" repo_share disk \
  445. source="${REPO_ROOT}" \
  446. path="${LXC_REPO_ROOT}" &>/dev/null
  447. # lxc config show "$1" && wait_key
  448. }
  449. lxc_boilerplate_all_containers() {
  450. rst_title "run LXC boilerplate scripts" section
  451. local boilerplate_script
  452. local image_name
  453. for ((i=0; i<${#LXC_SUITE[@]}; i+=2)); do
  454. image_name="${LXC_SUITE[i+1]}"
  455. boilerplate_script="${image_name}_boilerplate"
  456. boilerplate_script="${!boilerplate_script}"
  457. lxc_install_boilerplate "${LXC_HOST_PREFIX}-${image_name}" "$boilerplate_script"
  458. if [[ -z "${boilerplate_script}" ]]; then
  459. err_msg "[${_BBlue}${container_name}${_creset}] no boilerplate for image '${image_name}'"
  460. fi
  461. done
  462. }
  463. lxc_install_boilerplate() {
  464. # usage: lxc_install_boilerplate <container-name> <string: shell commands ..>
  465. #
  466. # usage: lxc_install_boilerplate searx-archlinux "${archlinux_boilerplate}"
  467. local container_name="$1"
  468. local boilerplate_script="$2"
  469. info_msg "[${_BBlue}${container_name}${_creset}] init .."
  470. if lxc start -q "${container_name}" &>/dev/null; then
  471. sleep 5 # guest needs some time to come up and get an IP
  472. fi
  473. lxc_init_container_env "${container_name}"
  474. info_msg "[${_BBlue}${container_name}${_creset}] install /.lxcenv.mk .."
  475. cat <<EOF | lxc exec "${container_name}" -- bash | prefix_stdout "[${_BBlue}${container_name}${_creset}] "
  476. rm -f "/.lxcenv.mk"
  477. ln -s "${LXC_REPO_ROOT}/utils/makefile.lxc" "/.lxcenv.mk"
  478. ls -l "/.lxcenv.mk"
  479. EOF
  480. info_msg "[${_BBlue}${container_name}${_creset}] run LXC boilerplate scripts .."
  481. if lxc start -q "${container_name}" &>/dev/null; then
  482. sleep 5 # guest needs some time to come up and get an IP
  483. fi
  484. if [[ -n "${boilerplate_script}" ]]; then
  485. echo "${boilerplate_script}" \
  486. | lxc exec "${container_name}" -- bash \
  487. | prefix_stdout "[${_BBlue}${container_name}${_creset}] "
  488. fi
  489. }
  490. # ----------------------------------------------------------------------------
  491. main "$@"
  492. # ----------------------------------------------------------------------------