Skip to content

Commit afe2f8f

Browse files
authored
Merge pull request #74 from yubiuser/development
vNext
2 parents cf179db + 56fe37d commit afe2f8f

File tree

10 files changed

+97
-12
lines changed

10 files changed

+97
-12
lines changed

.devcontainer/Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ ARG golang_version=1.21
44
FROM golang:${golang_version}-alpine${alpine_version}
55
RUN apk add --no-cache \
66
git \
7+
make \
78
nano\
89
openssh
910

.editorconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,6 @@ trim_trailing_whitespace = true
1313

1414
[*.go]
1515
indent_style = tab
16+
17+
[makefile]
18+
indent_style = tab

.github/workflows/ghcr.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,24 @@ jobs:
3535
3636
- name: Checkout Code
3737
uses: actions/[email protected]
38+
with:
39+
fetch-depth: 0
40+
ref: ${{ github.head_ref }}
41+
42+
- name: "Calculate required variables"
43+
id: variables
44+
run: |
45+
GIT_TAG=${{ github.event.release.tag_name }}
46+
# If GIT_TAG is set then GIT BRANCH should be "main", else get it from the branch name
47+
GIT_BRANCH=$([ -n "${GIT_TAG}" ] && echo "main" || git rev-parse --abbrev-ref HEAD)
48+
GIT_VERSION=$(git --no-pager describe --tags --always --abbrev=8 --dirty)
49+
GIT_COMMIT=$(git --no-pager describe --always --abbrev=8 --dirty)
50+
GIT_DATE=$(git --no-pager show --date=short --format=%at --name-only | head -n 1)
51+
echo "GIT_TAG=${GIT_TAG}" >> $GITHUB_ENV
52+
echo "GIT_BRANCH=${GIT_BRANCH}" >> $GITHUB_ENV
53+
echo "GIT_VERSION=${GIT_VERSION}" >> $GITHUB_ENV
54+
echo "GIT_COMMIT=${GIT_COMMIT}" >> $GITHUB_ENV
55+
echo "GIT_DATE=${GIT_DATE}" >> $GITHUB_ENV
3856
3957
- name: Set up QEMU
4058
uses: docker/[email protected]
@@ -63,6 +81,12 @@ jobs:
6381
with:
6482
context: .
6583
file: ./Dockerfile
84+
build-args: |
85+
GIT_TAG=${{ env.GIT_TAG }}
86+
GIT_BRANCH=${{ env.GIT_BRANCH }}
87+
GIT_VERSION=${{ env.GIT_VERSION }}
88+
GIT_COMMIT=${{ env.GIT_COMMIT }}
89+
GIT_DATE=${{ env.GIT_DATE }}
6690
platforms: ${{ matrix.platform }}
6791
labels: ${{ steps.meta.outputs.labels }}
6892
provenance: false

Dockerfile

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,19 @@ ARG alpine_version=3.19
22
ARG golang_version=1.21
33

44
FROM golang:${golang_version}-alpine${alpine_version} as builder
5+
ARG GIT_COMMIT
6+
ARG GIT_BRANCH
7+
ARG GIT_VERSION
8+
ARG GIT_DATE
9+
ARG GIT_TAG
10+
11+
RUN apk add --no-cache \
12+
git \
13+
make
514

615
COPY /src /src
716
WORKDIR /src
8-
RUN go mod download
9-
RUN CGO_ENABLED=0 go build -ldflags "-s -w" docker-event-monitor.go
17+
RUN make build
1018

1119
FROM scratch as deploy
1220
COPY --from=builder /src/docker-event-monitor docker-event-monitor

