DESKTOP EDITION CLOUD EDITION SERVER EDITION GUIDES FORUM

[contribution] Update script for server edition on linux


#1

Here’s the script I have made to update the server edition to the latest edition on my linux server. It needs some work before it’s actually good to distribute as it needs some tweaking to handle any errors that may arise (like download failure), but I thought someone might make use of it.

This script assumes the following:

  • Manager is the only process using mono on the server.
  • You save the files in /var/www
  • Manager starts using the user www-data
  • Manager starts at port 8080 (use nginx to proxy pass to it and use https)

Here it is:

#!/bin/bash
# manager-updater.sh ver 0.01
# Author: linuxawy - ahmed@linuxawy.org
# This script is free to use and distribute under GPLv2 or later
# This script has absolutely no warranty, use it at your own risk
#
# Script to update manager to the latest version
# -----------------------------------------------
# This script assumes the following:
# * Manager is the only process using mono on the server.
# * You save the files in /var/www
# * manager starts using the user www-data
# * manager starts at port 8080 (use nginx to proxy pass to it and use https)

###############################################
# This script is not safe yet to run via cron #
###############################################

PATH=$PATH:/bin:/sbin:/usr/bin:/usr/sbin

File=`curl -s  http://www.manager.io/download.json | sed 's/\,/\n/g' | grep tar | awk -F '"' {'print $4'}`
Dir="manager-`echo $File | awk -F/ {'print $5'}`"
PID=`ps aux|grep mono | egrep -v "grep|su|sh" | awk {'print $2'}`

cd /var/www
if [ -d "$Dir" ]; then
        echo "you are already on the latest version."
        exit 0;
else
        echo "New version available ($Dir), attempting to download it ..."
        mkdir $Dir
fi

cd $Dir
wget $File 2>&1 >>/dev/null

tar xzf ManagerServer.tar.gz
rm ManagerServer.tar.gz
cd ..
echo "New version downloaded, killing old manager ..."
kill $PID
tail -f /dev/null --pid $PID
rm manager.io
ln -s $Dir manager.io
# note to self: a nice point to do data backup
echo "starting new instance ..."
su www-data -c "cd /var/www/manager.io && /usr/bin/mono ManagerServer.exe -port 8080"  >>/dev/null 2>&1 &
if [ $? -eq 0 ]; then
        echo "done, enjoy!"
else
        echo "something's wrong!"
        echo "you are on your own now, call for help."
        exit 1
fi

Upgrading/Updating on Ubuntu
#2

You can use upstart to manage the process, I have been using the files below without any problems for me.

Using /var/www is potentially dangerous without additional web server config (such as proxy), or accidental misconfiguration, as http://server/00000000-0000-0000-0000-000000000000.manager gets the preferences file and it would be possible to brute force the IDs of other files.

/etc/default/manager-accounting


# First run upstart script
description     "run Manager server"

start on (local-filesystems and net-device-up)

respawn
respawn limit 3 10

umask 0077
setuid manager-accounting
setgid manager-accounting

pre-start script
  . /etc/default/manager-accounting
  if [ $ENABLED -eq 0 ]
  then
    echo "manager-accounting not enabled in /etc/default/manager-accounting"
    exit 1
  else
    exit 0
  fi
end script

script
  . /etc/default/manager-accounting
  /usr/bin/mono /opt/manager-accounting/ManagerServer.exe -port $PORT -path $DATAPATH
end script

/etc/init/manager-accounting.conf


# Setting up to use Upstart job
#
# ====== System ======
#
# make a non-priv system user:
#   useradd -r -s /bin/false -d /var/lib/manager-accounting/ manager-accounting
#
# create data dir
#   mkdir -m 700 /var/lib/manager-accounting
#
# set owner
#    chown manager-accounting:manager:accounting /var/lib/manager-accounting
#
# Runtime options for ManagerServer.exe

# Change to 1 to enable ManagerServer.exe at system startup
ENABLED=0

PORT=8080

DATAPATH=/var/lib/manager-accounting

#3

In relation to 00000000-0000-0000-0000-000000000000.manager file, script from @linuxawy is not overriding default value in -path switch so actual data files are safely outside of /var/www

One should be still careful when using -path switch though.

I’m interested to simplify these scripts a bit. For example, self-upgrading process could be easily part of the core program.


#4

It’d be nice if there’s simple button to upgrade the program to the latest version.
If you are planning to implement this feature, could you please enable it for Manager on cloud?
Thanks


#5

@pl3w5y a startup script will simplify it indeed, either upstart or even classic init.d style. The paths and the user in your scripts are better than mine. You chose /opt for the app, /var/lib for the data, and a separate user to run the application, which are all seems ultimately better than what I did.

Anyway, in my case, the user www-data doesn’t run anything except the web server and Manager. Note that in my nginx configurations I didn’t set a document root, so my /var/www is safe. The data in my case is inside it indeed, in /var/www/.local/share/Manager, but it’s safe as it’s outside the document root which doesn’t exist in the first place.

