turnkey-gitlab.sh 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #!/usr/bin/env bash
  2. # A primitive script to install TurnKey LXC templates using basic settings.
  3. # Copyright (c) 2021-2023 tteck
  4. # Author: tteck (tteckster)
  5. # License: MIT
  6. # https://github.com/tteck/Proxmox/raw/main/LICENSE
  7. # bash -c "$(wget -qLO - https://github.com/tteck/Proxmox/raw/main/turnkey/turnkey-gitlab.sh)"
  8. # Setup script environment
  9. NAME="gitlab"
  10. PASS="$(openssl rand -base64 8)"
  11. CTID=$(pvesh get /cluster/nextid)
  12. TEMPLATE_SEARCH="debian-11-turnkey-${NAME}_17.1-1_amd64.tar.gz"
  13. PCT_DISK_SIZE="8"
  14. PCT_OPTIONS="
  15. -features keyctl=1,nesting=1
  16. -hostname turnkey-${NAME}
  17. -tags proxmox-helper-scripts
  18. -onboot 1
  19. -cores 4
  20. -memory 4096
  21. -password $PASS
  22. -net0 name=eth0,bridge=vmbr0,ip=dhcp
  23. -unprivileged 1
  24. "
  25. DEFAULT_PCT_OPTIONS=(
  26. -arch $(dpkg --print-architecture)
  27. )
  28. function header_info {
  29. clear
  30. cat <<"EOF"
  31. ______ __ __ ______ __ __ __
  32. /_ __/_ _________ / //_/__ __ __ / ___(_) /_/ / ___ _/ /
  33. / / / // / __/ _ \/ ,< / -_) // / / (_ / / __/ /__/ _ `/ _ \
  34. /_/ \_,_/_/ /_//_/_/|_|\__/\_, / \___/_/\__/____/\_,_/_.__/
  35. /___/
  36. EOF
  37. }
  38. header_info
  39. set -o errexit #Exit immediately if a pipeline returns a non-zero status
  40. set -o errtrace #Trap ERR from shell functions, command substitutions, and commands from subshell
  41. set -o nounset #Treat unset variables as an error
  42. set -o pipefail #Pipe will exit with last non-zero status if applicable
  43. shopt -s expand_aliases
  44. alias die='EXIT=$? LINE=$LINENO error_exit'
  45. trap die ERR
  46. function error_exit() {
  47. trap - ERR
  48. local DEFAULT='Unknown failure occured.'
  49. local REASON="\e[97m${1:-$DEFAULT}\e[39m"
  50. local FLAG="\e[91m[ERROR] \e[93m$EXIT@$LINE"
  51. msg "$FLAG $REASON" 1>&2
  52. [ ! -z ${CTID-} ] && cleanup_ctid
  53. exit $EXIT
  54. }
  55. function warn() {
  56. local REASON="\e[97m$1\e[39m"
  57. local FLAG="\e[93m[WARNING]\e[39m"
  58. msg "$FLAG $REASON"
  59. }
  60. function info() {
  61. local REASON="$1"
  62. local FLAG="\e[36m[INFO]\e[39m"
  63. msg "$FLAG $REASON"
  64. }
  65. function msg() {
  66. local TEXT="$1"
  67. echo -e "$TEXT"
  68. }
  69. function cleanup_ctid() {
  70. if pct status $CTID &>/dev/null; then
  71. if [ "$(pct status $CTID | awk '{print $2}')" == "running" ]; then
  72. pct stop $CTID
  73. fi
  74. pct destroy $CTID
  75. fi
  76. }
  77. if systemctl is-active -q ping-instances.service; then
  78. systemctl stop ping-instances.service
  79. fi
  80. function select_storage() {
  81. local CLASS=$1
  82. local CONTENT
  83. local CONTENT_LABEL
  84. case $CLASS in
  85. container) CONTENT='rootdir'; CONTENT_LABEL='Container';;
  86. template) CONTENT='vztmpl'; CONTENT_LABEL='Container template';;
  87. *) false || die "Invalid storage class.";;
  88. esac
  89. # Query all storage locations
  90. local -a MENU
  91. while read -r line; do
  92. local TAG=$(echo $line | awk '{print $1}')
  93. local TYPE=$(echo $line | awk '{printf "%-10s", $2}')
  94. local FREE=$(echo $line | numfmt --field 4-6 --from-unit=K --to=iec --format %.2f | awk '{printf( "%9sB", $6)}')
  95. local ITEM=" Type: $TYPE Free: $FREE "
  96. local OFFSET=2
  97. if [[ $((${#ITEM} + $OFFSET)) -gt ${MSG_MAX_LENGTH:-} ]]; then
  98. local MSG_MAX_LENGTH=$((${#ITEM} + $OFFSET))
  99. fi
  100. MENU+=( "$TAG" "$ITEM" "OFF" )
  101. done < <(pvesm status -content $CONTENT | awk 'NR>1')
  102. # Select storage location
  103. if [ $((${#MENU[@]}/3)) -eq 0 ]; then # No storage locations are detected
  104. warn "'$CONTENT_LABEL' needs to be selected for at least one storage location."
  105. die "Unable to detect valid storage location."
  106. elif [ $((${#MENU[@]}/3)) -eq 1 ]; then # Only one storage location is detected
  107. printf ${MENU[0]}
  108. else # More than one storage location is detected
  109. local STORAGE
  110. while [ -z "${STORAGE:+x}" ]; do # Generate graphical menu
  111. STORAGE=$(whiptail --title "Storage Pools" --radiolist \
  112. "Which storage pool you would like to use for the ${CONTENT_LABEL,,}?\n\n" \
  113. 16 $(($MSG_MAX_LENGTH + 23)) 6 \
  114. "${MENU[@]}" 3>&1 1>&2 2>&3) || die "Menu aborted."
  115. done
  116. printf $STORAGE
  117. fi
  118. }
  119. # Get template storage
  120. TEMPLATE_STORAGE=$(select_storage template) || exit
  121. info "Using '$TEMPLATE_STORAGE' for template storage."
  122. # Get container storage
  123. CONTAINER_STORAGE=$(select_storage container) || exit
  124. info "Using '$CONTAINER_STORAGE' for container storage."
  125. # Update LXC template list
  126. msg "Updating LXC template list..."
  127. pveam update >/dev/null
  128. # Get LXC template string
  129. mapfile -t TEMPLATES < <(pveam available -section turnkeylinux | sed -n "s/.*\($TEMPLATE_SEARCH.*\)/\1/p" | sort -t - -k 2 -V)
  130. [ ${#TEMPLATES[@]} -gt 0 ] || die "Unable to find a template when searching for '$TEMPLATE_SEARCH'."
  131. TEMPLATE="${TEMPLATES[-1]}"
  132. # Download LXC template
  133. if ! pveam list $TEMPLATE_STORAGE | grep -q $TEMPLATE; then
  134. msg "Downloading LXC template (Patience)..."
  135. pveam download $TEMPLATE_STORAGE $TEMPLATE >/dev/null ||
  136. die "A problem occured while downloading the LXC template."
  137. fi
  138. PCT_OPTIONS=( ${PCT_OPTIONS[@]:-${DEFAULT_PCT_OPTIONS[@]}} )
  139. [[ " ${PCT_OPTIONS[@]} " =~ " -rootfs " ]] || PCT_OPTIONS+=( -rootfs $CONTAINER_STORAGE:${PCT_DISK_SIZE:-8} )
  140. # Create LXC
  141. msg "Creating LXC container..."
  142. pct create $CTID ${TEMPLATE_STORAGE}:vztmpl/${TEMPLATE} ${PCT_OPTIONS[@]} >/dev/null ||
  143. die "A problem occured while trying to create container."
  144. # Success message
  145. msg "Starting LXC Container..."
  146. pct start "$CTID"
  147. info "LXC container '$CTID' was successfully created."
  148. echo "TurnKey ${NAME} Password" >>~/turnkey-${NAME}.creds # file is located in the Proxmox root directory
  149. echo $PASS >>~/turnkey-${NAME}.creds
  150. if [[ -f /etc/systemd/system/ping-instances.service ]]; then
  151. systemctl start ping-instances.service
  152. fi
  153. info "Proceed to the LXC console to complete the setup."
  154. info "login: root"
  155. info "password: $PASS"