| 1 |
package detector |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/binary" |
| 5 |
"errors" |
| 6 |
"fmt" |
| 7 |
"io/ioutil" |
| 8 |
"net" |
| 9 |
"strconv" |
| 10 |
"strings" |
| 11 |
"sync" |
| 12 |
|
| 13 |
"github.com/gogo/protobuf/proto" |
| 14 |
log "github.com/golang/glog" |
| 15 |
mesos "github.com/mesos/mesos-go/api/v0/mesosproto" |
| 16 |
util "github.com/mesos/mesos-go/api/v0/mesosutil" |
| 17 |
"github.com/mesos/mesos-go/api/v0/upid" |
| 18 |
) |
| 19 |
|
| 20 |
var ( |
| 21 |
pluginLock sync.Mutex |
| 22 |
plugins = map[string]PluginFactory{} |
| 23 |
EmptySpecError = errors.New("empty master specification") |
| 24 |
|
| 25 |
defaultFactory = PluginFactory(func(spec string, _ ...Option) (Master, error) { |
| 26 |
if len(spec) == 0 { |
| 27 |
return nil, EmptySpecError |
| 28 |
} |
| 29 |
if strings.Index(spec, "@") < 0 { |
| 30 |
spec = "master@" + spec |
| 31 |
} |
| 32 |
if pid, err := upid.Parse(spec); err == nil { |
| 33 |
return NewStandalone(CreateMasterInfo(pid)), nil |
| 34 |
} else { |
| 35 |
return nil, err |
| 36 |
} |
| 37 |
}) |
| 38 |
) |
| 39 |
|
| 40 |
type PluginFactory func(string, ...Option) (Master, error) |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
func Register(prefix string, f PluginFactory) error { |
| 47 |
if prefix == "" { |
| 48 |
return fmt.Errorf("illegal prefix: '%v'", prefix) |
| 49 |
} |
| 50 |
if f == nil { |
| 51 |
return fmt.Errorf("nil plugin factories are not allowed") |
| 52 |
} |
| 53 |
|
| 54 |
pluginLock.Lock() |
| 55 |
defer pluginLock.Unlock() |
| 56 |
|
| 57 |
if _, found := plugins[prefix]; found { |
| 58 |
return fmt.Errorf("detection plugin already registered for prefix '%s'", prefix) |
| 59 |
} |
| 60 |
plugins[prefix] = f |
| 61 |
return nil |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
func New(spec string, options ...Option) (m Master, err error) { |
| 80 |
if strings.HasPrefix(spec, "file://") { |
| 81 |
var body []byte |
| 82 |
path := spec[7:] |
| 83 |
body, err = ioutil.ReadFile(path) |
| 84 |
if err != nil { |
| 85 |
log.V(1).Infof("failed to read from file at '%s'", path) |
| 86 |
} else { |
| 87 |
m, err = New(string(body), options...) |
| 88 |
} |
| 89 |
} else if f, ok := MatchingPlugin(spec); ok { |
| 90 |
m, err = f(spec, options...) |
| 91 |
} else { |
| 92 |
m, err = defaultFactory(spec, options...) |
| 93 |
} |
| 94 |
|
| 95 |
return |
| 96 |
} |
| 97 |
|
| 98 |
func MatchingPlugin(spec string) (PluginFactory, bool) { |
| 99 |
pluginLock.Lock() |
| 100 |
defer pluginLock.Unlock() |
| 101 |
|
| 102 |
for prefix, f := range plugins { |
| 103 |
if strings.HasPrefix(spec, prefix) { |
| 104 |
return f, true |
| 105 |
} |
| 106 |
} |
| 107 |
return nil, false |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
func CreateMasterInfo(pid *upid.UPID) *mesos.MasterInfo { |
| 119 |
if pid == nil { |
| 120 |
return nil |
| 121 |
} |
| 122 |
port, err := strconv.Atoi(pid.Port) |
| 123 |
if err != nil { |
| 124 |
log.Errorf("failed to parse port: %v", err) |
| 125 |
return nil |
| 126 |
} |
| 127 |
|
| 128 |
var ipv4 net.IP |
| 129 |
if ipv4 = net.ParseIP(pid.Host); ipv4 != nil { |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
} else if addrs, err := net.LookupIP(pid.Host); err == nil { |
| 134 |
for _, ip := range addrs { |
| 135 |
if ip = ip.To4(); ip != nil { |
| 136 |
ipv4 = ip |
| 137 |
break |
| 138 |
} |
| 139 |
} |
| 140 |
if ipv4 == nil { |
| 141 |
log.Errorf("host does not resolve to an IPv4 address: %v", pid.Host) |
| 142 |
return nil |
| 143 |
} |
| 144 |
} else { |
| 145 |
log.Errorf("failed to lookup IPs for host '%v': %v", pid.Host, err) |
| 146 |
return nil |
| 147 |
} |
| 148 |
packedip := binary.BigEndian.Uint32(ipv4) |
| 149 |
mi := util.NewMasterInfo(pid.ID, packedip, uint32(port)) |
| 150 |
mi.Pid = proto.String(pid.String()) |
| 151 |
if pid.Host != "" { |
| 152 |
mi.Hostname = proto.String(pid.Host) |
| 153 |
} |
| 154 |
return mi |
| 155 |
} |