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.
 
 
 

342 lines
10 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 stuff
  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. python-matplotlib \
  90. ipython \
  91. openvpn \
  92. network-manager-openvpn-gnome \
  93. openssl \
  94. tcpdump \
  95. screen \
  96. devscripts \
  97. mailutils
  98. # required
  99. apt-get -y install \
  100. openssh-server
  101. # Set up timezone to America/New_York for the live CD
  102. echo America/New_York > /etc/timezone
  103. dpkg-reconfigure -f noninteractive tzdata
  104. # Create nilmdb user to run the database
  105. adduser --system --group --shell /bin/bash --disabled-password nilmdb
  106. cp -rv /etc/skel/.??* /home/nilmdb
  107. chown -R nilmdb:nilmdb /home/nilmdb
  108. # Create WSGI scripts
  109. cat > /home/nilmdb/nilmdb.wsgi <<"EOF"
  110. import nilmdb.server
  111. application = nilmdb.server.wsgi_application("/home/nilmdb/db","/nilmdb")
  112. EOF
  113. cat > /home/nilmdb/nilmrun.wsgi <<"EOF"
  114. import nilmrun.server
  115. application = nilmrun.server.wsgi_application("/nilmrun")
  116. EOF
  117. # Create apache config by hacking up the default one. Might be a better way
  118. # to do this, and it'll probably break on apache 2.4, but...
  119. DEF=/etc/apache2/sites-available/default
  120. perl -ne 'print unless /## NilmDB start/../## NilmDB end/' $DEF > $DEF.orig
  121. perl -ne 'print unless m-^[^#]*</VirtualHost>-..1' $DEF.orig > $DEF
  122. cat >>$DEF <<"EOF"
  123. ## NilmDB start
  124. WSGIScriptAlias /nilmdb /home/nilmdb/nilmdb.wsgi
  125. WSGIDaemonProcess nilmdb-procgroup threads=32 user=nilmdb group=nilmdb
  126. <Location /nilmdb>
  127. WSGIProcessGroup nilmdb-procgroup
  128. WSGIApplicationGroup nilmdb-appgroup
  129. </Location>
  130. WSGIScriptAlias /nilmrun /home/nilmdb/nilmrun.wsgi
  131. WSGIDaemonProcess nilmrun-procgroup threads=32 user=nilmdb group=nilmdb
  132. <Location /nilmrun>
  133. WSGIProcessGroup nilmrun-procgroup
  134. WSGIApplicationGroup nilmrun-appgroup
  135. </Location>
  136. ## NilmDB end
  137. EOF
  138. perl -ne 'print if m-^[^#]*</VirtualHost>-..1' $DEF.orig >> $DEF
  139. # Create nilmdb capture, processing, and cleanup files
  140. cat > /home/nilmdb/capture.sh <<"EOF"
  141. #!/bin/bash -e
  142. # Don't run capture if we're running off a live CD
  143. if grep -q boot=casper /proc/cmdline ; then
  144. echo "Skipping capture, because this is a live CD."
  145. exit 0
  146. fi
  147. echo "Starting capture in background..."
  148. nilm-pipewatch --daemon --lock "/tmp/nilmdb-capture.lock" --timeout 30 \
  149. "ethstream -a 192.168.1.209 -n 6 -r 8000" \
  150. "nilm-insert -m 10 -r 8000 --live /data/raw"
  151. EOF
  152. cat > /home/nilmdb/process.sh <<"EOF"
  153. #!/bin/bash -e
  154. # Ensure only one copy of this code runs at a time:
  155. LOCKFILE="/tmp/nilmdb-process.lock"
  156. exec 99>"$LOCKFILE"
  157. flock -n -x 99 || exit 0
  158. trap 'rm -f "$LOCKFILE"' 0
  159. nilm-sinefit -c 4 /data/raw /data/sinefit
  160. nilm-prep -c 1 -r 0 /data/raw /data/sinefit /data/prep-a
  161. nilm-prep -c 2 -r 120 /data/raw /data/sinefit /data/prep-b
  162. nilm-prep -c 3 -r 240 /data/raw /data/sinefit /data/prep-c
  163. nilm-decimate-auto /data/raw "/data/prep*"
  164. nilm-cleanup --yes /home/nilmdb/cleanup.cfg
  165. EOF
  166. cat > /home/nilmdb/cleanup.cfg <<"EOF"
  167. [/data/prep-*]
  168. keep = 1y
  169. [/data/raw]
  170. keep = 2w
  171. [/data/sinefit]
  172. keep = 1y
  173. decimated = false
  174. EOF
  175. # Set up crontab
  176. cat > /home/nilmdb/crontab <<"EOF"
  177. SHELL=/bin/bash
  178. PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
  179. # Run capture and processing scripts every 5 minutes
  180. */5 * * * * chronic /home/nilmdb/capture.sh
  181. */5 * * * * chronic /home/nilmdb/process.sh
  182. # Try to run nilmdb-fsck on boot. It should hopefully run before
  183. # apache opens the database, and apache will return errors to clients
  184. # until nilmdb-fsck is done.
  185. @reboot chronic nilmdb-fsck --fix --no-data /home/nilmdb/db
  186. EOF
  187. crontab -u nilmdb /home/nilmdb/crontab
  188. # Fix permissions
  189. chown -R nilmdb:nilmdb /home/nilmdb
  190. chmod +x /home/nilmdb/{capture,process}.sh
  191. # Fetch and build everything. Put it in the nilmdb dir
  192. echo "machine git.jim.sh login nilm password nilm" > /home/nilmdb/.netrc
  193. GIT=https://git.jim.sh/jim/lees
  194. rm -rf /home/nilmdb/git
  195. mkdir /home/nilmdb/git
  196. chown nilmdb:nilmdb /home/nilmdb/.netrc /home/nilmdb/git
  197. REPOS="nilmdb nilmtools nilmrun ethstream"
  198. # check it out as nilmdb, so the .netrc gets used
  199. for repo in $REPOS; do
  200. sudo -i -u nilmdb git clone $GIT/$repo.git git/$repo
  201. done
  202. # build as root, because we need to do that for the install
  203. for repo in $REPOS; do
  204. make -C /home/nilmdb/git/$repo install
  205. done
  206. # fix up all permissions in git dir, so nilmdb user can play with it later
  207. chown -R nilmdb:nilmdb /home/nilmdb/git
  208. # Create the initial database and streams by running the standalone
  209. # server as nilmdb, making the right nilmtool calls, and killing it.
  210. sudo -i -u nilmdb nilmdb-server -a 127.0.0.1 -p 18646 -d /home/nilmdb/db &
  211. SERVERPID=$!
  212. trap "kill -9 $SERVERPID" 0
  213. for i in $(seq 1 120) ; do
  214. sleep 1
  215. echo waiting for nilmdb to start $i
  216. if nilmtool -u http://127.0.0.1:18646/ info ; then
  217. break
  218. fi
  219. done
  220. nilmtool -u http://127.0.0.1:18646/ destroy -R "/data/*" || true
  221. nilmtool -u http://127.0.0.1:18646/ create /data/raw uint16_6
  222. nilmtool -u http://127.0.0.1:18646/ create /data/sinefit float32_3
  223. nilmtool -u http://127.0.0.1:18646/ create /data/prep-a float32_8
  224. nilmtool -u http://127.0.0.1:18646/ create /data/prep-b float32_8
  225. nilmtool -u http://127.0.0.1:18646/ create /data/prep-c float32_8
  226. kill $SERVERPID
  227. wait
  228. trap "" 0
  229. # Put some default desktop shortcuts in place
  230. DESKTOP=/etc/skel/Desktop
  231. mkdir -p $DESKTOP
  232. cp /usr/share/applications/exo-terminal-emulator.desktop $DESKTOP || true
  233. cp /usr/share/applications/exo-web-browser.desktop $DESKTOP || true
  234. chmod +x $DESKTOP/* # needs to be executable for 13.04+
  235. # XFCE / theme customizations
  236. if [ -d /usr/share/themes/Clearlooks ] && \
  237. [ -d /usr/share/icons/elementary-xfce ] ; then
  238. cat > /usr/share/gconf/defaults/88_nilmdbuntu-settings <<"EOF"
  239. /desktop/gnome/interface/gtk_theme "Clearlooks"
  240. /desktop/gnome/interface/icon_theme "elementary-xfce"
  241. EOF
  242. update-gconf-defaults
  243. fi
  244. XML=/etc/xdg/xdg-xubuntu/xfce4/xfconf/xfce-perchannel-xml
  245. BG=/usr/share/xfce4/backdrops
  246. mkdir -p $XML
  247. cat >$XML/xfce4-desktop.xml <<"EOF"
  248. <?xml version="1.0" encoding="UTF-8"?>
  249. <channel name="xfce4-desktop" version="1.0">
  250. <property name="desktop-icons" type="empty">
  251. <property name="style" type="int" value="2"/>
  252. <property name="file-icons" type="empty">
  253. <property name="show-home" type="bool" value="true"/>
  254. <property name="show-filesystem" type="bool" value="true"/>
  255. <property name="show-removable" type="bool" value="true"/>
  256. <property name="show-trash" type="bool" value="true"/>
  257. </property>
  258. </property>
  259. <property name="backdrop" type="empty">
  260. <property name="screen0" type="empty">
  261. <property name="monitor0" type="empty">
  262. <property name="image-path" type="string"
  263. value="/usr/share/xfce4/backdrops/nilmdbuntu.png"/>
  264. <property name="image-show" type="bool" value="true"/>
  265. <property name="image-style" type="int" value="4"/>
  266. <property name="color-style" type="int" value="0"/>
  267. <property name="color1" type="array">
  268. <value type="uint" value="0"/>
  269. <value type="uint" value="0"/>
  270. <value type="uint" value="0"/>
  271. <value type="uint" value="65535"/>
  272. </property>
  273. </property>
  274. <property name="monitor1" type="empty">
  275. <property name="image-path" type="string"
  276. value="/usr/share/xfce4/backdrops/nilmdbuntu.png"/>
  277. <property name="image-show" type="bool" value="true"/>
  278. <property name="image-style" type="int" value="4"/>
  279. <property name="color-style" type="int" value="0"/>
  280. <property name="color1" type="array">
  281. <value type="uint" value="0"/>
  282. <value type="uint" value="0"/>
  283. <value type="uint" value="0"/>
  284. <value type="uint" value="65535"/>
  285. </property>
  286. </property>
  287. </property>
  288. </property>
  289. </channel>
  290. EOF
  291. sed -i -s -e 's/Greybird/Default/g' $XML/xfwm4.xml || true
  292. sed -i -s -e 's/Greybird/Clearlooks/g' $XML/xsettings.xml || true
  293. sed -i -s -e \
  294. 's/elementary-xfce-dark/elementary-xfce/g' $XML/xsettings.xml || true
  295. # Firefox defaults
  296. cat >/etc/firefox/syspref.js <<"EOF"
  297. pref("browser.startup.homepage", "http://nilmdb.com/");
  298. EOF
  299. cat >/etc/xul-ext/homepage.properties <<"EOF"
  300. browser.startup.homepage=http://nilmdb.com/
  301. EOF
  302. cat >/etc/xul-ext/ubufox.js <<"EOF"
  303. pref("browser.startup.homepage", "file:/etc/xul-ext/homepage.properties");
  304. EOF