2021-08-02 01:10:41 +00:00
|
|
|
package app
|
|
|
|
|
2021-08-29 11:41:29 +00:00
|
|
|
import (
|
2022-01-18 13:13:20 +00:00
|
|
|
"context"
|
2021-12-28 00:08:44 +00:00
|
|
|
"fmt"
|
2021-08-29 11:41:29 +00:00
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"coopcloud.tech/abra/cli/internal"
|
2021-12-11 23:17:39 +00:00
|
|
|
"coopcloud.tech/abra/pkg/autocomplete"
|
2021-12-28 00:08:44 +00:00
|
|
|
"coopcloud.tech/abra/pkg/client"
|
|
|
|
"coopcloud.tech/abra/pkg/config"
|
|
|
|
"coopcloud.tech/abra/pkg/container"
|
2021-12-28 00:24:23 +00:00
|
|
|
"coopcloud.tech/abra/pkg/formatter"
|
2023-07-26 06:16:07 +00:00
|
|
|
"coopcloud.tech/abra/pkg/runtime"
|
2021-12-28 00:08:44 +00:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/filters"
|
2023-01-31 15:09:09 +00:00
|
|
|
dockerClient "github.com/docker/docker/client"
|
2021-12-28 00:08:44 +00:00
|
|
|
"github.com/docker/docker/pkg/archive"
|
2021-08-29 11:41:29 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2022-01-18 13:13:20 +00:00
|
|
|
"github.com/urfave/cli"
|
2021-08-29 11:41:29 +00:00
|
|
|
)
|
2021-08-02 01:10:41 +00:00
|
|
|
|
2022-01-18 13:13:20 +00:00
|
|
|
var appCpCommand = cli.Command{
|
2021-08-02 01:10:41 +00:00
|
|
|
Name: "cp",
|
2021-09-04 23:34:56 +00:00
|
|
|
Aliases: []string{"c"},
|
2022-01-25 12:48:04 +00:00
|
|
|
ArgsUsage: "<domain> <src> <dst>",
|
2022-01-18 13:13:20 +00:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
internal.DebugFlag,
|
|
|
|
internal.NoInputFlag,
|
2023-07-26 06:16:07 +00:00
|
|
|
internal.OfflineFlag,
|
2022-01-18 13:13:20 +00:00
|
|
|
},
|
|
|
|
Before: internal.SubCommandBefore,
|
|
|
|
Usage: "Copy files to/from a running app service",
|
2021-11-13 21:49:53 +00:00
|
|
|
Description: `
|
2022-05-13 14:44:49 +00:00
|
|
|
Copy files to and from any app service file system.
|
2021-11-13 21:49:53 +00:00
|
|
|
|
|
|
|
If you want to copy a myfile.txt to the root of the app service:
|
|
|
|
|
2022-01-25 12:48:04 +00:00
|
|
|
abra app cp <domain> myfile.txt app:/
|
2021-11-13 21:49:53 +00:00
|
|
|
|
|
|
|
And if you want to copy that file back to your current working directory locally:
|
|
|
|
|
2022-01-25 12:48:04 +00:00
|
|
|
abra app cp <domain> app:/myfile.txt .
|
2021-11-13 21:49:53 +00:00
|
|
|
`,
|
2021-08-29 11:41:29 +00:00
|
|
|
Action: func(c *cli.Context) error {
|
2023-07-26 06:16:07 +00:00
|
|
|
conf := runtime.New(runtime.WithOffline(internal.Offline))
|
|
|
|
app := internal.ValidateApp(c, conf)
|
2021-08-29 11:41:29 +00:00
|
|
|
|
2023-01-31 15:09:09 +00:00
|
|
|
cl, err := client.New(app.Server)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-08-29 11:41:29 +00:00
|
|
|
src := c.Args().Get(1)
|
|
|
|
dst := c.Args().Get(2)
|
|
|
|
if src == "" {
|
|
|
|
logrus.Fatal("missing <src> argument")
|
|
|
|
} else if dst == "" {
|
|
|
|
logrus.Fatal("missing <dest> argument")
|
|
|
|
}
|
|
|
|
|
|
|
|
parsedSrc := strings.SplitN(src, ":", 2)
|
|
|
|
parsedDst := strings.SplitN(dst, ":", 2)
|
|
|
|
errorMsg := "one of <src>/<dest> arguments must take $SERVICE:$PATH form"
|
|
|
|
if len(parsedSrc) == 2 && len(parsedDst) == 2 {
|
|
|
|
logrus.Fatal(errorMsg)
|
|
|
|
} else if len(parsedSrc) != 2 {
|
|
|
|
if len(parsedDst) != 2 {
|
|
|
|
logrus.Fatal(errorMsg)
|
|
|
|
}
|
|
|
|
} else if len(parsedDst) != 2 {
|
|
|
|
if len(parsedSrc) != 2 {
|
|
|
|
logrus.Fatal(errorMsg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var service string
|
|
|
|
var srcPath string
|
|
|
|
var dstPath string
|
|
|
|
isToContainer := false // <container:src> <dst>
|
|
|
|
if len(parsedSrc) == 2 {
|
|
|
|
service = parsedSrc[0]
|
|
|
|
srcPath = parsedSrc[1]
|
|
|
|
dstPath = dst
|
2021-09-10 22:54:02 +00:00
|
|
|
logrus.Debugf("assuming transfer is coming FROM the container")
|
2021-08-29 11:41:29 +00:00
|
|
|
} else if len(parsedDst) == 2 {
|
|
|
|
service = parsedDst[0]
|
|
|
|
dstPath = parsedDst[1]
|
|
|
|
srcPath = src
|
|
|
|
isToContainer = true // <src> <container:dst>
|
2021-09-10 22:54:02 +00:00
|
|
|
logrus.Debugf("assuming transfer is going TO the container")
|
2021-08-29 11:41:29 +00:00
|
|
|
}
|
|
|
|
|
2021-11-13 21:50:14 +00:00
|
|
|
if !isToContainer {
|
|
|
|
if _, err := os.Stat(dstPath); os.IsNotExist(err) {
|
|
|
|
logrus.Fatalf("%s does not exist locally?", dstPath)
|
|
|
|
}
|
|
|
|
}
|
2023-01-31 15:09:09 +00:00
|
|
|
|
|
|
|
if err := configureAndCp(c, cl, app, srcPath, dstPath, service, isToContainer); err != nil {
|
2021-08-29 11:41:29 +00:00
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
2021-11-15 15:48:23 +00:00
|
|
|
|
2023-01-31 15:09:09 +00:00
|
|
|
return nil
|
2021-08-29 11:41:29 +00:00
|
|
|
},
|
2021-12-11 23:17:39 +00:00
|
|
|
BashComplete: autocomplete.AppNameComplete,
|
2021-08-02 01:10:41 +00:00
|
|
|
}
|
2021-12-28 00:08:44 +00:00
|
|
|
|
|
|
|
func configureAndCp(
|
|
|
|
c *cli.Context,
|
2023-01-31 15:09:09 +00:00
|
|
|
cl *dockerClient.Client,
|
2021-12-28 00:08:44 +00:00
|
|
|
app config.App,
|
|
|
|
srcPath string,
|
|
|
|
dstPath string,
|
|
|
|
service string,
|
|
|
|
isToContainer bool) error {
|
|
|
|
filters := filters.NewArgs()
|
2022-03-27 10:40:05 +00:00
|
|
|
filters.Add("name", fmt.Sprintf("^%s_%s", app.StackName(), service))
|
2021-12-28 00:08:44 +00:00
|
|
|
|
2022-01-25 10:39:38 +00:00
|
|
|
container, err := container.GetContainer(context.Background(), cl, filters, internal.NoInput)
|
2021-12-28 00:08:44 +00:00
|
|
|
if err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
logrus.Debugf("retrieved %s as target container on %s", formatter.ShortenID(container.ID), app.Server)
|
|
|
|
|
|
|
|
if isToContainer {
|
|
|
|
if _, err := os.Stat(srcPath); err != nil {
|
|
|
|
logrus.Fatalf("%s does not exist?", srcPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
toTarOpts := &archive.TarOptions{NoOverwriteDirNonDir: true, Compression: archive.Gzip}
|
|
|
|
content, err := archive.TarWithOptions(srcPath, toTarOpts)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
copyOpts := types.CopyToContainerOptions{AllowOverwriteDirWithFile: false, CopyUIDGID: false}
|
2022-01-18 13:13:20 +00:00
|
|
|
if err := cl.CopyToContainer(context.Background(), container.ID, dstPath, content, copyOpts); err != nil {
|
2021-12-28 00:08:44 +00:00
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
} else {
|
2022-01-18 13:13:20 +00:00
|
|
|
content, _, err := cl.CopyFromContainer(context.Background(), container.ID, srcPath)
|
2021-12-28 00:08:44 +00:00
|
|
|
if err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
defer content.Close()
|
|
|
|
fromTarOpts := &archive.TarOptions{NoOverwriteDirNonDir: true, Compression: archive.Gzip}
|
|
|
|
if err := archive.Untar(content, dstPath, fromTarOpts); err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2022-01-25 10:39:38 +00:00
|
|
|
|
2021-12-28 00:08:44 +00:00
|
|
|
return nil
|
|
|
|
}
|