uptimekuma-v2.sh 6.3 KB

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