postgresql-v4.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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 DNS Search Domain: ${BGN}Host${CL}"
  94. SD=""
  95. echo -e "${DGN}Using DNS Server Address: ${BGN}Host${CL}"
  96. NS=""
  97. echo -e "${DGN}Using MAC Address: ${BGN}Default${CL}"
  98. MAC=""
  99. echo -e "${DGN}Using VLAN Tag: ${BGN}Default${CL}"
  100. VLAN=""
  101. echo -e "${DGN}Enable Root SSH Access: ${BGN}No${CL}"
  102. SSH="no"
  103. echo -e "${DGN}Enable Verbose Mode: ${BGN}No${CL}"
  104. VERB="no"
  105. echo -e "${BL}Creating a ${APP} LXC using the above default settings${CL}"
  106. }
  107. function advanced_settings() {
  108. CT_TYPE=$(whiptail --title "CONTAINER TYPE" --radiolist --cancel-button Exit-Script "Choose Type" 10 58 2 \
  109. "1" "Unprivileged" ON \
  110. "0" "Privileged" OFF \
  111. 3>&1 1>&2 2>&3)
  112. exitstatus=$?
  113. if [ $exitstatus = 0 ]; then
  114. echo -e "${DGN}Using Container Type: ${BGN}$CT_TYPE${CL}"
  115. fi
  116. 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)
  117. exitstatus=$?
  118. if [ $exitstatus = 0 ]; then
  119. if [ -z $PW1 ]; then
  120. PW1="Automatic Login" PW=" "
  121. echo -e "${DGN}Using Root Password: ${BGN}$PW1${CL}"
  122. else
  123. PW="-password $PW1"
  124. echo -e "${DGN}Using Root Password: ${BGN}$PW1${CL}"
  125. fi
  126. fi
  127. CT_ID=$(whiptail --inputbox "Set Container ID" 8 58 $NEXTID --title "CONTAINER ID" --cancel-button Exit-Script 3>&1 1>&2 2>&3)
  128. exitstatus=$?
  129. if [ -z $CT_ID ]; then
  130. CT_ID="$NEXTID"
  131. echo -e "${DGN}Container ID: ${BGN}$CT_ID${CL}"
  132. else
  133. if [ $exitstatus = 0 ]; then echo -e "${DGN}Using Container ID: ${BGN}$CT_ID${CL}"; fi
  134. fi
  135. CT_NAME=$(whiptail --inputbox "Set Hostname" 8 58 $NSAPP --title "HOSTNAME" --cancel-button Exit-Script 3>&1 1>&2 2>&3)
  136. exitstatus=$?
  137. if [ -z $CT_NAME ]; then
  138. HN="$NSAPP"
  139. echo -e "${DGN}Using Hostname: ${BGN}$HN${CL}"
  140. else
  141. if [ $exitstatus = 0 ]; then
  142. HN=$(echo ${CT_NAME,,} | tr -d ' ')
  143. echo -e "${DGN}Using Hostname: ${BGN}$HN${CL}"
  144. fi
  145. fi
  146. 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)
  147. exitstatus=$?
  148. if [ -z $DISK_SIZE ]; then
  149. DISK_SIZE="$var_disk"
  150. echo -e "${DGN}Using Disk Size: ${BGN}$DISK_SIZE${CL}"
  151. else
  152. if [ $exitstatus = 0 ]; then echo -e "${DGN}Using Disk Size: ${BGN}$DISK_SIZE${CL}"; fi
  153. if ! [[ $DISK_SIZE =~ $INTEGER ]]; then
  154. echo -e "${RD}⚠ DISK SIZE MUST BE A INTEGER NUMBER!${CL}"
  155. advanced_settings
  156. fi
  157. fi
  158. CORE_COUNT=$(whiptail --inputbox "Allocate CPU Cores" 8 58 $var_cpu --title "CORE COUNT" --cancel-button Exit-Script 3>&1 1>&2 2>&3)
  159. exitstatus=$?
  160. if [ -z $CORE_COUNT ]; then
  161. CORE_COUNT="$var_cpu"
  162. echo -e "${DGN}Allocated Cores: ${BGN}$CORE_COUNT${CL}"
  163. else
  164. if [ $exitstatus = 0 ]; then echo -e "${DGN}Allocated Cores: ${BGN}$CORE_COUNT${CL}"; fi
  165. fi
  166. RAM_SIZE=$(whiptail --inputbox "Allocate RAM in MiB" 8 58 $var_ram --title "RAM" --cancel-button Exit-Script 3>&1 1>&2 2>&3)
  167. exitstatus=$?
  168. if [ -z $RAM_SIZE ]; then
  169. RAM_SIZE="$var_ram"
  170. echo -e "${DGN}Allocated RAM: ${BGN}$RAM_SIZE${CL}"
  171. else
  172. if [ $exitstatus = 0 ]; then echo -e "${DGN}Allocated RAM: ${BGN}$RAM_SIZE${CL}"; fi
  173. fi
  174. BRG=$(whiptail --inputbox "Set a Bridge" 8 58 vmbr0 --title "BRIDGE" --cancel-button Exit-Script 3>&1 1>&2 2>&3)
  175. exitstatus=$?
  176. if [ -z $BRG ]; then
  177. BRG="vmbr0"
  178. echo -e "${DGN}Using Bridge: ${BGN}$BRG${CL}"
  179. else
  180. if [ $exitstatus = 0 ]; then echo -e "${DGN}Using Bridge: ${BGN}$BRG${CL}"; fi
  181. fi
  182. 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)
  183. exitstatus=$?
  184. if [ -z $NET ]; then
  185. NET="dhcp"
  186. echo -e "${DGN}Using IP Address: ${BGN}$NET${CL}"
  187. else
  188. if [ $exitstatus = 0 ]; then echo -e "${DGN}Using IP Address: ${BGN}$NET${CL}"; fi
  189. fi
  190. 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)
  191. exitstatus=$?
  192. if [ $exitstatus = 0 ]; then
  193. if [ -z $GATE1 ]; then
  194. GATE1="Default" GATE=""
  195. echo -e "${DGN}Using Gateway IP Address: ${BGN}$GATE1${CL}"
  196. else
  197. GATE=",gw=$GATE1"
  198. echo -e "${DGN}Using Gateway IP Address: ${BGN}$GATE1${CL}"
  199. fi
  200. fi
  201. SD=$(whiptail --inputbox "Set a DNS Search Domain (leave blank for HOST)" 8 58 --title "DNS Search Domain" --cancel-button Exit-Script 3>&1 1>&2 2>&3)
  202. exitstatus=$?
  203. if [ $exitstatus = 0 ]; then
  204. if [ -z $SD ]; then
  205. SD=""
  206. echo -e "${DGN}Using DNS Search Domain: ${BGN}Host${CL}"
  207. else
  208. SX=$SD
  209. SD="-searchdomain=$SD"
  210. echo -e "${DGN}Using DNS Search Domain: ${BGN}$SX${CL}"
  211. fi
  212. fi
  213. NS=$(whiptail --inputbox "Set a DNS Server IP (leave blank for HOST)" 8 58 --title "DNS SERVER IP" --cancel-button Exit-Script 3>&1 1>&2 2>&3)
  214. exitstatus=$?
  215. if [ $exitstatus = 0 ]; then
  216. if [ -z $NS ]; then
  217. NS=""
  218. echo -e "${DGN}Using DNS Server IP Address: ${BGN}Host${CL}"
  219. else
  220. NX=$NS
  221. NS="-nameserver=$NS"
  222. echo -e "${DGN}Using DNS Server IP Address: ${BGN}$NX${CL}"
  223. fi
  224. fi
  225. 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)
  226. exitstatus=$?
  227. if [ $exitstatus = 0 ]; then
  228. if [ -z $MAC1 ]; then
  229. MAC1="Default" MAC=""
  230. echo -e "${DGN}Using MAC Address: ${BGN}$MAC1${CL}"
  231. else
  232. MAC=",hwaddr=$MAC1"
  233. echo -e "${DGN}Using MAC Address: ${BGN}$MAC1${CL}"
  234. fi
  235. fi
  236. VLAN1=$(whiptail --inputbox "Set a Vlan(leave blank for default)" 8 58 --title "VLAN" --cancel-button Exit-Script 3>&1 1>&2 2>&3)
  237. exitstatus=$?
  238. if [ $exitstatus = 0 ]; then
  239. if [ -z $VLAN1 ]; then
  240. VLAN1="Default" VLAN=""
  241. echo -e "${DGN}Using Vlan: ${BGN}$VLAN1${CL}"
  242. else
  243. VLAN=",tag=$VLAN1"
  244. echo -e "${DGN}Using Vlan: ${BGN}$VLAN1${CL}"
  245. fi
  246. fi
  247. if (whiptail --defaultno --title "SSH ACCESS" --yesno "Enable Root SSH Access?" 10 58); then
  248. echo -e "${DGN}Enable Root SSH Access: ${BGN}Yes${CL}"
  249. SSH="yes"
  250. else
  251. echo -e "${DGN}Enable Root SSH Access: ${BGN}No${CL}"
  252. SSH="no"
  253. fi
  254. if (whiptail --defaultno --title "VERBOSE MODE" --yesno "Enable Verbose Mode?" 10 58); then
  255. echo -e "${DGN}Enable Verbose Mode: ${BGN}Yes${CL}"
  256. VERB="yes"
  257. else
  258. echo -e "${DGN}Enable Verbose Mode: ${BGN}No${CL}"
  259. VERB="no"
  260. fi
  261. if (whiptail --title "ADVANCED SETTINGS COMPLETE" --yesno "Ready to create ${APP} LXC?" --no-button Do-Over 10 58); then
  262. echo -e "${RD}Creating a ${APP} LXC using the above advanced settings${CL}"
  263. else
  264. clear
  265. header_info
  266. echo -e "${RD}Using Advanced Settings${CL}"
  267. advanced_settings
  268. fi
  269. }
  270. function start_script() {
  271. if (whiptail --title "SETTINGS" --yesno "Use Default Settings?" --no-button Advanced 10 58); then
  272. header_info
  273. echo -e "${BL}Using Default Settings${CL}"
  274. default_settings
  275. else
  276. header_info
  277. echo -e "${RD}Using Advanced Settings${CL}"
  278. advanced_settings
  279. fi
  280. }
  281. clear
  282. start_script
  283. if [ "$VERB" == "yes" ]; then set -x; fi
  284. if [ "$CT_TYPE" == "1" ]; then
  285. FEATURES="nesting=1,keyctl=1"
  286. else
  287. FEATURES="nesting=1"
  288. fi
  289. TEMP_DIR=$(mktemp -d)
  290. pushd $TEMP_DIR >/dev/null
  291. export VERBOSE=$VERB
  292. export SSH_ROOT=${SSH}
  293. export CTID=$CT_ID
  294. export PCT_OSTYPE=$var_os
  295. export PCT_OSVERSION=$var_version
  296. export PCT_DISK_SIZE=$DISK_SIZE
  297. export PCT_OPTIONS="
  298. -features $FEATURES
  299. -hostname $HN
  300. $SD
  301. $NS
  302. -net0 name=eth0,bridge=$BRG$MAC,ip=$NET$GATE$VLAN
  303. -onboot 1
  304. -cores $CORE_COUNT
  305. -memory $RAM_SIZE
  306. -unprivileged $CT_TYPE
  307. $PW
  308. "
  309. bash -c "$(wget -qLO - https://raw.githubusercontent.com/tteck/Proxmox/main/ct/create_lxc.sh)" || exit
  310. msg_info "Starting LXC Container"
  311. pct start $CTID
  312. msg_ok "Started LXC Container"
  313. lxc-attach -n $CTID -- bash -c "$(wget -qLO - https://raw.githubusercontent.com/tteck/Proxmox/main/install/$var_install.sh)" || exit
  314. IP=$(pct exec $CTID ip a s dev eth0 | sed -n '/inet / s/\// /p' | awk '{print $2}')
  315. pct set $CTID -description "# ${APP} LXC
  316. ### https://tteck.github.io/Proxmox/
  317. <a href='https://ko-fi.com/D1D7EP4GF'><img src='https://img.shields.io/badge/☕-Buy me a coffee-red' /></a>"
  318. msg_ok "Completed Successfully!\n"