Skip to content

Commit

Permalink
fixes for linux docker
Browse files Browse the repository at this point in the history
  • Loading branch information
NorseGaud committed Jun 14, 2024
1 parent 731f726 commit 66bba2e
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 26 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
dist
.DS_Store
*.log
*.pid
*.pid
docker/anklet*
11 changes: 10 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ LATEST-GIT-SHA := $(shell git rev-parse HEAD)
VERSION := $(shell cat VERSION)
FLAGS := -X main.version=$(VERSION) -X main.commit=$(LATEST-GIT-SHA)
BIN := anklet
ARCH := $(shell arch)
ARCH ?= $(shell arch)
ifeq ($(ARCH), i386)
ARCH = amd64
endif
ifeq ($(ARCH), x86_64)
ARCH = amd64
endif
OS_TYPE ?= $(shell uname -s | tr '[:upper:]' '[:lower:]')
BIN_FULL ?= dist/$(BIN)_v$(VERSION)_$(OS_TYPE)_$(ARCH)
export PATH := $(shell go env GOPATH)/bin:$(PATH)
Expand Down Expand Up @@ -40,3 +43,9 @@ install:
mkdir -p ~/bin
cp -f dist/$(BIN)_v$(VERSION)_$(OS_TYPE)_$(ARCH) ~/bin/$(BIN)

go.build:
GOARCH=$(ARCH) go build $(RACE) -ldflags "-X main.version=$(VERSION)" -o docker/$(BIN)_$(OS_TYPE)_$(ARCH)
chmod +x docker/$(BIN)_$(OS_TYPE)_$(ARCH)

build-linux:
GOOS=linux OS_TYPE=linux $(MAKE) go.build
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.8.0
0.8.1
5 changes: 5 additions & 0 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM alpine:latest
RUN apk update && apk add ca-certificates
ARG TARGETARCH
COPY ./anklet_linux_${TARGETARCH} /usr/local/bin/anklet
ENTRYPOINT ["/usr/local/bin/anklet", "--attach"]
25 changes: 25 additions & 0 deletions docker/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
version: '3.1'
services:
anklet-metrics-aggregator:
# build:
# context: ./
# dockerfile: Dockerfile
image: veertu/anklet:latest
container_name: anklet-metrics-aggregator
# volumes:
# - /Users/nathanpierce/.config/anklet:/root/.config/anklet/
ports:
- "8081:8081"
environment:
# - LOG_LEVEL=dev
- ANKLET_METRICS_AGGREGATOR=true
- ANKLET_METRICS_PORT=8081
# - ANKLET_METRICS_URLS=http://anklet1:8080/metrics,http://anklet2:8081/metrics
- ANKLET_METRICS_URLS=http://host.docker.internal:8080/metrics
- ANKLET_METRICS_SLEEP_INTERVAL=10
- ANKLET_METRICS_DATABASE_ENABLED=true
- ANKLET_METRICS_DATABASE_URL=host.docker.internal
- ANKLET_METRICS_DATABASE_PORT=6379
- ANKLET_METRICS_DATABASE_USER=
- ANKLET_METRICS_DATABASE_PASSWORD=
- ANKLET_METRICS_DATABASE_DATABASE=0
21 changes: 21 additions & 0 deletions generate-dockerhub-tags.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/bash
set -exo pipefail
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
pushd "${SCRIPT_DIR}"
DOCKERFILE_PATH="${SCRIPT_DIR}/docker"
NAME="anklet"
cleanup() {
rm -f "${DOCKERFILE_PATH}/${NAME}"*
}
ARCH=amd64 make build-linux
ARCH=arm64 make build-linux
ls -alht "${DOCKERFILE_PATH}/"
trap cleanup EXIT
pushd "${DOCKERFILE_PATH}"
docker buildx create --name mybuilder --use || true
docker buildx install || true
# make sure docker login is handled
docker build --no-cache --platform linux/amd64,linux/arm64 \
-t veertu/"${NAME}":latest -t veertu/"$NAME:v$(cat "${SCRIPT_DIR}"/VERSION)" \
--push .
popd
20 changes: 9 additions & 11 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,23 +65,23 @@ type Service struct {
Workflows Workflow `yaml:"workflows"`
}

