lxc.sh 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  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|<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. cat <<EOF
  100. LXC suite: ${LXC_SUITE_NAME}
  101. $(echo " ${LXC_SUITE_INSTALL_INFO}" | $FMT)
  102. suite images:
  103. $(echo " ${LOCAL_IMAGES[*]}" | $FMT)
  104. suite containers:
  105. $(echo " ${CONTAINERS[*]}" | $FMT)
  106. EOF
  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. --source-only) ;;
  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" && usage_containers "unknown container: $2" && exit 42
  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. if ask_yn "Do you really want to delete these containers"; then
  322. for i in "${CONTAINERS[@]}"; do
  323. lxc_delete_container "$i"
  324. done
  325. fi
  326. echo
  327. lxc list "$LXC_HOST_PREFIX-"
  328. }
  329. # images
  330. # ------
  331. lxc_copy_images_localy() {
  332. rst_title "copy images" section
  333. for ((i=0; i<${#LXC_SUITE[@]}; i+=2)); do
  334. lxc_image_copy "${LXC_SUITE[i]}" "${LXC_SUITE[i+1]}"
  335. done
  336. # lxc image list local: && wait_key
  337. }
  338. lxc_delete_images_localy() {
  339. rst_title "Delete LXC images"
  340. rst_para "local existing images"
  341. echo
  342. lxc image list local:
  343. echo -en "\\n${_BRed}LXC images to delete::${_creset}\\n\\n ${LOCAL_IMAGES[*]}\\n"
  344. if ask_yn "Do you really want to delete these images"; then
  345. for i in "${LOCAL_IMAGES[@]}"; do
  346. lxc_delete_local_image "$i"
  347. done
  348. fi
  349. for i in $(lxc image list --format csv | grep '^,' | sed 's/,\([^,]*\).*$/\1/'); do
  350. if ask_yn "Image $i has no alias, do you want to delete the image?" Yn; then
  351. lxc_delete_local_image "$i"
  352. fi
  353. done
  354. echo
  355. lxc image list local:
  356. }
  357. show_images(){
  358. rst_title "local images"
  359. echo
  360. lxc image list local:
  361. echo -en "\\n${_Green}LXC suite images::${_creset}\\n\\n ${LOCAL_IMAGES[*]}\\n"
  362. wait_key
  363. for i in "${LOCAL_IMAGES[@]}"; do
  364. if lxc_image_exists "$i"; then
  365. info_msg "lxc image info ${_BBlue}${i}${_creset}"
  366. lxc image info "$i" | prefix_stdout "[${_BBlue}${i}${_creset}] "
  367. else
  368. warn_msg "image ${_BBlue}$i${_creset} does not yet exists"
  369. fi
  370. done
  371. }
  372. # container
  373. # ---------
  374. show_suite(){
  375. rst_title "LXC suite ($LXC_HOST_PREFIX-*)"
  376. echo
  377. lxc list "$LXC_HOST_PREFIX-"
  378. echo
  379. for i in "${CONTAINERS[@]}"; do
  380. if ! lxc_exists "$i"; then
  381. warn_msg "container ${_BBlue}$i${_creset} does not yet exists"
  382. else
  383. lxc exec -t "${i}" -- "${LXC_REPO_ROOT}/utils/lxc.sh" __show suite \
  384. | prefix_stdout "[${_BBlue}${i}${_creset}] "
  385. echo
  386. fi
  387. done
  388. }
  389. lxc_cmd() {
  390. for i in "${CONTAINERS[@]}"; do
  391. if ! lxc_exists "$i"; then
  392. warn_msg "container ${_BBlue}$i${_creset} does not yet exists"
  393. else
  394. info_msg "lxc $* $i"
  395. lxc "$@" "$i" | prefix_stdout "[${_BBlue}${i}${_creset}] "
  396. echo
  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. # ----------------------------------------------------------------------------