From 85f6db39a3b61d0740d58a701a95bc9453ba0b34 Mon Sep 17 00:00:00 2001
From: Bob Mottram <bob@freedombone.net>
Date: Sat, 17 Dec 2016 16:17:19 +0000
Subject: [PATCH] More turtl config

---
 src/freedombone-app-turtl  | 90 ++++++++++++++++++++++++++++----------
 src/freedombone-utils-lisp | 69 ++++++++++++++++++++++-------
 2 files changed, 120 insertions(+), 39 deletions(-)

diff --git a/src/freedombone-app-turtl b/src/freedombone-app-turtl
index 18da976fd..ea09c4350 100755
--- a/src/freedombone-app-turtl
+++ b/src/freedombone-app-turtl
@@ -126,6 +126,11 @@ function upgrade_turtl {
     function_check set_repo_commit
     set_repo_commit /var/www/$TURTL_DOMAIN_NAME/htdocs "turtl commit" "$TURTL_COMMIT" $TURTL_REPO
     set_repo_commit /var/www/$TURTL_DOMAIN_NAME/htdocs "turtl api commit" "$TURTL_API_COMMIT" $TURTL_API_REPO
+    nginx_dissite $TURTL_DOMAIN_NAME
+    cd /var/www/$TURTL_DOMAIN_NAME/htdocs
+    npm install
+    make minify
+    nginx_ensite $TURTL_DOMAIN_NAME
 }
 
 function backup_local_turtl {
@@ -240,17 +245,18 @@ function remove_turtl {
     remove_nodejs turtl
 
     remove_rethinkdb
+    remove_common_lisp
 
     function_check remove_ddns_domain
     remove_ddns_domain $TURTL_DOMAIN_NAME
 }
 
 function install_libuv {
-    if [ ! -d $INSTALL_DIR ]; then
-        mkdir -p $INSTALL_DIR
+    if [ ! -d /usr/local/src ]; then
+        mkdir -p /usr/local/src
     fi
 
-    cd $INSTALL_DIR
+    cd /usr/local/src
     if [ ! -f libuv-v${LIBUV_VERSION}.tar.gz ]; then
         wget http://dist.libuv.org/dist/v${LIBUV_VERSION}/libuv-v${LIBUV_VERSION}.tar.gz
     fi
@@ -264,15 +270,15 @@ function install_libuv {
         echo $"libuv hash does not match. Expected $LIBUV_HASH but found $hashstr"
         exit 27685
     fi
-    if [ ! -d $INSTALL_DIR/libuv-v${LIBUV_VERSION} ]; then
+    if [ ! -d /usr/local/src/libuv-v${LIBUV_VERSION} ]; then
         tar -xf libuv-v${LIBUV_VERSION}.tar.gz
     fi
-    if [ ! -d $INSTALL_DIR/libuv-v${LIBUV_VERSION} ]; then
+    if [ ! -d /usr/local/src/libuv-v${LIBUV_VERSION} ]; then
         rm libuv-v${LIBUV_VERSION}.tar.gz
         echo $'libuv could not be extracted'
         exit 72754
     fi
-    cd $INSTALL_DIR/libuv-v${LIBUV_VERSION}
+    cd /usr/local/src/libuv-v${LIBUV_VERSION}
     sh autogen.sh
     ./configure
     make
@@ -294,10 +300,9 @@ function install_turtl {
     fi
 
     apt-get -yq install git wget curl libtool subversion gcc make automake
-    apt-get -yq install cl-cffi cl-quicklisp libuv1-dev build-essential
 
-    if [ ! -d $INSTALL_DIR ]; then
-        mkdir -p $INSTALL_DIR
+    if [ ! -d /usr/local/src ]; then
+        mkdir -p /usr/local/src
     fi
 
     if [ -d /var/www/$TURTL_DOMAIN_NAME ]; then
@@ -366,9 +371,16 @@ function install_turtl {
     git checkout $TURTL_API_COMMIT -b $TURTL_API_COMMIT
     set_completion_param "turtl api commit" "$TURTL_API_COMMIT"
 
+    function_check install_libuv
     install_libuv
+
+    function_check install_rethinkdb
     install_rethinkdb
+
+    function_check install_common_lisp
     install_common_lisp
+
+    function_check install_quicklisp
     install_quicklisp
 
     function_check install_nodejs
@@ -377,10 +389,54 @@ function install_turtl {
     function_check add_ddns_domain
     add_ddns_domain $TURTL_DOMAIN_NAME
 
-    adduser --system --home=/var/www/$TURTL_DOMAIN_NAME/htdocs/ --group turtl
+    cp -r $INSTALL_DIR/lisp/* /var/www/$TURTL_DOMAIN_NAME/htdocs
+    sed -i 's|CCL_DEFAULT_DIRECTORY=.*|CCL_DEFAULT_DIRECTORY=$HOME/ccl|g' /var/www/$TURTL_DOMAIN_NAME/htdocs/ccl/scripts/ccl
+    sed -i 's|CCL_DEFAULT_DIRECTORY=.*|CCL_DEFAULT_DIRECTORY=$HOME/ccl|g' /var/www/$TURTL_DOMAIN_NAME/htdocs/ccl/scripts/ccl64
+
+    adduser --disabled-login --home=/var/www/$TURTL_DOMAIN_NAME/htdocs --gecos 'turtl' turtl
+    chown -R turtl:turtl /var/www/$TURTL_DOMAIN_NAME/htdocs
+    check_architecture=$(uname -a)
+    if [[ "$check_architecture" == *"64"* && "$check_architecture" != *"arm"* ]]; then
+        su -c '$HOME/ccl/scripts/ccl --load install.lisp --eval "(quit)"' - turtl
+    else
+        su -c '$HOME/ccl/scripts/ccl64 --load install.lisp --eval "(quit)"' - turtl
+    fi
+
+    echo "(pushnew \"./\" asdf:*central-registry* :test #'equal)" > /var/www/$TURTL_DOMAIN_NAME/htdocs/api/launch.lisp
+    echo '(load "start")' >> /var/www/$TURTL_DOMAIN_NAME/htdocs/api/launch.lisp
+
+    cd /var/www/$TURTL_DOMAIN_NAME/htdocs/quicklisp/local-projects
+    git clone git://github.com/orthecreedence/cl-hash-util
+    git clone git://github.com/orthecreedence/vom
+    git clone git://github.com/orthecreedence/cl-async
+    git clone git://github.com/orthecreedence/cffi
+    git clone git://github.com/orthecreedence/wookie
+    git clone git://github.com/orthecreedence/cl-rethinkdb
+    git clone git://github.com/orthecreedence/cl-libuv
+    git clone git://github.com/orthecreedence/drakma-async
+    git clone https://github.com/Inaimathi/cl-cwd.git
+
+    echo '#-quicklisp' > /var/www/$TURTL_DOMAIN_NAME/htdocs/.ccl-init.lisp
+    echo '(let ((quicklisp-init (merge-pathnames "quicklisp/setup.lisp" (user-homedir-pathname))))' >> /var/www/$TURTL_DOMAIN_NAME/htdocs/.ccl-init.lisp
+    echo '  (when (probe-file quicklisp-init)' >> /var/www/$TURTL_DOMAIN_NAME/htdocs/.ccl-init.lisp
+    echo '    (load quicklisp-init)))' >> /var/www/$TURTL_DOMAIN_NAME/htdocs/.ccl-init.lisp
+    echo '(cwd "$HOME/api")' >> /var/www/$TURTL_DOMAIN_NAME/htdocs/.ccl-init.lisp
+    echo '(load "$HOME/api/launch")' >> /var/www/$TURTL_DOMAIN_NAME/htdocs/.ccl-init.lisp
+
     chmod a+w /var/www/$TURTL_DOMAIN_NAME/htdocs
     chown -R turtl:turtl /var/www/$TURTL_DOMAIN_NAME/htdocs
 
+    # install the app
+    cd /var/www/$TURTL_DOMAIN_NAME/htdocs
+    npm install
+    make minify
+    if [ ! -f /var/www/$TURTL_DOMAIN_NAME/htdocs/index.html ]; then
+        echo $'Failed to install turtl app'
+        exit 823652
+    fi
+
+    chown -R turtl:turtl /var/www/$TURTL_DOMAIN_NAME/htdocs
+
     echo '[Unit]' > /etc/systemd/system/turtl.service
     echo 'Description=Note taking service' >> /etc/systemd/system/turtl.service
     echo 'Documentation=http://turtl.it' >> /etc/systemd/system/turtl.service
@@ -395,9 +451,9 @@ function install_turtl {
     echo "WorkingDirectory=/var/www/$TURTL_DOMAIN_NAME/htdocs/api/" >> /etc/systemd/system/turtl.service
     check_architecture=$(uname -a)
     if [[ "$check_architecture" == *"64"* && "$check_architecture" != *"arm"* ]]; then
-        echo 'ExecStart=/usr/bin/ccl64 -Q -b --load start.lisp' >> /etc/systemd/system/turtl.service
+        echo "ExecStart=/var/www/$TURTL_DOMAIN_NAME/htdocs/ccl/scripts/ccl64 -Q -b --load start.lisp" >> /etc/systemd/system/turtl.service
     else
-        echo 'ExecStart=/usr/bin/ccl -Q -b --load start.lisp' >> /etc/systemd/system/turtl.service
+        echo "ExecStart=/var/www/$TURTL_DOMAIN_NAME/htdocs/ccl/scripts/ccl -Q -b --load start.lisp" >> /etc/systemd/system/turtl.service
     fi
     echo '' >> /etc/systemd/system/turtl.service
     echo '[Install]' >> /etc/systemd/system/turtl.service
@@ -440,7 +496,6 @@ function install_turtl {
         echo '    proxy_buffering   off;' >> $turtl_nginx_site
         echo '  }' >> $turtl_nginx_site
         echo '' >> $turtl_nginx_site
-        nginx_keybase $TURTL_DOMAIN_NAME
         echo '}' >> $turtl_nginx_site
         echo '' >> $turtl_nginx_site
         echo 'server {' >> $turtl_nginx_site
@@ -471,7 +526,6 @@ function install_turtl {
         nginx_limits $TURTL_DOMAIN_NAME '15m'
         echo '  }' >> $turtl_nginx_site
         echo '' >> $turtl_nginx_site
-        nginx_keybase $TURTL_DOMAIN_NAME
         echo '}' >> $turtl_nginx_site
     else
         echo -n '' > $turtl_nginx_site
@@ -498,7 +552,6 @@ function install_turtl {
     echo '    proxy_buffering   off;' >> $turtl_nginx_site
     echo '  }' >> $turtl_nginx_site
     echo '' >> $turtl_nginx_site
-    nginx_keybase $TURTL_DOMAIN_NAME
     echo '}' >> $turtl_nginx_site
     echo '' >> $turtl_nginx_site
     echo 'server {' >> $turtl_nginx_site
@@ -522,7 +575,6 @@ function install_turtl {
     nginx_limits $TURTL_DOMAIN_NAME '15m'
     echo '  }' >> $turtl_nginx_site
     echo '' >> $turtl_nginx_site
-    nginx_keybase $TURTL_DOMAIN_NAME
     echo '}' >> $turtl_nginx_site
 
     function_check create_site_certificate
@@ -531,12 +583,6 @@ function install_turtl {
     if [ -f /etc/ssl/certs/${TURTL_DOMAIN_NAME}.crt ]; then
         mv /etc/ssl/certs/${TURTL_DOMAIN_NAME}.crt /etc/ssl/certs/${TURTL_DOMAIN_NAME}.pem
     fi
-    if [ -f /etc/ssl/certs/${TURTL_DOMAIN_NAME}.pem ]; then
-        chown turtl:turtl /etc/ssl/certs/${TURTL_DOMAIN_NAME}.pem
-    fi
-    if [ -f /etc/ssl/private/${TURTL_DOMAIN_NAME}.key ]; then
-        chown turtl:turtl /etc/ssl/private/${TURTL_DOMAIN_NAME}.key
-    fi
 
     function_check nginx_ensite
     nginx_ensite $TURTL_DOMAIN_NAME
diff --git a/src/freedombone-utils-lisp b/src/freedombone-utils-lisp
index d02fb3cc2..9d5c4c34d 100755
--- a/src/freedombone-utils-lisp
+++ b/src/freedombone-utils-lisp
@@ -32,11 +32,16 @@ COMMON_LISP_VERSION='1.11'
 
 function install_common_lisp {
     # http://ccl.clozure.com
-    if [ ! -d $INSTALL_DIR/lisp ]; then
-        mkdir -p $INSTALL_DIR/lisp
+    lisp_base=/usr/local/src
+    if [ $1 ]; then
+        lisp_base=$1
     fi
 
-    cd $INSTALL_DIR/lisp
+    if [ ! -d $lisp_base ]; then
+        mkdir -p $lisp_base
+    fi
+
+    cd $lisp_base
 
     check_architecture=$(uname -a)
     if [[ "$check_architecture" == *"arm"* ]]; then
@@ -45,25 +50,48 @@ function install_common_lisp {
         svn co http://svn.clozure.com/publicsvn/openmcl/release/${COMMON_LISP_VERSION}/linuxx86/ccl
     fi
 
-    if [ ! -d $INSTALL_DIR/lisp/ccl/scripts ]; then
+    if [ ! -d $lisp_base/ccl/scripts ]; then
         echo $'Unable to clone ccl repo'
         exit 728245
     fi
-    if [ ! -f $INSTALL_DIR/lisp/ccl/scripts/ccl ]; then
+    if [ ! -f $lisp_base/ccl/scripts/ccl ]; then
         echo $'ccl not found'
         exit 5825422
     fi
-    cp $INSTALL_DIR/lisp/ccl/scripts/ccl /usr/bin
-    if [ -f $INSTALL_DIR/lisp/ccl/scripts/ccl64 ]; then
-        cp $INSTALL_DIR/lisp/ccl/scripts/ccl64 /usr/bin
+    cp $lisp_base/ccl/scripts/ccl /usr/bin
+    sed -i 's|CCL_DEFAULT_DIRECTORY=.*|CCL_DEFAULT_DIRECTORY=/usr/local/src/ccl|g' /usr/bin/ccl
+    if [ -f $lisp_base/ccl/scripts/ccl64 ]; then
+        cp $lisp_base/ccl/scripts/ccl64 /usr/bin
+        sed -i 's|CCL_DEFAULT_DIRECTORY=.*|CCL_DEFAULT_DIRECTORY=/usr/local/src/ccl|g' /usr/bin/ccl64
+    fi
+}
+
+function remove_common_lisp {
+    if [ -f /usr/bin/ccl ]; then
+        rm /usr/bin/ccl
+    fi
+
+    if [ -f /usr/bin/ccl64 ]; then
+        rm /usr/bin/ccl64
+    fi
+
+    if [ -d /usr/local/src/ccl ]; then
+        rm -rf /usr/local/src/ccl
+    fi
+    if [ -d $INSTALL_DIR/lisp ]; then
+        rm -rf $INSTALL_DIR/lisp
     fi
 }
 
 function install_quicklisp {
-    if [ ! -d $INSTALL_DIR/lisp ]; then
-        mkdir -p $INSTALL_DIR/lisp
+    quicklisp_base=/usr/local/src
+    if [ $1 ]; then
+        quicklisp_base=$1
+    fi
+    if [ ! -d $quicklisp_base ]; then
+        mkdir -p $quicklisp_base
     fi
-    cd $INSTALL_DIR/lisp
+    cd $quicklisp_base
     if [ ! -f asdf.lisp ]; then
         wget https://common-lisp.net/project/asdf/asdf.lisp
     fi
@@ -79,17 +107,24 @@ function install_quicklisp {
         exit 80253
     fi
 
-    echo '(quicklisp-quickstart:install)' > install.lisp
+    echo '(load (compile-file "asdf.lisp"))' > install.lisp
+    echo '(load (compile-file "quicklisp.lisp"))' >> install.lisp
+    echo '(quicklisp-quickstart:install)' >> install.lisp
     echo '(ql:add-to-init-file)' >> install.lisp
-    echo '(load (compile-file "asdf.lisp"))' >> install.lisp
 
     check_architecture=$(uname -a)
     if [[ "$check_architecture" == *"64"* && "$check_architecture" != *"arm"* ]]; then
-        ccl64 --load quicklisp.lisp
-        ccl64 --load install.lisp
+        if [ ! $2 ]; then
+            ccl64 --load install.lisp
+        else
+            su -c 'ccl64 --load install.lisp' - $2
+        fi
     else
-        ccl --load quicklisp.lisp
-        ccl --load install.lisp
+        if [ ! $2 ]; then
+            ccl --load install.lisp
+        else
+            su -c 'ccl --load install.lisp' - $2
+        fi
     fi
 }
 
-- 
GitLab