lxc.sh 17 KB

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