2014-01-15 22:17:40 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
2014-08-29 18:24:41 +00:00
|
|
|
"bytes"
|
2015-01-08 03:11:21 +00:00
|
|
|
"crypto/md5"
|
2014-08-27 23:49:12 +00:00
|
|
|
crand "crypto/rand"
|
|
|
|
"fmt"
|
2014-01-15 22:17:40 +00:00
|
|
|
"math"
|
2014-01-15 22:34:17 +00:00
|
|
|
"math/rand"
|
2014-08-22 19:47:58 +00:00
|
|
|
"os"
|
2014-08-21 21:28:16 +00:00
|
|
|
"os/exec"
|
2015-01-21 00:21:23 +00:00
|
|
|
"os/user"
|
2014-08-21 21:28:16 +00:00
|
|
|
"runtime"
|
2015-01-20 21:44:27 +00:00
|
|
|
"strconv"
|
2014-01-15 22:17:40 +00:00
|
|
|
"time"
|
2014-08-29 18:24:41 +00:00
|
|
|
|
2014-10-18 01:26:19 +00:00
|
|
|
"github.com/hashicorp/go-msgpack/codec"
|
2014-01-15 22:17:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// This scale factor means we will add a minute after we
|
|
|
|
// cross 128 nodes, another at 256, another at 512, etc.
|
|
|
|
// By 8192 nodes, we will scale up by a factor of 8
|
|
|
|
aeScaleThreshold = 128
|
|
|
|
)
|
|
|
|
|
|
|
|
// aeScale is used to scale the time interval at which anti-entropy
|
|
|
|
// take place. It is used to prevent saturation as the cluster size grows
|
|
|
|
func aeScale(interval time.Duration, n int) time.Duration {
|
|
|
|
// Don't scale until we cross the threshold
|
|
|
|
if n <= aeScaleThreshold {
|
|
|
|
return interval
|
|
|
|
}
|
|
|
|
|
|
|
|
multiplier := math.Ceil(math.Log2(float64(n))-math.Log2(aeScaleThreshold)) + 1.0
|
|
|
|
return time.Duration(multiplier) * interval
|
|
|
|
}
|
2014-01-15 22:34:17 +00:00
|
|
|
|
|
|
|
// Returns a random stagger interval between 0 and the duration
|
|
|
|
func randomStagger(intv time.Duration) time.Duration {
|
|
|
|
return time.Duration(uint64(rand.Int63()) % uint64(intv))
|
|
|
|
}
|
2014-04-03 19:12:23 +00:00
|
|
|
|
|
|
|
// strContains checks if a list contains a string
|
|
|
|
func strContains(l []string, s string) bool {
|
|
|
|
for _, v := range l {
|
|
|
|
if v == s {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2014-08-21 21:28:16 +00:00
|
|
|
|
|
|
|
// ExecScript returns a command to execute a script
|
|
|
|
func ExecScript(script string) (*exec.Cmd, error) {
|
|
|
|
var shell, flag string
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
shell = "cmd"
|
|
|
|
flag = "/C"
|
|
|
|
} else {
|
|
|
|
shell = "/bin/sh"
|
|
|
|
flag = "-c"
|
|
|
|
}
|
2014-08-22 19:47:58 +00:00
|
|
|
if other := os.Getenv("SHELL"); other != "" {
|
|
|
|
shell = other
|
|
|
|
}
|
2014-08-21 21:28:16 +00:00
|
|
|
cmd := exec.Command(shell, flag, script)
|
|
|
|
return cmd, nil
|
|
|
|
}
|
2014-08-27 23:49:12 +00:00
|
|
|
|
|
|
|
// generateUUID is used to generate a random UUID
|
|
|
|
func generateUUID() string {
|
|
|
|
buf := make([]byte, 16)
|
|
|
|
if _, err := crand.Read(buf); err != nil {
|
|
|
|
panic(fmt.Errorf("failed to read random bytes: %v", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("%08x-%04x-%04x-%04x-%12x",
|
|
|
|
buf[0:4],
|
|
|
|
buf[4:6],
|
|
|
|
buf[6:8],
|
|
|
|
buf[8:10],
|
|
|
|
buf[10:16])
|
|
|
|
}
|
2014-08-29 18:24:41 +00:00
|
|
|
|
|
|
|
// decodeMsgPack is used to decode a MsgPack encoded object
|
|
|
|
func decodeMsgPack(buf []byte, out interface{}) error {
|
|
|
|
return codec.NewDecoder(bytes.NewReader(buf), msgpackHandle).Decode(out)
|
|
|
|
}
|
|
|
|
|
|
|
|
// encodeMsgPack is used to encode an object with msgpack
|
|
|
|
func encodeMsgPack(msg interface{}) ([]byte, error) {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
err := codec.NewEncoder(&buf, msgpackHandle).Encode(msg)
|
|
|
|
return buf.Bytes(), err
|
|
|
|
}
|
2015-01-08 03:11:21 +00:00
|
|
|
|
|
|
|
// stringHash returns a simple md5sum for a string.
|
|
|
|
func stringHash(s string) string {
|
|
|
|
return fmt.Sprintf("%x", md5.Sum([]byte(s)))
|
|
|
|
}
|
2015-01-20 21:44:27 +00:00
|
|
|
|
2015-01-21 02:53:18 +00:00
|
|
|
// FilePermissions is an interface which allows a struct to set
|
|
|
|
// ownership and permissions easily on a file it describes.
|
|
|
|
type FilePermissions interface {
|
|
|
|
// User returns a user ID or user name
|
|
|
|
User() string
|
|
|
|
|
|
|
|
// Group returns a group ID. Group names are not supported.
|
|
|
|
Group() string
|
|
|
|
|
|
|
|
// Mode returns a string of file mode bits e.g. "0644"
|
|
|
|
Mode() string
|
|
|
|
}
|
|
|
|
|
2015-01-20 21:44:27 +00:00
|
|
|
// setFilePermissions handles configuring ownership and permissions settings
|
2015-01-21 02:53:18 +00:00
|
|
|
// on a given file. It takes a path and any struct implementing the
|
|
|
|
// FilePermissions interface. All permission/ownership settings are optional.
|
|
|
|
// If no user or group is specified, the current user/group will be used. Mode
|
|
|
|
// is optional, and has no default (the operation is not performed if absent).
|
|
|
|
// User may be specified by name or ID, but group may only be specified by ID.
|
|
|
|
func setFilePermissions(path string, p FilePermissions) error {
|
2015-01-20 21:44:27 +00:00
|
|
|
var err error
|
|
|
|
uid, gid := os.Getuid(), os.Getgid()
|
2015-01-21 00:21:23 +00:00
|
|
|
|
2015-01-21 02:53:18 +00:00
|
|
|
if p.User() != "" {
|
|
|
|
if uid, err = strconv.Atoi(p.User()); err == nil {
|
2015-01-21 00:21:23 +00:00
|
|
|
goto GROUP
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try looking up the user by name
|
2015-01-21 02:53:18 +00:00
|
|
|
if u, err := user.Lookup(p.User()); err == nil {
|
2015-01-21 00:21:23 +00:00
|
|
|
uid, _ = strconv.Atoi(u.Uid)
|
|
|
|
goto GROUP
|
2015-01-20 21:44:27 +00:00
|
|
|
}
|
2015-01-21 00:21:23 +00:00
|
|
|
|
2015-01-21 02:53:18 +00:00
|
|
|
return fmt.Errorf("invalid user specified: %v", p.User())
|
2015-01-20 21:44:27 +00:00
|
|
|
}
|
2015-01-21 00:21:23 +00:00
|
|
|
|
|
|
|
GROUP:
|
2015-01-21 02:53:18 +00:00
|
|
|
if p.Group() != "" {
|
|
|
|
if gid, err = strconv.Atoi(p.Group()); err != nil {
|
|
|
|
return fmt.Errorf("invalid group specified: %v", p.Group())
|
2015-01-20 21:44:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := os.Chown(path, uid, gid); err != nil {
|
2015-01-21 00:21:23 +00:00
|
|
|
return fmt.Errorf("failed setting ownership to %d:%d on %q: %s",
|
2015-01-20 21:44:27 +00:00
|
|
|
uid, gid, path, err)
|
|
|
|
}
|
|
|
|
|
2015-01-21 02:53:18 +00:00
|
|
|
if p.Mode() != "" {
|
|
|
|
mode, err := strconv.ParseUint(p.Mode(), 8, 32)
|
2015-01-20 21:44:27 +00:00
|
|
|
if err != nil {
|
2015-01-21 02:53:18 +00:00
|
|
|
return fmt.Errorf("invalid mode specified: %v", p.Mode())
|
2015-01-20 21:44:27 +00:00
|
|
|
}
|
|
|
|
if err := os.Chmod(path, os.FileMode(mode)); err != nil {
|
|
|
|
return fmt.Errorf("failed setting permissions to %d on %q: %s",
|
|
|
|
mode, path, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|