Skip to content

Instantly share code, notes, and snippets.

@ChrisCummins
Last active December 19, 2015 17:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChrisCummins/5992357 to your computer and use it in GitHub Desktop.
Save ChrisCummins/5992357 to your computer and use it in GitHub Desktop.
A small script to provide convenient day-to-day management of a local file server.
#!/bin/bash
#
# server.sh
#
# Copyright (C) 2013 Chris Cummins
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Authors:
# Chris Cummins <chrisc.101@gmail.com
set -u
# Networking details
HOST=""
SSH_PORT=
MAC=
# A space delimited list of usernames with ssh access
USERS=""
# Git configuration
GIT_USER=""
GIT_DIR=""
GIT_URL=ssh://git@$HOST:$SSH_PORT
GIT_REMOTE_NAME="$HOST"
default_user=$(echo $USERS | cut -f1 -d ' ')
set +u
set -e
# Enable debugging if set
test -n "$DEBUG" && set -x
usage() {
echo "Usage:"
echo " $(basename $0) ping - ping server"
echo " $(basename $0) ssh [user] [args] - open ssh connection"
echo " $(basename $0) git <push|clone|ls|rm> [args]"
echo " ls - list all repositories"
echo " init <name> - initialise an empty repo"
echo " push <repo> - push the current repository to <repo>"
echo " rm <repo> - permanently delete <repo>"
echo " clone <repo> - clone <repo>"
echo " $(basename $0) update - system update"
echo " $(basename $0) on|off|reboot - control system power"
}
do_ssh() {
local user=$default_user
# Select our user, first from the command line, else use default.
if [ -n "$1" ]; then
user="$1"
shift
fi
ssh -p$SSH_PORT $user@$HOST $@
}
fail_if_repository_exists() {
local repo="$1"
set +e
do_git_ls | grep '^'"$repo"'$' &> /dev/null
if [ $? -eq 0 ]; then
echo "A repository named '$repo' already exists!" > /dev/stderr
exit 1
fi
set -e
}
fail_if_repository_not_exist() {
local repo="$1"
set +e
do_git_ls | grep '^'"$repo"'$' &> /dev/null
if [ $? -ne 0 ]; then
echo "No repository named '$repo' found!" > /dev/stderr
exit 1
fi
set -e
}
do_git_ls() {
do_ssh $GIT_USER "ls $GIT_DIR" | grep '.git$' | sed 's/.git$//'
}
do_git_clone() {
if [ -z "$1" ]; then
echo "No repository name specified!" > /dev/stderr
exit 1
fi
local repo="$1"
local repo_url="$GIT_DIR/$repo"
fail_if_repository_not_exist "$repo"
git clone $GIT_URL$repo_url
}
do_git_init() {
if [ -z "$1" ]; then
echo "No repository name specified!" > /dev/stderr
exit 1
fi
local repo="$1"
local repo_path="$GIT_DIR/$repo.git"
fail_if_repository_exists "$repo"
do_ssh $GIT_USER "mkdir $repo_path && GIT_DIR=$repo_path git init --bare"
}
do_git_rm() {
if [ -z "$1" ]; then
echo "No repository name specified!" > /dev/stderr
exit 1
fi
local repo="$1"
local repo_path="$GIT_DIR/$repo.git"
fail_if_repository_not_exist "$repo"
do_ssh $GIT_USER "rm -rf $repo_path"
echo "Deleted Git repository at '$repo_path'"
}
do_git_add() {
local dir=$(pwd)
local repo=${dir##*/}
local repo_path="$GIT_DIR/$repo.git"
if [[ ! -d .git ]]; then
echo "fatal: not a git repository!" > /dev/stderr
exit 1
fi
git remote add $GIT_REMOTE_NAME $GIT_URL$repo_path
set +e
do_git_ls | grep '^'"$repo"'$' &> /dev/null
if [ $? -ne 0 ]; then
echo "warning: a repository named '$repo' found!" > /dev/stderr
exit 1
fi
set -e
}
do_git_push() {
local dir=$(pwd)
local repo=${dir##*/}
do_git_init "$repo"
do_git_add
git push --all $GIT_REMOTE_NAME
}
do_update() {
do_ssh $default_user "sudo apt-get update && sudo apt-get upgrade"
}
do_startup() {
wol $MAC
}
do_shutdown() {
do_ssh $default_user "sudo shutdown -h now"
echo "Shutting down..."
}
do_reboot() {
do_ssh $default_user "sudo reboot"
echo "Rebooting..."
}
do_ping() {
ping $HOST
}
do_git() {
# If there's no argument, default to ls
if [ -z "$1" ]; then
do_git_ls
fi
case "$1" in
"ls")
do_git_ls
;;
"add")
shift
do_git_add $@
;;
"clone")
shift
do_git_clone $@
;;
"init")
shift
do_git_init $@
;;
"rm")
shift
do_git_rm $@
;;
"push")
shift
do_git_push $@
;;
*)
echo "fatal: unrecognised git command!" > /dev/stderr
exit 1
;;
esac
}
main() {
# Parse --help argument first.
for arg in $@; do
if [[ $arg =~ ((--)?help|-h) ]]; then
usage
exit 0
fi
done
# If there's no argument, default to ssh
if [ -z "$1" ]; then
do_ssh
fi
# Parse the command
case "$1" in
"ssh")
shift
do_ssh $@
;;
"git")
shift
do_git $@
;;
"update")
shift
do_update $@
;;
"on")
shift
do_startup $@
;;
"off")
shift
do_shutdown $@
;;
"reboot")
shift
do_reboot $@
;;
"ping")
shift
do_ping $@
;;
*)
echo "fatal: unrecognised command '$1'" > /dev/stderr
exit 1
;;
esac
}
main $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment