Compare commits
3 Commits
Author | SHA1 | Date |
---|---|---|
IamTheFij | 9deac7cc7e | |
IamTheFij | be4e0a179a | |
IamTheFij | c22eea76a1 |
|
@ -1,6 +1,3 @@
|
|||
ARG TARGETOS
|
||||
ARG TARGETARCH
|
||||
|
||||
FROM alpine:3.19
|
||||
|
||||
RUN mkdir /app
|
||||
|
@ -10,7 +7,9 @@ WORKDIR /app/
|
|||
RUN addgroup -S nomad-var-dirsync && adduser -S nomad-var-dirsync -G nomad-var-dirsync
|
||||
|
||||
# Copy binary in
|
||||
COPY ./dist/nomad-var-dirsync-TARGETOS-TARGETARCH ./nomad-var-dirsync
|
||||
ARG TARGETOS
|
||||
ARG TARGETARCH
|
||||
COPY ./dist/nomad-var-dirsync-$TARGETOS-$TARGETARCH /bin/nomad-var-dirsync
|
||||
|
||||
# Drop to non-root user
|
||||
USER nomad-var-dirsync
|
||||
|
|
|
@ -1,6 +1,3 @@
|
|||
ARG TARGETOS
|
||||
ARG TARGETARCH
|
||||
|
||||
FROM golang:1.21-alpine AS builder
|
||||
|
||||
RUN apk add --no-cache git=~2 && mkdir /app
|
||||
|
@ -12,10 +9,12 @@ RUN go mod download
|
|||
COPY ./*.go /app/
|
||||
|
||||
ARG VERSION=dev
|
||||
ENV CGO_ENABLED=0 GOOS=TARGETOS GOARCH=TARGETARCH
|
||||
ARG TARGETOS
|
||||
ARG TARGETARCH
|
||||
ENV CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH
|
||||
RUN go build -ldflags "-X main.version=VERSION" -a -installsuffix nocgo -o nomad-var-dirsync .
|
||||
|
||||
FROM REPO/alpine:3.19
|
||||
FROM alpine:3.19
|
||||
RUN mkdir /app
|
||||
WORKDIR /app/
|
||||
|
||||
|
|
2
Makefile
2
Makefile
|
@ -1,5 +1,5 @@
|
|||
APP_NAME = nomad-var-dirsync
|
||||
DOCKER_TAG ?= $(APP_NAME)-USER
|
||||
DOCKER_TAG ?= $(APP_NAME)-$(USER)
|
||||
VERSION ?= $(shell git describe --tags --dirty)
|
||||
GOFILES = *.go
|
||||
# Multi-arch targets are generated from this
|
||||
|
|
20
main.go
20
main.go
|
@ -45,6 +45,8 @@ func writeDir(client *nomad_api.Client, root string, sourceDir string) error {
|
|||
sanitizedPath := invalidPathChars.ReplaceAllString(path, "_")
|
||||
sanitizedPath = filepath.Join(root, sanitizedPath)
|
||||
|
||||
log.Printf("[INFO] Writing variable %s", sanitizedPath)
|
||||
|
||||
newVar := nomad_api.Variable{
|
||||
Path: sanitizedPath,
|
||||
Items: map[string]string{
|
||||
|
@ -76,11 +78,11 @@ func readDir(client *nomad_api.Client, root string, targetDir string, newDirPerm
|
|||
}
|
||||
|
||||
for _, varInfo := range vars {
|
||||
log.Printf("Reading variable %s", varInfo.Path)
|
||||
log.Printf("[INFO] Reading variable %s", varInfo.Path)
|
||||
|
||||
fileVar, _, err := client.Variables().Read(varInfo.Path, &nomad_api.QueryOptions{})
|
||||
if err != nil {
|
||||
log.Printf("Failed reading variable %s: %v", varInfo.Path, err)
|
||||
log.Printf("[ERROR] Failed reading variable %s: %v", varInfo.Path, err)
|
||||
}
|
||||
|
||||
filePath := filepath.Join(targetDir, fileVar.Items["path"])
|
||||
|
@ -125,35 +127,35 @@ func main() {
|
|||
target := flag.Arg(1)
|
||||
|
||||
if *root == "" {
|
||||
log.Fatal("Must provide a nomad variable root -root-var")
|
||||
log.Fatal("[ERROR] Must provide a nomad variable root -root-var")
|
||||
}
|
||||
|
||||
targetStat, err := os.Stat(target)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed reading target file `%s`. %v", target, err)
|
||||
log.Fatalf("[ERROR] Failed reading target file `%s`. %v", target, err)
|
||||
}
|
||||
|
||||
if !targetStat.IsDir() {
|
||||
log.Fatalf("must provide a path to a directory: %s", target)
|
||||
log.Fatalf("[ERROR] Must provide a path to a directory: %s", target)
|
||||
}
|
||||
|
||||
client, err := nomad_api.NewClient(&nomad_api.Config{
|
||||
SecretID: os.Getenv("NOMAD_TOKEN"),
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatalf("failed creating nomad client: %v", err)
|
||||
log.Fatalf("[ERROR] Failed creating nomad client: %v", err)
|
||||
}
|
||||
|
||||
switch action {
|
||||
case "write":
|
||||
if err = writeDir(client, *root, target); err != nil {
|
||||
log.Fatalf("Failed writing directory: %v", err)
|
||||
log.Fatalf("[ERROR] Failed writing directory: %v", err)
|
||||
}
|
||||
case "read":
|
||||
if err = readDir(client, *root, target, *newDirPerms); err != nil {
|
||||
log.Fatalf("Failed reading to files for path %v", err)
|
||||
log.Fatalf("[ERROR] Failed reading to files for path %v", err)
|
||||
}
|
||||
default:
|
||||
log.Fatalf("Expected action read or write, found %s", action)
|
||||
log.Fatalf("[ERROR] Expected action read or write, found %s", action)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue