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.
 
 
 

327 lines
9.8 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. try_install() {
  9. # try to install packages, but ignore failure
  10. for pkg in "$@"; do
  11. apt-get -y install "$pkg" || true
  12. done
  13. }
  14. # Set up live username and hostname
  15. cat >/etc/casper.conf <<"EOF"
  16. export USERNAME="ubuntu"
  17. export USERFULLNAME="Live session user"
  18. export HOST="nilmdb"
  19. export BUILD_SYSTEM="Ubuntu"
  20. export FLAVOUR="NilmDBuntu"
  21. EOF
  22. # Upgrade packages, remove old kernels
  23. apt-get update
  24. # in 13.04, doing upgrade & dist-upgrade together tries to install 2 kernels
  25. # at the same time, which breaks for some reason. Also, try the upgrade
  26. # multiple times since that can help
  27. apt-get -y upgrade || apt-get -y upgrade || true
  28. apt-get -y dist-upgrade || apt-get -y dist-upgrade || true
  29. apt-get -y --purge autoremove
  30. for VER in $(ls --sort=version /lib/modules/ | head -n -1) ; do
  31. apt-get -y --purge remove ".*$VER.*"
  32. done
  33. # Disable upgrade popups
  34. sed -i -s -e 's/Prompt=normal/Prompt=never/g' \
  35. /etc/update-manager/release-upgrades || true
  36. # some stuff we need from Ubuntu
  37. try_install \
  38. wbritish \
  39. thunderbird-locale-en-us
  40. # Set up & install postfix for local mail delivery
  41. debconf-set-selections <<"EOF"
  42. postfix postfix/mailname string localdomain
  43. postfix postfix/main_mailer_type select Local only
  44. EOF
  45. apt-get -y install postfix
  46. # install nilmdb things
  47. apt-get -y install \
  48. python2.7 \
  49. python2.7-dev \
  50. python-setuptools \
  51. python-pip \
  52. cython \
  53. git \
  54. build-essential \
  55. python-cherrypy3 \
  56. python-decorator \
  57. python-simplejson \
  58. python-requests \
  59. python-dateutil \
  60. python-tz \
  61. python-progressbar \
  62. python-psutil \
  63. python-numpy \
  64. python-nose \
  65. python-coverage \
  66. apache2 \
  67. libapache2-mod-wsgi \
  68. python-scipy \
  69. python-daemon
  70. # install other useful but optional utilities
  71. try_install \
  72. emacs-goodies-el \
  73. emacs23-nox \
  74. octave \
  75. octave-signal \
  76. octave-missing-functions \
  77. gnuplot \
  78. curl \
  79. gddrescue \
  80. help2man \
  81. luatex \
  82. pgf \
  83. moreutils \
  84. ntfsprogs \
  85. subversion \
  86. dlocate \
  87. ack-grep \
  88. mutt
  89. # required
  90. apt-get -y install \
  91. openssh-server
  92. # Set up timezone to America/New_York for the live CD
  93. echo America/New_York > /etc/timezone
  94. dpkg-reconfigure -f noninteractive tzdata
  95. # Create nilmdb user to run the database
  96. adduser --system --group --shell /bin/bash --disabled-password nilmdb
  97. cp -rv /etc/skel/.??* /home/nilmdb
  98. chown -R nilmdb:nilmdb /home/nilmdb
  99. # Create WSGI scripts
  100. cat > /home/nilmdb/nilmdb.wsgi <<"EOF"
  101. import nilmdb.server
  102. application = nilmdb.server.wsgi_application("/home/nilmdb/db","/nilmdb")
  103. EOF
  104. cat > /home/nilmdb/nilmrun.wsgi <<"EOF"
  105. import nilmrun.server
  106. application = nilmrun.server.wsgi_application("/nilmrun")
  107. EOF
  108. # Create apache config by hacking up the default one. Might be a better way
  109. # to do this, and it'll probably break on apache 2.4, but...
  110. DEF=/etc/apache2/sites-available/default
  111. perl -ne 'print unless /## NilmDB start/../## NilmDB end/' $DEF > $DEF.orig
  112. perl -ne 'print unless m-^[^#]*</VirtualHost>-..1' $DEF.orig > $DEF
  113. cat >>$DEF <<"EOF"
  114. ## NilmDB start
  115. WSGIScriptAlias /nilmdb /home/nilmdb/nilmdb.wsgi
  116. WSGIDaemonProcess nilmdb-procgroup threads=32 user=nilmdb group=nilmdb
  117. <Location /nilmdb>
  118. WSGIProcessGroup nilmdb-procgroup
  119. WSGIApplicationGroup nilmdb-appgroup
  120. </Location>
  121. WSGIScriptAlias /nilmrun /home/nilmdb/nilmrun.wsgi
  122. WSGIDaemonProcess nilmrun-procgroup threads=32 user=nilmdb group=nilmdb
  123. <Location /nilmrun>
  124. WSGIProcessGroup nilmrun-procgroup
  125. WSGIApplicationGroup nilmrun-appgroup
  126. </Location>
  127. ## NilmDB end
  128. EOF
  129. perl -ne 'print if m-^[^#]*</VirtualHost>-..1' $DEF.orig >> $DEF
  130. # Create nilmdb capture, processing, and cleanup files
  131. cat > /home/nilmdb/capture.sh <<"EOF"
  132. #!/bin/bash -e
  133. # Don't run capture if we're running off a live CD
  134. if grep -q boot=casper /proc/cmdline ; then
  135. echo "Skipping capture, because this is a live CD."
  136. exit 0
  137. fi
  138. echo "Starting capture in background..."
  139. nilm-pipewatch --daemon --lock "/tmp/nilmdb-capture.lock" --timeout 30 \
  140. "ethstream -a 192.168.1.209 -n 6 -r 8000" \
  141. "nilm-insert -m 10 -r 8000 --live /data/raw"
  142. EOF
  143. cat > /home/nilmdb/process.sh <<"EOF"
  144. #!/bin/bash -e
  145. # Ensure only one copy of this code runs at a time:
  146. LOCKFILE="/tmp/nilmdb-process.lock"
  147. exec 99>"$LOCKFILE"
  148. flock -n -x 99 || exit 0
  149. trap 'rm -f "$LOCKFILE"' 0
  150. nilm-sinefit -c 4 /data/raw /data/sinefit
  151. nilm-prep -c 1 -r 0 /data/raw /data/sinefit /data/prep-a
  152. nilm-prep -c 2 -r 120 /data/raw /data/sinefit /data/prep-b
  153. nilm-prep -c 3 -r 240 /data/raw /data/sinefit /data/prep-c
  154. nilm-decimate-auto /data/raw "/data/prep*"
  155. nilm-cleanup --yes /home/nilmdb/cleanup.cfg
  156. EOF
  157. cat > /home/nilmdb/cleanup.cfg <<"EOF"
  158. [/data/prep-*]
  159. keep = 1y
  160. [/data/raw]
  161. keep = 2w
  162. [/data/sinefit]
  163. keep = 1y
  164. decimated = false
  165. EOF
  166. # Set up crontab
  167. cat > /home/nilmdb/crontab <<"EOF"
  168. SHELL=/bin/bash
  169. PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
  170. # Run capture and processing scripts every 5 minutes
  171. */5 * * * * chronic /home/nilmdb/capture.sh
  172. */5 * * * * chronic /home/nilmdb/process.sh
  173. # Try to run nilmdb-fsck on boot. It should hopefully run before
  174. # apache opens the database, and apache will return errors to clients
  175. # until nilmdb-fsck is done.
  176. @reboot chronic nilmdb-fsck --fix --no-data /home/nilmdb/db
  177. EOF
  178. crontab -u nilmdb /home/nilmdb/crontab
  179. # Fix permissions
  180. chown -R nilmdb:nilmdb /home/nilmdb
  181. chmod +x /home/nilmdb/{capture,process}.sh
  182. # Fetch and build everything. Put it in the nilmdb dir
  183. echo "machine git.jim.sh login nilm password nilm" > /home/nilmdb/.netrc
  184. GIT=https://git.jim.sh/jim/lees
  185. rm -rf /home/nilmdb/git
  186. mkdir /home/nilmdb/git
  187. chown nilmdb:nilmdb /home/nilmdb/.netrc /home/nilmdb/git
  188. REPOS="nilmdb nilmtools nilmrun ethstream"
  189. # check it out as nilmdb, so the .netrc gets used
  190. for repo in $REPOS; do
  191. sudo -i -u nilmdb git clone $GIT/$repo.git git/$repo
  192. done
  193. # build as root, because we need to do that for the install
  194. for repo in $REPOS; do
  195. make -C /home/nilmdb/git/$repo install
  196. done
  197. # fix up all permissions in git dir, so nilmdb user can play with it later
  198. chown -R nilmdb:nilmdb /home/nilmdb/git
  199. # Create the initial database and streams by running the standalone
  200. # server as nilmdb, making the right nilmtool calls, and killing it.
  201. sudo -i -u nilmdb nilmdb-server -a 127.0.0.1 -p 18646 -d /home/nilmdb/db &
  202. SERVERPID=$!
  203. trap "kill -9 $SERVERPID" 0
  204. for i in $(seq 1 120) ; do
  205. sleep 1
  206. echo waiting for nilmdb to start $i
  207. if nilmtool -u http://127.0.0.1:18646/ info ; then
  208. break
  209. fi
  210. done
  211. nilmtool -u http://127.0.0.1:18646/ destroy -R "/data/*" || true
  212. nilmtool -u http://127.0.0.1:18646/ create /data/raw uint16_6
  213. nilmtool -u http://127.0.0.1:18646/ create /data/sinefit float32_3
  214. nilmtool -u http://127.0.0.1:18646/ create /data/prep-a float32_8
  215. nilmtool -u http://127.0.0.1:18646/ create /data/prep-b float32_8
  216. nilmtool -u http://127.0.0.1:18646/ create /data/prep-c float32_8
  217. kill $SERVERPID
  218. wait
  219. trap "" 0
  220. # Put some default desktop shortcuts in place
  221. DESKTOP=/etc/skel/Desktop
  222. mkdir -p $DESKTOP
  223. cp /usr/share/applications/exo-terminal-emulator.desktop $DESKTOP || true
  224. cp /usr/share/applications/exo-web-browser.desktop $DESKTOP || true
  225. chmod +x $DESKTOP/* # needs to be executable for 13.04+
  226. # XFCE / theme customizations
  227. if [ -d /usr/share/themes/Clearlooks ] && \
  228. [ -d /usr/share/icons/elementary-xfce ] ; then
  229. cat > /usr/share/gconf/defaults/88_nilmdbuntu-settings <<"EOF"
  230. /desktop/gnome/interface/gtk_theme "Clearlooks"
  231. /desktop/gnome/interface/icon_theme "elementary-xfce"
  232. EOF
  233. update-gconf-defaults
  234. fi
  235. XML=/etc/xdg/xdg-xubuntu/xfce4/xfconf/xfce-perchannel-xml
  236. BG=/usr/share/xfce4/backdrops
  237. mkdir -p $XML
  238. cat >$XML/xfce4-desktop.xml <<"EOF"
  239. <?xml version="1.0" encoding="UTF-8"?>
  240. <channel name="xfce4-desktop" version="1.0">
  241. <property name="desktop-icons" type="empty">
  242. <property name="style" type="int" value="2"/>
  243. <property name="file-icons" type="empty">
  244. <property name="show-home" type="bool" value="true"/>
  245. <property name="show-filesystem" type="bool" value="true"/>
  246. <property name="show-removable" type="bool" value="true"/>
  247. <property name="show-trash" type="bool" value="true"/>
  248. </property>
  249. </property>
  250. <property name="backdrop" type="empty">
  251. <property name="screen0" type="empty">
  252. <property name="monitor0" type="empty">
  253. <property name="image-path" type="string"
  254. value="/usr/share/xfce4/backdrops/nilmdbuntu.png"/>
  255. <property name="image-show" type="bool" value="true"/>
  256. <property name="image-style" type="int" value="4"/>
  257. <property name="color-style" type="int" value="0"/>
  258. <property name="color1" type="array">
  259. <value type="uint" value="0"/>
  260. <value type="uint" value="0"/>
  261. <value type="uint" value="0"/>
  262. <value type="uint" value="65535"/>
  263. </property>
  264. </property>
  265. <property name="monitor1" type="empty">
  266. <property name="image-path" type="string"
  267. value="/usr/share/xfce4/backdrops/nilmdbuntu.png"/>
  268. <property name="image-show" type="bool" value="true"/>
  269. <property name="image-style" type="int" value="4"/>
  270. <property name="color-style" type="int" value="0"/>
  271. <property name="color1" type="array">
  272. <value type="uint" value="0"/>
  273. <value type="uint" value="0"/>
  274. <value type="uint" value="0"/>
  275. <value type="uint" value="65535"/>
  276. </property>
  277. </property>
  278. </property>
  279. </property>
  280. </channel>
  281. EOF
  282. sed -i -s -e 's/Greybird/Default/g' $XML/xfwm4.xml || true
  283. sed -i -s -e 's/Greybird/Clearlooks/g' $XML/xsettings.xml || true
  284. sed -i -s -e \
  285. 's/elementary-xfce-dark/elementary-xfce/g' $XML/xsettings.xml || true
  286. # Firefox defaults
  287. cat >/etc/firefox/syspref.js <<"EOF"
  288. pref("browser.startup.homepage", "http://nilmdb.com/");
  289. EOF