From 7361f5a80523fc0968ac355a01fe32c0f0f1c39c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20B=C3=BClow?= Date: Tue, 30 Jan 2024 19:41:10 +0100 Subject: [PATCH] simple skel no work :c --- .../machines/default/libvirt/created_networks | 2 + Makefile | 6 +- pods/skel.go | 81 +++++++------------ readConfig.go | 2 +- 4 files changed, 36 insertions(+), 55 deletions(-) diff --git a/.vagrant/machines/default/libvirt/created_networks b/.vagrant/machines/default/libvirt/created_networks index 2df3177..a20a670 100644 --- a/.vagrant/machines/default/libvirt/created_networks +++ b/.vagrant/machines/default/libvirt/created_networks @@ -3,3 +3,5 @@ 5533801d-70e3-4c21-9942-82d20930c789 5533801d-70e3-4c21-9942-82d20930c789 5533801d-70e3-4c21-9942-82d20930c789 +5533801d-70e3-4c21-9942-82d20930c789 +5533801d-70e3-4c21-9942-82d20930c789 diff --git a/Makefile b/Makefile index 137d6e0..10388de 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ -.PHONY: release dev deps install +.PHONY: release dev deps install skel release: deps CGO_ENABLED=1 go build -buildvcs=true . @@ -22,3 +22,7 @@ install: cp -n podterminal.service /etc/systemd/system/ systemctl daemon-reload systemctl enable --now podterminal + +skel: + echo "Taring up /etc/podterminal/skel/" + tar -czvf /etc/podterminal/skel.tar.gz -C /etc/podterminal/skel . diff --git a/pods/skel.go b/pods/skel.go index acc34da..e464571 100644 --- a/pods/skel.go +++ b/pods/skel.go @@ -2,78 +2,53 @@ package pods import ( "bytes" - "io" "log" + "os" - buildahCopyah "github.com/containers/buildah/copier" "github.com/containers/podman/v4/pkg/bindings/containers" - "github.com/containers/podman/v4/pkg/copy" "github.com/spf13/viper" - "golang.org/x/sync/errgroup" ) // CopySkelToContainer copies an Archive into the containers home directory // (Assumes the use of a Webtop container) -// This Function is heavily inspired by https://github.com/containers/podman/blob/main/cmd/podman/containers/cp.go#L337 func CopySkelToContainer(id string) error { - log.Println("Copying skel directory to container") - var errorGroup errgroup.Group + trueVar := true + log.Println("Copying skel Archive to container") var buf bytes.Buffer var err error - _, err = copy.ResolveHostPath(viper.GetString("skel")) + + file, err := os.Open(viper.GetString("skel")) if err != nil { - log.Println("Could not find skel Directory", err) + log.Println("Could not open skel archive", err) return err } - reader, writer := io.Pipe() + defer file.Close() - // Create Function for copying from host to pipe - hostCopy := func() error { - getOptions := buildahCopyah.GetOptions{ - KeepDirectoryNames: true, - } - err := buildahCopyah.Get("/", "", getOptions, []string{viper.GetString("skel")}, &buf) - if err != nil { - log.Print("Error copying from Host: ", err) - } - - //defer closeAndLogWriter(writer) - return nil + // Use 'FileInfo' to get the size of the file + fileInfo, err := file.Stat() + if err != nil { + log.Println("Could not get size of skel archive", err) + return err } - // create function to copy from pipe to container - containerCopy := func() error { - _, err := containers.CopyFromArchive(Socket, id, "/config/", &buf) - if err != nil { - log.Print("Error copying to Container: ", err) - } - //defer closeAndLogReader(reader) - return nil + // Create a buffer with the size of the file + buffer := bytes.NewBuffer(make([]byte, 0, fileInfo.Size())) + + // Copy the file contents to the buffer + _, err = buffer.ReadFrom(file) + if err != nil { + log.Println("Could not get read in skel archive", err) + return err } - errorGroup.Go(hostCopy) - errorGroup.Go(containerCopy) - - if err := errorGroup.Wait(); err != nil { - log.Println("Could not Copy Skel", err) + log.Println("Writing file from buffer to container") + copyOptions := &containers.CopyOptions{ + Chown: &trueVar, } - closeAndLogReader(reader) - closeAndLogWriter(writer) + _, err = containers.CopyFromArchiveWithOptions(Socket, id, "/config/", &buf, copyOptions) + if err != nil { + log.Print("Error copying to Container: ", err) + } + return err } - -// closeAndLogReader takes a reader and closes it, logging any error -func closeAndLogReader(reader *io.PipeReader) { - err := reader.Close() - if err != nil { - log.Println("Could not close reader: ", err) - } -} - -// closeAndLogWriter takes a writer and closes it, logging any error -func closeAndLogWriter(writer *io.PipeWriter) { - err := writer.Close() - if err != nil { - log.Println("Could not close writer: ", err) - } -} diff --git a/readConfig.go b/readConfig.go index f5dc6f4..3adb0ef 100644 --- a/readConfig.go +++ b/readConfig.go @@ -16,7 +16,7 @@ func readConfigFile() { viper.SetDefault("maxAge", 10800) viper.SetDefault("dri", false) viper.SetDefault("dir_node", "/dev/dri/renderD128") - viper.SetDefault("skel", "/etc/podterminal/skel/") + viper.SetDefault("skel", "/etc/podterminal/skel.tar.gz") viper.SetDefault("block_filebrowser", false) viper.SetDefault("envvars", map[string]string{