I’m not sure about http://server/00000000-0000-0000-0000-000000000000.manager thing, I tried it on my side but it redirected me back to login page not logged in, and gave me broken 302 redirect when logged in. May be I should work on ehnancing such security concerns from nginx configurations. Are there known issues like this one?

I guess I’ll switch to your upstart scripts, and move my data directory accordingly. Then I’ll edit my script to match it. will post it here when done. I need to make this script more robust by handling some errors, such as a broken download, or a rollback procedure to the old version when the new one doesn’t start correctly.

@lubos why you need to simplify them? actually they need to get more sophesticated (my script at least), and others added like automated backup script. May be you can host them on manager.io/scripts/bla_bla.sh so you can mention on the totorial to download them and put them in place. May be a setup script for manager will help as well? could be wrapped up in a single deb/rpm package for better usability.


#6

Here it is updated to work with @pl3w5y upstart configurations. I added a simple roll back procedure just in case something wrong happened.

#!/bin/bash
# manager-updater.sh ver 0.02
# Author: linuxawy - ahmed@linuxawy.org
# This script is free to use, modify, and distribute under GPLv2 or later
# This script has absolutely no warranty, use it at your own risk
#
# Script to update manager to the latest version
# -----------------------------------------------
# This script assumes the following:
# * Manager is located in /opt/manager-accounting
# * Manager is handled by the upstart script manager-accounting
 
###############################################
# This script is not safe yet to run via cron #
###############################################

PATH=$PATH:/bin:/sbin:/usr/bin:/usr/sbin
File=`curl -s  http://www.manager.io/download.json | sed 's/\,/\n/g' | grep tar | awk -F '"' {'print $4'}`
Dir=manager-`echo $File | awk -F/ {'print $5'}`
Base=/opt

if [ -d "$Base/$Dir" ]; then
    echo "you are already on the latest version."
    exit 0;
else
        echo "New version available ($Dir), attempting to upgrade ..."
        mkdir $Base/$Dir
 fi

wget -q -O $Base/$Dir/ManagerServer.tar.gz $File
tar xzf $Base/$Dir/ManagerServer.tar.gz -C $Base/$Dir
rm $Base/$Dir/ManagerServer.tar.gz
service manager-accounting stop
rm $Base/manager-accounting.bak
mv $Base/manager-accounting $Base/manager-accounting.bak
ln -s $Base/$Dir $Base/manager-accounting
# note to self: a nice point to do data backup
service manager-accounting start
if [ $? -eq 0 ]; then
        echo "done, enjoy!"
else
        echo "something's wrong, rolling back!"
        rm -rf $Base/$Dir
        rm $Base/manager-accounting
        mv $Base/manager-accounting.bak $Base/manager-accounting
        service manager-accounting start
        if [ $? -eq 0 ]; then
              echo "roll back successful"
              exit 1
        else
        echo "roll back failed, call for help!"
        echo "you are on your own now, sorry ..."
        exit 1
     fi
fi

#7

Should have mentioned that I am installing packages on Ubuntu 12.04 server, just downloading with laptop and SCP over, not very good :smile: which is why its in opt.

Update could be put into the pre-start section of the upstart job, so then to get a new version the cron job simply needs to run ‘initclt restart manager-accounting’ then a choice just needs to be made whether to abandon startup if the update fails.

There is also ‘post-stop’ in Upstart, which could be a good place for a backup (with some attention given to file names, and how to not run the post-stop if the application crashes) and I’m sure there is more to think about too.

But for now I have to put Manager to its purpose


#8

lol, well as a proffessional linux system admin I’m telling you that opt is perfect for that :smiley:

About the backup and upgrade through the startup script, I don’t believe this is a good idea. They should be 2 separate scripts ultimately running on cron. I’d hate to restart the server and face issue with starting up manager cause of failed upgrade.

@lubos what’s the best way to backup all the data and preferences at once? is it safe to backup the files in the data directory or it could be inconsistent?


#9

I’ve also made myself a script which does almost the same thing, I’ve taken your curl one-liner to all the information, nice touch there. I’ve also taken your latest version test.

I think our scripts were written with different purposes in mind. My setup is Ubuntu 14.04 in an OpenVZ container on Proxmox, the only access is via VPN or on the LAN so I’m not in need of HTTPS hence use port 80, backups are done regularly of the entire container. Using upstart is a good idea, but using /etc/rc.local is more distro neutral - also because this container does nothing else, I don’t care.

What I’ve made isn’t meant to be run via cron, but instead a helper to go through the official installation method and update any files - it intentionally requires interaction. It makes a new folder in /opt/manager-accounting for each new version and only comments out lines in /etc/rc.local so I can roll back easily.

#!/bin/bash

