Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Fix podman file uploads #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ require (
github.com/opencontainers/go-digest v1.0.0-rc1 // indirect
github.com/opencontainers/image-spec v1.0.1 // indirect
github.com/stretchr/testify v1.4.0 // indirect
github.com/varlink/go v0.0.0-20191018142704-4ecdbb8a36c2
github.com/varlink/go v0.3.0
golang.org/x/net v0.0.0-20191014212845-da9a3fd4c582 // indirect
golang.org/x/sync v0.0.0-20190423024810-112230192c58
golang.org/x/time v0.0.0-20190921001708-c4c64cad1fd0 // indirect
Expand Down
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/varlink/go v0.0.0-20191018142704-4ecdbb8a36c2 h1:KcTzc0lm6PuIl9sAz5GMSGQyDhbJOa9NC3u69PVIc7Y=
github.com/varlink/go v0.0.0-20191018142704-4ecdbb8a36c2/go.mod h1:DKg9Y2ctoNkesREGAEak58l+jOC6JU2aqZvUYs5DynU=
github.com/varlink/go v0.3.0 h1:7IDKK8X3W9qqgw7oisE2RgtdTOxBxDwX5RDm2qVoKT8=
github.com/varlink/go v0.3.0/go.mod h1:DKg9Y2ctoNkesREGAEak58l+jOC6JU2aqZvUYs5DynU=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
Expand Down
69 changes: 45 additions & 24 deletions runtimes/podman/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,56 +4,77 @@ import (
"context"
"fmt"
"io"
"os"
"path/filepath"
"strings"

"github.com/uw-labs/podrick"
podman "github.com/uw-labs/podrick/runtimes/podman/iopodman"
"github.com/varlink/go/varlink"
)

func uploadFiles(ctx context.Context, conn *varlink.Connection, cID string, files ...podrick.File) (err error) {
mountDir, err := podman.MountContainer().Call(ctx, conn, cID)
if err != nil {
return fmt.Errorf("failed to mount container filesystem: %w", err)
}
defer func() {
uErr := podman.UnmountContainer().Call(context.Background(), conn, cID, true)
if err == nil {
err = uErr
}
}()

func uploadFiles(ctx context.Context, addr string, files ...podrick.File) (err error) {
for _, f := range files {
err = uploadFile(ctx, mountDir, f)
if err != nil {
return fmt.Errorf("failed to upload file: %w", err)
}
uploadFile(ctx, addr, f)
}

return nil
}

func uploadFile(ctx context.Context, mountDir string, file podrick.File) (err error) {
func uploadFile(ctx context.Context, addr string, file podrick.File) (err error) {
path := filepath.Clean(file.Path)
if !filepath.IsAbs(path) {
return fmt.Errorf("file paths must be absolute: %q", file.Path)
}
dest := filepath.Join(mountDir, path)
target, err := os.Create(dest)
//dest := filepath.Join(mountDir, path)

fConn, err := varlink.NewConnection(ctx, addr)
if err != nil {
return fmt.Errorf("failed to create file: %w", err)
return fmt.Errorf("failed to create new connection: %w", err)
}
defer func() {
cErr := target.Close()
cErr := fConn.Close()
if err == nil {
err = cErr
}
}()
_, err = io.Copy(target, file.Content)

reply, err := podman.SendFile().Upgrade(ctx, fConn, "", int64(file.Size))
if err != nil {
return fmt.Errorf("failed to copy file contents: %w", err)
return fmt.Errorf("failed to start connection upgrade: %w", err)
}

_, _, conn, err := reply(ctx)
if err != nil {
return fmt.Errorf("failed to upgrade connection: %w", err)
}

w := writerCtx{
WriterContext: conn,
ctx: ctx,
}

_, err = io.Copy(w, file.Content)
if err != nil {
return fmt.Errorf("failed to upload file: %w", err)
}

filename, err := conn.ReadBytes(ctx, ':')
if err != nil {
return fmt.Errorf("failed to read file name: %w", err)
}

podman.LoadImage().Call(ctx, fConn)

fmt.Println("Create file", strings.ReplaceAll(string(filename), ":", ""))

return nil
}

type writerCtx struct {
varlink.WriterContext
ctx context.Context
}

func (w writerCtx) Write(in []byte) (int, error) {
return w.WriterContext.Write(w.ctx, in)
}
Loading