lxc.sh 17 KB

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