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

Move copyfile function to client-go & remove all ocpath related code #503

Merged
merged 1 commit into from
Jul 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 10 additions & 4 deletions cmd/push.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are we no longer retrieving the sourceType?

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
21 changes: 7 additions & 14 deletions pkg/component/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,14 +165,13 @@ 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` as well as binary file path
// During copying binary components, path represent base directory path to binary and files contains path of binary
// During copying local source components, path represent base directory path whereas files is empty
// During `odo watch`, path represent base directory path whereas files contains list of changed Files
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 +186,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
Original file line number Diff line number Diff line change
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