Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Go API to use interfaces #155

Merged
merged 2 commits into from
Oct 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/agent/fos-agent/fos_agent.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1825,7 +1825,7 @@ let agent verbose_flag debug_flag configuration custom_uuid =
| true ->
(match uuid with
| Some plid -> MVar.read self >>= fun self ->
Yaks_connector.Local.Actual.remove_node_plugin (Apero.Option.get self.configuration.agent.uuid) plid self.yaks >>= Lwt.return
Yaks_connector.Global.Actual.remove_node_plugin sys_id Yaks_connector.default_tenant_id (Apero.Option.get self.configuration.agent.uuid) plid self.yaks >>= Lwt.return
| None -> Lwt.return_unit)

in
Expand Down
200 changes: 142 additions & 58 deletions src/api/go/fog05-go/runtime_plugin_fdu.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,71 @@
package fog05

import (
"fmt"
"os"
"time"

"github.com/google/uuid"
log "github.com/sirupsen/logrus"
)

// FOSRuntimePluginFDU represents a Runtime Plugin for Eclipse fog05
type FOSRuntimePluginFDU struct {
// FOSRuntimePluginInterface is the interface to be implenter for a Runtime Plugin
type FOSRuntimePluginInterface interface {

//StartRuntime starts the plugin
StartRuntime() error

//StopRuntime stops the plugin
StopRuntime() error

//GetFDUs gets all FDU and instances information
GetFDUs() map[string]FDURecord

//DefineFDU defines an FDU instance from the given record
DefineFDU(FDURecord) error

//UndefineFDU undefines the given FDU instance
UndefineFDU(string) error

//ConfigureFDU configures the given FDU instance
ConfigureFDU(string) error

//CleanFDU cleans the given FDU instance
CleanFDU(string) error

//RunFDU starts the given FDU instance
RunFDU(string) error

//StopFDU stops the given FDU instance
StopFDU(string) error

//MigrateFDU migrates the given FDU instance
MigrateFDU(string) error

//ScaleFDU scales the given FDU instance
ScaleFDU(string) error

//PauseFDU pauses the given FDU instance
PauseFDU(string) error

//ResumeFDU resumes the given FDU instance
ResumeFDU(string) error
}

// FOSRuntimePluginAbstract represents a Runtime Plugin for Eclipse fog05
type FOSRuntimePluginAbstract struct {
Pid int
Name string
Connector *YaksConnector
Node string
Configuration map[string]string
Plugin *FOSPlugin
Configuration map[string]interface{}
Logger *log.Logger
FOSRuntimePluginInterface
FOSPlugin
}

// NewFOSRuntimePluginFDU returns a new FOSRuntimePluginFDU object
func NewFOSRuntimePluginFDU(name string, version int, pluginid string, manifest Plugin) (*FOSRuntimePluginFDU, error) {
// NewFOSRuntimePluginAbstract returns a new FOSRuntimePluginFDU object
func NewFOSRuntimePluginAbstract(name string, version int, pluginid string, manifest Plugin) (*FOSRuntimePluginAbstract, error) {
if pluginid == "" {
pluginid = uuid.UUID.String(uuid.New())
}
Expand All @@ -34,36 +80,49 @@ func NewFOSRuntimePluginFDU(name string, version int, pluginid string, manifest
pl.connector = con
pl.node = conf["nodeid"].(string)

return &FOSRuntimePluginFDU{Pid: -1, Name: name, Connector: con, Node: conf["nodeid"].(string), Plugin: pl, Logger: log.New()}, nil
return &FOSRuntimePluginAbstract{Pid: os.Getpid(), Name: name, Connector: con, Node: conf["nodeid"].(string), FOSPlugin: *pl, Logger: log.New(), Configuration: conf}, nil
}

// Close closes the Plugin
func (rt *FOSRuntimePluginFDU) Close() {
// Start starts the Plugin and calls StartRuntime of FOSRuntimePluginInterface
func (rt *FOSRuntimePluginAbstract) Start() {
rt.WaitDependencies()
rt.Connector.Local.Desired.ObserveNodeRuntimeFDU(rt.Node, rt.FOSPlugin.UUID, rt.react)
err := rt.FOSRuntimePluginInterface.StartRuntime()
if err != nil {
rt.Logger.Error(fmt.Sprintf("Plugin StartRuntime returned error %s", err.Error()))
rt.Close()
}
}

// Close closes the Plugin, called by FOSRuntimePluginInterface.StopRuntime()
func (rt *FOSRuntimePluginAbstract) Close() {
rt.RemovePlugin()
rt.Connector.Close()
rt.Logger.Info("Plugin closed")
}

// WaitDestinationReady waits for the destination node of a migration to be ready
func (rt *FOSRuntimePluginFDU) WaitDestinationReady(fduid string, instanceid string, destinationid string) bool {
func (rt *FOSRuntimePluginAbstract) WaitDestinationReady(fduid string, instanceid string, destinationid string) bool {
return false
}

// WaitDependencies waits that the Agent, OS and NM Plugins are up and gets those from YAKS
func (rt *FOSRuntimePluginFDU) WaitDependencies() {
rt.Plugin.GetAgent()
for rt.Plugin.OS == nil {
rt.Plugin.GetOSPlugin()
func (rt *FOSRuntimePluginAbstract) WaitDependencies() {
rt.FOSPlugin.GetAgent()
for rt.FOSPlugin.OS == nil {
rt.FOSPlugin.GetOSPlugin()
time.Sleep(1 * time.Second)
}
for rt.Plugin.NM == nil {
rt.Plugin.GetNMPlugin()
for rt.FOSPlugin.NM == nil {
rt.FOSPlugin.GetNMPlugin()
time.Sleep(1 * time.Second)
}

}

// WriteFDUError given an fdu id, instance id, error number and error message, stores the error in YAKS
func (rt *FOSRuntimePluginFDU) WriteFDUError(fduid string, instanceid string, errno int, errmsg string) error {
record, err := rt.Connector.Local.Actual.GetNodeFDU(rt.Node, rt.Plugin.UUID, fduid, instanceid)
func (rt *FOSRuntimePluginAbstract) WriteFDUError(fduid string, instanceid string, errno int, errmsg string) error {
record, err := rt.Connector.Local.Actual.GetNodeFDU(rt.Node, rt.FOSPlugin.UUID, fduid, instanceid)
if err != nil {
return err
}
Expand All @@ -72,68 +131,93 @@ func (rt *FOSRuntimePluginFDU) WriteFDUError(fduid string, instanceid string, er
record.ErrorCode = &errno
record.ErrorMsg = &errmsg

err = rt.Connector.Local.Actual.AddNodeFDU(rt.Node, rt.Plugin.UUID, fduid, instanceid, *record)
err = rt.Connector.Local.Actual.AddNodeFDU(rt.Node, rt.FOSPlugin.UUID, fduid, instanceid, *record)
return err
}

// UpdateFDUStatus given an fdu id, instance id and status updates the status in YAKS
func (rt *FOSRuntimePluginFDU) UpdateFDUStatus(fduid string, instanceid string, status string) error {
record, err := rt.Connector.Local.Actual.GetNodeFDU(rt.Node, rt.Plugin.UUID, fduid, instanceid)
func (rt *FOSRuntimePluginAbstract) UpdateFDUStatus(fduid string, instanceid string, status string) error {
record, err := rt.Connector.Local.Actual.GetNodeFDU(rt.Node, rt.FOSPlugin.UUID, fduid, instanceid)
if err != nil {
return err
}

record.Status = status

err = rt.Connector.Local.Actual.AddNodeFDU(rt.Node, rt.Plugin.UUID, fduid, instanceid, *record)
err = rt.Connector.Local.Actual.AddNodeFDU(rt.Node, rt.FOSPlugin.UUID, fduid, instanceid, *record)
return err
}

// GetLocalInstances given an fdu id returns all the instances of that fdu running locally, returns string slice
func (rt *FOSRuntimePluginFDU) GetLocalInstances(fduid string) ([]string, error) {
func (rt *FOSRuntimePluginAbstract) GetLocalInstances(fduid string) ([]string, error) {
return rt.Connector.Local.Actual.GetNodeFDUInstances(rt.Node, fduid)

}

// RuntimePluginFDUInterface is the interface to be implenter for a Runtime Plugin
type RuntimePluginFDUInterface interface {

//StartRuntime starts the plugin
StartRuntime() error

//StopRuntime stops the plugin
StopRuntime() error

//GetFDUs gets all FDU and instances information
GetFDUs() map[string]FDURecord

//DefineFDU defines an FDU instance from the given record
DefineFDU(FDURecord) error

//UndefineFDU undefines the given FDU instance
UndefineFDU(string) error

//ConfigureFDU configures the given FDU instance
ConfigureFDU(string) error

//CleanFDU cleans the given FDU instance
CleanFDU(string) error
// RegisterPlugin registers the plugin in the node
func (rt *FOSRuntimePluginAbstract) RegisterPlugin(manifest *Plugin) {
rt.Connector.Local.Actual.AddNodePlugin(rt.Node, rt.FOSPlugin.UUID, *manifest)
}

//RunFDU starts the given FDU instance
RunFDU(string) error
// RemovePlugin removes the plugin in the node
func (rt *FOSRuntimePluginAbstract) RemovePlugin() {
rt.Connector.Local.Actual.RemoveNodePlugin(rt.Node, rt.FOSPlugin.UUID)
}

//StopFDU stops the given FDU instance
StopFDU(string) error
// GetFDUDescriptor retrives an FDURecord for the plugin
func (rt *FOSRuntimePluginAbstract) GetFDUDescriptor(fduid string, instanceid string) (*FDU, error) {
return rt.FOSPlugin.Agent.GetFDUInfo(rt.Node, fduid, instanceid)
}

//MigrateFDU migrates the given FDU instance
MigrateFDU(string) error
// GetFDURecord retrives an FDURecord for the plugin
func (rt *FOSRuntimePluginAbstract) GetFDURecord(instanceid string) (*FDURecord, error) {
return rt.Connector.Local.Actual.GetNodeFDU(rt.Node, rt.FOSPlugin.UUID, "*", instanceid)
}

//ScaleFDU scales the given FDU instance
ScaleFDU(string) error
// AddFDURecord adds an FDU record to the node
func (rt *FOSRuntimePluginAbstract) AddFDURecord(instanceid string, info *FDURecord) error {
record, err := rt.Connector.Local.Actual.GetNodeFDU(rt.Node, rt.FOSPlugin.UUID, "*", instanceid)
if err != nil {
return err
}
return rt.Connector.Local.Actual.AddNodeFDU(rt.Node, rt.FOSPlugin.UUID, record.FDUID, instanceid, *info)
}

//PauseFDU pauses the given FDU instance
PauseFDU(string) error
// RemoveFDURecord removes an FDURecord from the node
func (rt *FOSRuntimePluginAbstract) RemoveFDURecord(instanceid string) error {
record, err := rt.Connector.Local.Actual.GetNodeFDU(rt.Node, rt.FOSPlugin.UUID, "*", instanceid)
if err != nil {
return err
}
return rt.Connector.Local.Actual.RemoveNodeFDU(rt.Node, rt.FOSPlugin.UUID, record.FDUID, instanceid)
}

//ResumeFDU resumes the given FDU instance
ResumeFDU(string) error
func (rt *FOSRuntimePluginAbstract) react(info FDURecord) {

action := info.Status
id := info.UUID
switch action {
case DEFINE:
rt.DefineFDU(info)
case UNDEFINE:
rt.UndefineFDU(id)
case CLEAN:
rt.CleanFDU(id)
case CONFIGURE:
rt.ConfigureFDU(id)
case RUN:
rt.RunFDU(id)
case STOP:
rt.StopFDU(id)
case PAUSE:
rt.PauseFDU(id)
case RESUME:
rt.ResumeFDU(id)
case LAND:
rt.MigrateFDU(id)
case TAKEOFF:
rt.MigrateFDU(id)
default:
rt.Logger.Error(fmt.Sprintf("Action %s not recognized", action))
}
}
6 changes: 6 additions & 0 deletions src/api/go/fog05-go/yaks_connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -2386,6 +2386,12 @@ func (lad *LAD) AddNodePlugin(nodeid string, pluginid string, info Plugin) error
return err
}

// RemoveNodePlugin ...
func (lad *LAD) RemoveNodePlugin(nodeid string, pluginid string) error {
s := lad.GetNodePlguinInfoPath(nodeid, pluginid)
return lad.ws.Remove(s)
}

// GetAllPlugins ...
func (lad *LAD) GetAllPlugins(nodeid string) ([]string, error) {
s := lad.GetNodePlguinsSelector(nodeid)
Expand Down