예제 #1
0
func (e *ListSupervisorsExecutor) Execute(t *Task) (err error) {
	e.reply.Supervisors, err = datamodel.ListSupervisors()
	if err != nil {
		e.reply.Status = StatusError
	} else {
		sort.Strings(e.reply.Supervisors)
		e.reply.Status = StatusOk
	}
	return err
}
예제 #2
0
func updateSupervisors(name string, ips []string) error {
	// update all supervisors
	supers, err := datamodel.ListSupervisors()
	if err != nil {
		return err
	}
	for _, host := range supers {
		if _, err := supervisor.UpdateIPGroup(host, name, ips); err != nil {
			return err
		}
	}
	return nil
}
예제 #3
0
func DeleteIPGroup(name string) error {
	lock.Lock()
	defer lock.Unlock()
	log.Printf("[DeleteIPGroup] Deleting " + name)
	// delete the IP group
	group, err := datamodel.GetIPGroup(name)
	if err != nil {
		return err
	}
	if err := group.Delete(); err != nil {
		return err
	}
	// update all supervisors
	supers, err := datamodel.ListSupervisors()
	if err != nil {
		return err
	}
	for _, host := range supers {
		if _, err := supervisor.DeleteIPGroup(host, name); err != nil {
			return err
		}
	}
	return nil
}
예제 #4
0
func GetUsage() (map[string]*SupervisorUsage, error) {
	// for each supervisor
	//   get health check to figure out total CPUShares, Memory, Price
	//   get list of containers
	//   fill in data in SupervisorUsage
	supers, err := datamodel.ListSupervisors()
	if err != nil {
		return nil, err
	}
	usageMap := map[string]*SupervisorUsage{}
	for _, super := range supers {
		usage := &SupervisorUsage{Containers: map[string]*ContainerUsage{}}
		hreply, err := supervisor.HealthCheck(super)
		if err != nil {
			return nil, err
		}
		usage.Host = super
		price := hreply.Price
		total_cpu := hreply.CPUShares.Total
		total_mem := hreply.Memory.Total
		usage.TotalPrice = price
		usage.TotalContainers = hreply.Containers.Total
		usage.TotalCPUShares = total_cpu
		usage.TotalMemory = total_mem
		lreply, err := supervisor.List(super)
		if err != nil {
			return nil, err
		}
		var conts uint = 0
		var cpu uint = 0
		var mem uint = 0
		var cpu_price float64 = 0.0
		var mem_price float64 = 0.0
		for id, cont := range lreply.Containers {
			conts += 1
			cpu += cont.Manifest.CPUShares
			mem += cont.Manifest.MemoryLimit
			c := price * (float64(cont.Manifest.CPUShares) / float64(total_cpu))
			m := price * (float64(cont.Manifest.MemoryLimit) / float64(total_mem))
			cpu_price += c
			mem_price += m
			usage.Containers[id] = &ContainerUsage{
				ID:        id,
				App:       cont.App,
				Sha:       cont.Sha,
				Env:       cont.Env,
				CPUShares: cont.Manifest.CPUShares,
				Memory:    cont.Manifest.MemoryLimit,
				CPUPrice:  toPrice(c),
				MemPrice:  toPrice(m),
			}
		}
		usage.UsedContainers = conts
		usage.UsedCPUShares = cpu
		usage.UsedMemory = mem
		usage.UsedCPUPrice = toPrice(cpu_price)
		usage.UsedMemPrice = toPrice(mem_price)
		usageMap[super] = usage
	}
	return usageMap, nil
}
예제 #5
0
func getContainerIDsToTeardown(t *Task, arg ManagerTeardownArg) (hostMap map[string][]string, err error) {
	hostMap = map[string][]string{} // map of host -> []string container ids
	// TODO(edanaher,2014-07-02): This pile of conditionals is braindead and caused us to ignore an environment
	// with no sha, tearing down all of of an app instead of just one environment.
	// We really need to fix this to be reasonable, but for the moment, to fix it, I'm just adding another case.
	if arg.All {
		var hosts []string
		hosts, err = datamodel.ListSupervisors()
		if err != nil {
			return nil, errors.New("Error listing hosts: " + err.Error())
		}
		for _, host := range hosts {
			hostMap[host] = []string{}
		}
		return
	} else if arg.ContainerID != "" {
		var instance *datamodel.ZkInstance
		instance, err = datamodel.GetInstance(arg.ContainerID)
		if err != nil {
			return
		}
		hostMap[instance.Host] = []string{arg.ContainerID}
		return
	} else if arg.App != "" {
		containerIDs := []string{}
		if arg.Sha != "" {
			if arg.Env != "" {
				if containerIDs, err = getContainerIDsOfShaEnv(t, arg.App, arg.Sha, arg.Env); err != nil {
					return nil, err
				}
			} else {
				if containerIDs, err = getContainerIDsOfSha(t, arg.App, arg.Sha); err != nil {
					return nil, err
				}
			}
		} else {
			if arg.Env != "" {
				if containerIDs, err = getContainerIDsOfEnv(t, arg.App, arg.Env); err != nil {
					return nil, err
				}
			} else {
				if containerIDs, err = getContainerIDsOfApp(t, arg.App); err != nil {
					return nil, err
				}
			}
		}
		for _, containerID := range containerIDs {
			instance, err := datamodel.GetInstance(containerID)
			if err != nil {
				continue
			}
			currentIDs := hostMap[instance.Host]
			if currentIDs == nil {
				hostMap[instance.Host] = []string{containerID}
			} else {
				hostMap[instance.Host] = append(currentIDs, containerID)
			}
		}
		return
	}
	return nil, errors.New("Invalid Arguments")
}