-
Notifications
You must be signed in to change notification settings - Fork 3
/
setup.sh
executable file
·100 lines (83 loc) · 2.76 KB
/
setup.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/bin/bash
set -eu
#exit if not run by root
if [ "$EUID" -ne 0 ]
then echo "Please run as root"
exit
fi
#getting some environment vars
source /etc/lsb-release
# setting the location of the ansible playbook repository
export GITREPO="https://github.com/tna76874/hdhweg-homeschooling-stack.git"
export REPODIR="/root/hdhweg-homeschooling-stack"
export EXE="/usr/local/bin/setup.sh"
export SCRIPT=$(readlink -f "$0")
###### Some functions
# installing requirements
installing_requirements() {
echo -e "... update system sources, install ansible and git ... [this could take a few minutes now, depending on your internet connection]"
sudo apt update > /dev/null 2>&1
sudo apt install software-properties-common -y > /dev/null 2>&1
sudo apt-add-repository universe > /dev/null 2>&1
sudo apt-add-repository multiverse > /dev/null 2>&1
if [ "$DISTRIB_CODENAME" = "bionic" ]
then
sudo apt-add-repository --yes --update ppa:ansible/ansible > /dev/null 2>&1
fi
sudo apt update > /dev/null 2>&1
sudo apt install nano git ansible -y > /dev/null 2>&1
}
# pulling repo and updating content
pull_repo() {
# pulling latest changes
cd ${REPODIR}
sudo git -C ${REPODIR} pull > /dev/null 2>&1 && echo "... updated git repo ..."
# install galaxy roles
cd ${REPODIR}
ansible-galaxy install -r requirements.yml --force > /dev/null 2>&1 && echo "... updated galaxy packages ..."
}
#perform restic backup
function restic_backup() {
export RESTIC_REPOSITORY="/restic/backup"
export RESTIC_PASSWORD_FILE="/root/RESTIC_PASSWORD"
/snap/bin/restic "$@"
}
export -f restic_backup
#Usage print
usage() {
echo "Usage: $0 [-t TAG] [-b]" >&2
echo "
OPTIONS
-t [TAG], run playbook with given tag. Check README.md
-b, run restic backup operations. e.g. $0 -b backup /srv
">&2
exit 1
}
###### Run routines
# checking for required packages
git version > /dev/null 2>&1 && ansible-playbook -h > /dev/null 2>&1 || installing_requirements
# checking if repo dir is present
if [ ! -d "$REPODIR" ]; then
echo "Cloning repo to: $REPODIR"
sudo git clone ${GITREPO} ${REPODIR} > /dev/null 2>&1
sudo cp ${REPODIR}/vars.yml.example ${REPODIR}/vars.yml
sudo cp ${REPODIR}/inventory.example ${REPODIR}/inventory
echo -e "Edit variables now with:\nnano ${REPODIR}/vars.yml"
fi
# updating repo
pull_repo
# checking if script is deployed on system
if [ ! -f "$EXE" ]; then
echo "Deployed system script: $EXE"
sudo cp ${SCRIPT} ${EXE}
sudo chmod 775 ${EXE}
fi
###### processing args
while [ $# -gt 0 ] ; do
case $1 in
-t | --tag | tag) cd ${REPODIR}; sudo ansible-playbook main.yml -t base,"$2" ;;
-b | --backup | backup) sudo restic_backup "$2" ;;
-h | --help | help) usage ;;
esac
shift
done