-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathauto-update.sh
executable file
·153 lines (116 loc) · 3.2 KB
/
auto-update.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
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/env bash
# Colors
START=$(tput setaf 4)
SUCCESS=$(tput setaf 2)
WARNING=$(tput setaf 3)
INFO=$(tput setaf 6)
RESET=$(tput sgr0)
# Vars
REPO_URL="https://github.com/DavidGomesDev/RustyController"
RUSTY_HOME_DIR="$HOME/RustyController"
BINARY_PATH="$RUSTY_HOME_DIR/server/target/release/rusty_controller"
HASH_FILE="$RUSTY_HOME_DIR/current.sha256"
# Get params
show_usage () {
echo "Parameters:"
echo "-b: build binary (instead of downloading the latest release)"
echo "-l: always launch (even if already up-to-date; useful on reboot)"
}
while getopts "blh" opt; do
case ${opt} in
b)
BUILD_BINARY="y"
;;
l)
LAUNCH_ALWAYS="y"
;;
h)
show_usage
exit 0
;;
?)
echo "Invalid option: -${OPTARG}."
show_usage
exit 1
;;
esac
done
echo "$START* Updating server at $(date)$RESET"
cd "$HOME" || exit 1
download_latest () {
# Get arch (and trim output)
arch=$(uname -m | xargs echo -n)
mkdir -p target/release || exit 1
wget -q "$REPO_URL/releases/latest/download/server-$arch" -O "$BINARY_PATH" || exit 1
chmod +x "$BINARY_PATH" || exit 1
newest_hash=$(sha256sum "$BINARY_PATH" | gawk '{print $1}')
echo "$INFO* Downloaded latest release binary$RESET"
}
build () {
echo
echo "$INFO* Updating crates...$RESET"
echo
cargo update -q || exit 1
echo "$INFO* Build...$RESET"
echo
time cargo build --release -q || exit 1
newest_hash=$(sha256sum "$BINARY_PATH" | gawk '{print $1}')
echo "$SUCCESS* Built successfully!$RESET"
}
update () {
echo "$INFO* Checking out main...$RESET"
echo
git reset --hard > /dev/null || exit 1
# This way we only pull the main branch
git fetch origin main > /dev/null || exit 1
git switch main > /dev/null || exit 1
git pull > /dev/null || exit 1
cd server/ || exit 1
if [[ "$BUILD_BINARY" == "y" ]]; then
build
else
download_latest
fi
cd ..
}
launch () {
echo "$SUCCESS* Launching...$RESET"
. "$RUSTY_HOME_DIR/server/scripts/run-server.sh"
}
if [[ ! -d "$RUSTY_HOME_DIR" ]]; then
echo "$WARNING* Rusty repo not found. Cloning...$RESET"
git clone "$REPO_URL"
update
launch
echo "$START* Finished installing at $(date)$RESET"
exit 0
fi
cd "$RUSTY_HOME_DIR" || exit 1
if [[ -f "$HASH_FILE" ]]; then
current_hash=$(cat "$HASH_FILE")
update
if [[ "$current_hash" != "$newest_hash" ]]; then
newest_hash=$(sha256sum "$BINARY_PATH" | gawk '{print $1}')
echo "$newest_hash" > "$HASH_FILE"
echo "$INFO* There is a new version!$RESET"
launch
else
echo "$INFO* Version is already up-to-date.$RESET"
tmux has-session -t "RustyController" 2>/dev/null
if [ $? != 0 ]; then
printf "\n${INFO}Server is not running$RESET\n\n"
launch
fi
if [[ "$LAUNCH_ALWAYS" == "y" ]]; then
printf "\n${INFO}Launching due to always launch flag$RESET\n\n"
launch
fi
fi
else
echo "$WARNING* Couldn't find current hash. Updating to latest version anyway.$RESET"
update
launch
newest_hash=$(sha256sum "$BINARY_PATH" | gawk '{print $1}')
echo "$newest_hash" > "$HASH_FILE"
fi
echo "$START* Finished auto-update at $(date)$RESET"