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.
 
 
 

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