Readme.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ services:
6363
FILTER: 'event=start,event=stop,type=container'
6464
DELAY: '500ms'
6565
LOG_LEVEL: 'info'
66+
SERVER_TAG: ''
6667
```
6768
6869
### Build image locally
@@ -76,7 +77,7 @@ docker build -t docker-event-monitor:local .
7677

7778
### Build binary locally
7879

79-
If you have a suitable `Go` environment set up, you can build the binary from `/src/`. For development, a `devcontainer` with a suitable `Dockerfile` is provided as well.
80+
If you have a suitable `Go` environment set up, you can build the binary from `/src/`. For development, a `devcontainer` with a suitable `Dockerfile` is provided as well. If you run `make build` instead of `go build`, `git` commit/branch/date information are injected into to binary.
8081

8182
### Environment variables and configuration
8283

docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@ services:
2525
FILTER: 'event=start,event=stop,type=container'
2626
DELAY: '500ms'
2727
LOG_LEVEL: 'info'
28-
28+
SERVER_TAG: ''

src/docker-event-monitor.go

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ type args struct {
4444
Filter map[string][]string `arg:"-"`
4545
LogLevel string `arg:"env:LOG_LEVEL" default:"info" help:"Set log level. Use debug for more logging."`
4646
ServerTag string `arg:"env:SERVER_TAG" help:"Prefix to include in the title of notifications. Useful when running docker-event-monitors on multiple machines."`
47+
Version bool `arg:"-v" help:"Print version information."`
4748
}
4849

4950
// Creating a global logger
@@ -52,11 +53,31 @@ var logger zerolog.Logger
5253
// hold the supplied run-time arguments globally
5354
var glb_arguments args
5455

56+
// version information, are injected during build process
57+
var (
58+
version string = "n/a"
59+
commit string = "n/a"
60+
date string
61+
gitdate string
62+
branch string = "n/a"
63+
)
64+
5565
func init() {
5666
parseArgs()
57-
5867
configureLogger(glb_arguments.LogLevel)
5968

69+
// if the -v flag was set, print version information and exit
70+
if glb_arguments.Version {
71+
logger.Info().
72+
Str("Version", version).
73+
Str("Branch", branch).
74+
Str("Commit", commit).
75+
Time("Compile_date", stringToUnix(date)).
76+
Time("Git_date", stringToUnix(gitdate)).
77+
Msg("Version Information")
78+
os.Exit(0)
79+
}
80+
6081
if glb_arguments.Pushover {
6182
if len(glb_arguments.PushoverAPIToken) == 0 {
6283
logger.Fatal().Msg("Pushover enabled. Pushover API token required!")
@@ -121,6 +142,13 @@ func main() {
121142
Str("ServerTag", glb_arguments.ServerTag).
122143
Str("Filter", strings.Join(glb_arguments.FilterStrings, " ")),
123144
).
145+
Dict("version", zerolog.Dict().
146+
Str("Version", version).
147+
Str("Branch", branch).
148+
Str("Commit", commit).
149+
Time("Compile_date", stringToUnix(date)).
150+
Time("Git_date", stringToUnix(gitdate)),
151+
).
124152
Msg("Docker event monitor started")
125153

126154
timestamp := time.Now()
@@ -448,23 +476,32 @@ func parseArgs() {
448476
}
449477

450478
func configureLogger(LogLevel string) {
451-
// UNIX Time is faster and smaller than most timestamps
452-
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
479+
// Configure time/timestamp format
480+
zerolog.TimeFieldFormat = time.RFC1123Z
453481

454482
// Change logging level when debug flag is set
455483
if LogLevel == "debug" {
456-
logger = zerolog.New(os.Stderr).
484+
logger = zerolog.New(os.Stdout).
457485
Level(zerolog.DebugLevel).
458486
With().
459487
Timestamp().
460488
Str("service", "docker event monitor").
461489
Logger()
462490
} else {
463-
logger = zerolog.New(os.Stderr).
491+
logger = zerolog.New(os.Stdout).
464492
Level(zerolog.InfoLevel).
465493
With().
466494
Str("service", "docker event monitor").
467495
Timestamp().
468496
Logger()
469497
}
470498
}
499+
500+
func stringToUnix(str string) time.Time {
501+
i, err := strconv.ParseInt(str, 10, 64)
502+
if err != nil {
503+
logger.Fatal().Err(err).Msg("String to timestamp conversion failed")
504+
}
505+
tm := time.Unix(i, 0)
506+
return tm
507+
}

src/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.21
44

55
require (
66
github.com/alexflint/go-arg v1.4.3
7-
github.com/docker/docker v25.0.1+incompatible
7+
github.com/docker/docker v25.0.2+incompatible
88
github.com/gregdel/pushover v1.3.0
99
github.com/rs/zerolog v1.31.0
1010
golang.org/x/text v0.14.0

src/go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
1717
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1818
github.com/distribution/reference v0.5.0 h1:/FUIFXtfc/x2gpa5/VGfiGLuOIdYa1t65IKK2OFGvA0=
1919
github.com/distribution/reference v0.5.0/go.mod h1:BbU0aIcezP1/5jX/8MP0YiH4SdvB5Y4f/wlDRiLyi3E=
20-
github.com/docker/docker v25.0.1+incompatible h1:k5TYd5rIVQRSqcTwCID+cyVA0yRg86+Pcrz1ls0/frA=
21-
github.com/docker/docker v25.0.1+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
20+
github.com/docker/docker v25.0.2+incompatible h1:/OaKeauroa10K4Nqavw4zlhcDq/WBcPMc5DbjOGgozY=
21+
github.com/docker/docker v25.0.2+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
2222
github.com/docker/go-connections v0.5.0 h1:USnMq7hx7gwdVZq1L49hLXaFtUdTADjXGp+uj1Br63c=
2323
github.com/docker/go-connections v0.5.0/go.mod h1:ov60Kzw0kKElRwhNs9UlUHAE/F9Fe6GLaXnqyDdmEXc=
2424
github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4=

src/makefile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
BINARY_NAME := docker-event-monitor
2+
GIT_COMMIT ?= $(shell git --no-pager describe --always --abbrev=8 --dirty)
3+
GIT_BRANCH ?= $(shell git rev-parse --abbrev-ref HEAD)
4+
GIT_VERSION ?= $(shell git --no-pager describe --tags --always --abbrev=8 --dirty)
5+
GIT_DATE ?= $(shell git --no-pager show --date=short --format=%at --name-only | head -n 1)
6+
ifdef GIT_TAG
7+
override GIT_VERSION = ${GIT_TAG}
8+
endif
9+
DATE := $(shell date +%s)
10+
build:
11+
CGO_ENABLED=0 go build -ldflags "-s -w -X 'main.version=${GIT_VERSION}' -X 'main.gitdate=${GIT_DATE}' -X 'main.date=${DATE}' -X 'main.commit=${GIT_COMMIT}' -X 'main.branch=${GIT_BRANCH}'" -o=${BINARY_NAME} docker-event-monitor.go

0 commit comments

Comments
 (0)