2012-11-19 01:32:31 +08:00
|
|
|
// Sync files and directories to and from swift
|
|
|
|
//
|
|
|
|
// Nick Craig-Wood <nick@craig-wood.com>
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2012-11-28 19:17:31 +08:00
|
|
|
"crypto/md5"
|
2012-11-19 01:32:31 +08:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
2012-11-21 06:40:48 +08:00
|
|
|
"github.com/ncw/swift"
|
2012-11-28 19:17:31 +08:00
|
|
|
"io"
|
2012-11-19 01:32:31 +08:00
|
|
|
"log"
|
|
|
|
"os"
|
2012-11-21 06:40:48 +08:00
|
|
|
"path/filepath"
|
2012-11-19 01:32:31 +08:00
|
|
|
"runtime/pprof"
|
2012-11-28 19:17:31 +08:00
|
|
|
"strings"
|
2012-11-19 01:32:31 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// Globals
|
|
|
|
var (
|
|
|
|
// Flags
|
|
|
|
//fileSize = flag.Int64("s", 1E9, "Size of the check files")
|
|
|
|
cpuprofile = flag.String("cpuprofile", "", "Write cpu profile to file")
|
|
|
|
//duration = flag.Duration("duration", time.Hour*24, "Duration to run test")
|
|
|
|
//statsInterval = flag.Duration("stats", time.Minute*1, "Interval to print stats")
|
|
|
|
//logfile = flag.String("logfile", "stressdisk.log", "File to write log to set to empty to ignore")
|
|
|
|
|
|
|
|
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")
|
|
|
|
// FIXME make these part of swift so we get a standard set of flags?
|
2012-11-19 04:03:06 +08:00
|
|
|
authUrl = flag.String("auth", os.Getenv("ST_AUTH"), "Auth URL for server. Defaults to environment var ST_AUTH.")
|
2012-11-19 01:32:31 +08:00
|
|
|
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.")
|
|
|
|
)
|
|
|
|
|
2012-11-21 06:40:48 +08:00
|
|
|
type FsObject struct {
|
|
|
|
rel string
|
|
|
|
path string
|
|
|
|
info os.FileInfo
|
|
|
|
}
|
|
|
|
|
2012-11-28 19:38:14 +08:00
|
|
|
type FsObjects []FsObject
|
2012-11-21 06:40:48 +08:00
|
|
|
|
2012-11-29 01:12:22 +08:00
|
|
|
// Write debuging output for this FsObject
|
2012-11-29 07:40:09 +08:00
|
|
|
func (fs *FsObject) Debugf(text string, args ...interface{}) {
|
2012-11-29 01:12:22 +08:00
|
|
|
out := fmt.Sprintf(text, args...)
|
|
|
|
log.Printf("%s: %s", fs.rel, out)
|
|
|
|
}
|
|
|
|
|
2012-11-28 19:17:31 +08:00
|
|
|
// md5sum calculates the md5sum of a file returning a lowercase hex string
|
2012-11-29 01:12:22 +08:00
|
|
|
func (fs *FsObject) md5sum() (string, error) {
|
|
|
|
in, err := os.Open(fs.path)
|
2012-11-28 19:17:31 +08:00
|
|
|
if err != nil {
|
2012-11-29 01:12:22 +08:00
|
|
|
fs.Debugf("Failed to open: %s", err)
|
2012-11-28 19:17:31 +08:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer in.Close() // FIXME ignoring error
|
|
|
|
hash := md5.New()
|
|
|
|
_, err = io.Copy(hash, in)
|
|
|
|
if err != nil {
|
2012-11-29 01:12:22 +08:00
|
|
|
fs.Debugf("Failed to read: %s", err)
|
2012-11-28 19:17:31 +08:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%x", hash.Sum(nil)), nil
|
|
|
|
}
|
|
|
|
|
2012-11-29 01:24:00 +08:00
|
|
|
// Checks to see if an object has changed or not by looking at its size, mtime and MD5SUM
|
2012-11-28 06:28:46 +08:00
|
|
|
//
|
2012-11-29 01:24:00 +08:00
|
|
|
// If the remote object size is different then it is considered to be
|
|
|
|
// changed.
|
2012-11-28 19:17:31 +08:00
|
|
|
//
|
2012-11-29 01:24:00 +08:00
|
|
|
// If the size is the same and the mtime is the same then it is
|
|
|
|
// considered to be unchanged. This is the heuristic rsync uses when
|
|
|
|
// not using --checksum.
|
|
|
|
//
|
|
|
|
// If the size is the same and and mtime is different or unreadable
|
|
|
|
// and the MD5SUM is the same then the file is considered to be
|
2012-11-29 01:42:12 +08:00
|
|
|
// unchanged. In this case the mtime on the object is updated.
|
2012-11-29 01:24:00 +08:00
|
|
|
//
|
|
|
|
// Otherwise the file is considered to be changed.
|
2012-11-28 06:28:46 +08:00
|
|
|
func (fs *FsObject) changed(c *swift.Connection, container string) bool {
|
|
|
|
obj, h, err := c.Object(container, fs.rel)
|
|
|
|
if err != nil {
|
2012-11-29 01:12:22 +08:00
|
|
|
fs.Debugf("Failed to read info: %s", err)
|
2012-11-28 06:28:46 +08:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
if obj.Bytes != fs.info.Size() {
|
2012-11-29 01:12:22 +08:00
|
|
|
fs.Debugf("Sizes differ")
|
2012-11-28 06:28:46 +08:00
|
|
|
return true
|
|
|
|
}
|
2012-11-29 01:24:00 +08:00
|
|
|
|
|
|
|
// Size the same so check the mtime
|
2012-11-28 06:28:46 +08:00
|
|
|
m := h.ObjectMetadata()
|
|
|
|
t, err := m.GetModTime()
|
|
|
|
if err != nil {
|
2012-11-29 01:12:22 +08:00
|
|
|
fs.Debugf("Failed to read mtime: %s", err)
|
2012-11-29 01:24:00 +08:00
|
|
|
} else if !t.Equal(fs.info.ModTime()) {
|
|
|
|
fs.Debugf("Modification times differ")
|
|
|
|
} else {
|
|
|
|
fs.Debugf("Size and modification time the same - skipping")
|
2012-11-28 19:17:31 +08:00
|
|
|
return false
|
2012-11-29 07:40:09 +08:00
|
|
|
}
|
2012-11-29 01:24:00 +08:00
|
|
|
|
|
|
|
// mtime is unreadable or different but size is the same so
|
|
|
|
// check the MD5SUM
|
|
|
|
localMd5, err := fs.md5sum()
|
|
|
|
if err != nil {
|
|
|
|
fs.Debugf("Failed to calculate md5: %s", err)
|
|
|
|
return true
|
2012-11-28 06:28:46 +08:00
|
|
|
}
|
2012-11-29 01:24:00 +08:00
|
|
|
// fs.Debugf("Local MD5 %s", localMd5)
|
|
|
|
// fs.Debugf("Remote MD5 %s", obj.Hash)
|
|
|
|
if localMd5 != strings.ToLower(obj.Hash) {
|
|
|
|
fs.Debugf("Md5sums differ")
|
2012-11-28 06:28:46 +08:00
|
|
|
return true
|
|
|
|
}
|
2012-11-29 01:24:00 +08:00
|
|
|
|
2012-11-29 01:42:12 +08:00
|
|
|
// Update the mtime of the remote object here
|
|
|
|
m.SetModTime(fs.info.ModTime())
|
|
|
|
err = c.ObjectUpdate(container, fs.rel, m.ObjectHeaders())
|
|
|
|
if err != nil {
|
|
|
|
fs.Debugf("Failed to update mtime: %s", err)
|
|
|
|
}
|
|
|
|
fs.Debugf("Updated mtime")
|
2012-11-29 07:40:09 +08:00
|
|
|
|
2012-11-29 01:24:00 +08:00
|
|
|
fs.Debugf("Size and MD5SUM identical - skipping")
|
2012-11-28 06:28:46 +08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2012-11-21 06:40:48 +08:00
|
|
|
// Puts the FsObject into the container
|
|
|
|
func (fs *FsObject) put(c *swift.Connection, container string) {
|
|
|
|
mode := fs.info.Mode()
|
|
|
|
if mode&(os.ModeSymlink|os.ModeNamedPipe|os.ModeSocket|os.ModeDevice) != 0 {
|
2012-11-29 01:12:22 +08:00
|
|
|
fs.Debugf("Can't transfer non file/directory")
|
2012-11-21 06:40:48 +08:00
|
|
|
} else if mode&os.ModeDir != 0 {
|
|
|
|
// Debug?
|
2012-11-29 01:12:22 +08:00
|
|
|
fs.Debugf("FIXME Skipping directory")
|
2012-11-21 06:40:48 +08:00
|
|
|
} else {
|
2012-11-28 06:28:46 +08:00
|
|
|
// Check to see if changed or not
|
|
|
|
if !fs.changed(c, container) {
|
2012-11-29 01:12:22 +08:00
|
|
|
fs.Debugf("Unchanged skipping")
|
2012-11-28 06:28:46 +08:00
|
|
|
return
|
|
|
|
}
|
2012-11-21 06:40:48 +08:00
|
|
|
// FIXME content type
|
|
|
|
in, err := os.Open(fs.path)
|
|
|
|
if err != nil {
|
2012-11-29 01:12:22 +08:00
|
|
|
fs.Debugf("Failed to open: %s", err)
|
2012-11-21 06:40:48 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer in.Close()
|
2012-11-28 06:28:46 +08:00
|
|
|
m := swift.Metadata{}
|
|
|
|
m.SetModTime(fs.info.ModTime())
|
|
|
|
_, err = c.ObjectPut(container, fs.rel, in, true, "", "", m.ObjectHeaders())
|
2012-11-21 06:40:48 +08:00
|
|
|
if err != nil {
|
2012-11-29 01:12:22 +08:00
|
|
|
fs.Debugf("Failed to upload: %s", err)
|
2012-11-21 06:40:48 +08:00
|
|
|
return
|
|
|
|
}
|
2012-11-29 01:12:22 +08:00
|
|
|
fs.Debugf("Uploaded")
|
2012-11-21 06:40:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Walk the path
|
|
|
|
//
|
|
|
|
// FIXME ignore symlinks?
|
|
|
|
// FIXME what about hardlinks / etc
|
|
|
|
func walk(root string) FsObjects {
|
2012-11-28 19:38:14 +08:00
|
|
|
files := make(FsObjects, 0, 1024)
|
2012-11-21 06:40:48 +08:00
|
|
|
err := filepath.Walk(root, func(path string, f os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Failed to open directory: %s: %s", path, err)
|
|
|
|
} else {
|
2012-11-29 01:12:54 +08:00
|
|
|
info, err := os.Lstat(path)
|
2012-11-21 06:40:48 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("Failed to stat %s: %s", path, err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
rel, err := filepath.Rel(root, path)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Failed to get relative path %s: %s", path, err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if rel == "." {
|
|
|
|
rel = ""
|
|
|
|
}
|
2012-11-28 19:38:14 +08:00
|
|
|
files = append(files, FsObject{rel: rel, path: path, info: info})
|
2012-11-21 06:40:48 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Failed to open directory: %s: %s", root, err)
|
|
|
|
}
|
|
|
|
return files
|
|
|
|
}
|
|
|
|
|
2012-11-19 01:32:31 +08:00
|
|
|
// syntaxError prints the syntax
|
|
|
|
func syntaxError() {
|
|
|
|
fmt.Fprintf(os.Stderr, `Sync files and directores to and from swift
|
|
|
|
|
|
|
|
FIXME
|
|
|
|
|
|
|
|
Full options:
|
|
|
|
`)
|
|
|
|
flag.PrintDefaults()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exit with the message
|
|
|
|
func fatal(message string, args ...interface{}) {
|
|
|
|
syntaxError()
|
|
|
|
fmt.Fprintf(os.Stderr, message, args...)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2012-11-21 06:40:48 +08:00
|
|
|
// checkArgs checks there are enough arguments and prints a message if not
|
|
|
|
func checkArgs(args []string, n int, message string) {
|
|
|
|
if len(args) != n {
|
|
|
|
syntaxError()
|
|
|
|
fmt.Fprintf(os.Stderr, "%d arguments required: %s\n", n, message)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// uploads a file into a container
|
|
|
|
func upload(c *swift.Connection, root, container string) {
|
|
|
|
files := walk(root)
|
|
|
|
for _, fs := range files {
|
|
|
|
fs.put(c, container)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lists the containers
|
|
|
|
func listContainers(c *swift.Connection) {
|
2012-11-29 07:40:09 +08:00
|
|
|
containers, err := c.ContainersAll(nil)
|
2012-11-21 06:40:48 +08: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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lists files in a container
|
|
|
|
func list(c *swift.Connection, container string) {
|
2012-11-29 07:40:09 +08:00
|
|
|
//objects, err := c.ObjectsAll(container, &swift.ObjectsOpts{Prefix: "", Delimiter: '/'})
|
|
|
|
objects, err := c.ObjectsAll(container, nil)
|
2012-11-21 06:40:48 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Couldn't read container %q: %s", container, err)
|
|
|
|
}
|
|
|
|
for _, object := range objects {
|
|
|
|
if object.PseudoDirectory {
|
|
|
|
fmt.Printf("%9s %19s %s\n", "Directory", "-", object.Name)
|
|
|
|
} else {
|
|
|
|
fmt.Printf("%9d %19s %s\n", object.Bytes, object.LastModified.Format("2006-01-02 15:04:05"), object.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Makes a container
|
|
|
|
func mkdir(c *swift.Connection, container string) {
|
|
|
|
err := c.ContainerCreate(container, nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Couldn't create container %q: %s", container, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Removes a container
|
|
|
|
func rmdir(c *swift.Connection, container string) {
|
|
|
|
err := c.ContainerDelete(container)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Couldn't delete container %q: %s", container, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-19 01:32:31 +08:00
|
|
|
func main() {
|
|
|
|
flag.Usage = syntaxError
|
|
|
|
flag.Parse()
|
2012-11-21 06:40:48 +08:00
|
|
|
args := flag.Args()
|
2012-11-19 01:32:31 +08:00
|
|
|
//runtime.GOMAXPROCS(3)
|
|
|
|
|
|
|
|
// 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-21 06:40:48 +08:00
|
|
|
fmt.Println(args)
|
|
|
|
if len(args) < 1 {
|
|
|
|
fatal("No command supplied\n")
|
|
|
|
}
|
2012-11-19 01:32:31 +08:00
|
|
|
|
|
|
|
if *userName == "" {
|
2012-11-21 06:40:48 +08:00
|
|
|
log.Fatal("Need -user or environmental variable ST_USER")
|
2012-11-19 01:32:31 +08:00
|
|
|
}
|
|
|
|
if *apiKey == "" {
|
2012-11-21 06:40:48 +08:00
|
|
|
log.Fatal("Need -key or environmental variable ST_KEY")
|
2012-11-19 01:32:31 +08:00
|
|
|
}
|
|
|
|
if *authUrl == "" {
|
2012-11-21 06:40:48 +08:00
|
|
|
log.Fatal("Need -auth or environmental variable ST_AUTH")
|
2012-11-19 01:32:31 +08:00
|
|
|
}
|
2012-11-21 06:40:48 +08:00
|
|
|
c := &swift.Connection{
|
2012-11-19 01:32:31 +08:00
|
|
|
UserName: *userName,
|
|
|
|
ApiKey: *apiKey,
|
|
|
|
AuthUrl: *authUrl,
|
|
|
|
}
|
|
|
|
err := c.Authenticate()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Failed to authenticate", err)
|
|
|
|
}
|
|
|
|
|
2012-11-21 06:40:48 +08:00
|
|
|
command := args[0]
|
|
|
|
args = args[1:]
|
|
|
|
|
|
|
|
switch command {
|
|
|
|
case "up", "upload":
|
|
|
|
checkArgs(args, 2, "Need directory to read from and container to write to")
|
|
|
|
upload(c, args[0], args[1])
|
|
|
|
case "list", "ls":
|
|
|
|
if len(args) == 0 {
|
|
|
|
listContainers(c)
|
|
|
|
} else {
|
|
|
|
checkArgs(args, 1, "Need container to list")
|
|
|
|
list(c, args[0])
|
|
|
|
}
|
|
|
|
case "mkdir":
|
|
|
|
checkArgs(args, 1, "Need container to make")
|
|
|
|
mkdir(c, args[0])
|
|
|
|
case "rmdir":
|
|
|
|
checkArgs(args, 1, "Need container to delte")
|
|
|
|
rmdir(c, args[0])
|
|
|
|
default:
|
|
|
|
log.Fatalf("Unknown command %q", command)
|
|
|
|
}
|
|
|
|
|
2012-11-19 01:32:31 +08:00
|
|
|
}
|