コード例 #1
0
ファイル: machinegroup.go プロジェクト: koding/koding
// New creates a new Group object.
func New(opts *GroupOpts) (*Group, error) {
	if err := opts.Valid(); err != nil {
		return nil, err
	}

	g := &Group{}

	// Add logger to group.
	if opts.Log != nil {
		g.log = opts.Log.New("machines")
	} else {
		g.log = machine.DefaultLogger.New("machines")
	}

	// Use default discover client when not set.
	if opts.Discover != nil {
		g.discover = opts.Discover
	} else {
		g.discover = discover.NewClient()
	}

	// Create dynamic clients.
	var err error
	g.client, err = clients.New(&clients.ClientsOpts{
		Builder:         opts.Builder,
		DynAddrInterval: opts.DynAddrInterval,
		PingInterval:    opts.PingInterval,
		Log:             g.log,
	})
	if err != nil {
		g.log.Critical("Cannot create machine monitor: %s", err)
		return nil, err
	}

	// Add default components.
	g.address = addresses.New()
	g.alias = aliases.New()
	if opts.Storage == nil {
		return g, nil
	}

	// Try to add storage for Addresses.
	if address, err := addresses.NewCached(opts.Storage); err != nil {
		g.log.Warning("Cannot load addresses cache: %s", err)
	} else {
		g.address = address
	}

	// Try to add storage for Aliases.
	if alias, err := aliases.NewCached(opts.Storage); err != nil {
		g.log.Warning("Cannot load aliases cache: %s", err)
	} else {
		g.alias = alias
	}

	// Start memory workers.
	g.bootstrap()

	return g, nil
}
コード例 #2
0
ファイル: machine.go プロジェクト: koding/koding
// NewMachine initializes a new Machine struct with any internal vars created.
func NewMachine(meta MachineMeta, log logging.Logger, t Transport) (*Machine, error) {
	log = MachineLogger(meta, log)

	// Create our Pingers, to be used in the PingTrackers
	kitePinger := kitepinger.NewKitePinger(t)
	httpPinger, err := kitepinger.NewKiteHTTPPinger(meta.URL)
	if err != nil {
		log.Error(
			"Unable to create HTTPPinger from meta.URL. url:%s, err:%s", meta.URL, err,
		)
		return nil, err
	}

	m := &Machine{
		MachineMeta: meta,
		Log:         log,
		KiteTracker: kitepinger.NewPingTracker(kitePinger),
		HTTPTracker: kitepinger.NewPingTracker(httpPinger),
		Transport:   t,
		discover:    discover.NewClient(),
		mountLocker: util.NewMutexWithState(),
	}

	m.discover.Log = m.Log.New("discover")

	// Start our http pinger, to give online/offline statuses for all machines.
	m.HTTPTracker.Start()

	return m, nil
}
コード例 #3
0
ファイル: discover_test.go プロジェクト: koding/koding
func TestDiscover(t *testing.T) {
	args := args()
	if len(args) != 2 {
		t.Skip("usage: go test -run TestDiscover -- addr service")
	}

	e, err := discover.NewClient().Discover(args[0], args[1])
	if err != nil {
		t.Fatalf("Discover(%s, %s)=%s", args[0], args[1], err)
	}

	p, err := json.MarshalIndent(e, "", "\t")
	if err != nil {
		t.Fatal(err)
	}

	os.Stderr.Write(p)
}