-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathupdate.go
464 lines (394 loc) · 14.5 KB
/
update.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
// Copyright (c) 2024 Veritas Technologies LLC. All rights reserved. IP63-2828-7171-04-15-9
package update
import (
"flag"
"fmt"
"os"
"os/exec"
"path/filepath"
pm "github.com/VeritasOS/plugin-manager" // import "../plugin-manager"
"github.com/VeritasOS/plugin-manager/config"
logger "github.com/VeritasOS/plugin-manager/utils/log"
"github.com/VeritasOS/plugin-manager/utils/output"
"github.com/VeritasOS/software-update-manager/repo"
"github.com/VeritasOS/software-update-manager/utils/rpm"
)
// RPMInstallRepoPath is the path where RPM contents are expected to installed/extracted.
const RPMInstallRepoPath = "/system/upgrade/repository/"
// Status of execution used for display as well as logging to specified output file.
const (
dStatusFail = "Failed"
dStatusOk = "Succeeded"
)
// Status is the execution/run status of PM on a specified plugin type.
type Status struct {
// INFO: The Status contains info of all operations so as to support
// all operations with one call to sum with appropriate flags set to
// continue onto the next operation.
// Ex: Install can receive auto-reboot=true, in which after installation
// is completed successfully, `sum` will run reboot operation.
Install []pm.Plugin `yaml:",omitempty"`
Reboot []pm.Plugin `yaml:",omitempty"`
Rollback []pm.Plugin `yaml:",omitempty"`
Commit []pm.Plugin `yaml:",omitempty"`
Status string
StdOutErr string
}
// Commit runs the commit-precheck and commit plugins of the update workflow.
func Commit(result *Status, library string) bool {
logger.Debug.Println("Entering update::Commit")
defer logger.Debug.Println("Exiting update::Commit")
// Plugin Types to run for update workflow as part of user commit action.
pluginTypes := []string{"commit-precheck", "commit"}
for _, pt := range pluginTypes {
result.Commit = append(result.Commit, pm.Plugin{})
resIdx := len(result.Commit) - 1
err := runPM(&result.Commit[resIdx], pt, library)
if err != nil {
return false
}
}
result.Status = dStatusOk
output.Write(result)
return true
}
// Install runs the preinstall and install plugins of the update workflow.
func Install(result *Status, library string) bool {
logger.Debug.Println("Entering update::Install")
defer logger.Debug.Println("Exiting update::Install")
// Plugin Types to run for update workflow as part of install script.
pluginTypes := []string{"preinstall", "install"}
var err error
for _, pt := range pluginTypes {
result.Install = append(result.Install, pm.Plugin{})
resIdx := len(result.Install) - 1
err = runPM(&result.Install[resIdx], pt, library)
if err != nil {
result.Status = dStatusFail
break
}
}
if err != nil {
// INFO: Discard rollback errors, and always return false to
// indicate installation failure.
pt := "rollback"
result.Install = append(result.Install, pm.Plugin{})
resIdx := len(result.Install) - 1
runPM(&result.Install[resIdx], pt, library)
return false
}
result.Status = dStatusOk
output.Write(result)
return true
}
// runCmdFromRPM installs the specified software package from the software repo.
func runCmdFromRPM(action, swName, swType string, params map[string]string) error {
logger.Debug.Printf("Entering update::runCmdFromRPM(%s, %s, %s, %+v)",
action, swName, swType, params)
defer logger.Debug.Println("Exiting update::runCmdFromRPM")
if swName == "" {
return logger.ConsoleError.PrintNReturnError("Invalid usage. Software name must be specified.")
}
if swType == "" {
return logger.ConsoleError.PrintNReturnError("Invalid usage. Software type must be specified.")
}
swRepo := params["softwareRepo"]
if swRepo == "" {
swRepo = repo.SoftwareRepoPath
}
absSwPath := filepath.Clean(filepath.FromSlash(swRepo +
string(os.PathSeparator) + swType + string(os.PathSeparator) +
swName))
fi, err := os.Stat(absSwPath)
if err != nil {
logger.Error.Printf("Unable to stat on %s: %+v, err=%s", absSwPath, fi, err.Error())
return logger.ConsoleError.PrintNReturnError("Unable to install %s software %s. "+
"Specified software not found.",
swType, swName)
}
// INFO: If there was an attempt to install this RPM previously,
// then clean up and try installing it again.
// NOTE: The version validation would have failed if this RPM was
// successfully installed. So no need to worry about removing an RPM
// that was installed successfully.
params["softwareName"] = swName
params["softwareType"] = swType
listInfo, err := repo.List(params)
if err != nil {
logger.Error.Printf("Failed to repo.List(), err=%s", err.Error())
return err
}
// Since we had passed softwareName, there is expected to be only one in the list, so get that.
if 1 != len(listInfo) {
logger.Error.Printf("The repo list is expected to have details of %s software, but got %+v.", swName, listInfo)
return logger.ConsoleError.PrintNReturnError("Failed to get details of %s software.", swName)
}
rpmInfo := listInfo[0]
// INFO: Only for `install` action, we need to first install/extract the
// SUM RPM to get the install script. For all other actions, the
// install script is expected to run first, and hence they're
// expected to be present at the scripts location.
if "install" == action {
if rpm.IsInstalled(rpmInfo.GetRPMName()) {
rpm.Uninstall(rpmInfo.GetRPMName())
}
err = rpm.Install(absSwPath)
if err != nil {
return logger.ConsoleError.PrintNReturnError("Failed to install software.")
}
}
script := RPMInstallRepoPath + swType + string(os.PathSeparator) +
fmt.Sprintf("%s-%s-%s", rpmInfo.GetRPMName(),
rpmInfo.GetRPMVersion(), rpmInfo.GetRPMRelease()) +
string(os.PathSeparator) + action
logger.Info.Println("Script to be invoked:", script)
const cmdStr = "/bin/sh"
cmdParams := []string{script, "-output-file", output.GetFile(),
"-output-format", output.GetFormat()}
cmd := exec.Command(os.ExpandEnv(cmdStr), cmdParams...)
stdOutErr, err := cmd.CombinedOutput()
logger.Debug.Println("Stdout & Stderr:", string(stdOutErr))
if err != nil {
logger.Error.Printf("Failed to run %s script of %s RPM, err=%s", script, absSwPath, err.Error())
if "install" == action {
rpm.Uninstall(rpmInfo.GetRPMName())
}
return logger.ConsoleError.PrintNReturnError("Failed to %s software.", action)
}
logger.Info.Printf("Successfully completed %s of %s software", action, absSwPath)
logger.ConsoleInfo.Printf("Successfully completed %s of %s software %s from repository.\n",
action, swType, swName)
return nil
}
// Reboot runs the prereboot plugins and reboot the system as part of the update workflow.
func Reboot(result *Status, library string) bool {
logger.Debug.Println("Entering update::Reboot")
defer logger.Debug.Println("Exiting update::Reboot")
// Plugin Types to run for update workflow as part of prereboot script.
pluginTypes := []string{"prereboot"}
for _, pt := range pluginTypes {
result.Reboot = append(result.Reboot, pm.Plugin{})
resIdx := len(result.Reboot) - 1
err := runPM(&result.Reboot[resIdx], pt, library)
if err != nil {
result.Status = dStatusFail
result.StdOutErr = err.Error()
// INFO: Discard rollback errors, and always return false to
// indicate reboot failure.
pt := "rollback"
result.Reboot = append(result.Reboot, pm.Plugin{})
resIdx := len(result.Reboot) - 1
runPM(&result.Reboot[resIdx], pt, library)
return false
}
}
result.Status = dStatusOk
output.Write(result)
// Reboot the system after prereboot plugins are run successfully.
cmdStr := "systemctl"
cmdParams := []string{"reboot"}
cmd := exec.Command(os.ExpandEnv(cmdStr), cmdParams...)
stdOutErr, err := cmd.CombinedOutput()
logger.Debug.Println("Stdout & Stderr:", string(stdOutErr))
if err != nil {
logger.Error.Printf("Failed to reboot the system, err=%s", err.Error())
}
return true
}
func runPM(result *pm.Plugin, pluginType, library string) error {
logger.Debug.Println("Entering update::runPM")
defer logger.Debug.Println("Exiting update::runPM")
logger.ConsoleInfo.Printf("Running %s plugins...", pluginType)
config.SetPluginsLibrary(library)
err := pm.RunFromLibrary(result, pluginType, pm.RunOptions{Library: library})
if err != nil {
logger.Error.Printf("Failed to run %s plugins, err=%s", pluginType, err.Error())
return err
}
fmt.Println()
return nil
}
// Rollback runs the required rollback plugins of the update workflow in the new version/partition.
func Rollback(result *Status, library string) bool {
logger.Debug.Println("Entering update::Rollback")
defer logger.Debug.Println("Exiting update::Rollback")
// Plugin Types to run for update workflow as part of rollback script.
pluginTypes := []string{"rollback-precheck", "prerollback"}
for _, pt := range pluginTypes {
result.Rollback = append(result.Rollback, pm.Plugin{})
resIdx := len(result.Rollback) - 1
err := runPM(&result.Rollback[resIdx], pt, library)
if err != nil {
return false
}
}
result.Status = dStatusOk
output.Write(result)
return true
}
// cmdOptions contains subcommands and parameters of the pm command.
var cmdOptions struct {
commitCmd *flag.FlagSet
installCmd *flag.FlagSet
rebootCmd *flag.FlagSet
rollbackCmd *flag.FlagSet
// softwareName indicates the name of the software.
softwareName string
// softwareRepo indicates the path to the software repository.
softwareRepo string
// softwareType indicates the type of the software.
softwareType string
// logDir indicates the location for writing log file.
logDir string
// logFile indicates the log file name to write to in the logDir location.
logFile string
}
func registerCmdOptions(f *flag.FlagSet) {
f.StringVar(
&cmdOptions.softwareName,
"filename",
"",
"File name of the software.",
)
f.StringVar(
&cmdOptions.softwareRepo,
"repo",
"",
"Path of the software repository.",
)
f.StringVar(
&cmdOptions.softwareType,
"type",
"",
"Type of the software.",
)
output.RegisterCommandOptions(f, map[string]string{"output-format": "yaml"})
}
// registerCommandCommit registers install command and its options
func registerCommandCommit(progname string) {
logger.Debug.Printf("Entering update::registerCommandCommit(%s)", progname)
defer logger.Debug.Println("Exiting update::registerCommandCommit")
cmdOptions.commitCmd = flag.NewFlagSet(progname+" commit", flag.PanicOnError)
registerCmdOptions(cmdOptions.commitCmd)
}
// registerCommandInstall registers install command and its options
func registerCommandInstall(progname string) {
logger.Debug.Printf("Entering update::registerCommandInstall(%s)", progname)
defer logger.Debug.Println("Exiting update::registerCommandInstall")
cmdOptions.installCmd = flag.NewFlagSet(progname+" install", flag.PanicOnError)
registerCmdOptions(cmdOptions.installCmd)
}
// registerCommandReboot registers reboot command and its options
func registerCommandReboot(progname string) {
logger.Info.Printf("Entering update::registerCommandReboot(%s)", progname)
defer logger.Info.Println("Exiting update::registerCommandReboot")
cmdOptions.rebootCmd = flag.NewFlagSet(progname+" reboot", flag.PanicOnError)
registerCmdOptions(cmdOptions.rebootCmd)
}
// registerCommandRollback registers rollback command and its options
func registerCommandRollback(progname string) {
logger.Debug.Printf("Entering update::registerCommandRollback(%s)", progname)
defer logger.Debug.Println("Exiting update::registerCommandRollback")
cmdOptions.rollbackCmd = flag.NewFlagSet(progname+" rollback", flag.PanicOnError)
registerCmdOptions(cmdOptions.rollbackCmd)
}
// RegisterCommandOptions registers the command options that are supported
func RegisterCommandOptions(progname string) {
logger.Debug.Println("Entering update::RegisterCommandOptions")
defer logger.Debug.Println("Exiting update::RegisterCommandOptions")
registerCommandCommit(progname)
registerCommandInstall(progname)
registerCommandReboot(progname)
registerCommandRollback(progname)
}
// ScanCommandOptions scans for the command line options and makes appropriate
// function call.
// Input:
// 1. map[string]interface{}
// where, the options could be following:
// "progname": Name of the program along with any cmds (ex: sum pm)
// "cmd-index": Index to the cmd (ex: run)
func ScanCommandOptions(options map[string]interface{}) error {
logger.Debug.Printf("Entering ScanCommandOptions(%+v)...", options)
defer logger.Debug.Println("Exiting ScanCommandOptions")
progname := filepath.Base(os.Args[0])
cmdIndex := 1
if valI, ok := options["progname"]; ok {
progname = valI.(string)
}
if valI, ok := options["cmd-index"]; ok {
cmdIndex = valI.(int)
}
cmd := os.Args[cmdIndex]
logger.Debug.Println("Progname: ", progname, " cmd with arguments: ", os.Args[cmdIndex:])
var err error
switch cmd {
case "commit":
err = cmdOptions.commitCmd.Parse(os.Args[cmdIndex+1:])
case "install":
err = cmdOptions.installCmd.Parse(os.Args[cmdIndex+1:])
case "reboot":
err = cmdOptions.rebootCmd.Parse(os.Args[cmdIndex+1:])
case "rollback":
err = cmdOptions.rollbackCmd.Parse(os.Args[cmdIndex+1:])
case "help":
subcmd := ""
if len(os.Args) == cmdIndex+2 {
subcmd = os.Args[cmdIndex+1]
} else if len(os.Args) > cmdIndex+2 {
fmt.Fprintf(os.Stderr, "usage: %s help command\n\nToo many arguments (%d) given.\n", progname, len(os.Args))
os.Exit(2)
}
usage(progname, subcmd)
default:
fmt.Fprintf(os.Stderr, "%s: unknown command \"%s\"\n", progname, cmd)
fmt.Fprintf(os.Stderr, "Run '%s help [command]' for usage.\n", progname)
os.Exit(2)
}
if err != nil {
return logger.ConsoleError.PrintNReturnError("Command arguments parse error, cmd=%s, err=%s", cmd, err.Error())
}
if cmdOptions.softwareName != "" {
params := map[string]string{}
params["softwareRepo"] = cmdOptions.softwareRepo
err = runCmdFromRPM(cmd, cmdOptions.softwareName, cmdOptions.softwareType, params)
} else {
library := options["library"].(string)
var status Status
var ret bool
switch cmd {
case "commit":
ret = Commit(&status, library)
case "install":
ret = Install(&status, library)
case "reboot":
ret = Reboot(&status, library)
case "rollback":
ret = Rollback(&status, library)
}
if !ret {
err = logger.ConsoleError.PrintNReturnError("Failed to %s the update.", cmd)
status.Status = dStatusFail
status.StdOutErr = err.Error()
output.Write(status)
}
}
return err
}
// Usage of command.
func usage(progname, subcmd string) {
switch subcmd {
case "commit":
cmdOptions.commitCmd.Usage()
case "install":
cmdOptions.installCmd.Usage()
case "reboot":
cmdOptions.rebootCmd.Usage()
case "rollback":
cmdOptions.rollbackCmd.Usage()
default:
fmt.Fprintf(os.Stderr, "Unknown help topic `%s`. Run '%s'.", subcmd, progname+" help")
fmt.Println()
os.Exit(2)
}
}