lxc.sh 14 KB

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