Skip to content

Commit 75b0def

Browse files
authored
Merge pull request #141 from tribunadigital/feat_version_flag
Add version flag and installation of specific version
2 parents 4f95b6b + 500c894 commit 75b0def

File tree

4 files changed

+54
-19
lines changed

4 files changed

+54
-19
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ COPY go.sum .
66
RUN go mod download
77
COPY . .
88
ARG VERSION=unknown
9-
RUN CGO_ENABLED=1 go build -mod=readonly -ldflags "-X main.version=$VERSION" -o coroot-node-agent .
9+
RUN CGO_ENABLED=1 go build -mod=readonly -ldflags "-X 'github.com/coroot/coroot-node-agent/flags.Version=${VERSION}'" -o coroot-node-agent .
1010

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

flags/flags.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package flags
22

33
import (
4+
"fmt"
45
"os"
56
"strings"
67

@@ -39,6 +40,9 @@ var (
3940

4041
ScrapeInterval = kingpin.Flag("scrape-interval", "How often to gather metrics from the agent").Default("15s").Envar("SCRAPE_INTERVAL").Duration()
4142
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()
43+
44+
agentVersion = kingpin.Flag("version", "Print version and exit").Default("false").Bool()
45+
Version = "unknown"
4246
)
4347

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

63+
if *agentVersion {
64+
fmt.Println("Version:", Version)
65+
os.Exit(0)
66+
}
67+
5968
if *CollectorEndpoint != nil {
6069
u := *CollectorEndpoint
6170
if *MetricsEndpoint == nil {

install.sh

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ fi
1010

1111
BIN_DIR=/usr/bin
1212
SYSTEMD_DIR=/etc/systemd/system
13-
VERSION=
13+
VERSION="latest"
1414
SYSTEM_NAME=coroot-node-agent
1515
SYSTEMD_SERVICE=${SYSTEM_NAME}.service
1616
UNINSTALL_SH=${BIN_DIR}/${SYSTEM_NAME}-uninstall.sh
@@ -26,9 +26,17 @@ info()
2626
fatal()
2727
{
2828
echo '[ERROR] ' "$@" >&2
29+
show_help
2930
exit 1
3031
}
3132

33+
show_help() {
34+
echo "Usage: $0 [options]"
35+
echo "Options:"
36+
echo " -h, --help Show this help message and exit"
37+
echo " -v v1.22.2, --version v1.22.2 Specify the version to install (default: latest)"
38+
}
39+
3240
verify_system() {
3341
if [ -x /bin/systemctl ] || type systemctl > /dev/null 2>&1; then
3442
return
@@ -84,20 +92,24 @@ setup_tmp() {
8492
}
8593

8694
get_release_version() {
87-
info "Finding the latest release"
88-
latest_release_url=${GITHUB_URL}/latest
89-
case $DOWNLOADER in
90-
curl)
91-
VERSION=$(curl -w '%{url_effective}' -L -s -S ${latest_release_url} -o /dev/null | sed -e 's|.*/||')
92-
;;
93-
wget)
94-
VERSION=$(wget -SqO /dev/null ${latest_release_url} 2>&1 | grep -i Location | sed -e 's|.*/||')
95-
;;
96-
*)
97-
fatal "Incorrect downloader executable '$DOWNLOADER'"
98-
;;
99-
esac
100-
info "The latest release is ${VERSION}"
95+
if [ "$VERSION" = "latest" ]; then
96+
info "Finding the latest release"
97+
latest_release_url=${GITHUB_URL}/latest
98+
case $DOWNLOADER in
99+
curl)
100+
VERSION=$(curl -w '%{url_effective}' -L -s -S ${latest_release_url} -o /dev/null | sed -e 's|.*/||')
101+
;;
102+
wget)
103+
VERSION=$(wget -SqO /dev/null ${latest_release_url} 2>&1 | grep -i Location | sed -e 's|.*/||')
104+
;;
105+
*)
106+
fatal "Incorrect downloader executable '$DOWNLOADER'"
107+
;;
108+
esac
109+
info "The latest release is ${VERSION}"
110+
else
111+
info "Using specified version ${VERSION}"
112+
fi
101113
}
102114

103115
download_binary() {
@@ -136,7 +148,6 @@ download() {
136148
setup_binary
137149
}
138150

139-
140151
create_uninstall() {
141152
info "Creating uninstall script ${UNINSTALL_SH}"
142153
$SUDO tee ${UNINSTALL_SH} >/dev/null << EOF
@@ -229,7 +240,6 @@ systemd_start() {
229240
$SUDO systemctl restart ${SYSTEM_NAME}
230241
}
231242

232-
233243
service_enable_and_start() {
234244
systemd_enable
235245

@@ -244,6 +254,22 @@ service_enable_and_start() {
244254
return 0
245255
}
246256

257+
while [ $# -gt 0 ]; do
258+
case "$1" in
259+
-h|--help)
260+
show_help
261+
exit 0
262+
;;
263+
-v|--version)
264+
VERSION="$2"
265+
shift 2
266+
;;
267+
*)
268+
fatal "Unknown option: $1"
269+
;;
270+
esac
271+
done
272+
247273
{
248274
verify_system
249275
download

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
)
2626

2727
var (
28-
version = "unknown"
28+
version = flags.Version
2929
)
3030

3131
func uname() (string, string, error) {

0 commit comments

Comments
 (0)