Skip to content

Commit

Permalink
Move copyfiles function to client-go
Browse files Browse the repository at this point in the history
Resolves #443

This PR will remove dependency of oc binary for `copyfiles` function which copies file to component while `odo push`
  • Loading branch information
surajnarwade committed Jul 17, 2018
1 parent d2ab498 commit 844ddf3
Show file tree
Hide file tree
Showing 6 changed files with 198 additions and 260 deletions.
14 changes: 10 additions & 4 deletions cmd/push.go
Expand Up @@ -10,6 +10,8 @@ import (
"github.com/redhat-developer/odo/pkg/component"
"github.com/redhat-developer/odo/pkg/project"

"path/filepath"

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -77,12 +79,16 @@ var pushCmd = &cobra.Command{
checkError(err, "")
}

var asFile bool
if sourceType == "binary" {
asFile = true
if sourceType == "local" {
log.Debugf("Copying directory %s to pod", u.Path)
err = component.PushLocal(client, componentName, applicationName, u.Path, os.Stdout, []string{})
} else {
dir := filepath.Dir(u.Path)
log.Debugf("Copying file %s to pod", u.Path)
err = component.PushLocal(client, componentName, applicationName, dir, os.Stdout, []string{u.Path})
}
err = component.PushLocal(client, componentName, applicationName, u.Path, asFile, os.Stdout)
checkError(err, fmt.Sprintf("failed to push component: %v", componentName))

case "git":
// currently we don't support changing build type
// it doesn't make sense to use --dir with git build
Expand Down
15 changes: 2 additions & 13 deletions cmd/watch.go
Expand Up @@ -46,7 +46,7 @@ var watchCmd = &cobra.Command{
componentName = args[0]
}

sourceType, sourcePath, err := component.GetComponentSource(client, componentName, applicationName, projectName)
_, sourcePath, err := component.GetComponentSource(client, componentName, applicationName, projectName)
checkError(err, "Unable to get source for %s component.", componentName)

u, err := url.Parse(sourcePath)
Expand All @@ -58,18 +58,7 @@ var watchCmd = &cobra.Command{
}
watchPath := u.Path

var asFile bool
switch sourceType {
case "binary":
asFile = true
case "local":
asFile = false
default:
fmt.Printf("Watching component that has source type %s is not supported.", sourceType)
os.Exit(1)
}

err = component.WatchAndPush(client, componentName, applicationName, watchPath, asFile, stdout)
err = component.WatchAndPush(client, componentName, applicationName, watchPath, stdout)
checkError(err, "Error while trying to watch %s", watchPath)
},
}
Expand Down
18 changes: 4 additions & 14 deletions pkg/component/component.go
Expand Up @@ -165,14 +165,10 @@ func GetCurrent(client *occlient.Client, applicationName string, projectName str
}

// PushLocal push local code to the cluster and trigger build there.
// asFile indicates if it is a binary component or not
func PushLocal(client *occlient.Client, componentName string, applicationName string, path string, asFile bool, out io.Writer) error {
// files is list of changed files captured during `odo watch` or binary file
func PushLocal(client *occlient.Client, componentName string, applicationName string, path string, out io.Writer, files []string) error {
const targetPath = "/opt/app-root/src"

if !asFile {
// We need to make sure that there is a '/' at the end, otherwise rsync will sync files to wrong directory
path = fmt.Sprintf("%s/", path)
}
// Find DeploymentConfig for component
componentLabels := componentlabels.GetLabels(componentName, applicationName, false)
componentSelector := util.ConvertLabelsToSelector(componentLabels)
Expand All @@ -187,17 +183,11 @@ func PushLocal(client *occlient.Client, componentName string, applicationName st
if err != nil {
return errors.Wrapf(err, "error while waiting for pod %s", podSelector)
}
var syncOutput string
if !asFile {
syncOutput, err = client.RsyncPath(path, pod.Name, targetPath)
} else {
syncOutput, err = client.CopyFile(path, pod.Name, targetPath)

}
log.Debugf("Copying to pod %s", pod.Name)
err = client.CopyFile(path, pod.Name, targetPath, files)
if err != nil {
return errors.Wrap(err, "unable push files to pod")
}
fmt.Fprintf(out, syncOutput)
fmt.Fprintf(out, "Please wait, building component....\n")

// use pipes to write output from ExecCMDInContainer in yellow to 'out' io.Writer
Expand Down
16 changes: 14 additions & 2 deletions pkg/component/watch.go
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/redhat-developer/odo/pkg/occlient"

"github.com/fsnotify/fsnotify"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -77,7 +78,7 @@ func addRecursiveWatch(watcher *fsnotify.Watcher, path string, ignores []string)
// WatchAndPush watches path, if something changes in that path it calls PushLocal
// ignores .git/* by default
// inspired by https://github.com/openshift/origin/blob/e785f76194c57bd0e1674c2f2776333e1e0e4e78/pkg/oc/cli/cmd/rsync/rsync.go#L257
func WatchAndPush(client *occlient.Client, componentName string, applicationName, path string, asFile bool, out io.Writer) error {
func WatchAndPush(client *occlient.Client, componentName string, applicationName, path string, out io.Writer) error {
log.Debugf("starting WatchAndPush, path: %s, component: %s", path, componentName)

// it might be better to expose this as argument in the future
Expand Down Expand Up @@ -170,7 +171,18 @@ func WatchAndPush(client *occlient.Client, componentName string, applicationName
fmt.Fprintf(out, "File %s changed\n", file)
}
fmt.Fprintf(out, "Pushing files...\n")
err := PushLocal(client, componentName, applicationName, path, asFile, out)
fileInfo, err := os.Stat(path)
if err != nil {
return errors.Wrapf(err, "%s: file doesn't exist", path)
}
if fileInfo.IsDir() {
log.Debugf("Copying files %s to pod", changedFiles)
err = PushLocal(client, componentName, applicationName, path, out, changedFiles)
} else {
pathDir := filepath.Dir(path)
log.Debugf("Copying file %s to pod", path)
err = PushLocal(client, componentName, applicationName, pathDir, out, []string{path})
}
if err != nil {
// Intentionally not exiting on error here.
// We don't want to break watch when push failed, it might be fixed with the next change.
Expand Down

0 comments on commit 844ddf3

Please sign in to comment.