-
Notifications
You must be signed in to change notification settings - Fork 110
/
module_reload.go
183 lines (170 loc) · 6.22 KB
/
module_reload.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package cli
import (
"fmt"
"path/filepath"
"slices"
"strings"
"time"
"github.com/pkg/errors"
"github.com/urfave/cli/v2"
apppb "go.viam.com/api/app/v1"
goutils "go.viam.com/utils"
"google.golang.org/protobuf/types/known/structpb"
rdkConfig "go.viam.com/rdk/config"
"go.viam.com/rdk/logging"
rutils "go.viam.com/rdk/utils"
)
// ModuleMap is a type alias to indicate where a map represents a module config.
// We don't convert to rdkConfig.Module because it can get out of date with what's in the db.
// Using maps directly also saves a lot of high-maintenance ser/des work.
type ModuleMap map[string]any
// ServiceMap is the same kind of thing as ModuleMap (see above), a map representing a single service.
type ServiceMap map[string]any
// addShellService adds a shell service to the services slice if missing. Mutates part.RobotConfig.
func addShellService(c *cli.Context, vc *viamClient, part *apppb.RobotPart, wait bool) error {
partMap := part.RobotConfig.AsMap()
if _, ok := partMap["services"]; !ok {
partMap["services"] = make([]any, 0, 1)
}
services, _ := rutils.MapOver(partMap["services"].([]any), //nolint:errcheck
func(raw any) (ServiceMap, error) { return ServiceMap(raw.(map[string]any)), nil },
)
if slices.ContainsFunc(services, func(service ServiceMap) bool { return service["type"] == "shell" }) {
debugf(c.App.Writer, c.Bool(debugFlag), "shell service found on target machine, not installing")
return nil
}
services = append(services, ServiceMap{"name": "shell", "type": "shell"})
asAny, _ := rutils.MapOver(services, func(service ServiceMap) (any, error) { //nolint:errcheck
return map[string]any(service), nil
})
partMap["services"] = asAny
if err := writeBackConfig(part, partMap); err != nil {
return err
}
infof(c.App.Writer, "installing shell service on target machine for file transfer")
if err := vc.updateRobotPart(part, partMap); err != nil {
return err
}
if !wait {
return nil
}
// note: we wait up to 11 seconds; that's the 10 second default Cloud.RefreshInterval plus padding.
// If we don't wait, the reload command will usually fail on first run.
for i := 0; i < 11; i++ {
time.Sleep(time.Second)
_, closeClient, err := vc.connectToShellServiceFqdn(part.Fqdn, c.Bool(debugFlag), logging.NewLogger("shellsvc"))
if err == nil {
goutils.UncheckedError(closeClient(c.Context))
return nil
}
if !errors.Is(err, errNoShellService) {
return err
}
}
return errors.New("timed out waiting for shell service to start")
}
// writeBackConfig mutates part.RobotConfig with an edited config; this is necessary so that changes
// aren't lost when we make multiple updateRobotPart calls.
func writeBackConfig(part *apppb.RobotPart, configAsMap map[string]any) error {
modifiedConfig, err := structpb.NewStruct(configAsMap)
if err != nil {
return err
}
part.RobotConfig = modifiedConfig
return nil
}
// configureModule is the configuration step of module reloading. Returns (needsRestart, error). Mutates part.RobotConfig.
func configureModule(c *cli.Context, vc *viamClient, manifest *moduleManifest, part *apppb.RobotPart) (bool, error) {
if manifest == nil {
return false, fmt.Errorf("reconfiguration requires valid manifest json passed to --%s", moduleFlagPath)
}
partMap := part.RobotConfig.AsMap()
if _, ok := partMap["modules"]; !ok {
partMap["modules"] = make([]any, 0, 1)
}
modules, err := rutils.MapOver(
partMap["modules"].([]any),
func(raw any) (ModuleMap, error) { return ModuleMap(raw.(map[string]any)), nil },
)
if err != nil {
return false, err
}
modules, dirty, err := mutateModuleConfig(c, modules, *manifest)
if err != nil {
return false, err
}
// note: converting to any or else proto serializer will fail downstream in NewStruct.
modulesAsInterfaces, err := rutils.MapOver(modules, func(mod ModuleMap) (any, error) {
return map[string]any(mod), nil
})
if err != nil {
return false, err
}
partMap["modules"] = modulesAsInterfaces
if err := writeBackConfig(part, partMap); err != nil {
return false, err
}
if dirty {
debugf(c.App.Writer, c.Bool(debugFlag), "writing back config changes")
err = vc.updateRobotPart(part, partMap)
if err != nil {
return false, err
}
}
// if we modified config, caller doesn't need to restart module.
return !dirty, nil
}
// localizeModuleID converts a module ID to its 'local mode' name.
// TODO(APP-4019): remove this logic after registry modules can have local ExecPath.
func localizeModuleID(moduleID string) string {
return strings.ReplaceAll(moduleID, ":", "_") + "_from_reload"
}
// mutateModuleConfig edits the modules list to hot-reload with the given manifest.
func mutateModuleConfig(c *cli.Context, modules []ModuleMap, manifest moduleManifest) ([]ModuleMap, bool, error) {
var dirty bool
localName := localizeModuleID(manifest.ModuleID)
var foundMod ModuleMap
for _, mod := range modules {
if (mod["module_id"] == manifest.ModuleID) || (mod["name"] == localName) {
foundMod = mod
break
}
}
var absEntrypoint string
var err error
if c.Bool(moduleFlagLocal) {
absEntrypoint, err = filepath.Abs(manifest.Entrypoint)
if err != nil {
return nil, dirty, err
}
} else {
absEntrypoint = reloadingDestination(c, &manifest)
}
if foundMod == nil {
debugf(c.App.Writer, c.Bool(debugFlag), "module not found, inserting")
dirty = true
newMod := ModuleMap(map[string]any{
"name": localName,
"executable_path": absEntrypoint,
"type": string(rdkConfig.ModuleTypeLocal),
})
modules = append(modules, newMod)
} else {
if same, err := samePath(getMapString(foundMod, "executable_path"), absEntrypoint); err != nil {
debugf(c.App.Writer, c.Bool(debugFlag), "ExePath is right, doing nothing")
return nil, dirty, err
} else if !same {
dirty = true
debugf(c.App.Writer, c.Bool(debugFlag), "replacing entrypoint")
if getMapString(foundMod, "type") == string(rdkConfig.ModuleTypeRegistry) {
// warning: there's a chance of inserting a dupe name here in odd cases
warningf(c.App.Writer, "you're replacing a registry module. we're converting it to a local module")
foundMod["type"] = string(rdkConfig.ModuleTypeLocal)
foundMod["name"] = localName
foundMod["module_id"] = ""
}
foundMod["executable_path"] = absEntrypoint
}
}
return modules, dirty, nil
}