Skip to content

Commit

Permalink
added plugin update method
Browse files Browse the repository at this point in the history
  • Loading branch information
ehsan6sha committed Oct 9, 2024
1 parent f014faf commit c1751c3
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Fula.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'Fula' # Name for your pod
s.version = '1.54.14'
s.version = '1.54.15'
s.summary = 'Go-fula for iOS'
s.homepage = 'https://github.com/functionland/go-fula'

Expand Down
112 changes: 112 additions & 0 deletions blockchain/bl_plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
)

const activePluginsFile = "/internal/active-plugins.txt"
const updatePluginsFile = "/internal/update-plugins.txt"

type PluginInfo struct {
Name string `json:"name"`
Expand Down Expand Up @@ -652,6 +653,114 @@ func (bl *FxBlockchain) setPluginStatus(pluginName, status string) error {
return nil
}

func (bl *FxBlockchain) updatePluginImpl(ctx context.Context, pluginName string) ([]byte, error) {
log.Debug("updatePluginImpl started")

// Check for context cancellation
select {
case <-ctx.Done():
return json.Marshal(map[string]interface{}{
"msg": "Operation cancelled",
"status": false,
})
default:
}

// Read existing update plugins
updatePlugins, err := bl.readUpdatePlugins()
if err != nil {
return json.Marshal(map[string]interface{}{
"msg": fmt.Sprintf("Failed to read update plugins: %v", err),
"status": false,
})
}
log.Debugw("updatePluginImpl plugins:", "updatePlugins", updatePlugins, "pluginName", pluginName)

// Check if plugin already exists in update list
for _, p := range updatePlugins {
if p == pluginName {
return json.Marshal(map[string]interface{}{
"msg": "Plugin already in update queue",
"status": true,
})
}
}

// Append new plugin to update list
updatePlugins = append(updatePlugins, pluginName)

// Write updated list back to file
if err := bl.writeUpdatePlugins(updatePlugins); err != nil {
return json.Marshal(map[string]interface{}{
"msg": fmt.Sprintf("Failed to write update plugins: %v", err),
"status": false,
})
}

return json.Marshal(map[string]interface{}{
"msg": "Plugin update queued successfully",
"status": true,
})
}

func (bl *FxBlockchain) HandleUpdatePlugin(w http.ResponseWriter, r *http.Request) {
var req struct {
PluginName string `json:"plugin_name"`
}
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, "Invalid request body", http.StatusBadRequest)
return
}
result, err := bl.updatePluginImpl(r.Context(), req.PluginName)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(result)
}

func (bl *FxBlockchain) UpdatePlugin(ctx context.Context, to peer.ID, pluginName string) ([]byte, error) {
if bl.allowTransientConnection {
ctx = network.WithUseTransient(ctx, "fx.blockchain")
}

var buf bytes.Buffer
if err := json.NewEncoder(&buf).Encode(map[string]string{"plugin_name": pluginName}); err != nil {
return nil, err
}

req, err := http.NewRequestWithContext(ctx, http.MethodPost, "http://"+to.String()+".invalid/"+actionUpdatePlugin, &buf)
if err != nil {
return nil, err
}
resp, err := bl.c.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
return io.ReadAll(resp.Body)
}

func (bl *FxBlockchain) readUpdatePlugins() ([]string, error) {
content, err := os.ReadFile(updatePluginsFile)
if err != nil {
if os.IsNotExist(err) {
return []string{}, nil
}
return nil, fmt.Errorf("failed to read update plugins file: %w", err)
}
return strings.Split(strings.TrimSpace(string(content)), "\n"), nil
}

func (bl *FxBlockchain) writeUpdatePlugins(plugins []string) error {
content := strings.Join(plugins, "\n")
if err := os.WriteFile(updatePluginsFile, []byte(content), 0644); err != nil {
return fmt.Errorf("failed to write update plugins file: %w", err)
}
return nil
}

