postgresql-v5.sh 11 KB

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