2012-11-18 18:32:31 +01:00
|
|
|
// Sync files and directories to and from swift
|
|
|
|
//
|
|
|
|
// Nick Craig-Wood <nick@craig-wood.com>
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
2012-11-20 23:40:48 +01:00
|
|
|
"github.com/ncw/swift"
|
2012-11-18 18:32:31 +01:00
|
|
|
"log"
|
|
|
|
"os"
|
2012-11-29 23:13:58 +01:00
|
|
|
"runtime"
|
2012-11-18 18:32:31 +01:00
|
|
|
"runtime/pprof"
|
2012-11-28 12:17:31 +01:00
|
|
|
"strings"
|
2012-11-29 23:13:58 +01:00
|
|
|
"sync"
|
2012-11-18 18:32:31 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Globals
|
|
|
|
var (
|
|
|
|
// Flags
|
|
|
|
cpuprofile = flag.String("cpuprofile", "", "Write cpu profile to file")
|
2012-12-24 01:03:18 +01:00
|
|
|
snet = flag.Bool("snet", false, "Use internal service network") // FIXME not implemented
|
|
|
|
verbose = flag.Bool("verbose", false, "Print lots more stuff")
|
|
|
|
quiet = flag.Bool("quiet", false, "Print as little stuff as possible")
|
2012-11-18 18:32:31 +01:00
|
|
|
// FIXME make these part of swift so we get a standard set of flags?
|
2012-11-29 23:13:58 +01:00
|
|
|
authUrl = flag.String("auth", os.Getenv("ST_AUTH"), "Auth URL for server. Defaults to environment var ST_AUTH.")
|
|
|
|
userName = flag.String("user", os.Getenv("ST_USER"), "User name. Defaults to environment var ST_USER.")
|
|
|
|
apiKey = flag.String("key", os.Getenv("ST_KEY"), "API key (password). Defaults to environment var ST_KEY.")
|
|
|
|
checkers = flag.Int("checkers", 8, "Number of checkers to run in parallel.")
|
2012-12-23 10:32:33 +01:00
|
|
|
transfers = flag.Int("transfers", 4, "Number of file transfers to run in parallel.")
|
2012-11-18 18:32:31 +01:00
|
|
|
)
|
|
|
|
|
2012-12-26 13:23:58 +01:00
|
|
|
// Read FsObjects~s on in send to out if they need uploading
|
2012-11-29 23:13:58 +01:00
|
|
|
//
|
|
|
|
// FIXME potentially doing lots of MD5SUMS at once
|
2012-12-26 13:23:58 +01:00
|
|
|
func Checker(in, out FsObjectsChan, fdst Fs, wg *sync.WaitGroup) {
|
2012-11-29 23:13:58 +01:00
|
|
|
defer wg.Done()
|
2012-12-26 13:23:58 +01:00
|
|
|
for src := range in {
|
|
|
|
dst := fdst.NewFsObject(src.Remote())
|
|
|
|
if dst == nil {
|
|
|
|
src.Debugf("Couldn't find local file - download")
|
|
|
|
out <- src
|
|
|
|
continue
|
2012-12-23 10:32:33 +01:00
|
|
|
}
|
|
|
|
|
2012-11-29 23:13:58 +01:00
|
|
|
// Check to see if can store this
|
2012-12-26 13:23:58 +01:00
|
|
|
if !src.Storable() {
|
2012-11-29 23:13:58 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Check to see if changed or not
|
2012-12-26 13:23:58 +01:00
|
|
|
if Equal(src, dst) {
|
|
|
|
src.Debugf("Unchanged skipping")
|
2012-11-29 23:13:58 +01:00
|
|
|
continue
|
|
|
|
}
|
2012-12-26 13:23:58 +01:00
|
|
|
out <- src
|
2012-11-29 23:13:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-26 13:23:58 +01:00
|
|
|
// Read FsObjects on in and copy them
|
|
|
|
func Copier(in FsObjectsChan, fdst Fs, wg *sync.WaitGroup) {
|
2012-11-29 23:13:58 +01:00
|
|
|
defer wg.Done()
|
2012-12-26 13:23:58 +01:00
|
|
|
for src := range in {
|
|
|
|
fdst.Put(src)
|
2012-11-20 23:40:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-26 13:23:58 +01:00
|
|
|
// Copies fsrc into fdst
|
|
|
|
func Copy(fsrc, fdst Fs) {
|
|
|
|
err := fdst.Mkdir()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Failed to make destination")
|
|
|
|
}
|
|
|
|
|
|
|
|
to_be_checked := fsrc.List()
|
2012-12-23 10:32:33 +01:00
|
|
|
to_be_uploaded := make(FsObjectsChan, *transfers)
|
2012-11-29 23:13:58 +01:00
|
|
|
|
|
|
|
var checkerWg sync.WaitGroup
|
|
|
|
checkerWg.Add(*checkers)
|
|
|
|
for i := 0; i < *checkers; i++ {
|
2012-12-26 13:23:58 +01:00
|
|
|
go Checker(to_be_checked, to_be_uploaded, fdst, &checkerWg)
|
2012-11-29 23:13:58 +01:00
|
|
|
}
|
|
|
|
|
2012-12-26 13:23:58 +01:00
|
|
|
var copierWg sync.WaitGroup
|
|
|
|
copierWg.Add(*transfers)
|
2012-12-23 10:32:33 +01:00
|
|
|
for i := 0; i < *transfers; i++ {
|
2012-12-26 13:23:58 +01:00
|
|
|
go Copier(to_be_uploaded, fdst, &copierWg)
|
2012-11-29 23:13:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("Waiting for checks to finish")
|
|
|
|
checkerWg.Wait()
|
|
|
|
close(to_be_uploaded)
|
2012-12-26 13:23:58 +01:00
|
|
|
log.Printf("Waiting for transfers to finish")
|
|
|
|
copierWg.Wait()
|
2012-11-29 23:13:58 +01:00
|
|
|
}
|
|
|
|
|
2012-12-26 13:23:58 +01:00
|
|
|
// Syncs a directory into a container
|
|
|
|
func upload(c *swift.Connection, args []string) {
|
|
|
|
root, container := args[0], args[1]
|
|
|
|
// FIXME
|
|
|
|
fsrc := &FsLocal{root: root}
|
|
|
|
fdst := &FsSwift{c: *c, container: container}
|
2012-11-30 00:07:29 +01:00
|
|
|
|
2012-12-26 13:23:58 +01:00
|
|
|
Copy(fsrc, fdst)
|
2012-12-23 10:32:33 +01:00
|
|
|
}
|
|
|
|
|
2012-11-30 00:07:29 +01:00
|
|
|
// Syncs a container into a directory
|
|
|
|
//
|
|
|
|
// FIXME need optional stat in FsObject and to be able to make FsObjects from ObjectsAll
|
2012-12-04 01:09:22 +01:00
|
|
|
//
|
|
|
|
// FIXME should download and stat many at once
|
2012-12-04 00:58:17 +01:00
|
|
|
func download(c *swift.Connection, args []string) {
|
|
|
|
container, root := args[0], args[1]
|
2012-12-23 10:32:33 +01:00
|
|
|
|
2012-12-26 13:23:58 +01:00
|
|
|
// FIXME
|
|
|
|
fsrc := &FsSwift{c: *c, container: container}
|
|
|
|
fdst := &FsLocal{root: root}
|
2012-12-23 10:32:33 +01:00
|
|
|
|
2012-12-26 13:23:58 +01:00
|
|
|
Copy(fsrc, fdst)
|
2012-11-30 00:07:29 +01:00
|
|
|
}
|
|
|
|
|
2012-11-20 23:40:48 +01:00
|
|
|
// Lists the containers
|
|
|
|
func listContainers(c *swift.Connection) {
|
2012-11-29 00:40:09 +01:00
|
|
|
containers, err := c.ContainersAll(nil)
|
2012-11-20 23:40:48 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Couldn't list containers: %s", err)
|
|
|
|
}
|
|
|
|
for _, container := range containers {
|
|
|
|
fmt.Printf("%9d %12d %s\n", container.Count, container.Bytes, container.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-26 13:23:58 +01:00
|
|
|
// List the Fs to stdout
|
|
|
|
func List(f Fs) {
|
|
|
|
// FIXME error?
|
|
|
|
in := f.List()
|
|
|
|
for fs := range in {
|
|
|
|
// FIXME
|
|
|
|
// if object.PseudoDirectory {
|
|
|
|
// fmt.Printf("%9s %19s %s\n", "Directory", "-", fs.Remote())
|
|
|
|
// } else {
|
|
|
|
// FIXME ModTime is expensive?
|
|
|
|
modTime, _ := fs.ModTime()
|
|
|
|
fmt.Printf("%9d %19s %s\n", fs.Size(), modTime.Format("2006-01-02 15:04:05"), fs.Remote())
|
|
|
|
// fmt.Printf("%9d %19s %s\n", fs.Size(), object.LastModified.Format("2006-01-02 15:04:05"), fs.Remote())
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-20 23:40:48 +01:00
|
|
|
// Lists files in a container
|
2012-12-04 00:58:17 +01:00
|
|
|
func list(c *swift.Connection, args []string) {
|
|
|
|
if len(args) == 0 {
|
|
|
|
listContainers(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
container := args[0]
|
2012-12-26 13:23:58 +01:00
|
|
|
// FIXME
|
|
|
|
f := &FsSwift{c: *c, container: container}
|
|
|
|
List(f)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Local lists files
|
|
|
|
func llist(c *swift.Connection, args []string) {
|
|
|
|
root := args[0]
|
|
|
|
// FIXME
|
|
|
|
f := &FsLocal{root: root}
|
|
|
|
List(f)
|
2012-11-20 23:40:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Makes a container
|
2012-12-04 00:58:17 +01:00
|
|
|
func mkdir(c *swift.Connection, args []string) {
|
|
|
|
container := args[0]
|
2012-12-26 13:23:58 +01:00
|
|
|
// FIXME
|
|
|
|
fdst := &FsSwift{c: *c, container: container}
|
|
|
|
|
|
|
|
err := fdst.Mkdir()
|
2012-11-20 23:40:48 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Couldn't create container %q: %s", container, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Removes a container
|
2012-12-04 00:58:17 +01:00
|
|
|
func rmdir(c *swift.Connection, args []string) {
|
|
|
|
container := args[0]
|
2012-12-26 13:23:58 +01:00
|
|
|
// FIXME
|
|
|
|
fdst := &FsSwift{c: *c, container: container}
|
|
|
|
|
|
|
|
err := fdst.Rmdir()
|
2012-11-20 23:40:48 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Couldn't delete container %q: %s", container, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-04 01:09:22 +01:00
|
|
|
// Removes a container and all of its contents
|
|
|
|
//
|
|
|
|
// FIXME should make FsObjects and use debugging
|
|
|
|
func purge(c *swift.Connection, args []string) {
|
|
|
|
container := args[0]
|
2012-12-26 13:23:58 +01:00
|
|
|
// FIXME
|
|
|
|
fdst := &FsSwift{c: *c, container: container}
|
2012-12-04 01:09:22 +01:00
|
|
|
|
2012-12-26 13:23:58 +01:00
|
|
|
to_be_deleted := fdst.List()
|
2012-12-23 10:49:49 +01:00
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(*transfers)
|
|
|
|
for i := 0; i < *transfers; i++ {
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
2012-12-26 13:23:58 +01:00
|
|
|
for dst := range to_be_deleted {
|
|
|
|
err := dst.Remove()
|
2012-12-23 10:49:49 +01:00
|
|
|
if err != nil {
|
2012-12-26 13:23:58 +01:00
|
|
|
log.Printf("%s: Couldn't delete: %s\n", dst.Remote(), err)
|
2012-12-23 10:49:49 +01:00
|
|
|
} else {
|
2012-12-26 13:23:58 +01:00
|
|
|
log.Printf("%s: Deleted\n", dst.Remote())
|
2012-12-23 10:49:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("Waiting for deletions to finish")
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
log.Printf("Deleting container")
|
2012-12-04 01:09:22 +01:00
|
|
|
rmdir(c, args)
|
|
|
|
}
|
|
|
|
|
2012-12-04 00:58:17 +01:00
|
|
|
type Command struct {
|
|
|
|
name string
|
|
|
|
help string
|
|
|
|
run func(*swift.Connection, []string)
|
|
|
|
minArgs, maxArgs int
|
|
|
|
}
|
|
|
|
|
|
|
|
// checkArgs checks there are enough arguments and prints a message if not
|
|
|
|
func (cmd *Command) checkArgs(args []string) {
|
|
|
|
if len(args) < cmd.minArgs {
|
|
|
|
syntaxError()
|
|
|
|
fmt.Fprintf(os.Stderr, "Command %s needs %d arguments mininum\n", cmd.name, cmd.minArgs)
|
|
|
|
os.Exit(1)
|
|
|
|
} else if len(args) > cmd.maxArgs {
|
|
|
|
syntaxError()
|
|
|
|
fmt.Fprintf(os.Stderr, "Command %s needs %d arguments maximum\n", cmd.name, cmd.maxArgs)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var Commands = []Command{
|
|
|
|
{
|
|
|
|
"upload",
|
|
|
|
`<directory> <container>
|
|
|
|
Upload the local directory to the remote container. Doesn't
|
|
|
|
upload unchanged files, testing first by modification time
|
|
|
|
then by MD5SUM
|
|
|
|
`,
|
|
|
|
upload,
|
|
|
|
2, 2,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"download",
|
|
|
|
`<container> <directory>
|
|
|
|
Download the container to the local directory. Doesn't
|
|
|
|
download unchanged files
|
|
|
|
`,
|
|
|
|
download,
|
|
|
|
2, 2,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"ls",
|
|
|
|
`[<container>]
|
|
|
|
List the containers if no parameter supplied or the contents
|
|
|
|
of <container>
|
|
|
|
`,
|
|
|
|
list,
|
|
|
|
0, 1,
|
|
|
|
},
|
2012-12-26 13:23:58 +01:00
|
|
|
{
|
|
|
|
"lls",
|
|
|
|
`[<directory>]
|
|
|
|
List the directory
|
|
|
|
`,
|
|
|
|
llist,
|
|
|
|
1, 1,
|
|
|
|
},
|
2012-12-04 00:58:17 +01:00
|
|
|
{
|
|
|
|
"mkdir",
|
|
|
|
`<container>
|
|
|
|
Make the container if it doesn't already exist
|
|
|
|
`,
|
|
|
|
mkdir,
|
|
|
|
1, 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"rmdir",
|
|
|
|
`<container>
|
|
|
|
Remove the container. Note that you can't remove a container with
|
|
|
|
objects in - use rm for that
|
|
|
|
`,
|
|
|
|
rmdir,
|
|
|
|
1, 1,
|
|
|
|
},
|
2012-12-04 01:09:22 +01:00
|
|
|
{
|
|
|
|
"purge",
|
|
|
|
`<container>
|
|
|
|
Remove the container and all of the contents.
|
|
|
|
`,
|
|
|
|
purge,
|
|
|
|
1, 1,
|
|
|
|
},
|
2012-12-04 00:58:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// syntaxError prints the syntax
|
|
|
|
func syntaxError() {
|
|
|
|
fmt.Fprintf(os.Stderr, `Sync files and directories to and from swift
|
|
|
|
|
|
|
|
Syntax: [options] subcommand <parameters> <parameters...>
|
|
|
|
|
|
|
|
Subcommands:
|
|
|
|
`)
|
|
|
|
for i := range Commands {
|
|
|
|
cmd := &Commands[i]
|
|
|
|
fmt.Fprintf(os.Stderr, " %s: %s\n", cmd.name, cmd.help)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintf(os.Stderr, "Options:\n")
|
|
|
|
flag.PrintDefaults()
|
|
|
|
fmt.Fprintf(os.Stderr, `
|
|
|
|
It is only necessary to use a unique prefix of the subcommand, eg 'up' for 'upload'.
|
|
|
|
`)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exit with the message
|
|
|
|
func fatal(message string, args ...interface{}) {
|
|
|
|
syntaxError()
|
|
|
|
fmt.Fprintf(os.Stderr, message, args...)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2012-11-18 18:32:31 +01:00
|
|
|
func main() {
|
|
|
|
flag.Usage = syntaxError
|
|
|
|
flag.Parse()
|
2012-11-20 23:40:48 +01:00
|
|
|
args := flag.Args()
|
2012-11-29 23:13:58 +01:00
|
|
|
runtime.GOMAXPROCS(runtime.NumCPU())
|
2012-11-18 18:32:31 +01:00
|
|
|
|
|
|
|
// Setup profiling if desired
|
|
|
|
if *cpuprofile != "" {
|
|
|
|
f, err := os.Create(*cpuprofile)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
pprof.StartCPUProfile(f)
|
|
|
|
defer pprof.StopCPUProfile()
|
|
|
|
}
|
|
|
|
|
2012-11-20 23:40:48 +01:00
|
|
|
fmt.Println(args)
|
|
|
|
if len(args) < 1 {
|
|
|
|
fatal("No command supplied\n")
|
|
|
|
}
|
2012-11-18 18:32:31 +01:00
|
|
|
|
|
|
|
if *userName == "" {
|
2012-11-20 23:40:48 +01:00
|
|
|
log.Fatal("Need -user or environmental variable ST_USER")
|
2012-11-18 18:32:31 +01:00
|
|
|
}
|
|
|
|
if *apiKey == "" {
|
2012-11-20 23:40:48 +01:00
|
|
|
log.Fatal("Need -key or environmental variable ST_KEY")
|
2012-11-18 18:32:31 +01:00
|
|
|
}
|
|
|
|
if *authUrl == "" {
|
2012-11-20 23:40:48 +01:00
|
|
|
log.Fatal("Need -auth or environmental variable ST_AUTH")
|
2012-11-18 18:32:31 +01:00
|
|
|
}
|
2012-11-20 23:40:48 +01:00
|
|
|
c := &swift.Connection{
|
2012-11-18 18:32:31 +01:00
|
|
|
UserName: *userName,
|
|
|
|
ApiKey: *apiKey,
|
|
|
|
AuthUrl: *authUrl,
|
|
|
|
}
|
|
|
|
err := c.Authenticate()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Failed to authenticate", err)
|
|
|
|
}
|
|
|
|
|
2012-12-04 00:58:17 +01:00
|
|
|
cmd := strings.ToLower(args[0])
|
2012-11-20 23:40:48 +01:00
|
|
|
args = args[1:]
|
|
|
|
|
2012-12-04 00:58:17 +01:00
|
|
|
// Find the command doing a prefix match
|
|
|
|
var found *Command
|
|
|
|
for i := range Commands {
|
|
|
|
command := &Commands[i]
|
|
|
|
// exact command name found - use that
|
|
|
|
if command.name == cmd {
|
|
|
|
found = command
|
|
|
|
break
|
|
|
|
} else if strings.HasPrefix(command.name, cmd) {
|
|
|
|
if found != nil {
|
|
|
|
log.Fatalf("Not unique - matches multiple commands %q", cmd)
|
|
|
|
}
|
|
|
|
found = command
|
2012-11-20 23:40:48 +01:00
|
|
|
}
|
|
|
|
}
|
2012-12-04 00:58:17 +01:00
|
|
|
if found == nil {
|
|
|
|
log.Fatalf("Unknown command %q", cmd)
|
|
|
|
}
|
|
|
|
found.checkArgs(args)
|
|
|
|
found.run(c, args)
|
2012-11-18 18:32:31 +01:00
|
|
|
}
|