2024-01-30 16:45:03 +01:00
|
|
|
package pods
|
|
|
|
|
|
|
|
import (
|
2024-02-01 17:32:46 +01:00
|
|
|
"bytes"
|
2024-01-31 13:12:43 +01:00
|
|
|
"fmt"
|
2024-02-01 17:32:46 +01:00
|
|
|
"io"
|
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-02-01 17:32:46 +01:00
|
|
|
"strings"
|
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"
|
|
|
|
)
|
|
|
|
|
2024-01-31 08:21:43 +01:00
|
|
|
// 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,
|
|
|
|
}
|
2024-01-31 09:05:13 +01:00
|
|
|
|
|
|
|
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 {
|
2024-01-31 09:05:13 +01:00
|
|
|
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
|
|
|
|
2024-01-31 09:05:13 +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
|
|
|
|
}
|
2024-01-31 09:05:13 +01:00
|
|
|
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-02-01 17:32:46 +01:00
|
|
|
if viper.GetString("skel_user") != "" {
|
|
|
|
chownSkel(id)
|
|
|
|
|
|
|
|
}
|
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
|
|
|
}
|
2024-02-01 17:32:46 +01:00
|
|
|
|
|
|
|
func chownSkel(id string) {
|
|
|
|
command := fmt.Sprintf(
|
|
|
|
"chown -R %s %s \n\n",
|
|
|
|
viper.GetString("skel_user"),
|
|
|
|
viper.GetString("skel_target"),
|
|
|
|
)
|
|
|
|
done := make(chan bool)
|
|
|
|
detachKeys := "\n\n"
|
|
|
|
commandReader := strings.NewReader(command)
|
|
|
|
var output bytes.Buffer
|
|
|
|
outputWriter := io.Writer(&output)
|
|
|
|
options := &containers.AttachOptions{
|
|
|
|
DetachKeys: &detachKeys,
|
|
|
|
}
|
|
|
|
|
|
|
|
err := containers.Attach(Socket, id, commandReader, outputWriter, outputWriter, done, options)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error: %v \n %s\n", err, output.String())
|
|
|
|
} else {
|
|
|
|
log.Println(output.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|