Exemple #1
0
// Scp one cpFile to destination.
// If destination is not exist, be regarded as a dir default, then make it.
// If destination is a dir, make file in it.
// If destination is a file, cover it.
func (t *SSHClient) scp2Dest(client *sftp.Client, cpFile *CpFile) (string, bool) {
	filename := cpFile.Filename
	dest := cpFile.Destination
	content := cpFile.Content

	// Dest dir not exist, make it.
	destType := "file"
	if fi, err := client.Lstat(dest); err != nil { // Dest not exist,make as a dir.
		if err := client.Mkdir(dest); err != nil {
			return fmt.Sprintf("mkdir %s, %s", dest, err.Error()), false
		}
	} else {
		if fi.IsDir() == true {
			destType = "dir"
		}
	}
	destF := dest
	if destType == "dir" { // dir, build filepath
		destF = fmt.Sprintf("%s/%s", dest, filename)
	}

	f, err := client.Create(destF)
	if err != nil {
		return fmt.Sprintf("create: %s, %s", destF, err.Error()), false
	}
	if _, err := f.Write(content); err != nil {
		return fmt.Sprintf("write: %s, %s", destF, err.Error()), false
	}
	return fmt.Sprintf("cp: %s, %s => %s", filename, t.host, dest), true
}
Exemple #2
0
func (ft *SftpTask) uploadAllFiles(client *sftp.Client, ctx *builder.Context) error {
	var (
		createdDirs = make(map[string]bool)
		err         error
	)
	return ctx.Diff.Walk(func(name string, entry *builder.DiffEntry) error {
		rel, _ := filepath.Rel(ctx.DstDir, name)
		rel = filepath.ToSlash(rel)

		if entry.Behavior == builder.DIFF_REMOVE {
			log15.Debug("Deploy.Sftp.Delete", "file", rel)
			return client.Remove(path.Join(ft.opt.Directory, rel))
		}

		// create directory recursive
		dirs := getDirs(path.Dir(rel))
		if len(dirs) > 0 {
			for i := len(dirs) - 1; i >= 0; i-- {
				dir := dirs[i]
				if !createdDirs[dir] {
					if err = client.Mkdir(path.Join(ft.opt.Directory, dir)); err == nil {
						createdDirs[dir] = true
					}
				}
			}
		}

		// upload file
		f, err := os.Open(name)
		if err != nil {
			return err
		}
		defer f.Close()

		f2, err := client.Create(path.Join(ft.opt.Directory, rel))
		if err != nil {
			return err
		}
		defer f2.Close()

		if _, err = io.Copy(f2, f); err != nil {
			return err
		}
		log15.Debug("Deploy.Sftp.Stor", "file", rel)
		return nil
	})
}
Exemple #3
0
func sftpMkdir(path string, client *sftp.Client, fi os.FileInfo) error {
	log.Printf("[DEBUG] sftp: creating dir %s", path)

	if err := client.Mkdir(path); err != nil {
		// Do not consider it an error if the directory existed
		remoteFi, fiErr := client.Lstat(path)
		if fiErr != nil || !remoteFi.IsDir() {
			return err
		}
	}

	mode := fi.Mode().Perm()
	if err := client.Chmod(path, mode); err != nil {
		return err
	}
	return nil
}
Exemple #4
0
func (ft *SftpTask) uploadDiffFiles(client *sftp.Client, ctx *builder.Context) error {
	return ctx.Diff.Walk(func(name string, entry *builder.DiffEntry) error {
		rel, _ := filepath.Rel(ctx.DstDir, name)
		rel = filepath.ToSlash(rel)

		if entry.Behavior == builder.DIFF_REMOVE {
			log15.Debug("Deploy.Sftp.Delete", "file", rel)
			return client.Remove(path.Join(ft.opt.Directory, rel))
		}

		target := path.Join(ft.opt.Directory, rel)
		if entry.Behavior == builder.DIFF_KEEP {
			if fi, _ := client.Stat(target); fi != nil {
				// entry file should be older than uploaded file
				if entry.Time.Sub(fi.ModTime()).Seconds() < 0 {
					return nil
				}
			}
		}

		dirs := getDirs(path.Dir(rel))
		for i := len(dirs) - 1; i >= 0; i-- {
			client.Mkdir(path.Join(ft.opt.Directory, dirs[i]))
		}

		// upload file
		f, err := os.Open(name)
		if err != nil {
			return err
		}
		defer f.Close()

		f2, err := client.Create(target)
		if err != nil {
			return err
		}
		defer f2.Close()

		if _, err = io.Copy(f2, f); err != nil {
			return err
		}
		log15.Debug("Deploy.Sftp.Stor", "file", rel)
		return nil
	})
}
Exemple #5
-1
func mkdirAll(sftpClient *sftp.Client, dir string) error {
	if dir == "/" {
		// Must always exist
		return nil
	}

	stat, err := sftpClient.Lstat(dir)
	if err == nil {
		if !stat.IsDir() {
			return fmt.Errorf("not a directory: %q", dir)
		}
		return nil
	}

	parent := path.Dir(dir)
	err = mkdirAll(sftpClient, parent)
	if err != nil {
		return err
	}

	err = sftpClient.Mkdir(dir)
	if err != nil {
		return fmt.Errorf("error creating directory %q over sftp: %v", dir, err)
	}
	return nil
}
Exemple #6
-1
// make sftp directories
func makeSftpDir(client *sftp.Client, dirs []string) error {
	for i := len(dirs) - 1; i >= 0; i-- {
		if err := client.Mkdir(dirs[i]); err != nil {
			return err
		}
	}
	return nil
}
Exemple #7
-1
func putDir(p file, client *sftp.Client, basepath string, dryRun bool) {
	if dryRun {
		log.Info("!MKDIR", "path", p.relPath)
		return
	}

	log.Info("MKDIR", "path", p.relPath)
	err := client.Mkdir(basepath + "/" + p.relPath)
	if err != nil {
		log.Warn("MKDIR", "path", p.relPath, "err", err)
		return
	}
}