zwavejs2mqtt-v3.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. #!/usr/bin/env bash -ex
  2. set -euo pipefail
  3. shopt -s inherit_errexit nullglob
  4. NEXTID=$(pvesh get /cluster/nextid)
  5. INTEGER='^[0-9]+$'
  6. YW=`echo "\033[33m"`
  7. BL=`echo "\033[36m"`
  8. RD=`echo "\033[01;31m"`
  9. BGN=`echo "\033[4;92m"`
  10. GN=`echo "\033[1;92m"`
  11. DGN=`echo "\033[32m"`
  12. CL=`echo "\033[m"`
  13. BFR="\\r\\033[K"
  14. HOLD="-"
  15. CM="${GN}✓${CL}"
  16. CROSS="${RD}✗${CL}"
  17. APP="Zwavejs2MQTT"
  18. NSAPP=$(echo ${APP,,} | tr -d ' ')
  19. while true; do
  20. read -p "This will create a New ${APP} LXC. Proceed(y/n)?" yn
  21. case $yn in
  22. [Yy]* ) break;;
  23. [Nn]* ) exit;;
  24. * ) echo "Please answer yes or no.";;
  25. esac
  26. done
  27. clear
  28. function header_info {
  29. echo -e "${BL}
  30. ______ _ ___ __ __ ____ _______ _______
  31. |___ / (_) |__ \| \/ |/ __ \__ __|__ __|
  32. / /_ ____ ___ _____ _ ___ ) | \ / | | | | | | | |
  33. / /\ \ /\ / / _ \ \ / / _ \ / __| / /| |\/| | | | | | | | |
  34. / /__\ V V / (_| |\ V / __/ \__ \/ /_| | | | |__| | | | | |
  35. /_____|\_/\_/ \__,_| \_/ \___| |___/____|_| |_|\___\_\ |_| v3 |_|
  36. _/ |
  37. |__/
  38. ${CL}"
  39. }
  40. header_info
  41. function msg_info() {
  42. local msg="$1"
  43. echo -ne " ${HOLD} ${YW}${msg}..."
  44. }
  45. function msg_ok() {
  46. local msg="$1"
  47. echo -e "${BFR} ${CM} ${GN}${msg}${CL}"
  48. }
  49. function PVE_CHECK() {
  50. PVE=$(pveversion | grep "pve-manager/7" | wc -l)
  51. if [[ $PVE != 1 ]]; then
  52. echo -e "${RD}This script requires Proxmox Virtual Environment 7.0 or greater${CL}"
  53. echo -e "Exiting..."
  54. sleep 2
  55. exit
  56. fi
  57. }
  58. function default_settings() {
  59. clear
  60. header_info
  61. echo -e "${BL}Using Default Settings${CL}"
  62. echo -e "${DGN}Using CT Type ${BGN}Privileged${CL}"
  63. CT_TYPE="0"
  64. echo -e "${DGN}Using CT Password ${BGN}Automatic Login${CL}"
  65. PW=" "
  66. echo -e "${DGN}Using CT ID ${BGN}$NEXTID${CL}"
  67. CT_ID=$NEXTID
  68. echo -e "${DGN}Using CT Name ${BGN}$NSAPP${CL}"
  69. HN=$NSAPP
  70. echo -e "${DGN}Using Disk Size ${BGN}4GB${CL}"
  71. DISK_SIZE="4"
  72. echo -e "${DGN}Using ${BGN}2vCPU${CL}"
  73. CORE_COUNT="2"
  74. echo -e "${DGN}Using ${BGN}1024MiB${CL}${DGN} RAM${CL}"
  75. RAM_SIZE="1024"
  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="Unprivileged"
  93. CT_TYPE="1"
  94. echo -en "${DGN}Set CT Type ${BL}Unprivileged${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: 4Gb "
  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}"
  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}"
  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}"
  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}"
  175. echo -e "${DGN}Using ${BGN}${CORE_COUNT}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}"
  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}"
  190. echo -e "${DGN}Using ${BGN}${CORE_COUNT}vCPU${CL}"
  191. echo -e "${DGN}Using ${BGN}${RAM_SIZE}MiB${CL}${DGN} RAM${CL}"
  192. echo -e "${YW}Enter a Static IP Address, or Press [ENTER] for Default: DHCP "
  193. read NET
  194. if [ -z $NET ]; then NET="dhcp"; fi;
  195. echo -en "${DGN}Set Static IP Address To ${BL}$NET${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}"
  206. echo -e "${DGN}Using ${BGN}${CORE_COUNT}vCPU${CL}"
  207. echo -e "${DGN}Using ${BGN}${RAM_SIZE}MiB${CL}${DGN} RAM${CL}"
  208. echo -e "${DGN}Using Static IP Address ${BGN}$NET${CL}"
  209. echo -e "${YW}Enter a Gateway IP, or Press [ENTER] for Default: NONE "
  210. read GATE1
  211. if [ -z $GATE1 ]; then GATE1="NONE" GATE=" ";
  212. echo -en "${DGN}Set Gateway IP To ${BL}$GATE1${CL}"
  213. else
  214. GATE=",gw=$GATE1"
  215. echo -en "${DGN}Set Gateway IP To ${BL}$GATE1${CL}"
  216. fi;
  217. echo -e " ${CM}${CL} \n"
  218. sleep 1
  219. clear
  220. header_info
  221. echo -e "${RD}Using Advanced Settings${CL}"
  222. echo -e "${DGN}Using CT Type ${BGN}$CT_TYPE1${CL}"
  223. echo -e "${DGN}Using CT Password ${BGN}$PW1${CL}"
  224. echo -e "${DGN}Using CT ID ${BGN}$CT_ID${CL}"
  225. echo -e "${DGN}Using CT Name ${BGN}$HN${CL}"
  226. echo -e "${DGN}Using Disk Size ${BGN}$DISK_SIZE${CL}"
  227. echo -e "${DGN}Using ${BGN}${CORE_COUNT}vCPU${CL}"
  228. echo -e "${DGN}Using ${BGN}${RAM_SIZE}MiB${CL}${DGN} RAM${CL}"
  229. echo -e "${DGN}Using Static IP Address ${BGN}$NET${CL}"
  230. echo -e "${DGN}Using Gateway IP Address ${BGN}$GATE1${CL}"
  231. echo -e "${YW}Enter a VLAN Tag, or Press [ENTER] for Default: NONE "
  232. read VLAN1
  233. if [ -z $VLAN1 ]; then VLAN1="NONE" VLAN=" ";
  234. echo -en "${DGN}Set VLAN Tag To ${BL}$VLAN1${CL}"
  235. else
  236. VLAN=",tag=$VLAN1"
  237. echo -en "${DGN}Set VLAN Tag To ${BL}$VLAN1${CL}"
  238. fi;
  239. echo -e " ${CM}${CL} \n"
  240. sleep 1
  241. clear
  242. header_info
  243. echo -e "${RD}Using Advanced Settings${CL}"
  244. echo -e "${DGN}Using CT Type ${BGN}$CT_TYPE1${CL}"
  245. echo -e "${DGN}Using CT Password ${BGN}$PW1${CL}"
  246. echo -e "${DGN}Using CT ID ${BGN}$CT_ID${CL}"
  247. echo -e "${DGN}Using CT Name ${BGN}$HN${CL}"
  248. echo -e "${DGN}Using Disk Size ${BGN}$DISK_SIZE${CL}"
  249. echo -e "${DGN}Using ${BGN}${CORE_COUNT}vCPU${CL}"
  250. echo -e "${DGN}Using ${BGN}${RAM_SIZE}MiB${CL}${DGN} RAM${CL}"
  251. echo -e "${DGN}Using Static IP Address ${BGN}$NET${CL}"
  252. echo -e "${DGN}Using Gateway IP Address ${BGN}$GATE1${CL}"
  253. echo -e "${DGN}Using VLAN Tag ${BGN}$VLAN1${CL}"
  254. read -p "Are these settings correct(y/n)? " -n 1 -r
  255. echo
  256. if [[ ! $REPLY =~ ^[Yy]$ ]]
  257. then
  258. advanced_settings
  259. fi
  260. }
  261. function start_script() {
  262. echo -e "${YW}Type Advanced, or Press [ENTER] for Default Settings "
  263. read SETTINGS
  264. if [ -z $SETTINGS ]; then default_settings;
  265. else
  266. advanced_settings
  267. fi;
  268. }
  269. PVE_CHECK
  270. start_script
  271. if [ "$CT_TYPE" == "1" ]; then
  272. FEATURES="nesting=1,keyctl=1"
  273. else
  274. FEATURES="nesting=1"
  275. fi
  276. TEMP_DIR=$(mktemp -d)
  277. pushd $TEMP_DIR >/dev/null
  278. export CTID=$CT_ID
  279. export PCT_OSTYPE=debian
  280. export PCT_OSVERSION=11
  281. export PCT_DISK_SIZE=$DISK_SIZE
  282. export PCT_OPTIONS="
  283. -features $FEATURES
  284. -hostname $HN
  285. -net0 name=eth0,bridge=vmbr0,ip=$NET$GATE$VLAN
  286. -onboot 1
  287. -cores $CORE_COUNT
  288. -memory $RAM_SIZE
  289. -unprivileged $CT_TYPE
  290. $PW
  291. "
  292. bash -c "$(wget -qLO - https://raw.githubusercontent.com/tteck/Proxmox/main/ct/create_lxc.sh)" || exit
  293. LXC_CONFIG=/etc/pve/lxc/${CTID}.conf
  294. cat <<EOF >> $LXC_CONFIG
  295. lxc.cgroup2.devices.allow: a
  296. lxc.cap.drop:
  297. lxc.cgroup2.devices.allow: c 188:* rwm
  298. lxc.cgroup2.devices.allow: c 189:* rwm
  299. lxc.mount.entry: /dev/serial/by-id dev/serial/by-id none bind,optional,create=dir
  300. lxc.mount.entry: /dev/ttyUSB0 dev/ttyUSB0 none bind,optional,create=file
  301. lxc.mount.entry: /dev/ttyACM0 dev/ttyACM0 none bind,optional,create=file
  302. lxc.mount.entry: /dev/ttyACM1 dev/ttyACM1 none bind,optional,create=file
  303. EOF
  304. echo -en "${GN} Starting LXC Container... "
  305. pct start $CTID
  306. echo -e "${CM}${CL} \r"
  307. lxc-attach -n $CTID -- bash -c "$(wget -qLO - https://raw.githubusercontent.com/tteck/Proxmox/main/setup/zwavejs2mqtt-install.sh)" || exit
  308. IP=$(pct exec $CTID ip a s dev eth0 | sed -n '/inet / s/\// /p' | awk '{print $2}')
  309. msg_ok "Completed Successfully!\n"
  310. echo -e "${APP} should be reachable by going to the following URL.
  311. ${BL}http://${IP}:8091${CL} \n"