func LoadConfig(configPath string) (*Config, error) {
func LoadConfig(configPath string) (Config, error) {
config := Config{}
file, err := os.Open(configPath)
if err != nil {
return nil, err
return config, err
}
defer file.Close()

decoder := yaml.NewDecoder(file)
var config Config
err = decoder.Decode(&config)
if err != nil {
return nil, err
return config, err
}
return &config, nil
return config, nil
}

func LoadInEnvs(config *Config) (*Config, error) {
func LoadInEnvs(config Config) (Config, error) {
envAggregator := os.Getenv("ANKLET_METRICS_AGGREGATOR")
if envAggregator != "" {
config.Metrics.Aggregator = envAggregator == "true"
Expand All @@ -91,17 +91,15 @@ func LoadInEnvs(config *Config) (*Config, error) {
if envPort != "" {
config.Metrics.Port = envPort
}

envMetricsURLs := os.Getenv("ANKLET_METRICS_URLS")
if envMetricsURLs != "" {
config.Metrics.MetricsURLs = strings.Split(envMetricsURLs, ",")
}

envSleepInterval := os.Getenv("ANKLET_METRICS_SLEEP_INTERVAL")
if envSleepInterval != "" {
value, err := strconv.Atoi(envSleepInterval)
if err != nil {
return nil, err
return Config{}, err
}
config.Metrics.SleepInterval = value
}
Expand Down Expand Up @@ -130,7 +128,7 @@ func LoadInEnvs(config *Config) (*Config, error) {
if envDBPort != "" {
port, err := strconv.Atoi(envDBPort)
if err != nil {
return nil, err
return Config{}, err
}
config.Metrics.Database.Port = port
}
Expand All @@ -139,7 +137,7 @@ func LoadInEnvs(config *Config) (*Config, error) {
if envDBDatabase != "" {
database, err := strconv.Atoi(envDBDatabase)
if err != nil {
return nil, err
return Config{}, err
}
config.Metrics.Database.Database = database
}
Expand Down
29 changes: 17 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ var (
signalFlag = flag.String("s", "", `Send signal to the daemon:
drain — graceful shutdown, will wait until all jobs finish before exiting
stop — best effort graceful shutdown, interrupting the job as soon as possible`)
attachFlag = flag.Bool("attach", false, "Attach to the anklet and don't background it (useful for containers)")
stop = make(chan struct{})
done = make(chan struct{})
shutDownMessage = "anklet service shut down"
Expand Down Expand Up @@ -80,8 +81,10 @@ func main() {
// obtain config
loadedConfig, err := config.LoadConfig(configPath)
if err != nil {
panic(err)
logger.InfoContext(parentCtx, "unable to load config.yml", "error", err)
// panic(err)
}
logger.InfoContext(parentCtx, "loaded config", slog.Any("config", loadedConfig))
loadedConfig, err = config.LoadInEnvs(loadedConfig)
if err != nil {
panic(err)
Expand All @@ -95,9 +98,6 @@ func main() {
}
parentCtx = context.WithValue(parentCtx, config.ContextKey("suffix"), suffix)

logger.DebugContext(parentCtx, "loaded config", slog.Any("config", loadedConfig))
parentCtx = context.WithValue(parentCtx, config.ContextKey("config"), loadedConfig)

if loadedConfig.Log.FileDir == "" {
loadedConfig.Log.FileDir = "./"
}
Expand All @@ -108,6 +108,9 @@ func main() {
loadedConfig.WorkDir = "./"
}

logger.DebugContext(parentCtx, "loaded config", slog.Any("config", loadedConfig))
parentCtx = context.WithValue(parentCtx, config.ContextKey("config"), &loadedConfig)

daemonContext := &daemon.Context{
PidFileName: loadedConfig.PidFileDir + "anklet" + suffix + ".pid",
PidFilePerm: 0644,
Expand Down Expand Up @@ -157,16 +160,18 @@ func main() {
}
}

d, err := daemonContext.Reborn()
if err != nil {
log.Fatalln(err)
}
if d != nil {
return
if !*attachFlag {
d, err := daemonContext.Reborn()
if err != nil {
log.Fatalln(err)
}
if d != nil {
return
}
defer daemonContext.Release()
}
defer daemonContext.Release()

go worker(parentCtx, logger, *loadedConfig)
go worker(parentCtx, logger, loadedConfig)

err = daemon.ServeSignals()
if err != nil {
Expand Down

0 comments on commit 66bba2e

Please sign in to comment.