lxc.sh 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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]
  65. $_cmd copy [images]
  66. $_cmd remove [containers|<name>|images]
  67. $_cmd [start|stop] [containers|<name>]
  68. $_cmd show [info|config|suite|images]
  69. $_cmd cmd [--|<name>] '...'
  70. $_cmd install [suite|base|buildhost]
  71. build
  72. :containers: build, launch and 'install basic' packages on 'containers'
  73. copy:
  74. :images: copy remote images of the suite into local storage
  75. remove
  76. :containers: delete all 'containers' or only <container-name>
  77. :images: delete local images of the suite
  78. start/stop
  79. :containers: start/stop all 'containers' from the suite
  80. :<name>: start/stop conatiner <name> from suite
  81. show
  82. :info: show info of all the containers from LXC suite
  83. :config: show config of all the containers from the LXC suite
  84. :suite: show services of all the containers from the LXC suite
  85. :images: show information of local images
  86. cmd
  87. use single qoutes to evaluate in container's bash, e.g. 'echo $(hostname)'
  88. -- run command '...' in all containers of the LXC suite
  89. :<name>: run command '...' in container <name>
  90. install
  91. :suite: install LXC suite; ${lxc_suite_install_info}
  92. :base: prepare LXC; install basic packages
  93. :buildhost: prepare LXC; install buildhost 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. ! required_commands lxc && lxd_info && exit 42
  127. [[ -z $LXC_SUITE ]] && err_msg "missing LXC_SUITE" && exit 42
  128. fi
  129. case $1 in
  130. --source-only) ;;
  131. -h|--help) usage; exit 0;;
  132. build)
  133. sudo_or_exit
  134. case $2 in
  135. ''|containers) build_instances ;;
  136. *) usage "$_usage"; exit 42;;
  137. esac
  138. ;;
  139. copy)
  140. case $2 in
  141. ''|images) lxc_copy_images_localy;;
  142. *) usage "$_usage"; exit 42;;
  143. esac
  144. ;;
  145. remove)
  146. sudo_or_exit
  147. case $2 in
  148. ''|containers) remove_instances ;;
  149. images) lxc_delete_images_localy ;;
  150. ${LXC_HOST_PREFIX}-*)
  151. ! lxc_exists "$2" && usage_containers "unknown container: $2" && exit 42
  152. if ask_yn "Do you really want to delete conatiner $2"; then
  153. lxc_delete_container "$2"
  154. fi
  155. ;;
  156. *) usage "uknown or missing container <name> $2"; exit 42;;
  157. esac
  158. ;;
  159. start|stop)
  160. sudo_or_exit
  161. case $2 in
  162. ''|containers) lxc_cmd "$1" ;;
  163. ${LXC_HOST_PREFIX}-*)
  164. ! lxc_exists "$2" && usage_containers "unknown container: $2" && exit 42
  165. info_msg "lxc $1 $2"
  166. lxc "$1" "$2" | prefix_stdout "[${_BBlue}${i}${_creset}] "
  167. ;;
  168. *) usage "uknown or missing container <name> $2"; exit 42;;
  169. esac
  170. ;;
  171. show)
  172. sudo_or_exit
  173. case $2 in
  174. suite) show_suite ;;
  175. images) show_images ;;
  176. config)
  177. rst_title "container configurations"
  178. echo
  179. lxc list "$LXC_HOST_PREFIX-"
  180. echo
  181. lxc_cmd config show
  182. ;;
  183. info)
  184. rst_title "container info"
  185. echo
  186. lxc_cmd info
  187. ;;
  188. *) usage "$_usage"; exit 42;;
  189. esac
  190. ;;
  191. __show)
  192. # wrapped show commands, called once in each container
  193. case $2 in
  194. suite) lxc_suite_info ;;
  195. esac
  196. ;;
  197. cmd)
  198. sudo_or_exit
  199. shift
  200. case $1 in
  201. --) shift; lxc_exec "$@" ;;
  202. ${LXC_HOST_PREFIX}-*)
  203. ! lxc_exists "$1" && usage_containers "unknown container: $1" && exit 42
  204. local name=$1
  205. shift
  206. lxc_exec_cmd "${name}" "$@"
  207. ;;
  208. *) usage "uknown or missing container <name> $1"; exit 42;;
  209. esac
  210. ;;
  211. install)
  212. sudo_or_exit
  213. case $2 in
  214. suite|base|buildhost)
  215. lxc_exec "${LXC_REPO_ROOT}/utils/lxc.sh" __install $2
  216. ;;
  217. *) usage "$_usage"; exit 42 ;;
  218. esac
  219. ;;
  220. __install)
  221. # wrapped install commands, called once in each container
  222. case $2 in
  223. suite) lxc_suite_install ;;
  224. base) FORCE_TIMEOUT=0 lxc_install_base_packages ;;
  225. buildhost) lxc_suite_prepare_buildhost ;;
  226. esac
  227. ;;
  228. doc)
  229. echo
  230. echo ".. generic utils/lxc.sh documentation"
  231. ;;
  232. -*) usage "unknown option $1"; exit 42;;
  233. *) usage "unknown or missing command $1"; exit 42;;
  234. esac
  235. }
  236. build_instances() {
  237. rst_title "Build LXC instances"
  238. lxc_copy_images_localy
  239. echo
  240. rst_title "build containers" section
  241. echo
  242. lxc_init_containers
  243. lxc_config_containers
  244. lxc_boilerplate_containers
  245. echo
  246. lxc_exec "${LXC_REPO_ROOT}/utils/lxc.sh" __install base
  247. echo
  248. lxc list "$LXC_HOST_PREFIX"
  249. }
  250. remove_instances() {
  251. rst_title "Remove LXC instances"
  252. rst_para "existing containers matching ${_BGreen}$LXC_HOST_PREFIX-*${_creset}"
  253. echo
  254. lxc list "$LXC_HOST_PREFIX-"
  255. echo -en "\\n${_BRed}LXC containers to delete::${_creset}\\n\\n ${CONTAINERS[*]}\\n" | $FMT
  256. if ask_yn "Do you really want to delete these conatiners"; then
  257. for i in "${CONTAINERS[@]}"; do
  258. lxc_delete_container "$i"
  259. done
  260. fi
  261. echo
  262. lxc list "$LXC_HOST_PREFIX-"
  263. }
  264. # images
  265. # ------
  266. lxc_copy_images_localy() {
  267. rst_title "copy images" section
  268. echo
  269. for ((i=0; i<${#LXC_SUITE[@]}; i+=2)); do
  270. if lxc_image_exists "local:${LXC_SUITE[i+1]}"; then
  271. info_msg "image ${LXC_SUITE[i]} already copied --> ${LXC_SUITE[i+1]}"
  272. else
  273. info_msg "copy image locally ${LXC_SUITE[i]} --> ${LXC_SUITE[i+1]}"
  274. lxc image copy "${LXC_SUITE[i]}" local: \
  275. --alias "${LXC_SUITE[i+1]}" | prefix_stdout
  276. fi
  277. done
  278. # lxc image list local: && wait_key
  279. }
  280. lxc_delete_images_localy() {
  281. rst_title "Delete LXC images"
  282. rst_para "local existing images"
  283. echo
  284. lxc image list local:
  285. echo -en "\\n${_BRed}LXC images to delete::${_creset}\\n\\n ${LOCAL_IMAGES[*]}\\n"
  286. if ask_yn "Do you really want to delete these images"; then
  287. for i in "${LOCAL_IMAGES[@]}"; do
  288. lxc_delete_local_image "$i"
  289. done
  290. fi
  291. echo
  292. lxc image list local:
  293. }
  294. show_images(){
  295. rst_title "local images"
  296. echo
  297. lxc image list local:
  298. echo -en "\\n${_Green}LXC suite images::${_creset}\\n\\n ${LOCAL_IMAGES[*]}\\n"
  299. wait_key
  300. for i in "${LOCAL_IMAGES[@]}"; do
  301. if lxc_image_exists "$i"; then
  302. info_msg "lxc image info ${_BBlue}${i}${_creset}"
  303. lxc image info "$i" | prefix_stdout "[${_BBlue}${i}${_creset}] "
  304. else
  305. warn_msg "image ${_BBlue}$i${_creset} does not yet exists"
  306. fi
  307. done
  308. }
  309. # container
  310. # ---------
  311. show_suite(){
  312. rst_title "LXC suite ($LXC_HOST_PREFIX-*)"
  313. echo
  314. lxc list "$LXC_HOST_PREFIX-"
  315. echo
  316. for i in "${CONTAINERS[@]}"; do
  317. if ! lxc_exists "$i"; then
  318. warn_msg "container ${_BBlue}$i${_creset} does not yet exists"
  319. else
  320. lxc exec -t "${i}" -- "${LXC_REPO_ROOT}/utils/lxc.sh" __show suite \
  321. | prefix_stdout "[${_BBlue}${i}${_creset}] "
  322. echo
  323. fi
  324. done
  325. }
  326. lxc_cmd() {
  327. for i in "${CONTAINERS[@]}"; do
  328. if ! lxc_exists "$i"; then
  329. warn_msg "container ${_BBlue}$i${_creset} does not yet exists"
  330. else
  331. info_msg "lxc $* $i"
  332. lxc "$@" "$i" | prefix_stdout "[${_BBlue}${i}${_creset}] "
  333. echo
  334. fi
  335. done
  336. }
  337. lxc_exec_cmd() {
  338. local name="$1"
  339. shift
  340. exit_val=
  341. info_msg "[${_BBlue}${name}${_creset}] ${_BGreen}${*}${_creset}"
  342. lxc exec -t --cwd "${LXC_REPO_ROOT}" "${name}" -- bash -c "$*"
  343. exit_val=$?
  344. if [[ $exit_val -ne 0 ]]; then
  345. warn_msg "[${_BBlue}${name}${_creset}] exit code (${_BRed}${exit_val}${_creset}) from ${_BGreen}${*}${_creset}"
  346. else
  347. info_msg "[${_BBlue}${name}${_creset}] exit code (${exit_val}) from ${_BGreen}${*}${_creset}"
  348. fi
  349. }
  350. lxc_exec() {
  351. for i in "${CONTAINERS[@]}"; do
  352. if ! lxc_exists "$i"; then
  353. warn_msg "container ${_BBlue}$i${_creset} does not yet exists"
  354. else
  355. lxc_exec_cmd "${i}" "$@" | prefix_stdout "[${_BBlue}${i}${_creset}] "
  356. fi
  357. done
  358. }
  359. lxc_init_containers() {
  360. local image_name
  361. local container_name
  362. for ((i=0; i<${#LXC_SUITE[@]}; i+=2)); do
  363. image_name="${LXC_SUITE[i+1]}"
  364. container_name="${LXC_HOST_PREFIX}-${image_name}"
  365. if lxc info "${container_name}" &>/dev/null; then
  366. info_msg "container '${container_name}' already exists"
  367. else
  368. info_msg "create conatiner instance: ${container_name}"
  369. lxc init "local:${image_name}" "${container_name}"
  370. fi
  371. done
  372. }
  373. lxc_config_containers() {
  374. for i in "${CONTAINERS[@]}"; do
  375. info_msg "[${_BBlue}${i}${_creset}] configure container ..."
  376. info_msg "[${_BBlue}${i}${_creset}] map uid/gid from host to container"
  377. # https://lxd.readthedocs.io/en/latest/userns-idmap/#custom-idmaps
  378. echo -e -n "uid $HOST_USER_ID 0\\ngid $HOST_GROUP_ID 0"\
  379. | lxc config set "$i" raw.idmap -
  380. info_msg "[${_BBlue}${i}${_creset}] share ${REPO_ROOT} (repo_share) from HOST into container"
  381. # https://lxd.readthedocs.io/en/latest/instances/#type-disk
  382. lxc config device add "$i" repo_share disk \
  383. source="${REPO_ROOT}" \
  384. path="${LXC_REPO_ROOT}" &>/dev/null
  385. # lxc config show "$i" && wait_key
  386. done
  387. }
  388. lxc_boilerplate_containers() {
  389. local image_name
  390. local container_name
  391. local boilerplate_script
  392. for ((i=0; i<${#LXC_SUITE[@]}; i+=2)); do
  393. image_name="${LXC_SUITE[i+1]}"
  394. container_name="${LXC_HOST_PREFIX}-${image_name}"
  395. boilerplate_script="${image_name}_boilerplate"
  396. boilerplate_script="${!boilerplate_script}"
  397. info_msg "[${_BBlue}${container_name}${_creset}] init .."
  398. if lxc start -q "${container_name}" &>/dev/null; then
  399. sleep 5 # guest needs some time to come up and get an IP
  400. fi
  401. lxc_init_container "${container_name}"
  402. info_msg "[${_BBlue}${container_name}${_creset}] install /.lxcenv.mk .."
  403. cat <<EOF | lxc exec "${container_name}" -- bash | prefix_stdout "[${_BBlue}${container_name}${_creset}] "
  404. rm -f "/.lxcenv.mk"
  405. ln -s "${LXC_REPO_ROOT}/utils/makefile.lxc" "/.lxcenv.mk"
  406. ls -l "/.lxcenv.mk"
  407. EOF
  408. info_msg "[${_BBlue}${container_name}${_creset}] install boilerplate .."
  409. if lxc start -q "${container_name}" &>/dev/null; then
  410. sleep 5 # guest needs some time to come up and get an IP
  411. fi
  412. if [[ -n "${boilerplate_script}" ]]; then
  413. echo "${boilerplate_script}" \
  414. | lxc exec "${container_name}" -- bash \
  415. | prefix_stdout "[${_BBlue}${container_name}${_creset}] "
  416. else
  417. err_msg "[${_BBlue}${container_name}${_creset}] no boilerplate for image '${image_name}'"
  418. fi
  419. done
  420. }
  421. # ----------------------------------------------------------------------------
  422. main "$@"
  423. # ----------------------------------------------------------------------------