You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

226 lines
6.1 KiB

  1. #!/bin/bash
  2. if [ "$IN_CHROOT" != "1" ] ; then
  3. echo This is supposed to run inside the chroot, oops
  4. exit 1
  5. fi
  6. set -e
  7. set -x
  8. # Set up live username and hostname
  9. cat >/etc/casper.conf <<"EOF"
  10. export USERNAME="ubuntu"
  11. export USERFULLNAME="Live session user"
  12. export HOST="nilmdb"
  13. export BUILD_SYSTEM="Ubuntu"
  14. export FLAVOUR="NilmDBuntu"
  15. EOF
  16. # Upgrade packages, remove old kernels
  17. apt-get update
  18. apt-get -y dist-upgrade
  19. apt-get -y --purge autoremove
  20. for VER in $(ls --sort=version /lib/modules/ | head -n -1) ; do
  21. apt-get -y --purge remove ".*$VER.*"
  22. done
  23. # Disable upgrade popups
  24. sed -i -s -e 's/Prompt=normal/Prompt=never/g' \
  25. /etc/update-manager/release-upgrades
  26. # some stuff we need from Ubuntu
  27. apt-get -y install \
  28. wbritish \
  29. thunderbird-locale-en-us
  30. # install nilmdb things
  31. apt-get -y install \
  32. python2.7 \
  33. python2.7-dev \
  34. python-setuptools \
  35. python-pip \
  36. cython \
  37. git \
  38. build-essential \
  39. python-cherrypy3 \
  40. python-decorator \
  41. python-simplejson \
  42. python-requests \
  43. python-dateutil \
  44. python-tz \
  45. python-progressbar \
  46. python-psutil \
  47. python-numpy \
  48. python-nose \
  49. python-coverage \
  50. apache2 \
  51. libapache2-mod-wsgi \
  52. python-scipy \
  53. python-daemon
  54. # install other useful utilities
  55. apt-get -y install \
  56. emacs-goodies-el \
  57. emacs23-nox \
  58. octave \
  59. octave-signal \
  60. octave-missing-functions \
  61. gnuplot \
  62. curl \
  63. gddrescue \
  64. help2man \
  65. luatex \
  66. pgf \
  67. moreutils \
  68. ntfsprogs \
  69. subversion \
  70. dlocate \
  71. ack-grep \
  72. mutt \
  73. openssh-server
  74. # Set up timezone to America/New_York for the live CD
  75. echo America/New_York > /etc/timezone
  76. dpkg-reconfigure -f noninteractive tzdata
  77. # Set up & install postfix for local mail delivery
  78. debconf-set-selections <<"EOF"
  79. postfix postfix/mailname string "localdomain"
  80. postfix postfix/main_mailer_type select "Local only"
  81. EOF
  82. apt-get -y install postfix
  83. # Create nilmdb user to run the database
  84. adduser --system --group --shell /bin/bash --disabled-password nilmdb
  85. # Create WSGI scripts
  86. cat > /home/nilmdb/nilmdb.wsgi <<"EOF"
  87. import nilmdb.server
  88. application = nilmdb.server.wsgi_application("/home/nilmdb/db","/nilmdb")
  89. EOF
  90. cat > /home/nilmdb/nilmrun.wsgi <<"EOF"
  91. import nilmrun.server
  92. application = nilmrun.server.wsgi_application("/nilmrun")
  93. EOF
  94. # Create apache config by hacking up the default one. Might be a better way
  95. # to do this, and it'll probably break on apache 2.4, but...
  96. DEF=/etc/apache2/sites-available/default
  97. perl -ne 'print unless /## NilmDB start/../## NilmDB end/' $DEF > $DEF.orig
  98. perl -ne 'print unless m-^[^#]*</VirtualHost>-..1' $DEF.orig > $DEF
  99. cat >>$DEF <<"EOF"
  100. ## NilmDB start
  101. WSGIScriptAlias /nilmdb /home/nilmdb/nilmdb.wsgi
  102. WSGIDaemonProcess nilmdb-procgroup threads=32 user=nilmdb group=nilmdb
  103. <Location /nilmdb>
  104. WSGIProcessGroup nilmdb-procgroup
  105. WSGIApplicationGroup nilmdb-appgroup
  106. </Location>
  107. WSGIScriptAlias /nilmrun /home/nilmdb/nilmrun.wsgi
  108. WSGIDaemonProcess nilmrun-procgroup threads=32 user=nilmdb group=nilmdb
  109. <Location /nilmrun>
  110. WSGIProcessGroup nilmrun-procgroup
  111. WSGIApplicationGroup nilmrun-appgroup
  112. </Location>
  113. ## NilmDB end
  114. EOF
  115. perl -ne 'print if m-^[^#]*</VirtualHost>-..1' $DEF.orig >> $DEF
  116. # Create nilmdb capture, processing, and cleanup files
  117. cat > /home/nilmdb/capture.sh <<"EOF"
  118. #!/bin/bash -e
  119. # Don't run capture if we're running off a live CD
  120. if grep -q boot=casper /proc/cmdline ; then
  121. exit 0
  122. fi
  123. nilm-pipewatch --daemon --lock "/tmp/nilmdb-capture.lock" --timeout 30 \
  124. "ethstream -a 192.168.1.209 -n 6 -r 8000" \
  125. "nilm-insert -m 10 -r 8000 --live /data/raw"
  126. EOF
  127. cat > /home/nilmdb/process.sh <<"EOF"
  128. #!/bin/bash -e
  129. # Ensure only one copy of this code runs at a time:
  130. LOCKFILE="/tmp/nilmdb-process.lock"
  131. exec 99>"$LOCKFILE"
  132. flock -n -x 99 || exit 0
  133. trap 'rm -f "$LOCKFILE"' 0
  134. nilm-sinefit -c 4 /data/raw /data/sinefit
  135. nilm-prep -c 1 -r 0 /data/raw /data/sinefit /data/prep-a
  136. nilm-prep -c 2 -r 120 /data/raw /data/sinefit /data/prep-b
  137. nilm-prep -c 3 -r 240 /data/raw /data/sinefit /data/prep-c
  138. nilm-decimate-auto /data/raw "/data/prep*"
  139. nilm-cleanup --yes /home/nilmdb/cleanup.cfg
  140. EOF
  141. cat > /home/nilmdb/cleanup.cfg <<"EOF"
  142. [/data/prep-*]
  143. keep = 1y
  144. [/data/raw]
  145. keep = 2w
  146. [/data/sinefit]
  147. keep = 1y
  148. decimated = false
  149. EOF
  150. # Set up crontab
  151. cat > /home/nilmdb/crontab <<"EOF"
  152. SHELL=/bin/bash
  153. PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
  154. # Run capture and processing scripts every 5 minutes
  155. */5 * * * * chronic /home/nilmdb/capture.sh
  156. */5 * * * * chronic /home/nilmdb/process.sh
  157. # Try to run nilmdb-fsck on boot. It should hopefully run before
  158. # apache opens the database, and apache will return errors to clients
  159. # until nilmdb-fsck is done.
  160. @reboot chronic nilmdb-fsck --fix --no-data /home/nilmdb/db
  161. EOF
  162. crontab -u nilmdb /home/nilmdb/crontab
  163. # Fix permissions
  164. chown -R nilmdb:nilmdb /home/nilmdb
  165. chmod +x /home/nilmdb/{capture,process}.sh
  166. # Fetch and build everything. Put it in the nilmdb dir
  167. echo "machine git.jim.sh login nilm password nilm" > /home/nilmdb/.netrc
  168. GIT=https://git.jim.sh/jim/lees
  169. rm -rf /home/nilmdb/git
  170. mkdir /home/nilmdb/git
  171. chown nilmdb:nilmdb /home/nilmdb/.netrc /home/nilmdb/git
  172. REPOS="nilmdb nilmtools nilmrun ethstream"
  173. # check it out as nilmdb, so the .netrc gets used
  174. for repo in $REPOS; do
  175. sudo -i -u nilmdb git clone $GIT/$repo.git git/$repo
  176. done
  177. # build as root, because we need to do that for the install
  178. for repo in $REPOS; do
  179. make -C /home/nilmdb/git/$repo install
  180. done
  181. # fix up all permissions in git dir, so nilmdb user can play with it later
  182. chown -R nilmdb:nilmdb /home/nilmdb/git
  183. # Create the initial database and streams by running the standalone
  184. # server as nilmdb, making the right nilmtool calls, and killing it.
  185. sudo -i -u nilmdb nilmdb-server -a 127.0.0.1 -p 18646 &
  186. SERVER=$!
  187. sleep 1
  188. nilmtool -u http://127.0.0.1:18646/ destroy -R "/data/*" || true
  189. nilmtool -u http://127.0.0.1:18646/ create /data/raw uint16_6
  190. nilmtool -u http://127.0.0.1:18646/ create /data/sinefit float32_3
  191. nilmtool -u http://127.0.0.1:18646/ create /data/prep-a float32_8
  192. nilmtool -u http://127.0.0.1:18646/ create /data/prep-b float32_8
  193. nilmtool -u http://127.0.0.1:18646/ create /data/prep-c float32_8
  194. kill $!
  195. wait