From c991ef7295309a87ed9bd4129715da7896c97616 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20B=C3=BClow?= Date: Tue, 30 Jan 2024 21:24:31 +0100 Subject: [PATCH] skel finally works \o/ --- .../machines/default/libvirt/created_networks | 1 + Vagrantfile | 1 + pods/manager.go | 13 ++++- pods/skel.go | 53 ++++++++++--------- 4 files changed, 40 insertions(+), 28 deletions(-) diff --git a/.vagrant/machines/default/libvirt/created_networks b/.vagrant/machines/default/libvirt/created_networks index a20a670..2ad5939 100644 --- a/.vagrant/machines/default/libvirt/created_networks +++ b/.vagrant/machines/default/libvirt/created_networks @@ -5,3 +5,4 @@ 5533801d-70e3-4c21-9942-82d20930c789 5533801d-70e3-4c21-9942-82d20930c789 5533801d-70e3-4c21-9942-82d20930c789 +5533801d-70e3-4c21-9942-82d20930c789 diff --git a/Vagrantfile b/Vagrantfile index fb65d43..7d3d565 100644 --- a/Vagrantfile +++ b/Vagrantfile @@ -116,6 +116,7 @@ Vagrant.configure("2") do |config| rm go.tar.gz echo 'export PATH=$PATH:/usr/local/go/bin:$HOME/go/bin' >> /etc/profile echo 'CGO_ENABLED=1' >>/etc/profile + echo 'alias cleanpods="podman stop -a && podman rm -a"' >> /etc/profile echo 'CGO_ENABLED=1' >>/root/.bashrc export PATH=$PATH:/usr/local/go/bin go install github.com/cosmtrek/air@latest diff --git a/pods/manager.go b/pods/manager.go index e21f3d4..7925d4c 100644 --- a/pods/manager.go +++ b/pods/manager.go @@ -3,6 +3,7 @@ package pods import ( "context" "log" + "net" "time" "github.com/containers/podman/v4/pkg/bindings" @@ -13,6 +14,7 @@ import ( ) var Socket context.Context +var rawSocket net.Conn func socketConnection() context.Context { uri := "unix:///run/podman/podman.sock" @@ -23,12 +25,19 @@ func socketConnection() context.Context { return conn } -func TestPodman() { - _ = socketConnection() +func rawConnection() net.Conn { + connection, err := net.Dial("unix", "unix:///run/podman/podman.sock") + if err != nil { + log.Println( + "Could not establish raw UNIX socket connection, certain features will not work properly", + ) + } + return connection } func ConnectSocket() { Socket = socketConnection() + rawSocket = rawConnection() } func PullImage() error { diff --git a/pods/skel.go b/pods/skel.go index e464571..5c4dfbd 100644 --- a/pods/skel.go +++ b/pods/skel.go @@ -1,54 +1,55 @@ package pods import ( - "bytes" "log" + "net/http" + "net/url" "os" - "github.com/containers/podman/v4/pkg/bindings/containers" + "github.com/containers/podman/v4/pkg/bindings" "github.com/spf13/viper" ) // CopySkelToContainer copies an Archive into the containers home directory // (Assumes the use of a Webtop container) func CopySkelToContainer(id string) error { - trueVar := true log.Println("Copying skel Archive to container") - var buf bytes.Buffer var err error - + conn, err := bindings.GetClient(Socket) + if err != nil { + return err + } file, err := os.Open(viper.GetString("skel")) if err != nil { log.Println("Could not open skel archive", err) return err } + + fileInfo, err := os.Stat(viper.GetString("skel")) + log.Println("Skel Archive is ", fileInfo.Size(), "b") + defer file.Close() - // Use 'FileInfo' to get the size of the file - fileInfo, err := file.Stat() + params := url.Values{} + params.Set("path", "/config") + + response, err := conn.DoRequest( + Socket, + file, + http.MethodPut, + "/containers/%s/archive", + params, + nil, + id, + ) if err != nil { - log.Println("Could not get size of skel archive", err) return err } - // 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) + if response.StatusCode != http.StatusOK { + log.Println("Error putting archive into container: ") return err } - - log.Println("Writing file from buffer to container") - copyOptions := &containers.CopyOptions{ - Chown: &trueVar, - } - _, err = containers.CopyFromArchiveWithOptions(Socket, id, "/config/", &buf, copyOptions) - if err != nil { - log.Print("Error copying to Container: ", err) - } - - return err + log.Println(response.StatusCode, response.Body) + return response.Process(nil) }