provisioning tool for building opinionated architecture
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

189 lines
6.5 KiB

7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
  1. #!/bin/bash
  2. function check_container_missing() {
  3. if container_exists "$CONTAINER"; then
  4. echoerr "$CONTAINER already created!"
  5. exit 1
  6. fi
  7. }
  8. function usage() {
  9. echo 'USAGE with options:'
  10. echo -e "\t\tlxc-miaou-create <CONTAINER_NAME> -o sameuser[,nesting,ssh]"
  11. }
  12. function check() {
  13. check_container_missing || return 1
  14. return 0
  15. }
  16. function set_options() {
  17. declare -a options=("$@")
  18. length=${#options[@]}
  19. if [[ "$length" -ne 0 ]]; then
  20. if [[ "$length" -ne 2 ]]; then
  21. echoerr "unrecognized options: $@" && usage && exit 30
  22. else
  23. prefix="${options[0]}"
  24. option="${options[1]}"
  25. if [[ "$prefix" == '-o' ]]; then
  26. IFS=',' read -r -a options <<<"$option"
  27. for i in ${options[@]}; do
  28. case "$i" in
  29. sameuser) OPTION_SAMEUSER=true ;;
  30. nesting) OPTION_NESTING=true ;;
  31. ssh) OPTION_SSH=true ;;
  32. *) echoerr "unrecognized options: $@" && usage && exit 32 ;;
  33. esac
  34. done
  35. # echo "OPTION_SAMEUSER=$OPTION_SAMEUSER, OPTION_NESTING=$OPTION_NESTING, OPTION_SSH=$OPTION_SSH"
  36. else
  37. echoerr "unrecognized options prefix: $prefix" && usage && exit 31
  38. fi
  39. fi
  40. shift
  41. fi
  42. }
  43. function create() {
  44. local PREFIX="miaou:create"
  45. if [[ "$OPTION_SAMEUSER" == true ]]; then
  46. miaou_user=$(whoami)
  47. fi
  48. echo -n "creating new container <$CONTAINER> based on image <$CONTAINER_RELEASE>... "
  49. bridge_gw=$(lxc network get lxdbr0 ipv4.address | cut -d'/' -f1)
  50. packages=(git file bc bash-completion)
  51. [[ "$OPTION_SSH" == true ]] && packages+=(openssh-server)
  52. packages_string=$(join ', ' "${packages[@]}")
  53. user_data="$(
  54. cat <<EOF
  55. #cloud-config
  56. timezone: 'Indian/Reunion'
  57. apt:
  58. preserve_sources_list: false
  59. conf: |
  60. Acquire::Retries "60";
  61. DPkg::Lock::Timeout "60";
  62. primary:
  63. - arches: [default]
  64. uri: http://debian.mithril.re/debian
  65. security:
  66. - arches: [default]
  67. uri: http://debian.mithril.re/debian-security
  68. sources_list: |
  69. # generated by miaou-cloud
  70. deb \$PRIMARY \$RELEASE main
  71. deb \$PRIMARY \$RELEASE-updates main
  72. deb \$SECURITY \$RELEASE-security main
  73. package_update: true
  74. package_upgrade: true
  75. package_reboot_if_required: true
  76. packages: $packages_string
  77. write_files:
  78. - path: /etc/sudoers.d/10-add_TOOLBOX_to_secure_path
  79. content: >
  80. Defaults secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/TOOLBOX"
  81. runcmd:
  82. - [ systemctl, mask, systemd-hostnamed.service ]
  83. - [ systemctl, disable, e2scrub_reap.service ]
  84. - [ systemctl, disable, systemd-resolved.service, --now ]
  85. - [ systemctl, reset-failed ]
  86. - [ rm, /etc/resolv.conf]
  87. - [ rm, /etc/sudoers.d/90-cloud-init-users]
  88. - "echo nameserver $bridge_gw > /etc/resolv.conf"
  89. final_message: "Container from datasource \$datasource is finally up, after \$UPTIME seconds"
  90. EOF
  91. )"
  92. lxc init local:debian/$CONTAINER_RELEASE/cloud "$CONTAINER" --config user.user-data="$user_data" -q
  93. # allow directory `SHARED` to be read-write mounted
  94. lxc config set "$CONTAINER" raw.idmap "both $(id -u) 0" -q
  95. mkdir -p "$HOME/LXD/SHARED/$CONTAINER"
  96. lxc config device add "$CONTAINER" SHARED disk source="$HOME/LXD/SHARED/$CONTAINER" path=/mnt/SHARED -q
  97. lxc config device add "$CONTAINER" TOOLBOX disk source=/TOOLBOX path=/TOOLBOX -q
  98. lxc config device add "$CONTAINER" MIAOU_BASH disk source=$(realpath /opt/miaou-bash) path=/opt/miaou-bash -q
  99. # environment variables
  100. lxc config set "$CONTAINER" environment.PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/miaou-bash/tools:/TOOLBOX -q
  101. lxc config set "$CONTAINER" environment.container lxc -q
  102. if [[ "$OPTION_NESTING" == true ]]; then
  103. lxc config set "$CONTAINER" security.nesting true -q
  104. lxc config device add "$CONTAINER" miaou disk source=/opt/miaou path=/opt/miaou -q
  105. fi
  106. lxc start "$CONTAINER" -q
  107. # initializing miaou-bash
  108. lxc exec "$CONTAINER" -- /opt/miaou-bash/init.sh
  109. # default configuration files (btm,)
  110. lxc exec "$CONTAINER" -- mkdir -p /root/.config/bottom
  111. lxc file push "$MIAOU_BASEDIR/templates/bottom/bottom.toml" "$CONTAINER/root/.config/bottom/bottom.toml" -q
  112. # purge cloud-init after success
  113. attempt=0
  114. max_attempt=10
  115. delay=0.2
  116. while ! lxc exec "$CONTAINER" -- bash -c 'systemd-run -q -p After=cloud-final.service -p Type=oneshot --no-block bash -c "\
  117. cloud-init status --wait &&\
  118. cp /var/lib/cloud/data/status.json /root/cloud-status.json &&\
  119. systemctl stop cloud-{config,final,init-local,init}.service &&\
  120. systemctl disable cloud-{config,final,init-local,init}.service &&\
  121. systemctl stop cloud-config.target cloud-init.target &&\
  122. apt-get purge -y cloud-init &&\
  123. rm -rf /var/lib/cloud && \
  124. userdel -rf debian \
  125. " 2>/dev/null '; do
  126. attempt=$((attempt++))
  127. if [[ $attempt -gt $max_attempt ]]; then
  128. echoerr "systemd unavailable after $(bc <<<"$max_attempt * $delay") seconds"
  129. exit 1
  130. else
  131. sleep $delay
  132. fi
  133. done
  134. if [[ "$OPTION_SAMEUSER" == true ]]; then
  135. if ! lxc exec "$CONTAINER" -- grep "$miaou_user" /etc/passwd; then
  136. lxc exec "$CONTAINER" -- useradd -ms /bin/bash -G sudo "$miaou_user"
  137. fi
  138. if ! lxc exec "$CONTAINER" -- passwd -S "$miaou_user" | cut -d ' ' -f2 | grep -q ^P; then
  139. shadow_passwd=$(load_yaml_from_expanded credential.shadow)
  140. shadow_remainder=$(lxc exec "$CONTAINER" -- bash -c "grep $miaou_user /etc/shadow | cut -d':' -f3-")
  141. lxc exec "$CONTAINER" -- /opt/miaou-bash/tools/append_or_replace "^$miaou_user:.*:" "$miaou_user:$shadow_passwd:$shadow_remainder" /etc/shadow >/dev/null
  142. fi
  143. fi
  144. if [[ "$OPTION_SSH" == true && "$OPTION_SAMEUSER" == true ]]; then
  145. lxc-miaou-enable-ssh "$CONTAINER"
  146. fi
  147. PREFIX="" echoinfo OK
  148. echo "hint: \`lxc login $CONTAINER [--env user=<USER>]\`"
  149. [[ "$OPTION_SAMEUSER" == true ]] && echo "hint: \`lxc sameuser $CONTAINER\`"
  150. true
  151. }
  152. ## MAIN
  153. . "$MIAOU_BASEDIR/lib/init.sh"
  154. OPTION_SAMEUSER=false
  155. OPTION_NESTING=false
  156. OPTION_SSH=false
  157. PREFIX="miaou"
  158. arg1_required "$@" || (usage && exit 1)
  159. readonly CONTAINER=$1
  160. readonly CONTAINER_RELEASE="bookworm"
  161. shift
  162. set_options "$@"
  163. readonly FULL_OPTIONS="$@"
  164. check
  165. create