Пример #1
0
func routes(config Configuration, sts *stats.Stats) *mux.Router {
	ctx := &appContext{
		hostIP: config.HostIP,
	}

	logger.Instance().Debugf("Configurando API de Docker con los parametros %+v", config)

	var err error

	if config.TLSVerify {
		ctx.client, err = docker.NewTLSClient(config.Address, config.TLSCert, config.TLSKey, config.TLSCacert)
	} else if config.TLS {
		certPEMBlock, err := ioutil.ReadFile(config.TLSCert)
		if err != nil {
			logger.Instance().Fatalln("Error en el certificado cliente", err)
		}

		keyPEMBlock, err := ioutil.ReadFile(config.TLSKey)
		if err != nil {
			logger.Instance().Fatalln("Error en la llave del certificado cliente", err)
		}

		var caPEMCert []byte

		ctx.client, err = docker.NewTLSClientFromBytes(config.Address, certPEMBlock, keyPEMBlock, caPEMCert)
	} else {
		ctx.client, err = docker.NewClient(config.Address)
	}

	if err != nil {
		logger.Instance().Fatalln("Error al crear el cliente", err)
	}

	router := mux.NewRouter()

	router.Handle("/stats", &statsHandler{sts}).Methods("GET")

	// API v1
	v1Services := router.PathPrefix("/api/v1").Subrouter()

	for method, mappings := range routesMap {
		for path, h := range mappings {
			v1Services.Handle(path, errorHandler{h, ctx}).Methods(method)
		}
	}

	return router
}
Пример #2
0
func (eh errorHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	if err := eh.handler(eh.appCtx, w, r); err != nil {
		logger.Instance().Errorln(err)
		if err2, ok := err.(apiError); ok {
			jsonRenderer(w, err2)
			return
		}
		jsonRenderer(w, NewUnknownError(err.Error()))
	}
}
Пример #3
0
func getInspectContainer(c *appContext, w http.ResponseWriter, r *http.Request) error {
	vars := mux.Vars(r)
	id, ok := vars["id"]
	if !ok {
		return NewContainerNotFound(id)
	}

	container, err := c.client.InspectContainer(id)
	if err != nil {
		switch err.(type) {
		case *docker.NoSuchContainer:
			return NewContainerNotFound(id)
		default:
			return fmt.Errorf("Problema al obtener el contenedor %s", err)
		}

	}

	logger.Instance().Debugf("Container Dump %+v", container)

	ports := make(map[string]types.Port)

	for k, publicMap := range container.NetworkSettings.Ports {
		if len(publicMap) == 0 {
			continue
		}

		p, ok := ports[k.Port()]
		if !ok {
			p = types.Port{
				Type: k.Proto(),
			}
		}
		for _, publicPorts := range publicMap {
			p.Publics = append(p.Publics, publicPorts.HostPort)
		}
		ports[k.Port()] = p
	}

	resp := types.Container{
		ID:     container.ID,
		HostIP: c.hostIP,
		Ports:  ports,
	}

	jsonRenderer(w, map[string]interface{}{
		"status":    http.StatusOK,
		"container": resp})
	return nil
}
Пример #4
0
// RunApp punto de entrada para la aplicacion
// Procesa todos los comandos y argumentos de la APP
func RunApp() {
	app := cli.NewApp()
	app.Name = "inspector"
	app.Usage = "API para visualizar informacion de contenedores."
	app.Version = version.VERSION + " (" + version.GITCOMMIT + ")"

	app.Flags = globalFlags()

	app.Before = func(c *cli.Context) error {
		return setupApplication(c)
	}

	app.Commands = commands

	if err := app.Run(os.Args); err != nil {
		logger.Instance().Fatalln(err)
	}
}