zwavejs2mqtt.sh 6.9 KB

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