mariadb-v2.sh 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. #!/usr/bin/env bash
  2. YW=`echo "\033[33m"`
  3. BL=`echo "\033[36m"`
  4. RD=`echo "\033[01;31m"`
  5. CM='\xE2\x9C\x94\033'
  6. GN=`echo "\033[1;92m"`
  7. CL=`echo "\033[m"`
  8. while true; do
  9. read -p "This will create a New Mariadb LXC. Proceed(y/n)?" yn
  10. case $yn in
  11. [Yy]* ) break;;
  12. [Nn]* ) exit;;
  13. * ) echo "Please answer yes or no.";;
  14. esac
  15. done
  16. clear
  17. function header_info {
  18. echo -e "${BL}
  19. __ __ _ _ _
  20. | \/ | (_) | | |
  21. | \ / | __ _ _ __ _ __ _ __| | |__
  22. | |\/| |/ _ | __| |/ _ |/ _ | _ \
  23. | | | | (_| | | | | (_| | (_| | |_) |
  24. |_| |_|\__,_|_| |_|\__,_|\__,_|_.__/
  25. ${CL}"
  26. }
  27. header_info
  28. show_menu(){
  29. printf " ${YW} 1)${YW} Privileged ${CL}\n"
  30. printf " ${YW} 2)${GN} Unprivileged ${CL}\n"
  31. printf "Please choose a Install Method and hit enter or ${RD}x${CL} to exit."
  32. read opt
  33. }
  34. option_picked(){
  35. message1=${@:-"${CL}Error: No message passed"}
  36. printf " ${YW}${message1}${CL}\n"
  37. }
  38. show_menu
  39. while [ "$opt" != " " ]
  40. do
  41. case $opt in
  42. 1) clear;
  43. header_info;
  44. option_picked "Using Privileged Install";
  45. IM=0
  46. break;
  47. ;;
  48. 2) clear;
  49. header_info;
  50. option_picked "Using Unprivileged Install";
  51. IM=1
  52. break;
  53. ;;
  54. x)exit;
  55. ;;
  56. \n)exit;
  57. ;;
  58. *)clear;
  59. option_picked "Please choose a Install Method from the menu";
  60. show_menu;
  61. ;;
  62. esac
  63. done
  64. show_menu2(){
  65. printf " ${YW} 1)${GN} Use Automatic Login ${CL}\n"
  66. printf " ${YW} 2)${GN} Use Password (changeme) ${CL}\n"
  67. printf "Please choose a Password Type and hit enter or ${RD}x${CL} to exit."
  68. read opt
  69. }
  70. option_picked(){
  71. message2=${@:-"${CL}Error: No message passed"}
  72. printf " ${YW}${message1}${CL}\n"
  73. printf " ${YW}${message2}${CL}\n"
  74. }
  75. show_menu2
  76. while [ "$opt" != " " ]
  77. do
  78. case $opt in
  79. 1) clear;
  80. header_info;
  81. option_picked "Using Automatic Login";
  82. PW=" "
  83. break;
  84. ;;
  85. 2) clear;
  86. header_info;
  87. option_picked "Using Password (changeme)";
  88. PW="-password changeme"
  89. break;
  90. ;;
  91. x)exit;
  92. ;;
  93. \n)exit;
  94. ;;
  95. *)clear;
  96. option_picked "Please choose a Password Type from the menu";
  97. show_menu2;
  98. ;;
  99. esac
  100. done
  101. show_menu3(){
  102. printf " ${YW} 1)${GN} Automatic DHCP ${CL}\n"
  103. printf " ${YW} 2)${GN} Manual DHCP ${CL}\n"
  104. printf "Please choose a DHCP Type and hit enter or ${RD}x${CL} to exit."
  105. read opt
  106. }
  107. option_picked(){
  108. message3=${@:-"${CL}Error: No message passed"}
  109. printf " ${YW}${message1}${CL}\n"
  110. printf " ${YW}${message2}${CL}\n"
  111. printf " ${YW}${message3}${CL}\n"
  112. }
  113. show_menu3
  114. while [ "$opt" != " " ]
  115. do
  116. case $opt in
  117. 1) clear;
  118. header_info;
  119. option_picked "Using Automatic DHCP";
  120. DHCP=" "
  121. break;
  122. ;;
  123. 2) clear;
  124. header_info;
  125. option_picked "Using Manual DHCP";
  126. DHCP="1"
  127. break;
  128. ;;
  129. x)exit;
  130. ;;
  131. \n)exit;
  132. ;;
  133. *)clear;
  134. option_picked "Please choose a DHCP Type from the menu";
  135. show_menu3;
  136. ;;
  137. esac
  138. done
  139. set -o errexit
  140. set -o errtrace
  141. set -o nounset
  142. set -o pipefail
  143. shopt -s expand_aliases
  144. alias die='EXIT=$? LINE=$LINENO error_exit'
  145. trap die ERR
  146. trap cleanup EXIT
  147. function error_exit() {
  148. trap - ERR
  149. local DEFAULT='Unknown failure occured.'
  150. local REASON="\e[97m${1:-$DEFAULT}\e[39m"
  151. local FLAG="\e[91m[ERROR] \e[93m$EXIT@$LINE"
  152. msg "$FLAG $REASON"
  153. [ ! -z ${CTID-} ] && cleanup_ctid
  154. exit $EXIT
  155. }
  156. function warn() {
  157. local REASON="\e[97m$1\e[39m"
  158. local FLAG="\e[93m[WARNING]\e[39m"
  159. msg "$FLAG $REASON"
  160. }
  161. function info() {
  162. local REASON="$1"
  163. local FLAG="\e[36m[INFO]\e[39m"
  164. msg "$FLAG $REASON"
  165. }
  166. function msg() {
  167. local TEXT="$1"
  168. echo -e "$TEXT"
  169. }
  170. function cleanup_ctid() {
  171. if $(pct status $CTID &>/dev/null); then
  172. if [ "$(pct status $CTID | awk '{print $2}')" == "running" ]; then
  173. pct stop $CTID
  174. fi
  175. pct destroy $CTID
  176. elif [ "$(pvesm list $STORAGE --vmid $CTID)" != "" ]; then
  177. pvesm free $ROOTFS
  178. fi
  179. }
  180. function cleanup() {
  181. popd >/dev/null
  182. rm -rf $TEMP_DIR
  183. }
  184. if [ "$IM" == "1" ]; then
  185. FEATURES="nesting=1,keyctl=1"
  186. else
  187. FEATURES="nesting=1"
  188. fi
  189. TEMP_DIR=$(mktemp -d)
  190. pushd $TEMP_DIR >/dev/null
  191. export CTID=$(pvesh get /cluster/nextid)
  192. export PCT_OSTYPE=debian
  193. export PCT_OSVERSION=11
  194. export PCT_DISK_SIZE=4
  195. export PCT_OPTIONS="
  196. -features $FEATURES
  197. -hostname mariadb
  198. -net0 name=eth0,bridge=vmbr0,ip=dhcp
  199. -onboot 1
  200. -cores 1
  201. -memory 1024
  202. -unprivileged ${IM}
  203. ${PW}
  204. "
  205. bash -c "$(wget -qLO - https://raw.githubusercontent.com/tteck/Proxmox/main/ct/create_lxc.sh)" || exit
  206. STORAGE_TYPE=$(pvesm status -storage $(pct config $CTID | grep rootfs | awk -F ":" '{print $2}') | awk 'NR>1 {print $2}')
  207. if [ "$STORAGE_TYPE" == "zfspool" ]; then
  208. warn "Some addons may not work due to ZFS not supporting 'fallocate'."
  209. fi
  210. if [ "$DHCP" == "1" ]; then
  211. MAC=$(pct config $CTID \
  212. | grep -i hwaddr \
  213. | awk '{print substr($2, 31, length($3) 17 ) }') \
  214. echo -e "MAC Address ${BL}$MAC${CL}"
  215. dhcp_reservation(){
  216. printf "Please set DHCP reservation and press Enter."
  217. read
  218. }
  219. dhcp_reservation
  220. fi
  221. echo -en "${GN} Starting LXC Container... "
  222. pct start $CTID
  223. echo -e "${CM}${CL} \r"
  224. alias lxc-cmd="lxc-attach -n $CTID --"
  225. lxc-cmd bash -c "$(wget -qLO - https://raw.githubusercontent.com/tteck/Proxmox/main/setup/mariadb-install.sh)" || exit
  226. IP=$(pct exec $CTID ip a s dev eth0 | sed -n '/inet / s/\// /p' | awk '{print $2}')
  227. echo -e "${GN}Successfully created Mariadb LXC to${CL} ${BL}$CTID${CL}.
  228. ${BL}Adminer${CL} should be reachable by going to the following URL.
  229. ${BL}http://${IP}/adminer/${CL} \n"