Exemple #1
0
func client(w http.ResponseWriter, id string) (client *engine.Client, ok bool) {
	if misc.ZeroOrNil(id) {
		client, err := engine.Docker()
		if err == nil {
			return client, true
		}
		return nil, false
	}
	masters, err := models.LoadDockerClients()
	if err != nil {
		renderErrorJSON(w, err)
		return nil, false
	}
	for _, master := range masters {
		if !master.IsActive {
			continue
		}
		if master.ID == id {
			engine.Configure(master.Endpoint, master.CertPath)
			client, err := engine.Docker()
			if err != nil {
				master.IsActive = false
				master.Save()
			}
			if err == nil {
				return client, true
			}
			break
		}
	}
	return nil, false
}
Exemple #2
0
func _label(id, client, key string) string {
	var docker *engine.Client
	if misc.ZeroOrNil(client) {
		if c, err := engine.Docker(); err == nil {
			docker = c
		}
	}
	if docker == nil {
		masters, err := models.LoadDockerClients()
		if err != nil {
			return id
		}
		for _, master := range masters {
			if master.ID == client {
				engine.Configure(master.Endpoint, master.CertPath)
				if c, err := engine.Docker(); err == nil {
					docker = c
				}
				break
			}
		}
	}
	if docker != nil {
		meta := docker.InspectContainer(id)
		if meta.Error != nil {
			return id
		}
		if name, found := meta.Container.Config.Labels[key]; found {
			return name
		}
	}
	return id
}
func clients(w http.ResponseWriter) (clients []*engine.Client, ok bool) {
	masters, err := models.LoadDockerClients()
	if err != nil {
		renderErrorJSON(w, err)
		return nil, false
	}
	for _, master := range masters {
		if !master.IsActive {
			continue
		}
		engine.Configure(master.Endpoint, master.CertPath)
		client, err := engine.Docker()
		if err != nil {
			continue
		}
		clients = append(clients, client)
	}
	return clients, true
}
Exemple #4
0
func init() {
	cfg := config.NewConfig()

	http.Handle("/clients", util.Chain(func(w http.ResponseWriter, r *http.Request) {
		params := struct{ ViewOnly bool }{cfg.ViewOnly}
		util.RenderHTML(w, []string{"clients/index.tmpl"}, params, nil)
	}))
	http.Handle("/clients/export", util.Chain(func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Content-Disposition", "attachment; filename=docker-clients.json")
		w.Header().Set("Content-Type", "application/force-download")
		http.ServeFile(w, r, models.DockerClientSavePath)
	}))
	http.Handle("/clients/import", util.Chain(func(w http.ResponseWriter, r *http.Request) {
		var err error
		if err = r.ParseMultipartForm(32 << 20); nil != err {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
		for _, headers := range r.MultipartForm.File {
			var in multipart.File
			if in, err = headers[0].Open(); nil != err {
				http.Error(w, err.Error(), http.StatusInternalServerError)
				return
			}
			defer in.Close()

			var out *os.File
			if out, err = os.Create(models.DockerClientSavePath); nil != err {
				http.Error(w, err.Error(), http.StatusInternalServerError)
				return
			}
			defer out.Close()

			if _, err = io.Copy(out, in); nil != err {
				http.Error(w, err.Error(), http.StatusInternalServerError)
				return
			}
		}
		w.WriteHeader(http.StatusOK)
	}))

	/**
	 * Docker client's API
	 */
	http.Handle("/api/clients", util.Chain(func(w http.ResponseWriter, r *http.Request) {
		result := []cInformation{}
		clients, err := models.LoadDockerClients()
		if err != nil {
			renderErrorJSON(w, err)
			return
		}
		c := make(chan cInformation, len(clients))
		for _, client := range clients {
			go func(client *models.DockerClient) {
				engine.Configure(client.Endpoint, client.CertPath)
				client.IsActive = true

				docker, err := engine.Docker()
				if err != nil {
					client.IsActive = false
					c <- cInformation{client, nil, nil}
					return
				}
				info, _ := docker.Info()
				version, _ := docker.Version()
				c <- cInformation{client, info, version}
			}(client)
		}
		for i := 0; i < len(clients); i++ {
			info := <-c
			info.Client.Save()
			result = append(result, info)
		}
		close(c)
		util.RenderJSON(w, result, nil)
	}))

	http.Handle("/api/client/", util.Chain(func(w http.ResponseWriter, r *http.Request) {
		if endpoint, found := util.RequestPostParam(r, "endpoint"); found {
			cert, _ := util.RequestPostParam(r, "cert")
			engine.Configure(endpoint, cert)
			engine.Save()
			_, err := engine.Docker()
			if err != nil {
				models.RemoveDockerClientByEndpoint(endpoint)
				http.Error(w, err.Error(), http.StatusInternalServerError)
				return
			}
			http.Redirect(w, r, "/api/client/", http.StatusFound)
			return
		}
		if r.Method == "DELETE" {
			models.RemoveDockerClient(r.URL.Path[len("/api/client/"):])
			w.WriteHeader(http.StatusOK)
			return
		}
		docker, err := engine.Docker()
		if err != nil {
			renderErrorJSON(w, err)
			return
		}
		info, _ := docker.Info()
		version, _ := docker.Version()
		util.RenderJSON(w, cInformation{nil, info, version}, nil)
	}))
}