zwavejs2mqtt-v2.sh 6.7 KB

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