Esempio n. 1
0
func (self *version1_3) HandleRequest(requestType string, request []string, m manager.Manager, w http.ResponseWriter, r *http.Request) error {
	switch requestType {
	case eventsApi:
		return handleEventRequest(request, m, w, r)
	case hyperApi:
		glog.V(4).Infof("Api - Hyper(%v)", request)

		// Get the query request.
		query, err := getContainerInfoRequest(r.Body)
		if err != nil {
			return err
		}

		var containers map[string]info.ContainerInfo
		// map requests for "hyper/" to "hyper"
		if len(request) == 1 && len(request[0]) == 0 {
			request = request[:0]
		}
		switch len(request) {
		case 0:
			// Get all Hyper pods.
			containers, err = m.AllHyperContainers(query)
			if err != nil {
				return fmt.Errorf("failed to get all Hyper pods with error: %v", err)
			}
		case 1:
			// Get one Hyper pod.
			var cont info.ContainerInfo
			cont, err = m.HyperContainer(request[0], query)
			if err != nil {
				return fmt.Errorf("failed to get Docker container %q with error: %v", request[0], err)
			}
			containers = map[string]info.ContainerInfo{
				cont.Name: cont,
			}
		default:
			return fmt.Errorf("unknown request for Docker container %v", request)
		}

		// Only output the containers as JSON.
		err = writeResult(containers, w)
		if err != nil {
			return err
		}
		return nil
	default:
		return self.baseVersion.HandleRequest(requestType, request, m, w, r)
	}
}
Esempio n. 2
0
func serveHyperPage(m manager.Manager, w http.ResponseWriter, u *url.URL) error {
	start := time.Now()

	// The container name is the path after the handler
	containerName := u.Path[len(HyperPage)-1:]
	rootDir := getRootDir(containerName)

	var data *pageData
	if containerName == "/" {
		// Get the containers.
		reqParams := info.ContainerInfoRequest{
			NumStats: 0,
		}
		conts, err := m.AllHyperContainers(&reqParams)
		if err != nil {
			return fmt.Errorf("failed to get container %q with error: %v", containerName, err)
		}
		subcontainers := make([]link, 0, len(conts))
		for _, cont := range conts {
			subcontainers = append(subcontainers, link{
				Text: getHyperDisplayName(cont.ContainerReference),
				Link: path.Join(rootDir, HyperPage, cont.ContainerReference.Name),
			})
		}

		// // Get Hyper status
		// status, err := m.HyperInfo()
		// if err != nil {
		// 	return err
		// }
		//
		// dockerStatus, driverStatus := toStatusKV(status)
		// // Get Docker Images
		// images, err := m.DockerImages()
		// if err != nil {
		// 	return err
		// }

		hyperContainersText := "Hyper Containers"
		data = &pageData{
			DisplayName: hyperContainersText,
			ParentContainers: []link{
				{
					Text: hyperContainersText,
					Link: path.Join(rootDir, HyperPage),
				}},
			Subcontainers: subcontainers,
			Root:          rootDir,
			//DockerStatus:       dockerStatus,
			// DockerDriverStatus: driverStatus,
			// DockerImages: images,
		}
	} else {
		// Get the container.
		reqParams := info.ContainerInfoRequest{
			NumStats: 60,
		}
		cont, err := m.HyperContainer(containerName, &reqParams)
		if err != nil {
			return fmt.Errorf("failed to get container %q with error: %v", containerName, err)
		}
		displayName := getHyperDisplayName(cont.ContainerReference)

		// Make a list of the parent containers and their links
		var parentContainers []link
		parentContainers = append(parentContainers, link{
			Text: "Hyper Containers",
			Link: path.Join(rootDir, HyperPage),
		})
		parentContainers = append(parentContainers, link{
			Text: displayName,
			Link: path.Join(rootDir, HyperPage, cont.Name),
		})

		// Get the MachineInfo
		machineInfo, err := m.GetMachineInfo()
		if err != nil {
			return err
		}
		data = &pageData{
			DisplayName:            displayName,
			ContainerName:          escapeContainerName(cont.Name),
			ParentContainers:       parentContainers,
			Spec:                   cont.Spec,
			Stats:                  cont.Stats,
			MachineInfo:            machineInfo,
			ResourcesAvailable:     cont.Spec.HasCpu || cont.Spec.HasMemory || cont.Spec.HasNetwork,
			CpuAvailable:           cont.Spec.HasCpu,
			MemoryAvailable:        cont.Spec.HasMemory,
			NetworkAvailable:       cont.Spec.HasNetwork,
			FsAvailable:            cont.Spec.HasFilesystem,
			CustomMetricsAvailable: cont.Spec.HasCustomMetrics,
			Root: rootDir,
		}
	}

	err := pageTemplate.Execute(w, data)
	if err != nil {
		glog.Errorf("Failed to apply template: %s", err)
	}

	glog.V(5).Infof("Request took %s", time.Since(start))
	return nil
}