2024-01-30 16:45:03 +01:00
|
|
|
package pods
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"log"
|
2024-01-30 19:41:10 +01:00
|
|
|
"os"
|
2024-01-30 16:45:03 +01:00
|
|
|
|
|
|
|
"github.com/containers/podman/v4/pkg/bindings/containers"
|
|
|
|
"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 {
|
2024-01-30 19:41:10 +01:00
|
|
|
trueVar := true
|
|
|
|
log.Println("Copying skel Archive to container")
|
2024-01-30 16:45:03 +01:00
|
|
|
var buf bytes.Buffer
|
|
|
|
var err error
|
2024-01-30 19:41:10 +01:00
|
|
|
|
|
|
|
file, err := os.Open(viper.GetString("skel"))
|
2024-01-30 16:45:03 +01:00
|
|
|
if err != nil {
|
2024-01-30 19:41:10 +01:00
|
|
|
log.Println("Could not open skel archive", err)
|
2024-01-30 16:45:03 +01:00
|
|
|
return err
|
|
|
|
}
|
2024-01-30 19:41:10 +01:00
|
|
|
defer file.Close()
|
2024-01-30 16:45:03 +01:00
|
|
|
|
2024-01-30 19:41:10 +01:00
|
|
|
// 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
|
2024-01-30 16:45:03 +01:00
|
|
|
}
|
|
|
|
|
2024-01-30 19:41:10 +01:00
|
|
|
// Create a buffer with the size of the file
|
|
|
|
buffer := bytes.NewBuffer(make([]byte, 0, fileInfo.Size()))
|
2024-01-30 16:45:03 +01:00
|
|
|
|
2024-01-30 19:41:10 +01:00
|
|
|
// Copy the file contents to the buffer
|
|
|
|
_, err = buffer.ReadFrom(file)
|
2024-01-30 16:45:03 +01:00
|
|
|
if err != nil {
|
2024-01-30 19:41:10 +01:00
|
|
|
log.Println("Could not get read in skel archive", err)
|
|
|
|
return err
|
2024-01-30 16:45:03 +01:00
|
|
|
}
|
|
|
|
|
2024-01-30 19:41:10 +01:00
|
|
|
log.Println("Writing file from buffer to container")
|
|
|
|
copyOptions := &containers.CopyOptions{
|
|
|
|
Chown: &trueVar,
|
|
|
|
}
|
|
|
|
_, err = containers.CopyFromArchiveWithOptions(Socket, id, "/config/", &buf, copyOptions)
|
2024-01-30 16:45:03 +01:00
|
|
|
if err != nil {
|
2024-01-30 19:41:10 +01:00
|
|
|
log.Print("Error copying to Container: ", err)
|
2024-01-30 16:45:03 +01:00
|
|
|
}
|
2024-01-30 19:41:10 +01:00
|
|
|
|
|
|
|
return err
|
2024-01-30 16:45:03 +01:00
|
|
|
}
|