Exemple #1
0
func (l *Listener) Run(events <-chan *docker.APIEvents) {
	backendCh := l.backend.Watch()
	for {
		select {
		case event := <-events:
			switch event.Status {
			case "start":
				container, err := l.dc.InspectContainer(event.ID)
				if err != nil {
					log.Println("Failed to inspect container:", event.ID, err)
					continue
				}
				l.containers[event.ID] = container
				l.Register(container)
			case "die":
				container, found := l.containers[event.ID]
				if !found {
					log.Println("Unknown container:", event.ID)
					continue
				}
				l.Deregister(container)
			}
		case r := <-backendCh:
			serviceName, instanceName, err := data.DecodePath(r.Node.Key)
			if err != nil {
				log.Println(err)
				continue
			}
			switch {
			case r.Action == "delete" && serviceName == "":
				// everything deleted
				l.services = make(map[string]*service)
				log.Println("All services deleted")
			case r.Action == "delete" && instanceName == "":
				delete(l.services, serviceName)
				log.Println("Service deleted:", serviceName)
			case r.Action == "set" && instanceName == "details":
				s := &service{name: serviceName, details: data.Service{}}
				if err := json.Unmarshal([]byte(r.Node.Value), &s.details); err != nil {
					log.Println("Error unmarshalling: ", err)
					continue
				}
				l.services[serviceName] = &service{name: serviceName, details: data.Service{}}
				log.Println("Service", s.name, "updated:", s.details)
				// See if any containers match now.
				l.Sync()
			}
		}
	}
}
Exemple #2
0
func run() {
	ch := backend.Watch()

	for r := range ch {
		//fmt.Println(r.Action, r.Node)
		serviceName, _, err := data.DecodePath(r.Node.Key)
		if err != nil {
			log.Println(err)
			continue
		}
		if serviceName == "" {
			// everything deleted; can't cope
			continue
		}
		sendToAmber(serviceName)
	}
}
Exemple #3
0
func (l *Listener) run() {
	ch := l.backend.Watch()

	// Send initial state of each service
	l.backend.ForeachServiceInstance(func(name string, _ data.Service) {
		l.send(name)
	}, nil)

	for r := range ch {
		// log.Println(r.Action, r.Node)
		serviceName, _, err := data.DecodePath(r.Node.Key)
		if err != nil {
			log.Println(err)
			continue
		}
		if serviceName == "" {
			// everything deleted; can't cope
			continue
		}
		l.send(serviceName)
	}
}
Exemple #4
0
func run(cmd *cobra.Command, args []string) {
	initialize()
	fmt.Print(len(services), " services:")
	for name := range services {
		fmt.Print(" ", name)
	}
	fmt.Println()
	ch := backend.Watch()

	for r := range ch {
		//fmt.Println(r.Action, r.Node)
		serviceName, instanceName, err := data.DecodePath(r.Node.Key)
		if err != nil {
			log.Println(err)
			continue
		}
		switch r.Action {
		case "create":
			createService(serviceName, data.Service{})
			fmt.Println("Service created:", serviceName, "; there are now", len(services), "services")
		case "delete":
			if serviceName == "" {
				// everything deleted
				services = make(map[string]*service)
				fmt.Println("All services deleted")
			} else if instanceName == "" {
				delete(services, serviceName)
				fmt.Println("Service deleted:", serviceName, "; there are now", len(services), "services")
			} else {
				s, ok := services[serviceName]
				if !ok {
					log.Println("Service not found:", serviceName)
					continue
				}
				delete(s.instances, instanceName)
				fmt.Println("Instance", instanceName, "removed from", s.name, "which now has", len(s.instances), "instances")
			}
		case "set":
			s, ok := services[serviceName]
			if !ok {
				log.Println("Service not found:", serviceName)
				continue
			}
			if instanceName == "details" {
				if err := json.Unmarshal([]byte(r.Node.Value), &s.details); err != nil {
					log.Println("Error unmarshalling: ", err)
					continue
				}
				fmt.Println("Service", s.name, s.details)
			} else {
				var details data.Instance
				if err := json.Unmarshal([]byte(r.Node.Value), &details); err != nil {
					log.Fatal("Error unmarshalling: ", err)
				}
				i := createInstance(s, instanceName, details)
				fmt.Println("Instance", i.name, "is now enrolled in", s.name, "which now has", len(s.instances), "instances")
			}
		default:
			fmt.Println("Unhandled:", r.Action, r.Node)
		}
	}
}