-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_bot.sh
140 lines (113 loc) · 4.54 KB
/
update_bot.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/bin/sh
# update_bot.sh
# Clone from repository if required and update all, commits will compared before
# author: Thomas Kaulke, kaulketh@gmail.com
bot=$1
chat=$2
project=greenhouse
owner=kaulketh
# path to main config library
config='/home/pi/scripts/TelegramBot/conf/lib_global.py'
log=$(python ${config} file_log_update)
commit_id=$(python ${config} commit_id)
cloned_branch=$(python ${config} cloned_branch)
bot_dir=$(python ${config} bot_dir)
latest_release=$(python ${config} latest_release)
wait=3
# function display usage
display_usage() {
echo "Failed! Parameter is missing."
echo "Usage only possible at least with Telegram bot API token and Chat ID!"
}
# if less than 2 arguments supplied, display usage
if [[ $# -le 1 ]]
then
display_usage
exit 1
fi
# if third arguments supplied then will set as branch
if [[ $# -eq 3 ]]
then
branch=$3
echo ${branch} > ${cloned_branch}
else
# get default branch from repository
branch=$(curl -s https://api.github.com/repos/${owner}/${project} --insecure | grep -Po '(?<="default_branch":)(.*?)(?=,)' | sed "s/\"//g" | sed -e 's/^[[:space:]]*//')
echo ${branch} > ${cloned_branch}
fi
# get last commit id of branch
commit=$(curl -s https://api.github.com/repos/${owner}/${project}/commits/${branch} --insecure | grep -Po '(?<="sha":)(.*?)(?=,)' -m 1 | sed "s/\"//g" | sed -e 's/^[[:space:]]*//' | sed -e 's/[.]*$//')
# get saved commit
last_commit=$(cat ${commit_id})
# get latest release
release=$(curl -s https://api.github.com/repos/${owner}/${project}/releases/latest --insecure| grep -Po '"tag_name": "\K.*?(?=")')
echo ${release} > ${latest_release}
# all output to log file
exec >> ${log}
# function update
update() {
echo -------------------------------------------------------------------------------------------------------
echo "[$(date +'%F %H:%M:%S')] Starting update..."
# go into bot directory
cd ${bot_dir}
#remove old tmp, logs and pyc
echo "[$(date +'%F %H:%M:%S')] Removing some files..."
find ${bot_dir} -name *.pyc -type f -exec rm -fv {} \;
find ${bot_dir} -name *.log -type f -exec rm -fv {} \;
find ${bot_dir} -name *.tmp -type f -exec rm -fv {} \;
find ${bot_dir} -name *.save -type f -exec rm -fv {} \;
rm -fv /cmd.tmp
echo
# clone from github
echo "[$(date +'%F %H:%M:%S')] Cloning branch '${branch}' from repository to '${project}' folder..."
git clone -v https://github.com/${owner}/${project}.git -b ${branch}
echo
#change to cloned project folder
cd ${project}
# update python and shell scripts
echo "[$(date +'%F %H:%M:%S')] Updating files..."
cp -rv bot/* ${bot_dir}
# update config files
cp -v configs/motion.conf /etc/motion/motion.conf
cp -v configs/dhcpcd.conf /etc/dhcpcd.conf
#cp -v configs/ddclient.conf /etc/ddclient.conf
echo
# back to bot directory
cd ${bot_dir}
# remove cloned not needed files and folders
echo "[$(date +'%F %H:%M:%S')] Removing unnecessary files..."
rm -rfv ${project}
echo
# change owner and mode of new files
echo "[$(date +'%F %H:%M:%S')] Setting owner and permissions..."
# chown -v root:netdev /etc/ddclient.conf
chown -v root:root /etc/motion/motion.conf
chown -v root:root /etc/dhcpcd.conf
chown -Rv root:root ${bot_dir}
find ${bot_dir} ! -name *.ttf -exec chmod -Rv +x {} \;
find ${bot_dir} ! -name *.png -exec chmod -Rv +x {} \;
# chmod -Rv +x ${bot_dir}
echo
# update start script in /etc/init.d/
echo "[$(date +'%F %H:%M:%S')] Updating start script..."
mv -vf telegrambot.sh /etc/init.d/
echo
# save last commit id
echo ${commit} > ${commit_id}
sleep ${wait}
# reply message about update
curl -s -k https://api.telegram.org/bot${bot}/sendMessage -d text="[$(date +'%F %H:%M:%S')] Updated, build: ${commit:0:7}, branch: $branch, rebooted" -d chat_id=${chat} >> /dev/null
echo "[$(date +'%F %H:%M:%S')] Update finished, branch '$branch', commit ID '${commit:0:7}' saved, system rebooted."
reboot
}
# check if an update is required
if [[ ${commit} == ${last_commit} ]];
then
curl -s -k https://api.telegram.org/bot${bot}/sendMessage -d text="[$(date +'%F %H:%M:%S')] Update checked, not required, last commit '${commit:0:7}' of branch '${branch}'." -d chat_id=${chat} >> /dev/null
echo -------------------------------------------------------------------------------------------------------
echo "[$(date +'%F %H:%M:%S')] Update checked, not required, current version equals last commit '${last_commit:0:7}' of branch '${branch}'."
exit 1
else
curl -s -k https://api.telegram.org/bot${bot}/sendMessage -d text="[$(date +'%F %H:%M:%S')] Changes detected or update was forced, starting update." -d chat_id=${chat} >> /dev/null
update
fi