Skip to content

Commit 6861e4a

Browse files
authored
Update writefile (#153)
## Description Upgrade to Go 1.24. Use structured logging. Unmount disk device after use. ## Why is this needed Fixes: # ## How Has This Been Tested? ## How are existing users impacted? What migration steps/scripts do we need? ## Checklist: I have: - [ ] updated the documentation and/or roadmap (if required) - [ ] added unit or e2e tests - [ ] provided instructions on how to upgrade
2 parents 2ceabe4 + cc2dd88 commit 6861e4a

File tree

2 files changed

+39
-21
lines changed

2 files changed

+39
-21
lines changed

writefile/Dockerfile

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
# syntax=docker/dockerfile:1
22

3-
FROM golang:1.21-alpine AS writefile
3+
FROM golang:1.24-alpine AS writefile
44
RUN apk add --no-cache git ca-certificates gcc musl-dev
55
COPY . /src
66
WORKDIR /src/writefile
7-
RUN --mount=type=cache,sharing=locked,id=gomod,target=/go/pkg/mod/cache \
8-
--mount=type=cache,sharing=locked,id=goroot,target=/root/.cache/go-build \
9-
CGO_ENABLED=1 GOOS=linux go build -a -ldflags "-linkmode external -extldflags '-static' -s -w" -o writefile
7+
RUN CGO_ENABLED=1 GOOS=linux go build -a -ldflags "-linkmode external -extldflags '-static' -s -w" -o writefile
108

119
FROM scratch
1210
# Add Certificates into the image, for anything that does HTTPS calls

writefile/main.go

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"errors"
55
"fmt"
6+
"log/slog"
67
"os"
78
"path/filepath"
89
"strconv"
@@ -15,7 +16,8 @@ import (
1516
const mountAction = "/mountAction"
1617

1718
func main() {
18-
fmt.Printf("WriteFile - Write file to disk\n------------------------\n")
19+
logger := slog.New(slog.NewTextHandler(os.Stdout, nil))
20+
logger.Info("WriteFile - Write file to a disk device")
1921

2022
blockDevice := os.Getenv("DEST_DISK")
2123
filesystemType := os.Getenv("FS_TYPE")
@@ -29,69 +31,87 @@ func main() {
2931

3032
// Validate inputs
3133
if blockDevice == "" {
32-
log.Fatalf("No Block Device speified with Environment Variable [DEST_DISK]")
34+
logger.Error("No Block Device speified with Environment Variable [DEST_DISK]")
35+
os.Exit(1)
3336
}
3437

3538
if !filepath.IsAbs(filePath) {
36-
log.Fatal("Provide path must be an absolute path")
39+
logger.Error("Provide path must be an absolute path")
40+
os.Exit(1)
3741
}
3842

3943
modePrime, err := strconv.ParseUint(mode, 8, 32)
4044
if err != nil {
41-
log.Fatalf("Could not parse mode: %v", err)
45+
logger.Error("Could not parse mode", "error", err)
46+
os.Exit(1)
4247
}
4348

4449
fileMode := os.FileMode(modePrime)
4550

4651
dirModePrime, err := strconv.ParseUint(dirMode, 8, 32)
4752
if err != nil {
48-
log.Fatalf("Could not parse dirmode: %v", err)
53+
logger.Error("Could not parse dirmode", "error", err)
54+
os.Exit(1)
4955
}
5056

5157
newDirMode := os.FileMode(dirModePrime)
5258

5359
fileUID, err := strconv.Atoi(uid)
5460
if err != nil {
55-
log.Fatalf("Could not parse uid: %v", err)
61+
logger.Error("Could not parse uid", "error", err)
62+
os.Exit(1)
5663
}
5764

5865
fileGID, err := strconv.Atoi(gid)
5966
if err != nil {
60-
log.Fatalf("Could not parse gid: %v", err)
67+
logger.Error("Could not parse gid", "error", err)
68+
os.Exit(1)
6169
}
6270

6371
dirPath, fileName := filepath.Split(filePath)
6472
if len(fileName) == 0 {
65-
log.Fatal("Provide path must include a file component")
73+
logger.Error("Provide path must include a file component")
74+
os.Exit(1)
6675
}
6776

6877
// Create the /mountAction mountpoint (no folders exist previously in scratch container)
6978
if err := os.Mkdir(mountAction, os.ModeDir); err != nil {
70-
log.Fatalf("Error creating the action Mountpoint [%s]", mountAction)
79+
logger.Error("Error creating the action Mountpoint", "mountAction", mountAction, "error", err)
80+
os.Exit(1)
7181
}
7282

7383
// Mount the block device to the /mountAction point
7484
if err := syscall.Mount(blockDevice, mountAction, filesystemType, 0, ""); err != nil {
75-
log.Fatalf("Mounting [%s] -> [%s] error [%v]", blockDevice, mountAction, err)
76-
}
77-
78-
log.Infof("Mounted [%s] -> [%s]", blockDevice, mountAction)
85+
logger.Error("Mounting block device", "blockDevice", blockDevice, "mountAction", mountAction, "error", err)
86+
os.Exit(1)
87+
}
88+
defer func() {
89+
if err := syscall.Unmount(mountAction, 0); err != nil {
90+
logger.Error("Error unmounting device", "source", blockDevice, "destination", mountAction, "error", err)
91+
} else {
92+
logger.Info("Unmounted device successfully", "source", blockDevice, "destination", mountAction)
93+
}
94+
}()
95+
logger.Info("Mounted device successfully", "source", blockDevice, "destination", mountAction)
7996

8097
if err := recursiveEnsureDir(mountAction, dirPath, newDirMode, fileUID, fileGID); err != nil {
81-
log.Fatalf("Failed to ensure directory exists: %v", err)
98+
logger.Error("Failed to ensure directory exists", "error", err)
99+
os.Exit(1)
82100
}
83101

84102
fqFilePath := filepath.Join(mountAction, filePath)
85103
// Write the file to disk
86104
if err := os.WriteFile(fqFilePath, []byte(contents), fileMode); err != nil {
87-
log.Fatalf("Could not write file %s: %v", filePath, err)
105+
logger.Error("Could not write file", "filePath", filePath, "error", err)
106+
os.Exit(1)
88107
}
89108

90109
if err := os.Chown(fqFilePath, fileUID, fileGID); err != nil {
91-
log.Fatalf("Could not modify ownership of file %s: %v", filePath, err)
110+
logger.Error("Could not modify ownership of file", "filePath", filePath, "error", err)
111+
os.Exit(1)
92112
}
93113

94-
log.Infof("Successfully wrote file [%s] to device [%s]", filePath, blockDevice)
114+
logger.Info("Successfully wrote file", "filePath", filePath, "blockDevice", blockDevice)
95115
}
96116

97117
func dirExists(mountPath, path string) (bool, error) {

0 commit comments

Comments
 (0)