Ejemplo n.º 1
0
// bootstrap initializes machine group workers and checks loaded data for
// consistency.
func (g *Group) bootstrap() {
	var (
		aliasIDs   = g.alias.Registered()
		addressIDs = g.address.Registered()
	)

	// Report and generate missing aliases.
	if noAliases := idset.Diff(addressIDs, aliasIDs); len(noAliases) != 0 {
		g.log.Warning("Missing aliases for %v, regenerating...", noAliases)

		for _, id := range noAliases {
			alias, err := g.alias.Create(id)
			if err != nil {
				g.log.Error("Cannot create alias for %s: %s", id, err)
			}

			g.log.Info("Created alias for %s, %s", id, alias)
		}
	}

	// Start clients for stored IDs.
	for _, id := range addressIDs {
		if err := g.client.Create(id, g.dynamicAddr(id)); err != nil {
			g.log.Error("Cannot create client for %s: %s", id, err)
		}
	}

	n := len(idset.Union(aliasIDs, addressIDs))
	g.log.Info("Detected %d machines, started %d clients.", n, len(g.client.Registered()))
}
Ejemplo n.º 2
0
// balance ensures that stale clients and other resources will be closed and
// removed.
func (g *Group) balance(ids machine.IDSlice) {
	var (
		regAlias   = g.alias.Registered()
		regAddress = g.address.Registered()
		regClient  = g.client.Registered()
	)

	union := idset.Union(idset.Union(regAlias, regAddress), regClient)

	for _, id := range idset.Diff(union, ids) {
		var errored = false

		// Drop machine alias.
		if err := g.alias.Drop(id); err != nil {
			errored = true
			g.log.Warning("Alias of machine %s cannot be removed: %v", id, err)
		}

		// Drop machine client.
		if err := g.client.Drop(id); err != nil {
			errored = true
			g.log.Warning("Client for machine %s cannot be deleted: %v", id, err)
		}

		// Drop all machine addresses.
		if err := g.address.Drop(id); err != nil {
			errored = true
			g.log.Warning("Addresses of %s machine cannot be removed: %v", id, err)
		}

		if !errored {
			g.log.Info("Machine with ID: %s was removed.", id)
		}
	}
}