postgresql-v4.sh 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. #!/usr/bin/env bash
  2. echo -e "Loading..."
  3. APP="PostgreSQL"
  4. var_disk="4"
  5. var_cpu="1"
  6. var_ram="1024"
  7. var_os="debian"
  8. var_version="11"
  9. NSAPP=$(echo ${APP,,} | tr -d ' ')
  10. var_install="${NSAPP}-install"
  11. NEXTID=$(pvesh get /cluster/nextid)
  12. INTEGER='^[0-9]+$'
  13. YW=$(echo "\033[33m")
  14. BL=$(echo "\033[36m")
  15. RD=$(echo "\033[01;31m")
  16. BGN=$(echo "\033[4;92m")
  17. GN=$(echo "\033[1;92m")
  18. DGN=$(echo "\033[32m")
  19. CL=$(echo "\033[m")
  20. BFR="\\r\\033[K"
  21. HOLD="-"
  22. CM="${GN}✓${CL}"
  23. set -o errexit
  24. set -o errtrace
  25. set -o nounset
  26. set -o pipefail
  27. shopt -s expand_aliases
  28. alias die='EXIT=$? LINE=$LINENO error_exit'
  29. trap die ERR
  30. function error_exit() {
  31. trap - ERR
  32. local reason="Unknown failure occurred."
  33. local msg="${1:-$reason}"
  34. local flag="${RD}‼ ERROR ${CL}$EXIT@$LINE"
  35. echo -e "$flag $msg" 1>&2
  36. exit $EXIT
  37. }
  38. if (whiptail --title "${APP} LXC" --yesno "This will create a New ${APP} LXC. Proceed?" 10 58); then
  39. echo "User selected Yes"
  40. else
  41. clear
  42. echo -e "⚠ User exited script \n"
  43. exit
  44. fi
  45. function header_info {
  46. echo -e "${BL}
  47. ____ __ _____ ____ __
  48. / __ \____ _____/ /_____ _________ / ___// __ \ / /
  49. / /_/ / __ \/ ___/ __/ __ / ___/ _ \\__ \/ / / / / /
  50. / ____/ /_/ (__ ) /_/ /_/ / / / __/__/ / /_/ / / /___
  51. /_/ \____/____/\__/\__, /_/v4 \___/____/\___\_\/_____/
  52. /____/
  53. ${CL}"
  54. }
  55. function msg_info() {
  56. local msg="$1"
  57. echo -ne " ${HOLD} ${YW}${msg}..."
  58. }
  59. function msg_ok() {
  60. local msg="$1"
  61. echo -e "${BFR} ${CM} ${GN}${msg}${CL}"
  62. }
  63. function PVE_CHECK() {
  64. PVE=$(pveversion | grep "pve-manager/7" | wc -l)
  65. if [[ $PVE != 1 ]]; then
  66. echo -e "${RD}This script requires Proxmox Virtual Environment 7.0 or greater${CL}"
  67. echo -e "Exiting..."
  68. sleep 2
  69. exit
  70. fi
  71. }
  72. function default_settings() {
  73. echo -e "${DGN}Using Container Type: ${BGN}Unprivileged${CL} ${RD}NO DEVICE PASSTHROUGH${CL}"
  74. CT_TYPE="1"
  75. echo -e "${DGN}Using Root Password: ${BGN}Automatic Login${CL}"
  76. PW=""
  77. echo -e "${DGN}Using Container ID: ${BGN}$NEXTID${CL}"
  78. CT_ID=$NEXTID
  79. echo -e "${DGN}Using Hostname: ${BGN}$NSAPP${CL}"
  80. HN=$NSAPP
  81. echo -e "${DGN}Using Disk Size: ${BGN}$var_disk${CL}${DGN}GB${CL}"
  82. DISK_SIZE="$var_disk"
  83. echo -e "${DGN}Allocated Cores ${BGN}$var_cpu${CL}"
  84. CORE_COUNT="$var_cpu"
  85. echo -e "${DGN}Allocated Ram ${BGN}$var_ram${CL}"
  86. RAM_SIZE="$var_ram"
  87. echo -e "${DGN}Using Bridge: ${BGN}vmbr0${CL}"
  88. BRG="vmbr0"
  89. echo -e "${DGN}Using Static IP Address: ${BGN}dhcp${CL}"
  90. NET=dhcp
  91. echo -e "${DGN}Using Gateway Address: ${BGN}Default${CL}"
  92. GATE=""
  93. echo -e "${DGN}Using MAC Address: ${BGN}Default${CL}"
  94. MAC=""
  95. echo -e "${DGN}Using VLAN Tag: ${BGN}Default${CL}"
  96. VLAN=""
  97. echo -e "${DGN}Enable Root SSH Access: ${BGN}No${CL}"
  98. SSH="no"
  99. echo -e "${BL}Creating a ${APP} LXC using the above default settings${CL}"
  100. }
  101. function advanced_settings() {
  102. CT_TYPE=$(whiptail --title "CONTAINER TYPE" --radiolist --cancel-button Exit-Script "Choose Type" 8 58 2 \
  103. "1" "Unprivileged" ON \
  104. "0" "Privileged" OFF \
  105. 3>&1 1>&2 2>&3)
  106. exitstatus=$?
  107. if [ $exitstatus = 0 ]; then
  108. echo -e "${DGN}Using Container Type: ${BGN}$CT_TYPE${CL}"
  109. fi
  110. PW1=$(whiptail --inputbox "Set Root Password (needed for root ssh access)" 8 58 --title "PASSWORD(leave blank for automatic login)" --cancel-button Exit-Script 3>&1 1>&2 2>&3)
  111. exitstatus=$?
  112. if [ $exitstatus = 0 ]; then
  113. if [ -z $PW1 ]; then
  114. PW1="Automatic Login" PW=" "
  115. echo -e "${DGN}Using Root Password: ${BGN}$PW1${CL}"
  116. else
  117. PW="-password $PW1"
  118. echo -e "${DGN}Using Root Password: ${BGN}$PW1${CL}"
  119. fi
  120. fi
  121. CT_ID=$(whiptail --inputbox "Set Container ID" 8 58 $NEXTID --title "CONTAINER ID" --cancel-button Exit-Script 3>&1 1>&2 2>&3)
  122. exitstatus=$?
  123. if [ -z $CT_ID ]; then
  124. CT_ID="$NEXTID"
  125. echo -e "${DGN}Container ID: ${BGN}$CT_ID${CL}"
  126. else
  127. if [ $exitstatus = 0 ]; then echo -e "${DGN}Using Container ID: ${BGN}$CT_ID${CL}"; fi
  128. fi
  129. CT_NAME=$(whiptail --inputbox "Set Hostname" 8 58 $NSAPP --title "HOSTNAME" --cancel-button Exit-Script 3>&1 1>&2 2>&3)
  130. exitstatus=$?
  131. if [ -z $CT_NAME ]; then
  132. HN="$NSAPP"
  133. echo -e "${DGN}Using Hostname: ${BGN}$HN${CL}"
  134. else
  135. if [ $exitstatus = 0 ]; then
  136. HN=$(echo ${CT_NAME,,} | tr -d ' ')
  137. echo -e "${DGN}Using Hostname: ${BGN}$HN${CL}"
  138. fi
  139. fi
  140. DISK_SIZE=$(whiptail --inputbox "Set Disk Size in GB" 8 58 $var_disk --title "DISK SIZE" --cancel-button Exit-Script 3>&1 1>&2 2>&3)
  141. exitstatus=$?
  142. if [ -z $DISK_SIZE ]; then
  143. DISK_SIZE="$var_disk"
  144. echo -e "${DGN}Using Disk Size: ${BGN}$DISK_SIZE${CL}"
  145. else
  146. if [ $exitstatus = 0 ]; then echo -e "${DGN}Using Disk Size: ${BGN}$DISK_SIZE${CL}"; fi
  147. if ! [[ $DISK_SIZE =~ $INTEGER ]]; then
  148. echo -e "${RD}⚠ DISK SIZE MUST BE A INTEGER NUMBER!${CL}"
  149. advanced_settings
  150. fi
  151. fi
  152. CORE_COUNT=$(whiptail --inputbox "Allocate CPU Cores" 8 58 $var_cpu --title "CORE COUNT" --cancel-button Exit-Script 3>&1 1>&2 2>&3)
  153. exitstatus=$?
  154. if [ -z $CORE_COUNT ]; then
  155. CORE_COUNT="$var_cpu"
  156. echo -e "${DGN}Allocated Cores: ${BGN}$CORE_COUNT${CL}"
  157. else
  158. if [ $exitstatus = 0 ]; then echo -e "${DGN}Allocated Cores: ${BGN}$CORE_COUNT${CL}"; fi
  159. fi
  160. RAM_SIZE=$(whiptail --inputbox "Allocate RAM in MiB" 8 58 $var_ram --title "RAM" --cancel-button Exit-Script 3>&1 1>&2 2>&3)
  161. exitstatus=$?
  162. if [ -z $RAM_SIZE ]; then
  163. RAM_SIZE="$var_ram"
  164. echo -e "${DGN}Allocated RAM: ${BGN}$RAM_SIZE${CL}"
  165. else
  166. if [ $exitstatus = 0 ]; then echo -e "${DGN}Allocated RAM: ${BGN}$RAM_SIZE${CL}"; fi
  167. fi
  168. BRG=$(whiptail --inputbox "Set a Bridge" 8 58 vmbr0 --title "BRIDGE" --cancel-button Exit-Script 3>&1 1>&2 2>&3)
  169. exitstatus=$?
  170. if [ -z $BRG ]; then
  171. BRG="vmbr0"
  172. echo -e "${DGN}Using Bridge: ${BGN}$BRG${CL}"
  173. else
  174. if [ $exitstatus = 0 ]; then echo -e "${DGN}Using Bridge: ${BGN}$BRG${CL}"; fi
  175. fi
  176. NET=$(whiptail --inputbox "Set a Static IPv4 CIDR Address(/24)" 8 58 dhcp --title "IP ADDRESS" --cancel-button Exit-Script 3>&1 1>&2 2>&3)
  177. exitstatus=$?
  178. if [ -z $NET ]; then
  179. NET="dhcp"
  180. echo -e "${DGN}Using IP Address: ${BGN}$NET${CL}"
  181. else
  182. if [ $exitstatus = 0 ]; then echo -e "${DGN}Using IP Address: ${BGN}$NET${CL}"; fi
  183. fi
  184. GATE1=$(whiptail --inputbox "Set a Gateway IP (mandatory if Static IP was used)" 8 58 --title "GATEWAY IP" --cancel-button Exit-Script 3>&1 1>&2 2>&3)
  185. exitstatus=$?
  186. if [ $exitstatus = 0 ]; then
  187. if [ -z $GATE1 ]; then
  188. GATE1="Default" GATE=""
  189. echo -e "${DGN}Using Gateway IP Address: ${BGN}$GATE1${CL}"
  190. else
  191. GATE=",gw=$GATE1"
  192. echo -e "${DGN}Using Gateway IP Address: ${BGN}$GATE1${CL}"
  193. fi
  194. fi
  195. MAC1=$(whiptail --inputbox "Set a MAC Address(leave blank for default)" 8 58 --title "MAC ADDRESS" --cancel-button Exit-Script 3>&1 1>&2 2>&3)
  196. exitstatus=$?
  197. if [ $exitstatus = 0 ]; then
  198. if [ -z $MAC1 ]; then
  199. MAC1="Default" MAC=""
  200. echo -e "${DGN}Using MAC Address: ${BGN}$MAC1${CL}"
  201. else
  202. MAC=",hwaddr=$MAC1"
  203. echo -e "${DGN}Using MAC Address: ${BGN}$MAC1${CL}"
  204. fi
  205. fi
  206. VLAN1=$(whiptail --inputbox "Set a Vlan(leave blank for default)" 8 58 --title "VLAN" --cancel-button Exit-Script 3>&1 1>&2 2>&3)
  207. exitstatus=$?
  208. if [ $exitstatus = 0 ]; then
  209. if [ -z $VLAN1 ]; then
  210. VLAN1="Default" VLAN=""
  211. echo -e "${DGN}Using Vlan: ${BGN}$VLAN1${CL}"
  212. else
  213. VLAN=",tag=$VLAN1"
  214. echo -e "${DGN}Using Vlan: ${BGN}$VLAN1${CL}"
  215. fi
  216. fi
  217. if (whiptail --defaultno --title "SSH ACCESS" --yesno "Enable Root SSH Access?" 10 58); then
  218. echo -e "${DGN}Enable Root SSH Access: ${BGN}Yes${CL}"
  219. SSH="yes"
  220. else
  221. echo -e "${DGN}Enable Root SSH Access: ${BGN}No${CL}"
  222. SSH="no"
  223. fi
  224. if (whiptail --title "ADVANCED SETTINGS COMPLETE" --yesno "Ready to create ${APP} LXC?" --no-button Do-Over 10 58); then
  225. echo -e "${RD}Creating a ${APP} LXC using the above advanced settings${CL}"
  226. else
  227. clear
  228. header_info
  229. echo -e "${RD}Using Advanced Settings${CL}"
  230. advanced_settings
  231. fi
  232. }
  233. function start_script() {
  234. if (whiptail --title "SETTINGS" --yesno "Use Default Settings?" --no-button Advanced 10 58); then
  235. header_info
  236. echo -e "${BL}Using Default Settings${CL}"
  237. default_settings
  238. else
  239. header_info
  240. echo -e "${RD}Using Advanced Settings${CL}"
  241. advanced_settings
  242. fi
  243. }
  244. clear
  245. start_script
  246. if [ "$CT_TYPE" == "1" ]; then
  247. FEATURES="nesting=1,keyctl=1"
  248. else
  249. FEATURES="nesting=1"
  250. fi
  251. TEMP_DIR=$(mktemp -d)
  252. pushd $TEMP_DIR >/dev/null
  253. export SSH_ROOT=${SSH}
  254. export CTID=$CT_ID
  255. export PCT_OSTYPE=$var_os
  256. export PCT_OSVERSION=$var_version
  257. export PCT_DISK_SIZE=$DISK_SIZE
  258. export PCT_OPTIONS="
  259. -features $FEATURES
  260. -hostname $HN
  261. -net0 name=eth0,bridge=$BRG$MAC,ip=$NET$GATE$VLAN
  262. -onboot 1
  263. -cores $CORE_COUNT
  264. -memory $RAM_SIZE
  265. -unprivileged $CT_TYPE
  266. $PW
  267. "
  268. bash -c "$(wget -qLO - https://raw.githubusercontent.com/tteck/Proxmox/main/ct/create_lxc.sh)" || exit
  269. msg_info "Starting LXC Container"
  270. pct start $CTID
  271. msg_ok "Started LXC Container"
  272. lxc-attach -n $CTID -- bash -c "$(wget -qLO - https://raw.githubusercontent.com/tteck/Proxmox/main/setup/$var_install.sh)" || exit
  273. IP=$(pct exec $CTID ip a s dev eth0 | sed -n '/inet / s/\// /p' | awk '{print $2}')
  274. pct set $CTID -description "# ${APP} LXC
  275. ### https://tteck.github.io/Proxmox/
  276. <a href='https://ko-fi.com/D1D7EP4GF'><img src='https://img.shields.io/badge/☕-Buy me a coffee-red' /></a>"
  277. msg_ok "Completed Successfully!\n"