Skip to content

Commit

Permalink
Merge pull request #141 from tribunadigital/feat_version_flag
Browse files Browse the repository at this point in the history
Add version flag and installation of specific version
  • Loading branch information
apetruhin authored Jan 14, 2025
2 parents 4f95b6b + 500c894 commit 75b0def
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ COPY go.sum .
RUN go mod download
COPY . .
ARG VERSION=unknown
RUN CGO_ENABLED=1 go build -mod=readonly -ldflags "-X main.version=$VERSION" -o coroot-node-agent .
RUN CGO_ENABLED=1 go build -mod=readonly -ldflags "-X 'github.com/coroot/coroot-node-agent/flags.Version=${VERSION}'" -o coroot-node-agent .

FROM registry.access.redhat.com/ubi9/ubi

Expand Down
9 changes: 9 additions & 0 deletions flags/flags.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package flags

import (
"fmt"
"os"
"strings"

Expand Down Expand Up @@ -39,6 +40,9 @@ var (

ScrapeInterval = kingpin.Flag("scrape-interval", "How often to gather metrics from the agent").Default("15s").Envar("SCRAPE_INTERVAL").Duration()
WalDir = kingpin.Flag("wal-dir", "Path to where the agent stores data (e.g. the metrics Write-Ahead Log)").Default("/tmp/coroot-node-agent").Envar("WAL_DIR").String()

agentVersion = kingpin.Flag("version", "Print version and exit").Default("false").Bool()
Version = "unknown"
)

func GetString(fl *string) string {
Expand All @@ -56,6 +60,11 @@ func init() {
kingpin.HelpFlag.Short('h').Hidden()
kingpin.Parse()

if *agentVersion {
fmt.Println("Version:", Version)
os.Exit(0)
}

if *CollectorEndpoint != nil {
u := *CollectorEndpoint
if *MetricsEndpoint == nil {
Expand Down
60 changes: 43 additions & 17 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fi

BIN_DIR=/usr/bin
SYSTEMD_DIR=/etc/systemd/system
VERSION=
VERSION="latest"
SYSTEM_NAME=coroot-node-agent
SYSTEMD_SERVICE=${SYSTEM_NAME}.service
UNINSTALL_SH=${BIN_DIR}/${SYSTEM_NAME}-uninstall.sh
Expand All @@ -26,9 +26,17 @@ info()
fatal()
{
echo '[ERROR] ' "$@" >&2
show_help
exit 1
}

show_help() {
echo "Usage: $0 [options]"
echo "Options:"
echo " -h, --help Show this help message and exit"
echo " -v v1.22.2, --version v1.22.2 Specify the version to install (default: latest)"
}

verify_system() {
if [ -x /bin/systemctl ] || type systemctl > /dev/null 2>&1; then
return
Expand Down Expand Up @@ -84,20 +92,24 @@ setup_tmp() {
}

get_release_version() {
info "Finding the latest release"
latest_release_url=${GITHUB_URL}/latest
case $DOWNLOADER in
curl)
VERSION=$(curl -w '%{url_effective}' -L -s -S ${latest_release_url} -o /dev/null | sed -e 's|.*/||')
;;
wget)
VERSION=$(wget -SqO /dev/null ${latest_release_url} 2>&1 | grep -i Location | sed -e 's|.*/||')
;;
*)
fatal "Incorrect downloader executable '$DOWNLOADER'"
;;
esac
info "The latest release is ${VERSION}"
if [ "$VERSION" = "latest" ]; then
info "Finding the latest release"
latest_release_url=${GITHUB_URL}/latest
case $DOWNLOADER in
curl)
VERSION=$(curl -w '%{url_effective}' -L -s -S ${latest_release_url} -o /dev/null | sed -e 's|.*/||')
;;
wget)
VERSION=$(wget -SqO /dev/null ${latest_release_url} 2>&1 | grep -i Location | sed -e 's|.*/||')
;;
*)
fatal "Incorrect downloader executable '$DOWNLOADER'"
;;
esac
info "The latest release is ${VERSION}"
else
info "Using specified version ${VERSION}"
fi
}

download_binary() {
Expand Down Expand Up @@ -136,7 +148,6 @@ download() {
setup_binary
}


create_uninstall() {
info "Creating uninstall script ${UNINSTALL_SH}"
$SUDO tee ${UNINSTALL_SH} >/dev/null << EOF
Expand Down Expand Up @@ -229,7 +240,6 @@ systemd_start() {
$SUDO systemctl restart ${SYSTEM_NAME}
}


service_enable_and_start() {
systemd_enable

Expand All @@ -244,6 +254,22 @@ service_enable_and_start() {
return 0
}

while [ $# -gt 0 ]; do
case "$1" in
-h|--help)
show_help
exit 0
;;
-v|--version)
VERSION="$2"
shift 2
;;
*)
fatal "Unknown option: $1"
;;
esac
done

{
verify_system
download
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
)

var (
version = "unknown"
version = flags.Version
)

func uname() (string, string, error) {
Expand Down

0 comments on commit 75b0def

Please sign in to comment.