postgresql-v3.sh 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. #!/usr/bin/env bash
  2. APP="PostgreSQL"
  3. var_disk="4"
  4. var_cpu="1"
  5. var_ram="1024"
  6. var_os="debian"
  7. var_version="11"
  8. NEXTID=$(pvesh get /cluster/nextid)
  9. INTEGER='^[0-9]+$'
  10. NSAPP=$(echo ${APP,,} | tr -d ' ')
  11. var_install="${NSAPP}-install"
  12. YW=`echo "\033[33m"`
  13. BL=`echo "\033[36m"`
  14. RD=`echo "\033[01;31m"`
  15. BGN=`echo "\033[4;92m"`
  16. GN=`echo "\033[1;92m"`
  17. DGN=`echo "\033[32m"`
  18. CL=`echo "\033[m"`
  19. BFR="\\r\\033[K"
  20. HOLD="-"
  21. CM="${GN}✓${CL}"
  22. set -o errexit
  23. set -o errtrace
  24. set -o nounset
  25. set -o pipefail
  26. shopt -s expand_aliases
  27. alias die='EXIT=$? LINE=$LINENO error_exit'
  28. trap die ERR
  29. function error_exit() {
  30. trap - ERR
  31. local reason="Unknown failure occured."
  32. local msg="${1:-$reason}"
  33. local flag="${RD}‼ ERROR ${CL}$EXIT@$LINE"
  34. echo -e "$flag $msg" 1>&2
  35. exit $EXIT
  36. }
  37. function msg_info() {
  38. local msg="$1"
  39. echo -ne " ${HOLD} ${YW}${msg}..."
  40. }
  41. function msg_ok() {
  42. local msg="$1"
  43. echo -e "${BFR} ${CM} ${GN}${msg}${CL}"
  44. }
  45. while true; do
  46. read -p "This will create a New ${APP} LXC. Proceed(y/n)?" yn
  47. case $yn in
  48. [Yy]* ) break;;
  49. [Nn]* ) exit;;
  50. * ) echo "Please answer yes or no.";;
  51. esac
  52. done
  53. clear
  54. function header_info {
  55. echo -e "${BL}
  56. _____ _ _____ ____ _
  57. | __ \ | | / ____|/ __ \| |
  58. | |__) |__ ___| |_ __ _ _ __ ___| (___ | | | | |
  59. | ___/ _ \/ __| __/ _ | __/ _ \\___ \| | | | |
  60. | | | (_) \__ \ || (_| | | | __/____) | |__| | |____
  61. |_| \___/|___/\__\__, |_| \___|_____/ \___\_\______|
  62. __/ |
  63. v3|___/
  64. ${CL}"
  65. }
  66. header_info
  67. function PVE_CHECK() {
  68. PVE=$(pveversion | grep "pve-manager/7" | wc -l)
  69. if [[ $PVE != 1 ]]; then
  70. echo -e "${RD}This script requires Proxmox Virtual Environment 7.0 or greater${CL}"
  71. echo -e "Exiting..."
  72. sleep 2
  73. exit
  74. fi
  75. }
  76. function default_settings() {
  77. clear
  78. header_info
  79. echo -e "${BL}Using Default Settings${CL}"
  80. echo -e "${DGN}Using CT Type ${BGN}Unprivileged${CL} ${RD}NO DEVICE PASSTHROUGH${CL}"
  81. CT_TYPE="1"
  82. echo -e "${DGN}Using CT Password ${BGN}Automatic Login${CL}"
  83. PW=" "
  84. echo -e "${DGN}Using CT ID ${BGN}$NEXTID${CL}"
  85. CT_ID=$NEXTID
  86. echo -e "${DGN}Using CT Name ${BGN}$NSAPP${CL}"
  87. HN=$NSAPP
  88. echo -e "${DGN}Using Disk Size ${BGN}$var_disk${CL}${DGN}GB${CL}"
  89. DISK_SIZE="$var_disk"
  90. echo -e "${DGN}Using ${BGN}$var_cpu${CL}${DGN}vCPU${CL}"
  91. CORE_COUNT="$var_cpu"
  92. echo -e "${DGN}Using ${BGN}$var_ram${CL}${DGN}MiB RAM${CL}"
  93. RAM_SIZE="$var_ram"
  94. echo -e "${DGN}Using Bridge ${BGN}vmbr0${CL}"
  95. BRG="vmbr0"
  96. echo -e "${DGN}Using Static IP Address ${BGN}DHCP${CL}"
  97. NET=dhcp
  98. echo -e "${DGN}Using Gateway Address ${BGN}NONE${CL}"
  99. GATE=""
  100. echo -e "${DGN}Using VLAN Tag ${BGN}NONE${CL}"
  101. VLAN=""
  102. }
  103. function advanced_settings() {
  104. clear
  105. header_info
  106. echo -e "${RD}Using Advanced Settings${CL}"
  107. echo -e "${YW}Type Privileged, or Press [ENTER] for Default: Unprivileged (${RD}NO DEVICE PASSTHROUGH${CL}${YW})"
  108. read CT_TYPE1
  109. if [ -z $CT_TYPE1 ]; then CT_TYPE1="Unprivileged" CT_TYPE="1";
  110. echo -en "${DGN}Set CT Type ${BL}$CT_TYPE1${CL}"
  111. else
  112. CT_TYPE1="Privileged"
  113. CT_TYPE="0"
  114. echo -en "${DGN}Set CT Type ${BL}Privileged${CL}"
  115. fi;
  116. echo -e " ${CM}${CL} \r"
  117. sleep 1
  118. clear
  119. header_info
  120. echo -e "${RD}Using Advanced Settings${CL}"
  121. echo -e "${DGN}Using CT Type ${BGN}$CT_TYPE1${CL}"
  122. echo -e "${YW}Set Password, or Press [ENTER] for Default: Automatic Login "
  123. read PW1
  124. if [ -z $PW1 ]; then PW1="Automatic Login" PW=" ";
  125. echo -en "${DGN}Set CT ${BL}$PW1${CL}"
  126. else
  127. PW="-password $PW1"
  128. echo -en "${DGN}Set CT Password ${BL}$PW1${CL}"
  129. fi;
  130. echo -e " ${CM}${CL} \r"
  131. sleep 1
  132. clear
  133. header_info
  134. echo -e "${RD}Using Advanced Settings${CL}"
  135. echo -e "${DGN}Using CT Type ${BGN}$CT_TYPE1${CL}"
  136. echo -e "${DGN}Using CT Password ${BGN}$PW1${CL}"
  137. echo -e "${YW}Enter the CT ID, or Press [ENTER] to automatically generate (${NEXTID}) "
  138. read CT_ID
  139. if [ -z $CT_ID ]; then CT_ID=$NEXTID; fi;
  140. echo -en "${DGN}Set CT ID To ${BL}$CT_ID${CL}"
  141. echo -e " ${CM}${CL} \r"
  142. sleep 1
  143. clear
  144. header_info
  145. echo -e "${RD}Using Advanced Settings${CL}"
  146. echo -e "${DGN}Using CT Type ${BGN}$CT_TYPE1${CL}"
  147. echo -e "${DGN}Using CT Password ${BGN}$PW1${CL}"
  148. echo -e "${DGN}Using CT ID ${BGN}$CT_ID${CL}"
  149. echo -e "${YW}Enter CT Name (no-spaces), or Press [ENTER] for Default: $NSAPP "
  150. read CT_NAME
  151. if [ -z $CT_NAME ]; then
  152. HN=$NSAPP
  153. else
  154. HN=$(echo ${CT_NAME,,} | tr -d ' ')
  155. fi
  156. echo -en "${DGN}Set CT Name To ${BL}$HN${CL}"
  157. echo -e " ${CM}${CL} \r"
  158. sleep 1
  159. clear
  160. header_info
  161. echo -e "${RD}Using Advanced Settings${CL}"
  162. echo -e "${DGN}Using CT Type ${BGN}$CT_TYPE1${CL}"
  163. echo -e "${DGN}Using CT Password ${BGN}$PW1${CL}"
  164. echo -e "${DGN}Using CT ID ${BGN}$CT_ID${CL}"
  165. echo -e "${DGN}Using CT Name ${BGN}$HN${CL}"
  166. echo -e "${YW}Enter a Disk Size, or Press [ENTER] for Default: $var_disk "
  167. read DISK_SIZE
  168. if [ -z $DISK_SIZE ]; then DISK_SIZE="$var_disk"; fi;
  169. if ! [[ $DISK_SIZE =~ $INTEGER ]] ; then echo "ERROR! DISK SIZE MUST HAVE INTEGER NUMBER!"; exit; fi;
  170. echo -en "${DGN}Set Disk Size To ${BL}$DISK_SIZE${CL}${DGN}GB${CL}"
  171. echo -e " ${CM}${CL} \r"
  172. sleep 1
  173. clear
  174. header_info
  175. echo -e "${RD}Using Advanced Settings${CL}"
  176. echo -e "${DGN}Using CT Type ${BGN}$CT_TYPE1${CL}"
  177. echo -e "${DGN}Using CT Password ${BGN}$PW1${CL}"
  178. echo -e "${DGN}Using CT ID ${BGN}$CT_ID${CL}"
  179. echo -e "${DGN}Using CT Name ${BGN}$HN${CL}"
  180. echo -e "${DGN}Using Disk Size ${BGN}$DISK_SIZE${CL}${DGN}GB${CL}"
  181. echo -e "${YW}Allocate CPU cores, or Press [ENTER] for Default: $var_cpu "
  182. read CORE_COUNT
  183. if [ -z $CORE_COUNT ]; then CORE_COUNT="$var_cpu"; fi;
  184. echo -en "${DGN}Set Cores To ${BL}$CORE_COUNT${CL}${DGN}vCPU${CL}"
  185. echo -e " ${CM}${CL} \r"
  186. sleep 1
  187. clear
  188. header_info
  189. echo -e "${RD}Using Advanced Settings${CL}"
  190. echo -e "${DGN}Using CT Type ${BGN}$CT_TYPE1${CL}"
  191. echo -e "${DGN}Using CT Password ${BGN}$PW1${CL}"
  192. echo -e "${DGN}Using CT ID ${BGN}$CT_ID${CL}"
  193. echo -e "${DGN}Using CT Name ${BGN}$HN${CL}"
  194. echo -e "${DGN}Using Disk Size ${BGN}$DISK_SIZE${CL}${DGN}GB${CL}"
  195. echo -e "${DGN}Using ${BGN}${CORE_COUNT}${CL}${DGN}vCPU${CL}"
  196. echo -e "${YW}Allocate RAM in MiB, or Press [ENTER] for Default: $var_ram "
  197. read RAM_SIZE
  198. if [ -z $RAM_SIZE ]; then RAM_SIZE="$var_ram"; fi;
  199. echo -en "${DGN}Set RAM To ${BL}$RAM_SIZE${CL}${DGN}MiB RAM${CL}"
  200. echo -e " ${CM}${CL} \n"
  201. sleep 1
  202. clear
  203. header_info
  204. echo -e "${RD}Using Advanced Settings${CL}"
  205. echo -e "${DGN}Using CT Type ${BGN}$CT_TYPE1${CL}"
  206. echo -e "${DGN}Using CT Password ${BGN}$PW1${CL}"
  207. echo -e "${DGN}Using CT ID ${BGN}$CT_ID${CL}"
  208. echo -e "${DGN}Using CT Name ${BGN}$HN${CL}"
  209. echo -e "${DGN}Using Disk Size ${BGN}$DISK_SIZE${CL}${DGN}GB${CL}"
  210. echo -e "${DGN}Using ${BGN}${CORE_COUNT}${CL}${DGN}vCPU${CL}"
  211. echo -e "${DGN}Using ${BGN}${RAM_SIZE}${CL}${DGN}MiB RAM${CL}"
  212. echo -e "${YW}Enter a Bridge, or Press [ENTER] for Default: vmbr0 "
  213. read BRG
  214. if [ -z $BRG ]; then BRG="vmbr0"; fi;
  215. echo -en "${DGN}Set Bridge To ${BL}$BRG${CL}"
  216. echo -e " ${CM}${CL} \n"
  217. sleep 1
  218. clear
  219. header_info
  220. echo -e "${RD}Using Advanced Settings${CL}"
  221. echo -e "${DGN}Using CT Type ${BGN}$CT_TYPE1${CL}"
  222. echo -e "${DGN}Using CT Password ${BGN}$PW1${CL}"
  223. echo -e "${DGN}Using CT ID ${BGN}$CT_ID${CL}"
  224. echo -e "${DGN}Using CT Name ${BGN}$HN${CL}"
  225. echo -e "${DGN}Using Disk Size ${BGN}$DISK_SIZE${CL}${DGN}GB${CL}"
  226. echo -e "${DGN}Using ${BGN}${CORE_COUNT}${CL}${DGN}vCPU${CL}"
  227. echo -e "${DGN}Using ${BGN}${RAM_SIZE}${CL}${DGN}MiB RAM${CL}"
  228. echo -e "${DGN}Using Bridge ${BGN}${BRG}${CL}"
  229. echo -e "${YW}Enter a Static IPv4 CIDR Address, or Press [ENTER] for Default: DHCP "
  230. read NET
  231. if [ -z $NET ]; then NET="dhcp"; fi;
  232. echo -en "${DGN}Set Static IP Address To ${BL}$NET${CL}"
  233. echo -e " ${CM}${CL} \n"
  234. sleep 1
  235. clear
  236. header_info
  237. echo -e "${RD}Using Advanced Settings${CL}"
  238. echo -e "${DGN}Using CT Type ${BGN}$CT_TYPE1${CL}"
  239. echo -e "${DGN}Using CT Password ${BGN}$PW1${CL}"
  240. echo -e "${DGN}Using CT ID ${BGN}$CT_ID${CL}"
  241. echo -e "${DGN}Using CT Name ${BGN}$HN${CL}"
  242. echo -e "${DGN}Using Disk Size ${BGN}$DISK_SIZE${CL}${DGN}GB${CL}"
  243. echo -e "${DGN}Using ${BGN}${CORE_COUNT}${CL}${DGN}vCPU${CL}"
  244. echo -e "${DGN}Using ${BGN}${RAM_SIZE}${CL}${DGN}MiB RAM${CL}"
  245. echo -e "${DGN}Using Bridge ${BGN}${BRG}${CL}"
  246. echo -e "${DGN}Using Static IP Address ${BGN}$NET${CL}"
  247. echo -e "${YW}Enter a Gateway IP (mandatory if static IP is used), or Press [ENTER] for Default: NONE "
  248. read GATE1
  249. if [ -z $GATE1 ]; then GATE1="NONE" GATE="";
  250. echo -en "${DGN}Set Gateway IP To ${BL}$GATE1${CL}"
  251. else
  252. GATE=",gw=$GATE1"
  253. echo -en "${DGN}Set Gateway IP To ${BL}$GATE1${CL}"
  254. fi;
  255. echo -e " ${CM}${CL} \n"
  256. sleep 1
  257. clear
  258. header_info
  259. echo -e "${RD}Using Advanced Settings${CL}"
  260. echo -e "${DGN}Using CT Type ${BGN}$CT_TYPE1${CL}"
  261. echo -e "${DGN}Using CT Password ${BGN}$PW1${CL}"
  262. echo -e "${DGN}Using CT ID ${BGN}$CT_ID${CL}"
  263. echo -e "${DGN}Using CT Name ${BGN}$HN${CL}"
  264. echo -e "${DGN}Using Disk Size ${BGN}$DISK_SIZE${CL}${DGN}GB${CL}"
  265. echo -e "${DGN}Using ${BGN}${CORE_COUNT}${CL}${DGN}vCPU${CL}"
  266. echo -e "${DGN}Using ${BGN}${RAM_SIZE}${CL}${DGN}MiB RAM${CL}"
  267. echo -e "${DGN}Using Bridge ${BGN}${BRG}${CL}"
  268. echo -e "${DGN}Using Static IP Address ${BGN}$NET${CL}"
  269. echo -e "${DGN}Using Gateway IP Address ${BGN}$GATE1${CL}"
  270. echo -e "${YW}Enter a VLAN Tag, or Press [ENTER] for Default: NONE "
  271. read VLAN1
  272. if [ -z $VLAN1 ]; then VLAN1="NONE" VLAN="";
  273. echo -en "${DGN}Set VLAN Tag To ${BL}$VLAN1${CL}"
  274. else
  275. VLAN=",tag=$VLAN1"
  276. echo -en "${DGN}Set VLAN Tag To ${BL}$VLAN1${CL}"
  277. fi;
  278. echo -e " ${CM}${CL} \n"
  279. sleep 1
  280. clear
  281. header_info
  282. echo -e "${RD}Using Advanced Settings${CL}"
  283. echo -e "${DGN}Using CT Type ${BGN}$CT_TYPE1${CL}"
  284. echo -e "${DGN}Using CT Password ${BGN}$PW1${CL}"
  285. echo -e "${DGN}Using CT ID ${BGN}$CT_ID${CL}"
  286. echo -e "${DGN}Using CT Name ${BGN}$HN${CL}"
  287. echo -e "${DGN}Using Disk Size ${BGN}$DISK_SIZE${CL}${DGN}GB${CL}"
  288. echo -e "${DGN}Using ${BGN}${CORE_COUNT}${CL}${DGN}vCPU${CL}"
  289. echo -e "${DGN}Using ${BGN}${RAM_SIZE}${CL}${DGN}MiB RAM${CL}"
  290. echo -e "${DGN}Using Bridge ${BGN}${BRG}${CL}"
  291. echo -e "${DGN}Using Static IP Address ${BGN}$NET${CL}"
  292. echo -e "${DGN}Using Gateway IP Address ${BGN}$GATE1${CL}"
  293. echo -e "${DGN}Using VLAN Tag ${BGN}$VLAN1${CL}"
  294. read -p "Are these settings correct(y/n)? " -n 1 -r
  295. echo
  296. if [[ ! $REPLY =~ ^[Yy]$ ]]
  297. then
  298. advanced_settings
  299. fi
  300. }
  301. function start_script() {
  302. echo -e "${YW}Type Advanced, or Press [ENTER] for Default Settings "
  303. read SETTINGS
  304. if [ -z $SETTINGS ]; then default_settings;
  305. else
  306. advanced_settings
  307. fi;
  308. }
  309. start_script
  310. if [ "$CT_TYPE" == "1" ]; then
  311. FEATURES="nesting=1,keyctl=1"
  312. else
  313. FEATURES="nesting=1"
  314. fi
  315. export CTID=$CT_ID
  316. export PCT_OSTYPE=$var_os
  317. export PCT_OSVERSION=$var_version
  318. export PCT_DISK_SIZE=$DISK_SIZE
  319. export PCT_OPTIONS="
  320. -features $FEATURES
  321. -hostname $HN
  322. -net0 name=eth0,bridge=$BRG,ip=$NET$GATE$VLAN
  323. -onboot 1
  324. -cores $CORE_COUNT
  325. -memory $RAM_SIZE
  326. -unprivileged $CT_TYPE
  327. $PW
  328. "
  329. bash -c "$(wget -qLO - https://raw.githubusercontent.com/tteck/Proxmox/main/ct/create_lxc.sh)" || exit
  330. msg_info "Starting LXC Container"
  331. pct start $CTID
  332. msg_ok "Started LXC Container"
  333. lxc-attach -n $CTID -- bash -c "$(wget -qLO - https://raw.githubusercontent.com/tteck/Proxmox/main/setup/$var_install.sh)" || exit
  334. IP=$(pct exec $CTID ip a s dev eth0 | sed -n '/inet / s/\// /p' | awk '{print $2}')
  335. msg_ok "Completed Successfully!\n"
  336. echo -e "Adminer should be reachable by going to the following URL.
  337. ${BL}http://${IP}/adminer/${CL} \n"