-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdater.sh
executable file
·71 lines (57 loc) · 2.67 KB
/
updater.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
#!/bin/bash
# Perform updates of the branch program.
# -------------------------------- Verify Root ------------------------------- #
if [ "$EUID" -ne 0 ]
then echo "Please run as root with sudo."
exit 1
fi
# ---------------------------- Read Stem Variables --------------------------- #
if [ -f /opt/Stem/stem.json ]; then
branch_dir=$(jq --raw-output '.branch.directory' /opt/Stem/stem.json)
config_file=$(jq --raw-output '.branch.config_file' /opt/Stem/stem.json)
version_url_endpoint=$(jq --raw-output '.branch.version_url_endpoint' /opt/Stem/stem.json)
else
echo "Required file stem.json file not found."
exit 1
fi
# --------------------------- Read Branch Variables -------------------------- #
if [ -f "${branch_dir}${config_file}" ]; then
current_version=$(jq --raw-output '.current_version' "${branch_dir}${config_file}")
latest_version=$(curl --silent "${version_url_endpoint}")
update_url=$(jq --raw-output '.update_url' "${branch_dir}${config_file}")
access_token=$(jq --raw-output '.access_token' "${branch_dir}${config_file}")
else
echo "Required file ${branch_dir}${config_file} not found."
exit 1
fi
# ------------------------------- Update Branch ------------------------------ #
if [ "$current_version" != "$latest_version" ]; then
# Create staging directory.
if [ -d "/opt/Stem/branch_staging" ]; then
rm -r /opt/Stem/branch_staging
fi
mkdir -p /opt/Stem/branch_staging;
# Convert latest version separators to underscores.
latest_version_underscored=$(echo "${latest_version}" | tr '.' '_')
# Download the latest version.
curl -H "Authorization: token ${access_token}" \
--output "/opt/Stem/branch_staging/${latest_version_underscored}.zip" \
--silent --location "${update_url}"
unzip /opt/Stem/branch_staging/"$latest_version_underscored".zip -d /opt/Stem/branch_staging # Unzip the latest version.
rm /opt/Stem/branch_staging/"$latest_version_underscored".zip # Remove the zip file.
# Rename the extracted directory to the latest version.
mv /opt/Stem/branch_staging/"$(ls -N /opt/Stem/branch_staging)" /opt/Stem/branch_staging/"${latest_version_underscored}"
cd /opt/Stem/branch_staging/"$latest_version_underscored" || exit
# Run the installer.
if ! sudo ./install.sh; then
echo "Installer failed."
else
echo "Branch updated to version ${latest_version}."
tmp=$(mktemp)
jq --arg version "${latest_version}" '.current_version = $version' "${branch_dir}${config_file}" > "$tmp" && mv "$tmp" "${branch_dir}${config_file}"
exit 0
fi
else
echo "Branch is up to date."
exit 0
fi