github.com/a4a881d4/docker@v1.9.0-rc2/api/server/router/local/info.go (about)

     1  package local
     2  
     3  import (
     4  	"encoding/json"
     5  	"net/http"
     6  	"runtime"
     7  	"time"
     8  
     9  	"github.com/Sirupsen/logrus"
    10  	"github.com/docker/docker/api"
    11  	"github.com/docker/docker/api/server/httputils"
    12  	"github.com/docker/docker/api/types"
    13  	"github.com/docker/docker/autogen/dockerversion"
    14  	"github.com/docker/docker/pkg/ioutils"
    15  	"github.com/docker/docker/pkg/jsonmessage"
    16  	"github.com/docker/docker/pkg/parsers/filters"
    17  	"github.com/docker/docker/pkg/parsers/kernel"
    18  	"github.com/docker/docker/utils"
    19  	"golang.org/x/net/context"
    20  )
    21  
    22  func (s *router) getVersion(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
    23  	v := &types.Version{
    24  		Version:    dockerversion.VERSION,
    25  		APIVersion: api.Version,
    26  		GitCommit:  dockerversion.GITCOMMIT,
    27  		GoVersion:  runtime.Version(),
    28  		Os:         runtime.GOOS,
    29  		Arch:       runtime.GOARCH,
    30  		BuildTime:  dockerversion.BUILDTIME,
    31  	}
    32  
    33  	version := httputils.VersionFromContext(ctx)
    34  
    35  	if version.GreaterThanOrEqualTo("1.19") {
    36  		v.Experimental = utils.ExperimentalBuild()
    37  	}
    38  
    39  	if kernelVersion, err := kernel.GetKernelVersion(); err == nil {
    40  		v.KernelVersion = kernelVersion.String()
    41  	}
    42  
    43  	return httputils.WriteJSON(w, http.StatusOK, v)
    44  }
    45  
    46  func (s *router) getInfo(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
    47  	info, err := s.daemon.SystemInfo()
    48  	if err != nil {
    49  		return err
    50  	}
    51  
    52  	return httputils.WriteJSON(w, http.StatusOK, info)
    53  }
    54  
    55  func buildOutputEncoder(w http.ResponseWriter) *json.Encoder {
    56  	w.Header().Set("Content-Type", "application/json")
    57  	outStream := ioutils.NewWriteFlusher(w)
    58  	// Write an empty chunk of data.
    59  	// This is to ensure that the HTTP status code is sent immediately,
    60  	// so that it will not block the receiver.
    61  	outStream.Write(nil)
    62  	return json.NewEncoder(outStream)
    63  }
    64  
    65  func (s *router) getEvents(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
    66  	if err := httputils.ParseForm(r); err != nil {
    67  		return err
    68  	}
    69  	since, err := httputils.Int64ValueOrDefault(r, "since", -1)
    70  	if err != nil {
    71  		return err
    72  	}
    73  	until, err := httputils.Int64ValueOrDefault(r, "until", -1)
    74  	if err != nil {
    75  		return err
    76  	}
    77  
    78  	timer := time.NewTimer(0)
    79  	timer.Stop()
    80  	if until > 0 {
    81  		dur := time.Unix(until, 0).Sub(time.Now())
    82  		timer = time.NewTimer(dur)
    83  	}
    84  
    85  	ef, err := filters.FromParam(r.Form.Get("filters"))
    86  	if err != nil {
    87  		return err
    88  	}
    89  
    90  	enc := buildOutputEncoder(w)
    91  	d := s.daemon
    92  	es := d.EventsService
    93  	current, l := es.Subscribe()
    94  	defer es.Evict(l)
    95  
    96  	eventFilter := d.GetEventFilter(ef)
    97  	handleEvent := func(ev *jsonmessage.JSONMessage) error {
    98  		if eventFilter.Include(ev) {
    99  			if err := enc.Encode(ev); err != nil {
   100  				return err
   101  			}
   102  		}
   103  		return nil
   104  	}
   105  
   106  	if since == -1 {
   107  		current = nil
   108  	}
   109  	for _, ev := range current {
   110  		if ev.Time < since {
   111  			continue
   112  		}
   113  		if err := handleEvent(ev); err != nil {
   114  			return err
   115  		}
   116  	}
   117  
   118  	var closeNotify <-chan bool
   119  	if closeNotifier, ok := w.(http.CloseNotifier); ok {
   120  		closeNotify = closeNotifier.CloseNotify()
   121  	}
   122  
   123  	for {
   124  		select {
   125  		case ev := <-l:
   126  			jev, ok := ev.(*jsonmessage.JSONMessage)
   127  			if !ok {
   128  				continue
   129  			}
   130  			if err := handleEvent(jev); err != nil {
   131  				return err
   132  			}
   133  		case <-timer.C:
   134  			return nil
   135  		case <-closeNotify:
   136  			logrus.Debug("Client disconnected, stop sending events")
   137  			return nil
   138  		}
   139  	}
   140  }