-
Notifications
You must be signed in to change notification settings - Fork 15
/
install.sh
executable file
·156 lines (125 loc) · 4.64 KB
/
install.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
154
155
156
#!/bin/bash
set -e
error() {
echo "$1" >&2
exit 1
}
info() {
echo "$1"
}
version_ge() {
[ "$(printf '%s\n' "$1" "$2" | sort -V | tail -n1)" = "$1" ]
}
OS="$(uname | tr '[:upper:]' '[:lower:]')"
case "$OS" in
darwin*) OS="darwin" ;;
linux*) OS="linux" ;;
*) error "Unsupported OS: $OS" ;;
esac
info "Detected OS: $OS"
ARCH="$(uname -m)"
case "$ARCH" in
x86_64 | amd64) ARCH="amd64" ;;
arm64 | aarch64) ARCH="arm64" ;;
*) error "Unsupported architecture: $ARCH" ;;
esac
info "Detected Architecture: $ARCH"
REPO_OWNER="ethpandaops"
REPO_NAME="xatu"
BASE_INSTALL_PATH="${BASE_INSTALL_PATH:-/usr/local/bin}"
STANDARD_BINARY_NAME="${REPO_NAME}"
INSTALL_PATH="${BASE_INSTALL_PATH}/${STANDARD_BINARY_NAME}"
fetch_latest_tag() {
info "Fetching the latest release information from GitHub..."
LATEST_RELEASE_JSON=$(curl -s "https://api.github.com/repos/${REPO_OWNER}/${REPO_NAME}/releases/latest") || error "Failed to fetch release information."
if [ "$OS" = "darwin" ]; then
TAG=$(echo "$LATEST_RELEASE_JSON" | sed -n 's/.*"tag_name": *"\([^"]*\)".*/\1/p') || error "Failed to parse release tag."
else
TAG=$(echo "$LATEST_RELEASE_JSON" | grep -oP '"tag_name":\s*"\K(.*)(?=")') || error "Failed to parse release tag."
fi
[[ -z "$TAG" ]] && error "Release tag not found."
info "Latest release tag: $TAG"
}
get_installed_version() {
if command -v ${INSTALL_PATH} >/dev/null 2>&1; then
VERSION_OUTPUT=$(${INSTALL_PATH} version 2>/dev/null) || {
info "xatu is installed but failed to retrieve version information."
return 1
}
if [[ $VERSION_OUTPUT =~ Xatu/v([0-9]+\.[0-9]+\.[0-9]+)-([a-f0-9]+) ]]; then
INSTALLED_VERSION="${BASH_REMATCH[1]}"
info "Installed xatu version: $INSTALLED_VERSION"
return 0
else
info "Failed to parse xatu version from output: $VERSION_OUTPUT"
return 1
fi
else
info "xatu is not installed or not found in PATH."
return 1
fi
}
check_existing_installation() {
if which xatu >/dev/null 2>&1; then
if [ "$(which xatu)" != "$INSTALL_PATH" ]; then
info "xatu is already installed at $(which xatu) but expecting it at $INSTALL_PATH. You might want to remove it."
fi
fi
}
check_version() {
if get_installed_version; then
LATEST_VERSION="${TAG#v}"
info "Latest released xatu version: $LATEST_VERSION"
if version_ge "$INSTALLED_VERSION" "$LATEST_VERSION"; then
info "xatu is already up to date (version $INSTALLED_VERSION, location: ${INSTALL_PATH}). Skipping installation."
exit 0
else
info "A newer version of xatu is available (installed: $INSTALLED_VERSION, latest: $LATEST_VERSION). Proceeding with installation."
fi
else
info "xatu is not installed or version could not be determined. Proceeding with installation."
fi
}
uninstall() {
if [ -f "$INSTALL_PATH" ]; then
info "Uninstalling xatu from $INSTALL_PATH..."
sudo rm "$INSTALL_PATH" || error "Failed to remove xatu from $INSTALL_PATH"
info "xatu has been successfully uninstalled."
else
info "xatu is not installed at $INSTALL_PATH. Nothing to uninstall."
fi
exit 0
}
if [ "$1" = "uninstall" ]; then
uninstall
fi
check_existing_installation
fetch_latest_tag
check_version
ASSET_NAME="${REPO_NAME}_${TAG#v}_${OS}_${ARCH}.tar.gz"
info "Constructed asset name: $ASSET_NAME"
DOWNLOAD_URL="https://github.com/${REPO_OWNER}/${REPO_NAME}/releases/download/${TAG}/${ASSET_NAME}"
info "Download URL: $DOWNLOAD_URL"
TMP_DIR=$(mktemp -d) || error "Failed to create temporary directory."
info "Created temporary directory: $TMP_DIR"
cleanup() {
rm -rf "$TMP_DIR"
}
trap cleanup EXIT
info "Downloading ${ASSET_NAME}..."
curl -L -o "${TMP_DIR}/${ASSET_NAME}" "$DOWNLOAD_URL" || error "Failed to download the asset."
info "Extracting the binary..."
tar -xzf "${TMP_DIR}/${ASSET_NAME}" -C "$TMP_DIR" || error "Failed to extract the archive."
BINARY_PATTERN="${REPO_NAME}-*"
BINARY_FILE=$(find "$TMP_DIR" -type f -name "$BINARY_PATTERN" | head -n 1)
[[ -z "$BINARY_FILE" ]] && error "No binary matching pattern '${BINARY_PATTERN}' found in the archive."
info "Found binary: $(basename "$BINARY_FILE")"
RENAMED_BINARY="${TMP_DIR}/${STANDARD_BINARY_NAME}"
info "Renaming binary to: $STANDARD_BINARY_NAME"
mv "$BINARY_FILE" "$RENAMED_BINARY" || error "Failed to rename the binary."
info "Installing the binary to ${INSTALL_PATH}..."
sudo mv "$RENAMED_BINARY" "$INSTALL_PATH" || error "Failed to move the binary to ${INSTALL_PATH}."
sudo chmod +x "$INSTALL_PATH" || error "Failed to make the binary executable."
info "Installation successful! '${REPO_NAME}' has been installed to ${INSTALL_PATH}."
info "You can run it using '${REPO_NAME}' from your terminal."
exit 0