View Single Post
Old 04-07-2014, 03:27 PM   #1
eschwartz
Ex-Helpdesk Junkie
eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.
 
eschwartz's Avatar
 
Posts: 19,422
Karma: 85397180
Join Date: Nov 2012
Location: The Beaten Path, USA, Roundworld, This Side of Infinity
Device: Kindle Touch fw5.3.7 (Wifi only)
Fully automatic calibre installer/updater for linux

Growing out of this discussion: Official Calibre PPA?, here is a simple bash script to update calibre on linux.
Note: This can also perform the first-time install, since the version check returns the same failure for out-of-date as it does if there is no calibre installed.

Thanks to aleyx for working on the version checking!

EDIT: The script has been updated to fix a few problems. Also, you can now install the script and add the systemd timer or cron job (defaults to systemd timer, falls back on cron) by running the following one-liner in a terminal:
Code:
sudo -v && wget -nv -O- https://github.com/eli-schwartz/calibre-installer/raw/master/linux/calibre-installer.sh | sudo bash -
Everything is managed through my GitHub repository



What this is doing, the manual way



New code:
Spoiler:
Code:
#!/bin/bash

# Unset git development source
export CALIBRE_DEVELOP_FROM=
# by default, switch is off
force_upgrade=0


# Functions

calibre_is_installed()
{
    command -v calibre >/dev/null 2>&1
}

usage()
{
	cat <<- _EOF_
		Usage: calibre-upgrade.sh [OPTIONS]
		Upgrades calibre installation. Automatically checks if the current version is up to date.
		Must be run as root.

		OPTIONS
		    -f, --force       Force an update. This is only useful if binaries
		                      were updated for a critical error. :shrug:
		    -h, --help        Displays this help message.
_EOF_
}

do_upgrade()
{
    if calibre_is_installed; then
        # shutdown calibre as each logged-in user.
        for i in $(users | tr ' ' '\n' | sort -u); do
            sudo -u $i calibre --shutdown-running-calibre
        done
        killall -q -v calibre-server && echo -e "Restart when upgrade is finished. ;)\n\n" || echo -e "No running calibre servers.\n\n"
    fi
    wget -nv -O- https://github.com/kovidgoyal/calibre/raw/master/setup/linux-installer.py | python -c "import sys; main=lambda x,y:sys.stderr.write('Download failed\n'); exec(sys.stdin.read()); main()"
}

# Options
while [ "$1" != "" ]; do
    case $1 in
        -h|--help)      usage
                        exit
                        ;;
        -f|--force)     shift
                        force_upgrade=1
                        ;;
        *)              echo "calibre-upgrade.sh: unrecognized option '$1'"
                        echo "Try 'calibre-upgrade.sh --help' for more information."
                        exit 1
    esac
    shift
done

# Main

## Check that we are running as root
if [[ $EUID -ne 0 ]]; then
    echo -e "You can only install calibre if you have root permission."
    exit 1
fi



if calibre_is_installed; then
    calibre-debug -c "import urllib as u; from calibre.constants import numeric_version; raise SystemExit(int(numeric_version  < (tuple(map(int, u.urlopen('http://code.calibre-ebook.com/latest').read().split('.'))))))"
    UP_TO_DATE=$?
else
    echo -e "Calibre is not installed, installing...\n\n"
    UP_TO_DATE=1
fi

if [ "$UP_TO_DATE" = 0 ]; then
    echo "Calibre is up-to-date"

    if [ "$force_upgrade" = 1 ]; then
        echo ""
        echo "Forcing upgrade anyway -- are you sure you want to continue? [y/n]"
        read answer

        if [[ "$answer" = "y" || "$answer" = "yes" ]]; then
            do_upgrade
        else
            echo "Exiting..."
            exit 1
        fi
    fi
else
    calibre_is_installed && echo -e "Calibre is out-of-date. Upgrading...\n\n"
    do_upgrade
fi


Old code:
Spoiler:
Code:
#!/bin/bash

calibre-debug -c "import urllib as u; from calibre.constants import numeric_version; raise SystemExit(int(numeric_version  < (tuple(map(int, u.urlopen('http://calibre-ebook.com/downloads/latest_version').read().split('.'))))))"

UP_TO_DATE=$?

if [ $UP_TO_DATE = 0 ]; then
	echo "Calibre is up-to-date"
else
	calibre --shutdown-running-calibre
	killall calibre-server

	sudo -v; wget -nv -O- https://github.com/kovidgoyal/calibre/raw/master/setup/linux-installer.py | sudo python -c "import sys; main=lambda:sys.stderr.write('Download failed\n'); exec(sys.stdin.read()); main()"
fi


Explanation:
Spoiler:
Very simply:
  1. we start off with a one-liner which runs calibre-debug to determine the currently installed version, and compares it to the version declared at http://calibre-ebook.com/downloads/latest_version
  2. if it returns negative, calibre is already up to date, and it tells you so. Why bother updating to the current version?
  3. if not, we:
    1. close any running instances of calibre
    2. kill calibre-server, if running
    3. run Kovid's one-liner install code. If you use a non-default install directory, you can edit this!
      Just drop in Kovid's other code.


Save this as "calibre-upgrade.sh" (or whatever you want) and stick it in "/usr/bin/", to add it to your path. ("$HOME/bin/" works too, but this is for a system install, so this is nicer. )

You can now update calibre by running
Code:
sudo calibre-upgrade.sh
but to make this fully automatic, simply add:
Code:
0 6 * * 5 /usr/bin/calibre-upgrade.sh > /dev/null 2>&1
or if you want the output emailed to you by cron, add
Code:
0 6 * * 5 /usr/bin/calibre-upgrade.sh
to your crontab (with the command "sudo crontab -e") and the updater will run every Friday at 6:00 AM. Presumably, you aren't awake at that time, but you can fill in whatever time you want.

Attached is a zip of the updater script (in a bin directory, and marked as executable), which can be extracted to "/usr/" (or $HOME) for your convenience.
Attached Files
File Type: gz calibre-upgrade.sh.tar.gz (1.3 KB, 1157 views)

Last edited by eschwartz; 04-17-2016 at 05:59 PM. Reason: new script, updated instructions
eschwartz is offline   Reply With Quote