github.com/akashshinde/docker@v1.9.1/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 (s *router) getEvents(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
    56  	if err := httputils.ParseForm(r); err != nil {
    57  		return err
    58  	}
    59  	since, err := httputils.Int64ValueOrDefault(r, "since", -1)
    60  	if err != nil {
    61  		return err
    62  	}
    63  	until, err := httputils.Int64ValueOrDefault(r, "until", -1)
    64  	if err != nil {
    65  		return err
    66  	}
    67  
    68  	timer := time.NewTimer(0)
    69  	timer.Stop()
    70  	if until > 0 {
    71  		dur := time.Unix(until, 0).Sub(time.Now())
    72  		timer = time.NewTimer(dur)
    73  	}
    74  
    75  	ef, err := filters.FromParam(r.Form.Get("filters"))
    76  	if err != nil {
    77  		return err
    78  	}
    79  
    80  	w.Header().Set("Content-Type", "application/json")
    81  
    82  	// This is to ensure that the HTTP status code is sent immediately,
    83  	// so that it will not block the receiver.
    84  	w.WriteHeader(http.StatusOK)
    85  	if flusher, ok := w.(http.Flusher); ok {
    86  		flusher.Flush()
    87  	}
    88  
    89  	output := ioutils.NewWriteFlusher(w)
    90  	defer output.Close()
    91  
    92  	enc := json.NewEncoder(output)
    93  	d := s.daemon
    94  	es := d.EventsService
    95  	current, l := es.Subscribe()
    96  	defer es.Evict(l)
    97  
    98  	eventFilter := d.GetEventFilter(ef)
    99  	handleEvent := func(ev *jsonmessage.JSONMessage) error {
   100  		if eventFilter.Include(ev) {
   101  			if err := enc.Encode(ev); err != nil {
   102  				return err
   103  			}
   104  		}
   105  		return nil
   106  	}
   107  
   108  	if since == -1 {
   109  		current = nil
   110  	}
   111  	for _, ev := range current {
   112  		if ev.Time < since {
   113  			continue
   114  		}
   115  		if err := handleEvent(ev); err != nil {
   116  			return err
   117  		}
   118  	}
   119  
   120  	var closeNotify <-chan bool
   121  	if closeNotifier, ok := w.(http.CloseNotifier); ok {
   122  		closeNotify = closeNotifier.CloseNotify()
   123  	}
   124  
   125  	for {
   126  		select {
   127  		case ev := <-l:
   128  			jev, ok := ev.(*jsonmessage.JSONMessage)
   129  			if !ok {
   130  				continue
   131  			}
   132  			if err := handleEvent(jev); err != nil {
   133  				return err
   134  			}
   135  		case <-timer.C:
   136  			return nil
   137  		case <-closeNotify:
   138  			logrus.Debug("Client disconnected, stop sending events")
   139  			return nil
   140  		}
   141  	}
   142  }