# Set variables
# Don't change unless you know what you are doing.
INST='/opt/manager-accounting'
URL=$(curl -s  http://www.manager.io/download.json | sed 's/\,/\n/g' | grep tar | awk -F '"' {'print $4'})
VER=$(echo $URL | awk -F/ {'print $5'})
FN=$(echo $URL | awk -F/ {'print $6'})
DIR=$INST/$VER
EXE='ManagerServer.exe -port 80 >>/dev/null 2>&1 &'

echo ''
echo 'Version 0.1a by AdmiralAkber, MIT Licence'
echo 'This program comes with no warranty - use at your own risk!'
echo ''
echo 'This script helps to install/update ManagerServer:'
echo 'Tested on Ubuntu 14.04 LTS'
echo ''
if [ -d "$DIR" ]; then
    echo "You appear to already have the latest version. Exiting..."
    exit 0;
else
    echo "Attempting to install/upgrade ManagerServer"
    mkdir -p $DIR
fi
echo '---------'
echo 'ManagerServer Version: '$VER
echo 'Install DIR: '$DIR
echo '---------'
echo 'Continue?'
select cont in "Yes" "No"; do
   case $cont in
        Yes) break;;
        No) exit 0;;
   esac
done

echo 'Starting download of Manager '$VER
echo ''
wget -O $DIR/$FN $URL
echo ''
echo 'Extracting...'
tar -zxvf $DIR/$FN -C $DIR
echo ''
echo 'Do you wish to start this version of ManagerServer at startup?'
echo 'This will edit your /etc/rc.local file'
select yn in "Yes" "No"; do
   case $yn in
        Yes)
          sed -i -e '/ManagerServer.exe/ s/^/#/' /etc/rc.local
          sed -i -e '$i \'"$(which mono) $DIR/$EXE \n" /etc/rc.local
          echo 'Writing to /etc/rc.local complete'
          break
          ;;
        No)
          break;;
   esac
done

echo 'Do you want to launch this version of ManagerServer now?'
echo 'This will attempt to kill any ManagerServer that is currently running first.'
select ny in "Yes" "No"; do
   case $ny in
        Yes)
          ps -ef | grep [M]anagerServer.exe |\
          awk '(/mono/) {print $2}' | xargs kill
          nohup $(which mono) $DIR/$EXE &
          echo 'ManagerServer should now be running'
          break
          ;;
        No) break;;
   esac
done

echo 'All done, get back to work.'
exit 0

#10

“Phoenixing” this thread for a reason. The URL’s and pulling out the version has changed slightly and required a bit of modification.

#!/bin/bash

# Set variables
# Don't change unless you know what you are doing.
INST='/opt/manager-accounting'
URL='http://download.manager.io/manager-accounting.zip'

EXE='ManagerServer.exe -port 80 >>/dev/null 2>&1 &'

echo ''
echo 'Version 0.2a by AdmiralAkber, MIT Licence'
echo 'This program comes with no warranty - use at your own risk!'
echo ''
echo 'This script helps to install/update ManagerServer:'
echo 'Tested on Ubuntu 14.04 LTS'
echo ''

echo 'Continue to download Manager?'
select cont in "Yes" "No"; do
    case $cont in
	Yes) break;;
	No) exit 0;;
    esac
done
# Get latest manager
wget -nc $URL
VER=$(unzip -v "manager-accounting.zip" | grep tar | awk '{print $8}' | awk -F"[_.]" '{print $2"."$3"."$4}')

DIR=$INST/$VER

if [ -d "$DIR" ]; then
    echo "You appear to already have the latest version. Exiting..."
    exit 0;
else
    echo "Attempting to install/upgrade ManagerServer"
fi
echo '---------'
echo 'ManagerServer Version: '$VER
echo 'Install DIR: '$DIR
echo '---------'
echo 'Continue?'
select cont in "Yes" "No"; do
   case $cont in
	Yes) break;;
	No) exit 0;;
   esac
done

# Make directory and extract
mkdir -p $DIR
echo 'Extracting...'
unzip manager-accounting.zip -d $DIR
rm manager-accounting.zip
# Extract manager properly
tar -C $DIR -zxf $DIR/manager-accounting_${VER}.tar.gz
mv -v $DIR/manager-accounting/opt/manager-accounting/* $DIR

echo ''
echo 'Do you wish to start this version of ManagerServer at startup?'
echo 'This will edit your /etc/rc.local file'
select yn in "Yes" "No"; do
   case $yn in
	Yes)
	  sed -i -e '/ManagerServer.exe/ s/^/#/' /etc/rc.local
	  sed -i -e '$i \'"$(which mono) $DIR/$EXE \n" /etc/rc.local
	  echo 'Writing to /etc/rc.local complete'
	  break
	  ;;
	No)
	  break;;
   esac
done

echo 'Do you want to launch this version of ManagerServer now?'
echo 'This will attempt to kill any ManagerServer that is currently running first.'
select ny in "Yes" "No"; do
   case $ny in
	Yes)
	  ps -ef | grep [M]anagerServer.exe |\
	  awk '(/mono/) {print $2}' | xargs kill
	  nohup $(which mono) $DIR/$EXE &
	  echo 'ManagerServer should now be running'
	  break
	  ;;
	No) break;;
   esac
done

echo 'All done, get back to work.'
exit 0

#11

This is very nice thank you. All I have done is script the install procedure Lubos has given for the Manager install and then reboot the system. Very quick and only do it say once a month.


#12

Thank you for the notification, @AdmiralAkber, I didn’t even notice it was changed. I modified my script accordingly. Much appreciated.