lxc.sh 17 KB

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