Skip to content

Commit

Permalink
feat(docker): Mount base dir into the container (#113)
Browse files Browse the repository at this point in the history
Signed-off-by: Ce Gao <cegao@tensorchord.ai>
  • Loading branch information
gaocegege committed May 7, 2022
1 parent 3c28ffb commit aeb846e
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 7 deletions.
24 changes: 24 additions & 0 deletions pkg/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ import (
"github.com/cockroachdb/errors"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/mount"
"github.com/docker/docker/client"
"github.com/docker/go-connections/nat"
"github.com/sirupsen/logrus"
"github.com/tensorchord/MIDI/pkg/editor/jupyter"
"github.com/tensorchord/MIDI/pkg/lang/ir"
"github.com/tensorchord/MIDI/pkg/util/fileutil"
)

var (
Expand Down Expand Up @@ -170,8 +172,30 @@ func (c generalClient) StartMIDI(ctx context.Context, tag, name string,
},
ExposedPorts: nat.PortSet{},
}
path, err := fileutil.CWD()
if err != nil {
return "", "", errors.Wrap(err, "failed to get current working directory")
}
base, err := fileutil.RootDir()
if err != nil {
return "", "", errors.Wrap(err, "failed to get root directory")
}
base = fmt.Sprintf("/root/%s", base)
config.WorkingDir = base

logger.WithFields(logrus.Fields{
"mount-path": path,
"working-dir": base,
}).Debug("setting up container working directory")
hostConfig := &container.HostConfig{
PortBindings: nat.PortMap{},
Mounts: []mount.Mount{
{
Type: mount.TypeBind,
Source: path,
Target: base,
},
},
}
// TODO(gaocegege): Avoid specific logic to set the port.
if g.JupyterConfig != nil {
Expand Down
13 changes: 13 additions & 0 deletions pkg/util/fileutil/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package fileutil

import (
"os"
"path/filepath"

"github.com/cockroachdb/errors"
)
Expand Down Expand Up @@ -45,3 +46,15 @@ func DirExists(filename string) (bool, error) {
}
return info.IsDir(), nil
}

func CWD() (string, error) {
return os.Getwd()
}

func RootDir() (string, error) {
cwd, err := CWD()
if err != nil {
return "", err
}
return filepath.Base(cwd), nil
}
22 changes: 15 additions & 7 deletions pkg/vscode/vsocde.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (

"github.com/tensorchord/MIDI/pkg/home"
"github.com/tensorchord/MIDI/pkg/unzip"
"github.com/tensorchord/MIDI/pkg/util/fileutil"
)

type Client interface {
Expand Down Expand Up @@ -53,9 +54,21 @@ func (c generalClient) DownloadOrCache(p Plugin) error {
url := fmt.Sprintf(vscodePackageURLTemplate,
p.Publisher, p.Publisher, p.Extension, p.Version)

// TODO(gaocegege): Cache the file.
filename := fmt.Sprintf("%s/%s.%s-%s.vsix", home.GetManager().CacheDir(),
p.Publisher, p.Extension, p.Version)
logger := logrus.WithFields(logrus.Fields{
"publisher": p.Publisher,
"extension": p.Extension,
"version": p.Version,
"url": url,
"file": filename,
})
if ok, err := fileutil.FileExists(filename); err != nil {
return err
} else if ok {
logger.Debug("vscode plugin is cached")
return nil
}
out, err := os.Create(filename)

if err != nil {
Expand All @@ -67,12 +80,7 @@ func (c generalClient) DownloadOrCache(p Plugin) error {
if err != nil {
return err
}
logrus.WithFields(logrus.Fields{
"publisher": p.Publisher,
"extension": p.Extension,
"version": p.Version,
"url": url,
}).Debugf("downloading vscode plugin")
logger.Debugf("downloading vscode plugin")

defer resp.Body.Close()
_, err = io.Copy(out, resp.Body)
Expand Down

0 comments on commit aeb846e

Please sign in to comment.