postgresql-v4.sh 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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 ${var_os} Version: ${BGN}${var_version}${CL}"
  74. echo -e "${DGN}Using Container Type: ${BGN}Unprivileged${CL} ${RD}NO DEVICE PASSTHROUGH${CL}"
  75. CT_TYPE="1"
  76. echo -e "${DGN}Using Root Password: ${BGN}Automatic Login${CL}"
  77. PW=""
  78. echo -e "${DGN}Using Container ID: ${BGN}$NEXTID${CL}"
  79. CT_ID=$NEXTID
  80. echo -e "${DGN}Using Hostname: ${BGN}$NSAPP${CL}"
  81. HN=$NSAPP
  82. echo -e "${DGN}Using Disk Size: ${BGN}$var_disk${CL}${DGN}GB${CL}"
  83. DISK_SIZE="$var_disk"
  84. echo -e "${DGN}Allocated Cores ${BGN}$var_cpu${CL}"
  85. CORE_COUNT="$var_cpu"
  86. echo -e "${DGN}Allocated Ram ${BGN}$var_ram${CL}"
  87. RAM_SIZE="$var_ram"
  88. echo -e "${DGN}Using Bridge: ${BGN}vmbr0${CL}"
  89. BRG="vmbr0"
  90. echo -e "${DGN}Using Static IP Address: ${BGN}dhcp${CL}"
  91. NET=dhcp
  92. echo -e "${DGN}Using Gateway Address: ${BGN}Default${CL}"
  93. GATE=""
  94. echo -e "${DGN}Using MAC Address: ${BGN}Default${CL}"
  95. MAC=""
  96. echo -e "${DGN}Using VLAN Tag: ${BGN}Default${CL}"
  97. VLAN=""
  98. echo -e "${BL}Creating a ${APP} LXC using the above default settings${CL}"
  99. }
  100. function advanced_settings() {
  101. var_version=$(whiptail --title "DEBIAN VERSION" --radiolist "Choose Version" 10 58 2 \
  102. "10" "Buster" OFF \
  103. "11" "Bullseye" ON \
  104. 3>&1 1>&2 2>&3)
  105. exitstatus=$?
  106. if [ $exitstatus = 0 ]; then
  107. echo -e "${DGN}Using Debian Version: ${BGN}$var_version${CL}"
  108. else
  109. exit
  110. fi
  111. CT_TYPE=$(whiptail --title "CONTAINER TYPE" --radiolist "Choose Type" 8 58 2 \
  112. "1" "Unprivileged" ON \
  113. "0" "Privileged" OFF \
  114. 3>&1 1>&2 2>&3)
  115. exitstatus=$?
  116. if [ $exitstatus = 0 ]; then
  117. echo -e "${DGN}Using Container Type: ${BGN}$CT_TYPE${CL}"
  118. else
  119. exit
  120. fi
  121. PW1=$(whiptail --inputbox "Set Root Password" 8 58 --title "PASSWORD(leave blank for automatic login)" 3>&1 1>&2 2>&3)
  122. exitstatus=$?
  123. if [ $exitstatus = 0 ]; then
  124. if [ -z $PW1 ]; then PW1="Automatic Login" PW=" ";
  125. echo -e "${DGN}Using Root Password: ${BGN}$PW1${CL}"
  126. else
  127. PW="-password $PW1"
  128. echo -e "${DGN}Using Root Password: ${BGN}$PW1${CL}"
  129. fi
  130. fi
  131. CT_ID=$(whiptail --inputbox "Set Container ID" 8 58 $NEXTID --title "CONTAINER ID" 3>&1 1>&2 2>&3)
  132. exitstatus=$?
  133. if [ $exitstatus = 0 ]; then
  134. echo -e "${DGN}Using Container ID: ${BGN}$CT_ID${CL}"
  135. else
  136. exit
  137. fi
  138. CT_NAME=$(whiptail --inputbox "Set Hostname" 8 58 $NSAPP --title "HOSTNAME" 3>&1 1>&2 2>&3)
  139. exitstatus=$?
  140. if [ $exitstatus = 0 ]; then
  141. HN=$(echo ${CT_NAME,,} | tr -d ' ')
  142. echo -e "${DGN}Using Hostname: ${BGN}$HN${CL}"
  143. else
  144. exit
  145. fi
  146. DISK_SIZE=$(whiptail --inputbox "Set Disk Size in GB" 8 58 $var_disk --title "DISK SIZE" 3>&1 1>&2 2>&3)
  147. exitstatus=$?
  148. if [ $exitstatus = 0 ]; then
  149. echo -e "${DGN}Using Disk Size: ${BGN}$DISK_SIZE${CL}"
  150. else
  151. exit
  152. fi
  153. CORE_COUNT=$(whiptail --inputbox "Allocate CPU Cores" 8 58 $var_cpu --title "CORE COUNT" 3>&1 1>&2 2>&3)
  154. exitstatus=$?
  155. if [ $exitstatus = 0 ]; then
  156. echo -e "${DGN}Allocated Cores: ${BGN}$CORE_COUNT${CL}"
  157. else
  158. exit
  159. fi
  160. RAM_SIZE=$(whiptail --inputbox "Allocate RAM in MiB" 8 58 $var_ram --title "RAM" 3>&1 1>&2 2>&3)
  161. exitstatus=$?
  162. if [ $exitstatus = 0 ]; then
  163. echo -e "${DGN}Allocated RAM: ${BGN}$RAM_SIZE${CL}"
  164. else
  165. exit
  166. fi
  167. BRG=$(whiptail --inputbox "Set a Bridge" 8 58 vmbr0 --title "BRIDGE" 3>&1 1>&2 2>&3)
  168. exitstatus=$?
  169. if [ $exitstatus = 0 ]; then
  170. echo -e "${DGN}Using Bridge: ${BGN}$BRG${CL}"
  171. else
  172. exit
  173. fi
  174. NET=$(whiptail --inputbox "Set a Static IPv4 CIDR Address(/24)" 8 58 dhcp --title "IP ADDRESS" 3>&1 1>&2 2>&3)
  175. exitstatus=$?
  176. if [ $exitstatus = 0 ]; then
  177. echo -e "${DGN}Using IP Address: ${BGN}$NET${CL}"
  178. else
  179. exit
  180. fi
  181. GATE1=$(whiptail --inputbox "Set a Gateway IP (mandatory if Static IP was used)" 8 58 --title "GATEWAY IP" 3>&1 1>&2 2>&3)
  182. exitstatus=$?
  183. if [ $exitstatus = 0 ]; then
  184. if [ -z $GATE1 ]; then GATE1="Default" GATE="";
  185. echo -e "${DGN}Using Gateway IP Address: ${BGN}$GATE1${CL}"
  186. else
  187. GATE=",gw=$GATE1"
  188. echo -e "${DGN}Using Gateway IP Address: ${BGN}$GATE1${CL}"
  189. fi
  190. fi
  191. MAC1=$(whiptail --inputbox "Set a MAC Address(leave blank for default)" 8 58 --title "MAC ADDRESS" 3>&1 1>&2 2>&3)
  192. exitstatus=$?
  193. if [ $exitstatus = 0 ]; then
  194. if [ -z $MAC1 ]; then MAC1="Default" MAC="";
  195. echo -e "${DGN}Using MAC Address: ${BGN}$MAC1${CL}"
  196. else
  197. MAC=",hwaddr=$MAC1"
  198. echo -e "${DGN}Using MAC Address: ${BGN}$MAC1${CL}"
  199. fi
  200. fi
  201. VLAN1=$(whiptail --inputbox "Set a Vlan(leave blank for default)" 8 58 --title "VLAN" 3>&1 1>&2 2>&3)
  202. exitstatus=$?
  203. if [ $exitstatus = 0 ]; then
  204. if [ -z $VLAN1 ]; then VLAN1="Default" VLAN="";
  205. echo -e "${DGN}Using Vlan: ${BGN}$VLAN1${CL}"
  206. else
  207. VLAN=",tag=$VLAN1"
  208. echo -e "${DGN}Using Vlan: ${BGN}$VLAN1${CL}"
  209. fi
  210. fi
  211. if (whiptail --title "ADVANCED SETTINGS COMPLETE" --yesno "Ready to create ${APP} LXC?" 10 58); then
  212. echo -e "${RD}Creating a ${APP} LXC using the above advanced settings${CL}"
  213. else
  214. clear
  215. header_info
  216. echo -e "${RD}Using Advanced Settings${CL}"
  217. advanced_settings
  218. fi
  219. }
  220. function start_script() {
  221. if (whiptail --title "SETTINGS" --yesno "Use Default Settings?" 10 58); then
  222. header_info
  223. echo -e "${BL}Using Default Settings${CL}"
  224. default_settings
  225. else
  226. header_info
  227. echo -e "${RD}Using Advanced Settings${CL}"
  228. advanced_settings
  229. fi
  230. }
  231. clear
  232. start_script
  233. if [ "$CT_TYPE" == "1" ]; then
  234. FEATURES="nesting=1,keyctl=1"
  235. else
  236. FEATURES="nesting=1"
  237. fi
  238. TEMP_DIR=$(mktemp -d)
  239. pushd $TEMP_DIR >/dev/null
  240. export CTID=$CT_ID
  241. export PCT_OSTYPE=$var_os
  242. export PCT_OSVERSION=$var_version
  243. export PCT_DISK_SIZE=$DISK_SIZE
  244. export PCT_OPTIONS="
  245. -features $FEATURES
  246. -hostname $HN
  247. -net0 name=eth0,bridge=$BRG$MAC,ip=$NET$GATE$VLAN
  248. -onboot 1
  249. -cores $CORE_COUNT
  250. -memory $RAM_SIZE
  251. -unprivileged $CT_TYPE
  252. $PW
  253. "
  254. bash -c "$(wget -qLO - https://raw.githubusercontent.com/tteck/Proxmox/main/ct/create_lxc.sh)" || exit
  255. msg_info "Starting LXC Container"
  256. pct start $CTID
  257. msg_ok "Started LXC Container"
  258. lxc-attach -n $CTID -- bash -c "$(wget -qLO - https://raw.githubusercontent.com/tteck/Proxmox/main/setup/$var_install.sh)" || exit
  259. IP=$(pct exec $CTID ip a s dev eth0 | sed -n '/inet / s/\// /p' | awk '{print $2}')
  260. pct set $CTID -description "# ${APP} LXC
  261. ### https://tteck.github.io/Proxmox/"
  262. msg_ok "Completed Successfully!\n"
  263. echo -e "Adminer should be reachable by going to the following URL.
  264. ${BL}http://${IP}/adminer/${CL} \n"