diff options
author | Sebastien Douheret <sebastien.douheret@iot.bzh> | 2018-02-23 18:52:49 +0100 |
---|---|---|
committer | Sebastien Douheret <sebastien.douheret@iot.bzh> | 2018-04-05 01:29:05 +0200 |
commit | 069de98bdd926cb25954aad94fe23be0272a7b5e (patch) | |
tree | cb77c241cc0a257d574187e333e106b811ad4f3d /lib | |
parent | 6aa5e4ccb5adadcc0cb802c44c1e88a35a20a925 (diff) |
Added target and terminal support in Dashboard
Signed-off-by: Sebastien Douheret <sebastien.douheret@iot.bzh>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/agent/apiv1-targets.go | 105 | ||||
-rw-r--r-- | lib/agent/apiv1.go | 6 | ||||
-rw-r--r-- | lib/agent/projects.go | 2 | ||||
-rw-r--r-- | lib/xaapiv1/events.go | 68 |
4 files changed, 171 insertions, 10 deletions
diff --git a/lib/agent/apiv1-targets.go b/lib/agent/apiv1-targets.go index 5a7862a..cc08822 100644 --- a/lib/agent/apiv1-targets.go +++ b/lib/agent/apiv1-targets.go @@ -18,6 +18,7 @@ package agent import ( + "encoding/json" "fmt" "net/http" @@ -52,6 +53,110 @@ func (s *APIService) targetsPassthroughInit(svr *XdsServer) error { return nil } +// targetsEventsForwardInit Register events forwarder for targets +func (s *APIService) targetsEventsForwardInit(svr *XdsServer) error { + + if !svr.Connected { + return fmt.Errorf("Cannot register events: XDS Server %v not connected", svr.ID) + } + + // Forward Target events from XDS-server to client + if _, err := svr.EventOn(xsapiv1.EVTTargetAdd, xaapiv1.EVTTargetAdd, s._targetsEventCB); err != nil { + s.Log.Errorf("XDS Server EventOn '%s' failed: %v", xsapiv1.EVTTargetAdd, err) + return err + } + if _, err := svr.EventOn(xsapiv1.EVTTargetRemove, xaapiv1.EVTTargetRemove, s._targetsEventCB); err != nil { + s.Log.Errorf("XDS Server EventOn '%s' failed: %v", xsapiv1.EVTTargetRemove, err) + return err + } + if _, err := svr.EventOn(xsapiv1.EVTTargetStateChange, xaapiv1.EVTTargetStateChange, s._targetsEventCB); err != nil { + s.Log.Errorf("XDS Server EventOn '%s' failed: %v", xsapiv1.EVTTargetStateChange, err) + return err + } + + return nil +} + +func (s *APIService) _targetsEventCB(privD interface{}, data interface{}) error { + evt := xsapiv1.EventMsg{} + d, err := json.Marshal(data) + if err != nil { + s.Log.Errorf("Cannot marshal XDS Server Target event err=%v, data=%v", err, data) + return err + } + if err = json.Unmarshal(d, &evt); err != nil { + s.Log.Errorf("Cannot unmarshal XDS Server Target event err=%v, d=%v", err, string(d)) + return err + } + + // assume that xsapiv1.TargetConfig == xaapiv1.TargetConfig + target, err := evt.DecodeTargetEvent() + if err != nil { + s.Log.Errorf("Cannot decode XDS Server Target event: err=%v, data=%v", err, data) + return err + } + + evtName := privD.(string) + + if err := s.events.Emit(evtName, target, ""); err != nil { + s.Log.Warningf("Cannot notify %s (from server): %v", evtName, err) + return err + } + return nil +} + +// terminalsEventsForwardInit Register events forwarder for terminals +func (s *APIService) terminalsEventsForwardInit(svr *XdsServer) error { + + if !svr.Connected { + return fmt.Errorf("Cannot register events: XDS Server %v not connected", svr.ID) + } + + // Forward Terminal events from XDS-server to client + if _, err := svr.EventOn(xsapiv1.EVTTargetTerminalAdd, xaapiv1.EVTTargetTerminalAdd, s._terminalsEventCB); err != nil { + s.Log.Errorf("XDS Server EventOn '%s' failed: %v", xsapiv1.EVTTargetTerminalAdd, err) + return err + } + if _, err := svr.EventOn(xsapiv1.EVTTargetTerminalRemove, xaapiv1.EVTTargetTerminalRemove, s._terminalsEventCB); err != nil { + s.Log.Errorf("XDS Server EventOn '%s' failed: %v", xsapiv1.EVTTargetTerminalRemove, err) + return err + } + if _, err := svr.EventOn(xsapiv1.EVTTargetTerminalStateChange, xaapiv1.EVTTargetTerminalStateChange, s._terminalsEventCB); err != nil { + s.Log.Errorf("XDS Server EventOn '%s' failed: %v", xsapiv1.EVTTargetTerminalStateChange, err) + return err + } + + return nil +} + +func (s *APIService) _terminalsEventCB(privD interface{}, data interface{}) error { + evt := xsapiv1.EventMsg{} + d, err := json.Marshal(data) + if err != nil { + s.Log.Errorf("Cannot marshal XDS Server Target event err=%v, data=%v", err, data) + return err + } + if err = json.Unmarshal(d, &evt); err != nil { + s.Log.Errorf("Cannot unmarshal XDS Server Target event err=%v, d=%v", err, string(d)) + return err + } + + // assume that xsapiv1.TargetConfig == xaapiv1.TargetConfig + target, err := evt.DecodeTerminalEvent() + if err != nil { + s.Log.Errorf("Cannot decode XDS Server Target event: err=%v, data=%v", err, data) + return err + } + + evtName := privD.(string) + + if err := s.events.Emit(evtName, target, ""); err != nil { + s.Log.Warningf("Cannot notify %s (from server): %v", evtName, err) + return err + } + return nil +} + // GetServerFromTargetID Retrieve XDS Server definition from a target ID func (s *APIService) GetServerFromTargetID(targetID, termID string) (*XdsServer, string, error) { diff --git a/lib/agent/apiv1.go b/lib/agent/apiv1.go index 730e7c0..97165b3 100644 --- a/lib/agent/apiv1.go +++ b/lib/agent/apiv1.go @@ -137,6 +137,12 @@ func (s *APIService) AddXdsServer(cfg xdsconfig.XDSServerConf) (*XdsServer, erro if err := s.sdksEventsForwardInit(server); err != nil { s.Log.Errorf("XDS Server %v - sdk events forwarding error: %v", server.ID, err) } + if err := s.targetsEventsForwardInit(server); err != nil { + s.Log.Errorf("XDS Server %v - target events forwarding error: %v", server.ID, err) + } + if err := s.terminalsEventsForwardInit(server); err != nil { + s.Log.Errorf("XDS Server %v - terminal events forwarding error: %v", server.ID, err) + } // Load projects if err := s.projects.Init(server); err != nil { diff --git a/lib/agent/projects.go b/lib/agent/projects.go index ff28f96..0bd5315 100644 --- a/lib/agent/projects.go +++ b/lib/agent/projects.go @@ -26,10 +26,10 @@ import ( "time" st "gerrit.automotivelinux.org/gerrit/src/xds/xds-agent/lib/syncthing" + "gerrit.automotivelinux.org/gerrit/src/xds/xds-server.git/lib/xsapiv1" "gerrit.automotivelinux.org/gerrit/src/xds/xds-agent/lib/xaapiv1" common "gerrit.automotivelinux.org/gerrit/src/xds/xds-common.git/golib" - "gerrit.automotivelinux.org/gerrit/src/xds/xds-server.git/lib/xsapiv1" "github.com/franciscocpg/reflectme" "github.com/syncthing/syncthing/lib/sync" ) diff --git a/lib/xaapiv1/events.go b/lib/xaapiv1/events.go index 6520057..3a47e49 100644 --- a/lib/xaapiv1/events.go +++ b/lib/xaapiv1/events.go @@ -40,15 +40,25 @@ const ( EventTypePrefix = "event:" // following by event type // Supported Events type - EVTAll = EventTypePrefix + "all" - EVTServerConfig = EventTypePrefix + "server-config" // type EventMsg with Data type xaapiv1.ServerCfg - EVTProjectAdd = EventTypePrefix + "project-add" // type EventMsg with Data type xaapiv1.ProjectConfig - EVTProjectDelete = EventTypePrefix + "project-delete" // type EventMsg with Data type xaapiv1.ProjectConfig - EVTProjectChange = EventTypePrefix + "project-state-change" // type EventMsg with Data type xaapiv1.ProjectConfig - EVTSDKAdd = EventTypePrefix + "sdk-add" // type EventMsg with Data type xaapiv1.SDK - EVTSDKRemove = EventTypePrefix + "sdk-remove" // type EventMsg with Data type xaapiv1.SDK - EVTSDKManagement = EventTypePrefix + "sdk-management" // type EventMsg with Data type xaapiv1.SDKManagementMsg - EVTSDKStateChange = EventTypePrefix + "sdk-state-change" // type EventMsg with Data type xaapiv1.SDK + EVTAll = EventTypePrefix + "all" + + EVTServerConfig = EventTypePrefix + "server-config" // type EventMsg with Data type xaapiv1.ServerCfg + EVTProjectAdd = EventTypePrefix + "project-add" // type EventMsg with Data type xaapiv1.ProjectConfig + EVTProjectDelete = EventTypePrefix + "project-delete" // type EventMsg with Data type xaapiv1.ProjectConfig + EVTProjectChange = EventTypePrefix + "project-state-change" // type EventMsg with Data type xaapiv1.ProjectConfig + + EVTSDKAdd = EventTypePrefix + "sdk-add" // type EventMsg with Data type xaapiv1.SDK + EVTSDKRemove = EventTypePrefix + "sdk-remove" // type EventMsg with Data type xaapiv1.SDK + EVTSDKManagement = EventTypePrefix + "sdk-management" // type EventMsg with Data type xaapiv1.SDKManagementMsg + EVTSDKStateChange = EventTypePrefix + "sdk-state-change" // type EventMsg with Data type xaapiv1.SDK + + EVTTargetAdd = EventTypePrefix + "target-add" // type EventMsg with Data type xaapiv1.TargetConfig + EVTTargetRemove = EventTypePrefix + "target-remove" // type EventMsg with Data type xaapiv1.TargetConfig + EVTTargetStateChange = EventTypePrefix + "target-state-change" // type EventMsg with Data type xaapiv1.TargetConfig + + EVTTargetTerminalAdd = EventTypePrefix + "target-terminal-add" // type EventMsg with Data type xaapiv1.TerminalConfig + EVTTargetTerminalRemove = EventTypePrefix + "target-terminal-remove" // type EventMsg with Data type xaapiv1.TerminalConfig + EVTTargetTerminalStateChange = EventTypePrefix + "target-terminal-state-change" // type EventMsg with Data type xaapiv1.TerminalConfig ) // EVTAllList List of all supported events @@ -61,6 +71,12 @@ var EVTAllList = []string{ EVTSDKRemove, EVTSDKManagement, EVTSDKStateChange, + EVTTargetAdd, + EVTTargetRemove, + EVTTargetStateChange, + EVTTargetTerminalAdd, + EVTTargetTerminalRemove, + EVTTargetTerminalStateChange, } // EventMsg Event message send over Websocket, data format depend to Type (see DecodeXXX function) @@ -132,3 +148,37 @@ func (e *EventMsg) DecodeSDKEvent() (SDK, error) { } return s, err } + +// DecodeTargetEvent Helper to decode Data field type TargetConfig +func (e *EventMsg) DecodeTargetEvent() (TargetConfig, error) { + var err error + p := TargetConfig{} + switch e.Type { + case EVTTargetAdd, EVTTargetRemove, EVTTargetStateChange: + d := []byte{} + d, err = json.Marshal(e.Data) + if err == nil { + err = json.Unmarshal(d, &p) + } + default: + err = fmt.Errorf("Invalid type") + } + return p, err +} + +// DecodeTerminalEvent Helper to decode Data field type TerminalConfig +func (e *EventMsg) DecodeTerminalEvent() (TerminalConfig, error) { + var err error + p := TerminalConfig{} + switch e.Type { + case EVTTargetTerminalAdd, EVTTargetTerminalRemove, EVTTargetTerminalStateChange: + d := []byte{} + d, err = json.Marshal(e.Data) + if err == nil { + err = json.Unmarshal(d, &p) + } + default: + err = fmt.Errorf("Invalid type") + } + return p, err +} |