Skip to content

Commit c1751c3

Browse files
committed
added plugin update method
1 parent f014faf commit c1751c3

File tree

5 files changed

+134
-2
lines changed

5 files changed

+134
-2
lines changed

Fula.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = 'Fula' # Name for your pod
3-
s.version = '1.54.14'
3+
s.version = '1.54.15'
44
s.summary = 'Go-fula for iOS'
55
s.homepage = 'https://github.com/functionland/go-fula'
66

blockchain/bl_plugins.go

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
)
1818

1919
const activePluginsFile = "/internal/active-plugins.txt"
20+
const updatePluginsFile = "/internal/update-plugins.txt"
2021

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

656+
func (bl *FxBlockchain) updatePluginImpl(ctx context.Context, pluginName string) ([]byte, error) {
657+
log.Debug("updatePluginImpl started")
658+
659+
// Check for context cancellation
660+
select {
661+
case <-ctx.Done():
662+
return json.Marshal(map[string]interface{}{
663+
"msg": "Operation cancelled",
664+
"status": false,
665+
})
666+
default:
667+
}
668+
669+
// Read existing update plugins
670+
updatePlugins, err := bl.readUpdatePlugins()
671+
if err != nil {
672+
return json.Marshal(map[string]interface{}{
673+
"msg": fmt.Sprintf("Failed to read update plugins: %v", err),
674+
"status": false,
675+
})
676+
}
677+
log.Debugw("updatePluginImpl plugins:", "updatePlugins", updatePlugins, "pluginName", pluginName)
678+
679+
// Check if plugin already exists in update list
680+
for _, p := range updatePlugins {
681+
if p == pluginName {
682+
return json.Marshal(map[string]interface{}{
683+
"msg": "Plugin already in update queue",
684+
"status": true,
685+
})
686+
}
687+
}
688+
689+
// Append new plugin to update list
690+
updatePlugins = append(updatePlugins, pluginName)
691+
692+
// Write updated list back to file
693+
if err := bl.writeUpdatePlugins(updatePlugins); err != nil {
694+
return json.Marshal(map[string]interface{}{
695+
"msg": fmt.Sprintf("Failed to write update plugins: %v", err),
696+
"status": false,
697+
})
698+
}
699+
700+
return json.Marshal(map[string]interface{}{
701+
"msg": "Plugin update queued successfully",
702+
"status": true,
703+
})
704+
}
705+
706+
func (bl *FxBlockchain) HandleUpdatePlugin(w http.ResponseWriter, r *http.Request) {
707+
var req struct {
708+
PluginName string `json:"plugin_name"`
709+
}
710+
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
711+
http.Error(w, "Invalid request body", http.StatusBadRequest)
712+
return
713+
}
714+
result, err := bl.updatePluginImpl(r.Context(), req.PluginName)
715+
if err != nil {
716+
http.Error(w, err.Error(), http.StatusInternalServerError)
717+
return
718+
}
719+
w.Header().Set("Content-Type", "application/json")
720+
w.Write(result)
721+
}
722+
723+
func (bl *FxBlockchain) UpdatePlugin(ctx context.Context, to peer.ID, pluginName string) ([]byte, error) {
724+
if bl.allowTransientConnection {
725+
ctx = network.WithUseTransient(ctx, "fx.blockchain")
726+
}
727+
728+
var buf bytes.Buffer
729+
if err := json.NewEncoder(&buf).Encode(map[string]string{"plugin_name": pluginName}); err != nil {
730+
return nil, err
731+
}
732+
733+
req, err := http.NewRequestWithContext(ctx, http.MethodPost, "http://"+to.String()+".invalid/"+actionUpdatePlugin, &buf)
734+
if err != nil {
735+
return nil, err
736+
}
737+
resp, err := bl.c.Do(req)
738+
if err != nil {
739+
return nil, err
740+
}
741+
defer resp.Body.Close()
742+
return io.ReadAll(resp.Body)
743+
}
744+
745+
func (bl *FxBlockchain) readUpdatePlugins() ([]string, error) {
746+
content, err := os.ReadFile(updatePluginsFile)
747+
if err != nil {
748+
if os.IsNotExist(err) {
749+
return []string{}, nil
750+
}
751+
return nil, fmt.Errorf("failed to read update plugins file: %w", err)
752+
}
753+
return strings.Split(strings.TrimSpace(string(content)), "\n"), nil
754+
}
755+
756+
func (bl *FxBlockchain) writeUpdatePlugins(plugins []string) error {
757+
content := strings.Join(plugins, "\n")
758+
if err := os.WriteFile(updatePluginsFile, []byte(content), 0644); err != nil {
759+
return fmt.Errorf("failed to write update plugins file: %w", err)
760+
}
761+
return nil
762+
}
763+
655764
func (bl *FxBlockchain) handlePluginAction(ctx context.Context, from peer.ID, w http.ResponseWriter, r *http.Request, action string) {
656765
log := log.With("action", action, "from", from)
657766
log.Debug("started handlePluginAction")
@@ -702,6 +811,9 @@ func (bl *FxBlockchain) handlePluginAction(ctx context.Context, from peer.ID, w
702811
case actionGetInstallStatus:
703812
log.Debugw("handlePluginAction: calling method actionGetInstallStatus", "PluginName", req.PluginName)
704813
result, err = bl.getInstallStatusImpl(ctx, req.PluginName)
814+
case actionUpdatePlugin:
815+
log.Debug("handlePluginAction: calling method actionUpdatePlugin")
816+
result, err = bl.updatePluginImpl(ctx, req.PluginName)
705817
default:
706818
log.Error("Invalid action")
707819
http.Error(w, "Invalid action", http.StatusBadRequest)

blockchain/blockchain.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,9 @@ func (bl *FxBlockchain) serve(w http.ResponseWriter, r *http.Request) {
565565
actionGetInstallStatus: func(from peer.ID, w http.ResponseWriter, r *http.Request) {
566566
bl.handlePluginAction(r.Context(), from, w, r, actionGetInstallStatus)
567567
},
568+
actionUpdatePlugin: func(from peer.ID, w http.ResponseWriter, r *http.Request) {
569+
bl.handlePluginAction(r.Context(), from, w, r, actionUpdatePlugin)
570+
},
568571
}
569572

570573
// Look up the function in the map and call it
@@ -978,7 +981,7 @@ func (bl *FxBlockchain) authorized(pid peer.ID, action string) bool {
978981
switch action {
979982
case actionReplicateInPool:
980983
return (bl.authorizer == bl.h.ID() || bl.authorizer == "")
981-
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:
984+
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:
982985
bl.authorizedPeersLock.RLock()
983986
_, ok := bl.authorizedPeers[pid]
984987
bl.authorizedPeersLock.RUnlock()

blockchain/interface.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ const (
6363
actionListActivePlugins = "list-active-plugins"
6464
actionGetInstallOutput = "get-install-output"
6565
actionGetInstallStatus = "get-install-status"
66+
actionUpdatePlugin = "update-plugin"
6667
)
6768

6869
type ReplicateRequest struct {
@@ -460,6 +461,14 @@ type GetInstallStatusResponse struct {
460461
Status string `json:"status"`
461462
}
462463

464+
type UpdatePluginRequest struct {
465+
PluginName string `json:"plugin_name"`
466+
}
467+
type UpdatePluginResponse struct {
468+
Msg string `json:"msg"`
469+
Status bool `json:"status"`
470+
}
471+
463472
type Blockchain interface {
464473
Seeded(context.Context, peer.ID, SeededRequest) ([]byte, error)
465474
AccountExists(context.Context, peer.ID, AccountExistsRequest) ([]byte, error)
@@ -510,6 +519,7 @@ type Blockchain interface {
510519
ShowPluginStatus(context.Context, string, int) ([]byte, error)
511520
GetInstallOutput(context.Context, peer.ID, string, string) ([]byte, error)
512521
GetInstallStatus(context.Context, peer.ID, string) ([]byte, error)
522+
UpdatePlugin(context.Context, peer.ID, string) ([]byte, error)
513523
}
514524

515525
var requestTypes = map[string]reflect.Type{
@@ -563,6 +573,7 @@ var requestTypes = map[string]reflect.Type{
563573
actionShowPluginStatus: reflect.TypeOf(ShowPluginStatusRequest{}),
564574
actionGetInstallOutput: reflect.TypeOf(GetInstallOutputRequest{}),
565575
actionGetInstallStatus: reflect.TypeOf(GetInstallStatusRequest{}),
576+
actionUpdatePlugin: reflect.TypeOf(UpdatePluginRequest{}),
566577
}
567578

568579
var responseTypes = map[string]reflect.Type{
@@ -616,4 +627,5 @@ var responseTypes = map[string]reflect.Type{
616627
actionShowPluginStatus: reflect.TypeOf(ShowPluginStatusResponse{}),
617628
actionGetInstallOutput: reflect.TypeOf(GetInstallOutputResponse{}),
618629
actionGetInstallStatus: reflect.TypeOf(GetInstallStatusResponse{}),
630+
actionUpdatePlugin: reflect.TypeOf(UpdatePluginResponse{}),
619631
}

mobile/blockchain.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,3 +315,8 @@ func (c *Client) GetInstallStatus(pluginName string) ([]byte, error) {
315315
ctx := context.TODO()
316316
return c.bl.GetInstallStatus(ctx, c.bloxPid, pluginName)
317317
}
318+
319+
func (c *Client) UpdatePlugin(pluginName string) ([]byte, error) {
320+
ctx := context.TODO()
321+
return c.bl.UpdatePlugin(ctx, c.bloxPid, pluginName)
322+
}

0 commit comments

Comments
 (0)