Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor upgrade script #53

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 84 additions & 25 deletions UPDATE.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,32 +1,91 @@
#!/bin/bash
distro=$1
if [ -z ${distro+x} ]; then
distro='master'
#!/usr/bin/env bash

progname=$(basename $0)

tmpd=$(mktemp -d)
trap "rm -rf $tmpd" EXIT

set -e

err() {
echo "$progname: ""$@" >&2
}

usage() {
echo "usage: $progname: -b <git branch> -d </full/path/to/shinobi>"
echo ""
echo "-b dev|master the branch of Shinobi you want to use"
echo "-d /opt/Shinobi the full path to your Shinobi checkout"
echo "-h show help"
}

if [[ $# -eq 0 ]]; then
usage >&2
exit 99
fi
rm -rf $distro master_temp
wget https://github.com/ShinobiCCTV/Shinobi/tarball/$distro
mkdir master_temp
tar -xzf $distro -C master_temp --strip-components=1

while getopts hd:b: opt; do
case $opt in
b)
case $OPTARG in
dev|master)
distro=$OPTARG
;;
*)
err "branch must be dev or master"
exit 1
;;
esac
;;
d)
if [[ ! -w $OPTARG ]]; then
err "$OPTARG is not writable"
exit 2
fi
shinobi=$OPTARG
;;
h)
usage
exit 0
;;
*)
usage
exit 10
;;
esac
done
shift $((OPTIND-1))

if [[ -z $distro ]]; then
err "-b <branch> is required"
usage >&2
exit 99
fi

if [[ -z $shinobi ]]; then
err "-d </full/path/to/shinobi/checkout> is required"
usage >&2
exit 99
fi

# download source
wget -O $tmpd/$distro https://github.com/ShinobiCCTV/Shinobi/tarball/$distro

# stop existing processes
cd $shinobi
pm2 stop camera.js
pm2 stop cron.js
pm2 kill
mv master_temp/UPDATE.sh UPDATE.sh
chmod +x UPDATE.sh
sed -i 's/\r//' UPDATE.sh
mv master_temp/languages languages
mv master_temp/definitions definitions
mv master_temp/web web
mv master_temp/LICENSE LICENSE
mv master_temp/COPYING COPYING
mv master_temp/package.json package.json
mv master_temp/camera.js camera.js
mv master_temp/cron.js cron.js
mv master_temp/plugins/motion/shinobi-motion.js plugins/motion/shinobi-motion.js
mv master_temp/plugins/opencv/shinobi-opencv.js plugins/motion/shinobi-opencv.js

# unpack new files to shinobi directory
tar -xzf $tmpd/$distro --strip-components=1

# update nodejs modules and start shinobi processes
npm install
rm -rf $distro master_temp
pm2 start camera.js
pm2 start cron.js
if [ ! -f plugins/motion/conf.json ]; then
pm2 start plugins/motion/shinobi-motion.js
fi
if [[ -f plugins/motion/conf.json ]]; then
pm2 start plugins/motion/shinobi-motion.js
fi

exit 0