Advertisement
Guest User

Untitled

a guest
Feb 7th, 2016
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 12.51 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Get time as a UNIX timestamp (seconds elapsed since Jan 1, 1970 0:00 UTC)
  4. startTime="$(date +%s)"
  5. columns=$(tput cols)
  6. user_response=""
  7.  
  8. # Reset
  9. Color_Off='\e[0m'       # Text Reset
  10.  
  11. RED=$(tput setaf 1)
  12. GREEN=$(tput setaf 2)
  13. NORMAL=$(tput sgr0)
  14.  
  15. # High Intensity
  16. IBlack='\e[0;90m'       # Black
  17. IRed='\e[0;91m'         # Red
  18. IGreen='\e[0;92m'       # Green
  19. IYellow='\e[0;93m'      # Yellow
  20. IBlue='\e[0;94m'        # Blue
  21. IPurple='\e[0;95m'      # Purple
  22. ICyan='\e[0;96m'        # Cyan
  23. IWhite='\e[0;97m'       # White
  24.  
  25. # Bold High Intensity
  26. BIBlack='\e[1;90m'      # Black
  27. BIRed='\e[1;91m'        # Red
  28. BIGreen='\e[1;92m'      # Green
  29. BIYellow='\e[1;93m'     # Yellow
  30. BIBlue='\e[1;94m'       # Blue
  31. BIPurple='\e[1;95m'     # Purple
  32. BICyan='\e[1;96m'       # Cyan
  33. BIWhite='\e[1;97m'      # White
  34. #!/bin/bash
  35.  
  36. skip=0
  37.  
  38. clean_stdin()
  39. {
  40. while read -r -t 0; do
  41.     read -n 256 -r -s
  42. done
  43. }
  44.  
  45. # Permanent loop until both passwords are the same..
  46. function user_input {
  47.   local VARIABLE_NAME=${1}
  48.   local VARIABLE_NAME_1="A"
  49.   local VARIABLE_NAME_2="B"
  50.   while true; do
  51.       printf "${BICyan}$2: ${BIWhite}";
  52.       if [ "$3" = "hide" ] ; then
  53.         stty -echo;
  54.       fi
  55.       read VARIABLE_NAME_1;
  56.       stty echo;
  57.       if [ "$3" = "hide" ] ; then
  58.           printf "\n${BICyan}$2 (again) : ${BIWhite}";
  59.           stty -echo;
  60.           read VARIABLE_NAME_2;
  61.           stty echo;
  62.       else
  63.           VARIABLE_NAME_2=$VARIABLE_NAME_1;
  64.       fi
  65.       if [ $VARIABLE_NAME_1 != $VARIABLE_NAME_2 ] ; then
  66.          printf "\n${BIRed}Sorry, did not match!${BIWhite}\n";
  67.       else
  68.          break;
  69.        fi
  70.   done
  71.   readonly ${VARIABLE_NAME}=$VARIABLE_NAME_1;
  72.   if [ "$3" == "hide" ] ; then
  73.      printf "\n";
  74.   fi     
  75. }
  76.  
  77. timecount(){
  78.     sec=30
  79.     while [ $sec -ge 0 ]; do
  80.         printf "${BIPurple}Continue Y(es)/n(0)/s(kip)/a(ll)-  00:0$min:$sec remaining\033[0K\r${BIWhite}"
  81.         sec=$((sec-1))
  82.         trap '' 2
  83.         stty -echo
  84.         read -t 1 -n 1 user_response
  85.         stty echo
  86.         trap - 2
  87.         if [ -n  "$user_response" ]; then
  88.             break
  89.         fi                 
  90.     done
  91. }
  92.  
  93. task_start(){
  94. printf "${BIGreen}%*s\n" $columns | tr ' ' -
  95. printf "$1"
  96. clean_stdin
  97. skip=0
  98. printf "\n%*s${BIWhite}\n" $columns | tr ' ' -
  99. elapsedTime="$(($(date +%s)-startTime))"
  100. printf "Elapsed Time: %02d hrs %02d mins %02d secs\n" "$((elapsedTime/3600%24))" "$((elapsedTime/60%60))" "$((elapsedTime%60))"
  101. clean_stdin
  102. if [ "$user_response" != "a" ]; then
  103.     timecount
  104. fi
  105. echo -e "                                                                        \033[0K\r"
  106. if  [ "$user_response" = "n" ]; then
  107.     printf "${BIWhite}"
  108.     exit 1
  109. fi 
  110. if  [ "$user_response" = "s" ];then
  111.     skip=1
  112. fi 
  113. if [ -n  "$2" ]; then
  114.     if [ $skip -eq 0 ]; then
  115.         printf "${BIYellow}$2${BIWhite}\n"
  116.     else
  117.         printf "${BICyan}%*s${BIWhite}\n" $columns '[SKIPPED]'
  118.     fi
  119. fi
  120. }
  121.  
  122. task_end(){
  123.     printf "${BICyan}%*s${BIWhite}\n" $columns '[OK]'
  124. }
  125.  
  126.  
  127.  
  128.  
  129. # This script is intended for the Orange Pi PC 2 using the basic setup here
  130. # http://www.orangepi.org/orangepibbsen/forum.php?mod=viewthread&tid=867&extra=page%3D1&page=1
  131. #
  132. # Assuming access as orangepi user. Please note this will NOT WORK AS ROOT - The Node-Red install will fail.
  133. # Do not use this script as SUDO.
  134. #
  135. # including Mosquitto with web sockets (Port 9001), SQLITE ( xxx.xxx.xxx.xxx/phpliteadmin),
  136. # Node-Red-UI (xxx.xxx.xxx.xxx:1880/ui) and Webmin(xxx.xxx.xxx:10000)
  137. #
  138. # http://tech.scargill.net - much of this was thanks to help from others!
  139. #
  140. # If you want security you need to add this to the settings.js file in /home/orangepi/.node-red
  141. #
  142. # Suggested improvements welcome - I'm learning!!
  143. #
  144. #    functionGlobalContext: {
  145. #        // os:require('os'),
  146. #        // bonescript:require('bonescript'),
  147. #        // jfive:require("johnny-five"),
  148. #       moment:require('moment'),
  149. #       fs:require('fs')
  150. #    },
  151.  
  152. #    adminAuth: {
  153. #    type: "credentials",
  154. #    users: [{
  155. #       username: "admin",
  156. #        password: "your encrypted password see node red site",
  157. #        permissions: "*"
  158. #    }]
  159. #},
  160. #
  161. #  httpNodeAuth: {user:"user", pass:"your encrypted password see node red site"},
  162.  
  163. task_start "Update" "Updating repositories then any programs"
  164. if [ $skip -eq 0 ]  
  165. then
  166. cd
  167. sudo apt-get remove -y --purge nginx nginx-common
  168. sudo apt-get autoremove -y
  169. sudo apt-get -qq -o=Dpkg::Use-Pty=0 --yes --force-yes update
  170. sudo apt-get -qq -o=Dpkg::Use-Pty=0 --yes --force-yes upgrade
  171. task_end
  172. fi
  173.  
  174. task_start "Prerequisites" "Enabling PING and SAMBA (to access the hostname externally - and STUFF"
  175. if [ $skip -eq 0 ]  
  176. then
  177. # fix for RPI treating PING as a root function - by Dave
  178. sudo setcap cap_net_raw=ep /bin/ping
  179. sudo setcap cap_net_raw=ep /bin/ping6
  180. # this one ensures the unit shows up on the network by hostname... works a treat
  181. # sudo apt-get install samba samba-common-bin
  182.  
  183.  
  184. # Prerequisite suggested by Julian
  185. sudo apt-get install -y bash-completion unzip build-essential git python-serial scons libboost-filesystem-dev libboost-program-options-dev libboost-system-dev libsqlite3-dev subversion libcurl4-openssl-dev libusb-dev cmake # libboost-thread-dev libboost-all-dev
  186. task_end
  187. fi
  188.  
  189. task_start "Mosquitto" "Loading Mosquitto and setting up user"
  190. if [ $skip -eq 0 ]  
  191. then
  192. # installation of Mosquitto/w/Websockets
  193. cd
  194. wget http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key
  195. sudo apt-key add mosquitto-repo.gpg.key
  196. cd /etc/apt/sources.list.d/
  197. sudo wget http://repo.mosquitto.org/debian/mosquitto-jessie.list
  198. sudo apt-get update -y
  199. cd
  200. sudo apt-get install -y mosquitto
  201.  
  202. # Setup kill anonymous, emable websockets then set username and password for accessing Mosquitto
  203. # As we are PI - create temporary file with changes - then add to Mosquitto.conf
  204. echo '======================================= UPdating mosquitto.conf'
  205. cat <<EOT > /tmp/tmpnoderedfile
  206. listener 9001
  207. protocol websockets
  208. listener 1883
  209. allow_anonymous false
  210. password_file /etc/mosquitto/passwords
  211. EOT
  212. sudo bash -c "cat /tmp/tmpnoderedfile >> /etc/mosquitto/mosquitto.conf"
  213. rm /tmp/tmpnoderedfile
  214.  
  215. # Create or append to mosquitto passwords then add user
  216. sudo bash -c "echo '' >> /etc/mosquitto/passwords"
  217. user_input mquser "Enter desired user (admin for example) for Mosquitto"
  218. user_input mqpass "Enter desired password for Mosquitto" "hide"
  219. sudo mosquitto_passwd  -b /etc/mosquitto/passwords $mquser $mqpass
  220. task_end
  221. fi
  222.  
  223. task_start "Node-Red" "Loading Node-Red"
  224. if [ $skip -eq 0 ]  
  225. then
  226. # Dave provided most of this - installation of Node-Red and my selection of nodes
  227. sudo apt-get remove -y nodered nodejs nodejs-legacy npm
  228. sudo apt-get autoremove -y
  229. curl -sL https://deb.nodesource.com/setup_4.x | sudo bash -
  230. sudo apt-get install -y build-essential python-dev nodejs
  231. sudo npm cache clean
  232. sudo npm install -g --unsafe-perm node-red
  233. sudo wget https://raw.githubusercontent.com/node-red/raspbian-deb-package/master/resources/nodered.service -O /lib/systemd/system/nodered.service
  234. sudo wget https://raw.githubusercontent.com/node-red/raspbian-deb-package/master/resources/node-red-start -O /usr/bin/node-red-start
  235. sudo wget https://raw.githubusercontent.com/node-red/raspbian-deb-package/master/resources/node-red-stop -O /usr/bin/node-red-stop
  236. sudo sed -i -e 's#=pi#=orangepi#g' /lib/systemd/system/nodered.service
  237. sudo chmod +x /usr/bin/node-red-st*
  238. sudo systemctl daemon-reload
  239. mkdir .node-red
  240. cd .node-red
  241. sudo npm install -g node-red-admin
  242. npm install moment
  243. npm install node-red-contrib-grove
  244. npm install node-red-contrib-bigtimer
  245. npm install node-red-contrib-esplogin
  246. npm install node-red-node-pushbullet
  247. npm install node-red-contrib-freeboard
  248. npm install node-red-node-openweathermap
  249. npm install node-red-node-google
  250. npm install node-red-node-sqlite
  251. npm install node-red-contrib-ui
  252. npm install node-red-node-emoncms
  253. npm install node-red-node-geofence
  254. #npm install node-red-contrib-ivona
  255. npm install node-red-contrib-moment
  256. npm install node-red-contrib-particle
  257. npm install node-red-contrib-graphs
  258. npm install node-red-node-ledborg
  259. npm install node-red-node-ping
  260. npm install node-red-node-random
  261. npm install node-red-node-smooth
  262. npm install node-red-contrib-npm
  263. #npm install raspi-io
  264. #npm install node-red-contrib-gpio
  265. npm install node-red-contrib-admin
  266. npm install node-red-node-arduino
  267. task_end
  268. fi
  269.  
  270. task_start "Install Webmin" "Installing Webmin - expect interaction"
  271. if [ $skip -eq 0 ]  
  272. then
  273. cd
  274. mkdir webmin
  275. cd webmin
  276. wget --no-verbose http://prdownloads.sourceforge.net/webadmin/webmin-1.780.tar.gz
  277. sudo gunzip -q webmin-1.780.tar.gz
  278. sudo tar xf webmin-1.780.tar
  279. cd webmin-1.780
  280. sudo ./setup.sh /usr/local/Webmin
  281. cd
  282. cd webmin
  283. sudo rm *.tar
  284. task_end
  285. fi
  286.  
  287. task_start "Apache Installation with PHP and SQLITE and PHPLITEADMIN" "Installing Apache and PHP and SQLITE and PHPLITEADMIN..."
  288. if [ $skip -eq 0 ]  
  289. then
  290. cd
  291. sudo groupadd -f -g33 www-data
  292. sudo apt-get -qq --yes -o=Dpkg::Use-Pty=0 --force-yes install apache2 php5 libapache2-mod-php5
  293.  
  294.  
  295. cd
  296. sudo apt-get install -y sqlite3 php5-sqlite
  297. mkdir dbs
  298. sqlite3 /home/orangepi/dbs/iot.db << EOF
  299. CREATE TABLE IF NOT EXISTS \`pinDescription\` (
  300.   \`pinID\` INTEGER PRIMARY KEY NOT NULL,
  301.   \`pinNumber\` varchar(2) NOT NULL,
  302.   \`pinDescription\` varchar(255) NOT NULL
  303. );
  304. CREATE TABLE IF NOT EXISTS \`pinDirection\` (
  305.   \`pinID\` INTEGER PRIMARY KEY NOT NULL,
  306.   \`pinNumber\` varchar(2) NOT NULL,
  307.   \`pinDirection\` varchar(3) NOT NULL
  308. );
  309. CREATE TABLE IF NOT EXISTS \`pinStatus\` (
  310.   \`pinID\` INTEGER PRIMARY KEY NOT NULL,
  311.   \`pinNumber\` varchar(2)  NOT NULL,
  312.   \`pinStatus\` varchar(1) NOT NULL
  313. );
  314. CREATE TABLE IF NOT EXISTS \`users\` (
  315.   \`userID\` INTEGER PRIMARY KEY NOT NULL,
  316.   \`username\` varchar(28) NOT NULL,
  317.   \`password\` varchar(64) NOT NULL,
  318.   \`salt\` varchar(8) NOT NULL
  319. );
  320. CREATE TABLE IF NOT EXISTS \`device_list\` (
  321.   \`device_name\` varchar(80) NOT NULL DEFAULT '',
  322.   \`device_description\` varchar(80) DEFAULT NULL,
  323.   \`device_attribute\` varchar(80) DEFAULT NULL,
  324.   \`logins\` int(11) DEFAULT NULL,
  325.   \`creation_date\` datetime DEFAULT NULL,
  326.   \`last_update\` datetime DEFAULT NULL,
  327.   PRIMARY KEY (\`device_name\`)
  328. );
  329. CREATE TABLE IF NOT EXISTS \`readings\` (
  330.   \`location\` varchar(20) NOT NULL,
  331.   \`value\` int(11) NOT NULL,
  332.   \`logged\` timestamp NULL DEFAULT CURRENT_TIMESTAMP
  333. );
  334. CREATE TABLE IF NOT EXISTS \`pins\` (
  335.   \`gpio0\` int(11) NOT NULL DEFAULT '0',
  336.   \`gpio1\` int(11) NOT NULL DEFAULT '0',
  337.   \`gpio2\` int(11) NOT NULL DEFAULT '0',
  338.   \`gpio3\` int(11) NOT NULL DEFAULT '0'
  339. );
  340. INSERT INTO PINS VALUES(0,0,0,0);
  341. CREATE TABLE IF NOT EXISTS \`temperature_record\` (
  342.   \`device_name\` varchar(64) NOT NULL,
  343.   \`rec_num\` INTEGER PRIMARY KEY,
  344.   \`temperature\` float NOT NULL,
  345.   \`date_time\` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
  346. );
  347. .exit
  348. EOF
  349.  
  350. chmod 777 /home/orangepi/dbs
  351. chmod 666 /home/orangepi/dbs/iot.db
  352. cd
  353.  
  354. cd /var/www/html
  355. sudo mkdir phpliteadmin
  356. cd phpliteadmin
  357. sudo wget --no-verbose https://bitbucket.org/phpliteadmin/public/downloads/phpLiteAdmin_v1-9-6.zip
  358. sudo unzip phpLiteAdmin_v1-9-6.zip
  359. sudo mv phpliteadmin.php index.php
  360. sudo mv phpliteadmin.config.sample.php phpliteadmin.config.php
  361. sudo rm *.zip
  362. sudo mkdir themes
  363. cd themes
  364. sudo wget --no-verbose https://bitbucket.org/phpliteadmin/public/downloads/phpliteadmin_themes_2013-12-26.zip
  365. sudo unzip phpliteadmin_themes_2013-12-26.zip
  366. sudo rm *.zip
  367. user_input litepasswd "Enter desired password for PHPLiteAdmin" "hide"
  368. sudo sed -i -e 's#\$directory = \x27.\x27;#\$directory = \x27/home/orangepi/dbs/\x27;#g' /var/www/html/phpliteadmin/phpliteadmin.config.php
  369. sudo sed -i -e "s#\$password = \x27admin\x27;#\$password = \x27$litepasswd\x27;#g" /var/www/html/phpliteadmin/phpliteadmin.config.php
  370. sudo sed -i -e "s#\$subdirectories = false;#\$subdirectories = true;#g" /var/www/html/phpliteadmin/phpliteadmin.config.php
  371. cd
  372.  
  373. task_end
  374. fi
  375.  
  376. task_start "MP3 Installation" "Installing mpg123..."
  377. if [ $skip -eq 0 ]  
  378. then
  379. sudo apt-get install -y mpg123
  380. task_end
  381. fi
  382.  
  383. #task_start "Internet Time Updater for Webmin" "Ntpdate installing..."
  384. #if [ $skip -eq 0 ]  
  385. #then
  386. #sudo apt-get -qq --yes -o=Dpkg::Use-Pty=0 --force-yes install ntpdate
  387. #task_end
  388. #fi
  389.  
  390.  
  391. #task_start "Email SMTP Installation" "Installing mail utils and SMTP..."
  392. #if [ $skip -eq 0 ]  
  393. #then
  394. #cd
  395. #sudo apt-get  -qq --yes -o=Dpkg::Use-Pty=0 --force-yes install mailutils ssmtp
  396. #task_end
  397. #fi
  398.  
  399. task_start "Install SCREEN" "Installing SCREEN"
  400. if [ $skip -eq 0 ]  
  401. then
  402. cd
  403. sudo apt-get install screen
  404. task_end
  405. fi
  406.  
  407. sudo service nodered start ; sleep 5 ; sudo service nodered stop
  408. sed -i -e "s/\/\/\ os:require('os')\,/os:require('os')\,\n\tmoment:require('moment')/g" .node-red/settings.js
  409. echo " "
  410. echo "All done. Rebooting"
  411. sudo reboot
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement