podterminal/pods/skel.go

72 lines
1.5 KiB
Go
Raw Permalink Normal View History

2024-01-30 16:45:03 +01:00
package pods
import (
2024-01-31 13:12:43 +01:00
"fmt"
2024-01-30 16:45:03 +01:00
"log"
2024-01-30 21:24:31 +01:00
"net/http"
2024-01-30 19:41:10 +01:00
"os"
2024-01-30 16:45:03 +01:00
2024-01-30 21:24:31 +01:00
"github.com/containers/podman/v4/pkg/bindings"
2024-01-31 13:12:43 +01:00
"github.com/containers/podman/v4/pkg/bindings/containers"
2024-01-30 16:45:03 +01:00
"github.com/spf13/viper"
)
// CopySkelToContainer copies an Archive into the containers root
2024-01-30 16:45:03 +01:00
func CopySkelToContainer(id string) error {
2024-01-31 13:12:43 +01:00
chownBool := viper.GetBool("skel_chown")
2024-01-30 19:41:10 +01:00
log.Println("Copying skel Archive to container")
2024-01-30 16:45:03 +01:00
var err error
2024-01-30 21:24:31 +01:00
conn, err := bindings.GetClient(Socket)
2024-01-30 16:45:03 +01:00
if err != nil {
return err
}
2024-01-31 13:12:43 +01:00
options := containers.CopyOptions{
Chown: &chownBool,
}
fileInfo, err := os.Stat(viper.GetString("skel"))
2024-01-31 13:12:43 +01:00
2024-01-30 19:41:10 +01:00
if err != nil {
log.Println("Could not access Skel archive")
2024-01-30 19:41:10 +01:00
return err
2024-01-30 16:45:03 +01:00
}
2024-01-30 21:24:31 +01:00
log.Println("Skel Archive is ", fileInfo.Size(), "b")
2024-01-30 16:45:03 +01:00
file, err := os.Open(viper.GetString("skel"))
if err != nil {
log.Println("Could not open skel archive", err)
return err
}
2024-01-30 21:24:31 +01:00
defer file.Close()
2024-01-31 13:12:43 +01:00
params, err := options.ToParams()
if err != nil {
log.Println("Could not parse skel options", err)
return err
}
params.Set("path", viper.GetString("skel_target"))
2024-01-30 21:24:31 +01:00
2024-01-31 13:12:43 +01:00
fmt.Println("Container", id, " Params: ", params.Encode())
2024-01-30 21:24:31 +01:00
response, err := conn.DoRequest(
Socket,
file,
http.MethodPut,
"/containers/%s/archive",
params,
nil,
id,
)
2024-01-30 16:45:03 +01:00
if err != nil {
2024-01-30 19:41:10 +01:00
return err
2024-01-30 16:45:03 +01:00
}
2024-01-30 21:24:31 +01:00
if response.StatusCode != http.StatusOK {
2024-01-31 13:12:43 +01:00
log.Println("Error putting archive into container: ", response.StatusCode, response.Body)
2024-01-30 21:24:31 +01:00
return err
2024-01-30 16:45:03 +01:00
}
2024-01-30 21:24:31 +01:00
log.Println(response.StatusCode, response.Body)
2024-01-31 13:12:43 +01:00
// Manually chown the Directory, because podmans chown is sometimes unreliable
2024-01-30 21:24:31 +01:00
return response.Process(nil)
2024-01-30 16:45:03 +01:00
}