zwavejs2mqtt-v3.sh 13 KB

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