func (bl *FxBlockchain) handlePluginAction(ctx context.Context, from peer.ID, w http.ResponseWriter, r *http.Request, action string) {
log := log.With("action", action, "from", from)
log.Debug("started handlePluginAction")
Expand Down Expand Up @@ -702,6 +811,9 @@ func (bl *FxBlockchain) handlePluginAction(ctx context.Context, from peer.ID, w
case actionGetInstallStatus:
log.Debugw("handlePluginAction: calling method actionGetInstallStatus", "PluginName", req.PluginName)
result, err = bl.getInstallStatusImpl(ctx, req.PluginName)
case actionUpdatePlugin:
log.Debug("handlePluginAction: calling method actionUpdatePlugin")
result, err = bl.updatePluginImpl(ctx, req.PluginName)
default:
log.Error("Invalid action")
http.Error(w, "Invalid action", http.StatusBadRequest)
Expand Down
5 changes: 4 additions & 1 deletion blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,9 @@ func (bl *FxBlockchain) serve(w http.ResponseWriter, r *http.Request) {
actionGetInstallStatus: func(from peer.ID, w http.ResponseWriter, r *http.Request) {
bl.handlePluginAction(r.Context(), from, w, r, actionGetInstallStatus)
},
actionUpdatePlugin: func(from peer.ID, w http.ResponseWriter, r *http.Request) {
bl.handlePluginAction(r.Context(), from, w, r, actionUpdatePlugin)
},
}

// Look up the function in the map and call it
Expand Down Expand Up @@ -978,7 +981,7 @@ func (bl *FxBlockchain) authorized(pid peer.ID, action string) bool {
switch action {
case actionReplicateInPool:
return (bl.authorizer == bl.h.ID() || bl.authorizer == "")
case actionBloxFreeSpace, actionAccountFund, actionManifestBatchUpload, actionAssetsBalance, actionGetDatastoreSize, actionGetFolderSize, actionFindBestAndTargetInLogs, actionFetchContainerLogs, actionEraseBlData, actionWifiRemoveall, actionReboot, actionPartition, actionDeleteWifi, actionDisconnectWifi, actionDeleteFulaConfig, actionGetAccount, actionSeeded, actionAccountExists, actionPoolCreate, actionPoolJoin, actionPoolCancelJoin, actionPoolRequests, actionPoolList, actionPoolVote, actionPoolLeave, actionManifestUpload, actionManifestStore, actionManifestAvailable, actionManifestRemove, actionManifestRemoveStorer, actionManifestRemoveStored, actionTransferToMumbai, actionListPlugins, actionListActivePlugins, actionInstallPlugin, actionUninstallPlugin, actionGetInstallStatus, actionGetInstallOutput:
case actionBloxFreeSpace, actionAccountFund, actionManifestBatchUpload, actionAssetsBalance, actionGetDatastoreSize, actionGetFolderSize, actionFindBestAndTargetInLogs, actionFetchContainerLogs, actionEraseBlData, actionWifiRemoveall, actionReboot, actionPartition, actionDeleteWifi, actionDisconnectWifi, actionDeleteFulaConfig, actionGetAccount, actionSeeded, actionAccountExists, actionPoolCreate, actionPoolJoin, actionPoolCancelJoin, actionPoolRequests, actionPoolList, actionPoolVote, actionPoolLeave, actionManifestUpload, actionManifestStore, actionManifestAvailable, actionManifestRemove, actionManifestRemoveStorer, actionManifestRemoveStored, actionTransferToMumbai, actionListPlugins, actionListActivePlugins, actionInstallPlugin, actionUninstallPlugin, actionGetInstallStatus, actionGetInstallOutput, actionUpdatePlugin:
bl.authorizedPeersLock.RLock()
_, ok := bl.authorizedPeers[pid]
bl.authorizedPeersLock.RUnlock()
Expand Down
12 changes: 12 additions & 0 deletions blockchain/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ const (
actionListActivePlugins = "list-active-plugins"
actionGetInstallOutput = "get-install-output"
actionGetInstallStatus = "get-install-status"
actionUpdatePlugin = "update-plugin"
)

type ReplicateRequest struct {
Expand Down Expand Up @@ -460,6 +461,14 @@ type GetInstallStatusResponse struct {
Status string `json:"status"`
}

type UpdatePluginRequest struct {
PluginName string `json:"plugin_name"`
}
type UpdatePluginResponse struct {
Msg string `json:"msg"`
Status bool `json:"status"`
}

type Blockchain interface {
Seeded(context.Context, peer.ID, SeededRequest) ([]byte, error)
AccountExists(context.Context, peer.ID, AccountExistsRequest) ([]byte, error)
Expand Down Expand Up @@ -510,6 +519,7 @@ type Blockchain interface {
ShowPluginStatus(context.Context, string, int) ([]byte, error)
GetInstallOutput(context.Context, peer.ID, string, string) ([]byte, error)
GetInstallStatus(context.Context, peer.ID, string) ([]byte, error)
UpdatePlugin(context.Context, peer.ID, string) ([]byte, error)
}

var requestTypes = map[string]reflect.Type{
Expand Down Expand Up @@ -563,6 +573,7 @@ var requestTypes = map[string]reflect.Type{
actionShowPluginStatus: reflect.TypeOf(ShowPluginStatusRequest{}),
actionGetInstallOutput: reflect.TypeOf(GetInstallOutputRequest{}),
actionGetInstallStatus: reflect.TypeOf(GetInstallStatusRequest{}),
actionUpdatePlugin: reflect.TypeOf(UpdatePluginRequest{}),
}

var responseTypes = map[string]reflect.Type{
Expand Down Expand Up @@ -616,4 +627,5 @@ var responseTypes = map[string]reflect.Type{
actionShowPluginStatus: reflect.TypeOf(ShowPluginStatusResponse{}),
actionGetInstallOutput: reflect.TypeOf(GetInstallOutputResponse{}),
actionGetInstallStatus: reflect.TypeOf(GetInstallStatusResponse{}),
actionUpdatePlugin: reflect.TypeOf(UpdatePluginResponse{}),
}
5 changes: 5 additions & 0 deletions mobile/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,3 +315,8 @@ func (c *Client) GetInstallStatus(pluginName string) ([]byte, error) {
ctx := context.TODO()
return c.bl.GetInstallStatus(ctx, c.bloxPid, pluginName)
}

func (c *Client) UpdatePlugin(pluginName string) ([]byte, error) {
ctx := context.TODO()
return c.bl.UpdatePlugin(ctx, c.bloxPid, pluginName)
}

0 comments on commit c1751c3

Please sign in to comment.