deploy-busunit-interactive 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. #!/bin/bash
  2. #
  3. # Copyright (c) 2019 Clementine Computing LLC.
  4. #
  5. # This file is part of PopuFare.
  6. #
  7. # PopuFare is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU Affero General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # PopuFare is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU Affero General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Affero General Public License
  18. # along with PopuFare. If not, see <https://www.gnu.org/licenses/>.
  19. #
  20. GITURL="https://tree.clementinecomputing.com/clementinecomputing/popufare"
  21. echo ""
  22. echo ' _____ __ '
  23. echo ' | __ \ / _| '
  24. echo ' | |__) |__ _ __ _ _| |_ __ _ _ __ ___ '
  25. echo ' | ___/ _ \| '"'"'_ \| | | | _/ _` | '"'"'__/ _ \'
  26. echo ' | | | (_) | |_) | |_| | || (_| | | | __/'
  27. echo ' |_| \___/| .__/ \__,_|_| \__,_|_| \___|'
  28. echo ' | | '
  29. echo ' |_| '
  30. echo ""
  31. echo "This script attempts to setup a default installation of Popufare on a busunit (a Raspberry Pi, say)"
  32. echo "by setting up and configuring the system."
  33. echo ""
  34. echo "THIS SCRIPT CHANGES SYSTEM WIDE PARAMETERS AND COULD HAVE SERIOUS CONSEQUENCES TO THE MACHINE IT'S RUN"
  35. echo "ON."
  36. echo ""
  37. echo "As such, this scripts needs root access (via sudo)."
  38. echo "This script is meant to be a 'helper' script and isn't meant to be all encompassing. Among other things,"
  39. echo "this script will:"
  40. echo ""
  41. echo " * setup a 'bus' user and group"
  42. echo " * add the 'pi' user to the 'bus' group"
  43. echo " * clone the Popufare repo, build it's binaries and install them in the '/home/bus' user directory"
  44. echo " * copy over appropriate config files to the appropriate locations in the '/hom/ebus' user directory"
  45. echo " * setup ssh keys and credentials"
  46. echo " * setup the PPP config files"
  47. echo " * alter /boot/config.txt and /etc/modules to enable serial and sound setup"
  48. echo " * setup a 'kiosk' service for the front end DIU"
  49. echo " * setup 'rc.local' to start and monitor bus Popufare services on startup"
  50. echo " * setup legacy data directories in '/mnt/data2'"
  51. echo ""
  52. echo "Most parameters have options to prompt the user for installation."
  53. echo ""
  54. echo "This script should be used with caution and as a helper for an installation rather than a 'turn key' soluation."
  55. echo "For example, the ssh keys default to the UNSECURE 'snakeoil' default keys provided the repo for easy setup and"
  56. echo "MUST BE CHANGED for a production installation."
  57. echo ""
  58. echo "In addition, the host and other config file options should be set on a per installation basis, as should the custom"
  59. echo "DIU interface, rules, drivers.txt entries, etc."
  60. echo ""
  61. echo "This script should help in getting a Raspberry Pi ready for use with Popufare but should be thought of also as a"
  62. echo "record of what relevant files there are to change and where to look."
  63. echo ""
  64. echo "As Popufare matures, this installation script or something similar can accomodate a more seamless installation but"
  65. echo "this is what is provided now."
  66. echo ""
  67. echo "Popufare is free software so please feel free to contribute at:"
  68. echo ""
  69. echo " https://tree.clementinecomputing.com/clementinecomputing/popufare"
  70. echo ""
  71. echo -n "Hit return to continue [ok]: "
  72. read hitreturn
  73. ###### basic sanity checks to make sure we're installing on a Raspberry Pi
  74. ######
  75. if [[ ! -e /etc/os-release ]] ; then
  76. echo -n "Are you sure you want to install? '/etc/os-release' does not exist and this system might not be a Raspberry Pi. Continue? [y/N]: "
  77. read fnfok
  78. if [[ ! "$fnfok" =~ ^[yY]$ ]] ; then
  79. echo "cancelling installalation"
  80. echo ""
  81. exit
  82. else
  83. echo "ok, continuing installation..."
  84. fi
  85. else
  86. grep -P '^PRETTY_NAME="Raspbian' /etc/os-release 2>&1 > /dev/null
  87. r=$?
  88. if [[ $r != 0 ]] ; then
  89. echo -n "Raspbian not detected in '/etc/os-release'. This might not be Raspberry Pi. Continue with installation? [y/N]: "
  90. read okcontinue
  91. if [[ ! "$okcontinue" =~ ^[yY]$ ]] ; then
  92. echo "cancelling installation"
  93. echo ""
  94. exit
  95. else
  96. echo "ok, continuing installation..."
  97. fi
  98. fi
  99. fi
  100. echo ""
  101. ######
  102. ######
  103. echo -n "Create 'bus' user? [Y/n]: "
  104. read createbususer
  105. if [[ "$createbususer" =~ ^[yY]?$ ]]; then
  106. echo "... creating 'bus' user and group"
  107. sudo adduser bus
  108. fi
  109. echo ""
  110. echo -n "Add 'pi' to 'bus' group? [Y/n]: "
  111. read addpibus
  112. if [[ "$addpibus" =~ ^[yY]$ ]]; then
  113. echo "... add 'pi' to 'bus' group"
  114. sudo usermod -a -G bus pi
  115. fi
  116. echo ""
  117. echo "Creating /home/bus/(bin|database|config) with group write permissions"
  118. sudo mkdir -p /home/bus/bin
  119. sudo mkdir -p /home/bus/database
  120. sudo mkdir -p /home/bus/config
  121. sudo chown -R bus:bus /home/bus/bin
  122. sudo chown -R bus:bus /home/bus/database
  123. sudo chown -R bus:bus /home/bus/config
  124. sudo chmod -R g+w /home/bus/bin
  125. sudo chmod -R g+w /home/bus/database
  126. sudo chmod -R g+w /home/bus/config
  127. echo ""
  128. echo -n "Repo path (hit return to automatically clone the repo and use that) []: "
  129. read repolocation
  130. tmpdir=""
  131. if [[ "$repolocation" == "" ]] ; then
  132. tmpdir=`mktemp -d`
  133. repolocation="$tmpdir/popufare"
  134. pushd $tmpdir
  135. git clone "$GITURL"
  136. popd
  137. fi
  138. echo "Setting up binaries..."
  139. bd=/home/bus/popufare/busunit
  140. d=/home/bus/popufare/busunit/bin/native
  141. diudir=/home/bus/popufare/busunit/DIUv2
  142. tc=/home/bus/popufare/busunit/testing/config
  143. sudo cp -R $repolocation /home/bus
  144. sudo chown -R bus:bus /home/bus/popufare
  145. sudo bash -c " su - bus -c ' cd /home/bus/popufare/busunit ; ./build_all.sh native ; ' "
  146. sudo bash -c " cd /home/bus/bin ; ln -s $d/avls . ; ln -s $d/billdb . ; ln -s $d/client_supervisor . ; ln -s $d/debug_client . ; ln -s $d/diu_minder . ; ln -s $d/ipc_server . "
  147. sudo bash -c " cd /home/bus/bin ; ln -s $d/paddlemgr . ; ln -s $d/passdb . ; ln -s $d/piu_minder . ; ln -s $d/send_billing_record . ; ln -s $d/send_magstripe . "
  148. sudo bash -c " cd /home/bus/bin ; ln -s $bd/DIUv2/diu_kiosk . "
  149. sudo bash -c " cd /home/bus/bin ; ln -s $bd/scripts/init_bus.sh . ; ln -s $bd/scripts/update_loop.sh . ; ln -s $bd/scripts/setup-serial.py . ; ln -s $bd/scripts/get_net_ids.sh . "
  150. sudo bash -c " cd /home/bus/bin ; ln -s $bd/scripts/connection_tether.sh . "
  151. sudo bash -c " cd /home/bus/config ; ln -s $diudir/html . ; "
  152. sudo bash -c " cd /home/bus/config ; cp $bd/passdb/init.scm . ; cp $bd/passdb/rfid_patterns.txt . "
  153. sudo bash -c " cp $tc/equipnum.txt . ; cp $tc/config.tgz.checksum . ; cp $tc/config.tgz.version . ; cp $tc/firmware.tgz.checksum . ; cp $tc/firmware.tgz.version . ; cp $tc/serial.txt serial_num "
  154. sudo bash -c " rm -f /home/bus/bin/common_values.sh ; cp $bd/scripts/common_values.sh /home/bus/bin "
  155. echo ""
  156. ##### paddle setup
  157. #####
  158. echo -n "Setup paddles? [Y/n]: "
  159. read setuppaddles
  160. if [[ "$setuppaddles" =~ ^[yY]?$ ]] ; then
  161. echo -n "Paddle location? (blank for default testing paddles) []: "
  162. read paddlocation
  163. if [[ "$paddlelocation" == "" ]] ; then
  164. paddlelocation=$bd/busunit/testing
  165. fi
  166. echo "Using '$paddlelocation/*.paddle'..."
  167. sudo bash -c " cd /home/bus/config ; cp $bd/testing/*.paddle . "
  168. fi
  169. echo ""
  170. ##### rules setup
  171. #####
  172. echo -n "Setup rules? [Y/n]: "
  173. read setuprules
  174. if [[ "$setuprules" =~ ^[yY]?$ ]] ; then
  175. echo -n "Rule file? (blank for default testing rules) []: "
  176. read rulelocation
  177. if [[ "$rulelocation" == "" ]] ; then
  178. rulelocation=$bd/testing/rules-ORG.scm
  179. fi
  180. echo "Setting up rules from '$rulelocation'..."
  181. sudo bash -c " cd /home/bus/config ; cp $rulelocation rules.scm "
  182. fi
  183. echo ""
  184. ##### equipment number setup
  185. #####
  186. echo -n "Equipment number? [9999]: "
  187. read equipnum
  188. if [[ "$equipnum" == "" ]] ; then
  189. equipnum=9999
  190. fi
  191. echo "Setting equipment number to '$equipnum'..."
  192. sudo bash -c " echo $equipnum > /home/bus/config/equipnum.txt "
  193. echo ""
  194. ##### key setup
  195. #####
  196. echo -n "Setup secure communcation setup? [Y/n]: "
  197. read tunnelsetup
  198. if [[ "$tunnelsetup" =~ ^[yY]?$ ]] ; then
  199. echo -n " 'id_rsa' private key location? (default to snakeoil) []: "
  200. read keylocation
  201. if [[ "$keylocation" == "" ]] ; then
  202. keylocation=/home/bus/popufare/server/docker/snakeoil_id_rsa
  203. fi
  204. echo " Using '$keylocation' for private key..."
  205. sudo mkdir -p /home/bus/.ssh
  206. sudo chown -R bus:bus /home/bus/.ssh
  207. sudo cp $keylocation /home/bus/.ssh/id_rsa_bus
  208. sudo chown bus:bus /home/bus/.ssh/id_rsa_bus
  209. sudo chmod 400 /home/bus/.ssh/id_rsa_bus
  210. echo ""
  211. echo -n " Tunnel port? [6055]: "
  212. read tunnelport
  213. if [[ "$tunnelport" == "" ]] ; then
  214. tunnelport=6055
  215. fi
  216. echo ""
  217. echo -n " Tunnel user and host? [bus@example.com]: "
  218. read tunnelhost
  219. if [[ "$tunnelhost" == "" ]] ; then
  220. tunnelhost='bus@example.com'
  221. fi
  222. echo ""
  223. echo -n " Adding values to '/home/bus/bin/common_values.sh'..."
  224. sed -i 's;^SSH_DEFAULT_TARGET=;#SSH_DEFAULT_TARGET=;g' /home/bus/bin/common_values.sh
  225. sed -i 's;^SSH_DEFAULT_PORT=;#SSH_DEFAULT_PORT=;g' /home/bus/bin/common_values.sh
  226. sed -i 's;^SSH_DEFAULT_IDENTITY=;#SSH_DEFAULT_IDENTITY=;g' /home/bus/bin/common_values.sh
  227. sed -i 's;^SSH_TARGET=;#SSH_TARGET=;g' /home/bus/bin/common_values.sh
  228. sed -i 's;^SSH_PORT=;#SSH_PORT=;g' /home/bus/bin/common_values.sh
  229. sed -i 's;^SSH_IDENTITY=;#SSH_IDENTITY=;g' /home/bus/bin/common_values.sh
  230. echo 'SSH_DEFAULT_TARGET="'"$tunnelhost"'"' >> /home/bus/bin/common_values.sh
  231. echo 'SSH_DEFAULT_PORT="'"$tunnelport"'"' >> /home/bus/bin/common_values.sh
  232. echo 'SSH_DEFAULT_IDENTITY="/home/bus/.ssh/id_rsa_bus"' >> /home/bus/bin/common_values.sh
  233. echo '' >> /home/bus/bin/common_values.sh
  234. echo 'SSH_TARGET="$SSH_DEFAULT_TARGET"' >> /home/bus/bin/common_values.sh
  235. echo 'SSH_PORT="$SSH_DEFAULT_PORT"' >> /home/bus/bin/common_values.sh
  236. echo 'SSH_IDENTITY="$SSH_DEFAULT_IDENTITY"' >> /home/bus/bin/common_values.sh
  237. echo ""
  238. fi
  239. echo ""
  240. ##### drivers setup
  241. #####
  242. echo -n "'drivers.txt' location? (default to test) []: "
  243. read driverlocation
  244. if [[ "$driverlocation" == "" ]] ; then
  245. driverlocation=/home/bus/popufare/busunit/DIUv2/drivers.txt
  246. fi
  247. echo "Using '$driverlocation' for drivers.txt ..."
  248. sudo cp $driverlocation /home/bus/config
  249. echo ""
  250. ##### setup fona ppp
  251. #####
  252. echo -n "Setup FONA ppp files? [Y/n]: "
  253. read setupfona
  254. if [[ "$setupfona" =~ ^[yY]?$ ]] ; then
  255. echo " /home/bus/popufare/busunit/ppp-dialer/etc/ppp/peers/fona -> /etc/ppp/peers/gprs"
  256. echo " /home/bus/popufare/busunit/ppp-dialer/etc/chatscripts/gprs -> /etc/chatscripts/gprs"
  257. sudo cp /home/bus/popufare/busunit/ppp-dialer/etc/ppp/peers/fona /etc/ppp/peers/gprs
  258. sudo cp /home/bus/popufare/busunit/ppp-dialer/etc/chatscripts/gprs /etc/chatscripts/gprs
  259. echo ""
  260. fi
  261. #echo -n "Setup FONA to connect at startup? [Y/n]: "
  262. #read fonanetwork
  263. #
  264. #if [[ "$fonanetwork" =~ ^[yY]?$ ]] ; then
  265. # echo " Creating '/etc/network/interfaces.d/fona'..."
  266. # sudo echo 'auto fona' > /etc/network/interfaces.d/fona
  267. # sudo echo 'iface fona inet ppp' >> /etc/network/interfaces.d/fona
  268. # sudo echo ' provider fona' >> /etc/network/interfaces.d/fona
  269. #fi
  270. ##### Setup config.txt
  271. #####
  272. echo -n "Setup '/boot/config.txt'? (Note this could have serious consequences, use with caution) [y/N]: "
  273. read setupconfig
  274. if [[ "$setupconfig" =~ ^[yY]$ ]] ; then
  275. tmpfn=`mktemp`
  276. bfn=`basename $tmpfn`
  277. echo " Saving 'config.txt' to /root/$bfn"
  278. sudo cp /boot/config.txt $tmpfn
  279. sudo mv $tmpfn /root/$bfn
  280. echo " adding> enable_uart=1"
  281. sudo sed -i 's;^enable_uart=;#enable_uart=;g' /boot/config.txt
  282. sudo bash -c 'echo "enable_uart=1" >> /boot/config.txt'
  283. echo " adding> dtoverlay=pi3-disable-bt"
  284. sudo sed -i 's;^dtoverlay=pi3-disable-bt;#dtoverlay=pi3-disable-bt;g' /boot/config.txt
  285. sudo bash -c 'echo "dtoverlay=pi3-disable-bt" >> /boot/config.txt'
  286. echo " adding> dtoverlay=pi3-disable-wifi"
  287. sudo sed -i 's;^dtoverlay=pi3-disable-wifi;#dtoverlay=pi3-disable-wifi;g' /boot/config.txt
  288. sudo bash -c 'echo "dtoverlay=pi3-disable-wifi" >> /boot/config.txt'
  289. fi
  290. echo ""
  291. ##### Setup sound in modules file
  292. #####
  293. echo -n "Setup sound (alter '/etc/modules')? [y/N]: "
  294. read setupsound
  295. if [[ "$setupsound" =~ ^[yY]$ ]] ; then
  296. tmpfn=`mktemp`
  297. bfn=`basename $tmpfn`
  298. echo " Saving 'modules' to /root/$bfn"
  299. sudo cp /etc/modules $tmpfn
  300. sudo mv $tmpfn /root/$bfn
  301. sudo sed -i 's;^snd-bcm2835;#snd-bcm2835;g' /etc/modules
  302. echo ' adding> snd-bcm2835'
  303. sudo bash -c 'echo "snd-bcm2835" >> /etc/modules'
  304. fi
  305. ##### Setup kiosk
  306. #####
  307. echo -n "Setup kiosk service? [Y/n]: "
  308. read setupkiosk
  309. if [[ "$setupkiosk" =~ ^[yY]?$ ]] ; then
  310. echo " /home/bus/popufare/busunit/scripts/kiosk.service -> /lib/systemd/system/kiosk.service"
  311. sudo cp /home/bus/popufare/busunit/scripts/kiosk.service /lib/systemd/system/kiosk.service
  312. echo " enabling kiosk.service ('systemctl enable kiosk.service')"
  313. sudo systemctl enable kiosk.service
  314. fi
  315. echo ""
  316. ##### Setup startup
  317. #####
  318. echo -n "Setup startup 'rc.local'? [Y/n]: "
  319. read setupstartup
  320. if [[ "$setupstartup" =~ ^[yY]?$ ]] ; then
  321. tmpfn=`mktemp`
  322. bfn=`basename $tmpfn`
  323. echo " saving /etc/rc.local to /root/$bfn"
  324. sudo cp /etc/rc.local $tmpfn
  325. sudo mv $tmpfn /root/$bfn
  326. echo " /home/bus/popufare/busunit/scripts/rc.local -> /etc/rc.local"
  327. sudo cp /home/bus/popufare/busunit/scripts/rc.local /etc/rc.local
  328. fi
  329. echo ""
  330. ##### setup legacy data directories
  331. #####
  332. echo -n "Setup legacy data directories? [y/N]: "
  333. read setuplegacy
  334. if [[ "$setuplegacy" =~ ^[yY]$ ]] ; then
  335. sudo mkdir -p /mnt/data
  336. sudo mkdir -p /mnt/data2
  337. sudo chown -R bus:bus /mnt/data
  338. sudo chown -R bus:bus /mnt/data2
  339. fi
  340. #####
  341. #####
  342. ##### final permissions setup
  343. #####
  344. echo "Setting up final permission for /home/bus/(bin|database|config)"
  345. sudo chown -R bus:bus /home/bus/bin
  346. sudo chown -R bus:bus /home/bus/database
  347. sudo chown -R bus:bus /home/bus/config
  348. echo ""
  349. if [[ "$tmpdir" != '' ]] ; then
  350. echo "... should remove $tmpdir"
  351. #rm -rf $tmdpri
  352. fi
  353. echo "done"