podterminal/pods/skel.go

59 lines
1.1 KiB
Go
Raw Normal View History

2024-01-30 16:45:03 +01:00
package pods
import (
"log"
2024-01-30 21:24:31 +01:00
"net/http"
"net/url"
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-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-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
}
fileInfo, err := os.Stat(viper.GetString("skel"))
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()
params := url.Values{}
params.Set("path", viper.GetString("skel_target"))
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 {
log.Println("Error putting archive into container: ")
return err
2024-01-30 16:45:03 +01:00
}
2024-01-30 21:24:31 +01:00
log.Println(response.StatusCode, response.Body)
return response.Process(nil)
2024-01-30 16:45:03 +01:00
}