/
manager_cmdline.go
605 lines (555 loc) · 19.5 KB
/
manager_cmdline.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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
// Copyright 2015 The Vanadium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package profilescmdline provides a command line driver
// (for v.io/x/lib/cmdline) for implementing jiri 'profile' subcommands.
// The intent is to support project specific instances of such profiles
// for managing software dependencies.
//
// There are two ways of using the cmdline support, one is to read profile
// information, via RegisterReaderCommands and
// RegisterReaderCommandsUsingParent; the other is to manage profile
// installations via the RegisterManagementCommands function. The management
// commands can manage profiles that are linked into the binary itself
// or invoke external commands that implement the profile management. These
// external 'installer' commands are accessed by specifing them as a prefix
// to the profile name. For example myproject::go will invoke the external
// command jiri-profile-myproject with "go" as the profile name. Thus the
// following invocations are equivalent:
// jiri profile install myproject::go
// jiri profile-myproject install go
//
// Regardless of which is used, the profile name, as seen by profile
// database readers will be myproject::go.
package profilescmdline
import (
"bytes"
"flag"
"fmt"
"os"
"strings"
"v.io/jiri"
"v.io/jiri/profiles"
"v.io/jiri/profiles/profilesmanager"
"v.io/x/lib/cmdline"
"v.io/x/lib/lookpath"
)
// newCmdOSPackages represents the "profile os-packages" command.
func newCmdOSPackages() *cmdline.Command {
return &cmdline.Command{
Runner: jiri.RunnerFunc(runPackages),
Name: "os-packages",
Short: "List the commands to install the OS packages required by the given profiles",
Long: "List or optionally run the commands to install the OS packages required by the given profiles.",
ArgsName: "<profiles>",
ArgsLong: "<profiles> is a list of profiles to list OS packages for.",
}
}
// newCmdInstall represents the "profile install" command.
func newCmdInstall() *cmdline.Command {
return &cmdline.Command{
Runner: jiri.RunnerFunc(runInstall),
Name: "install",
Short: "Install the given profiles",
Long: "Install the given profiles.",
ArgsName: "<profiles>",
ArgsLong: "<profiles> is a list of profiles to install.",
}
}
// newCmdUninstall represents the "profile uninstall" command.
func newCmdUninstall() *cmdline.Command {
return &cmdline.Command{
Runner: jiri.RunnerFunc(runUninstall),
Name: "uninstall",
Short: "Uninstall the given profiles",
Long: "Uninstall the given profiles.",
ArgsName: "<profiles>",
ArgsLong: "<profiles> is a list of profiles to uninstall.",
}
}
// newCmdUpdate represents the "profile update" command.
func newCmdUpdate() *cmdline.Command {
return &cmdline.Command{
Runner: jiri.RunnerFunc(runUpdate),
Name: "update",
Short: "Install the latest default version of the given profiles",
Long: "Install the latest default version of the given profiles.",
ArgsName: "<profiles>",
ArgsLong: "<profiles> is a list of profiles to update, if omitted all profiles are updated.",
}
}
// newCmdCleanup represents the "profile cleanup" command.
func newCmdCleanup() *cmdline.Command {
return &cmdline.Command{
Runner: jiri.RunnerFunc(runCleanup),
Name: "cleanup",
Short: "Cleanup the locally installed profiles",
Long: "Cleanup the locally installed profiles. This is generally required when recovering from earlier bugs or when preparing for a subsequent change to the profiles implementation.",
ArgsName: "<profiles>",
ArgsLong: "<profiles> is a list of profiles to cleanup, if omitted all profiles are cleaned.",
}
}
// newCmdAvailable represents the "profile available" command.
func newCmdAvailable() *cmdline.Command {
return &cmdline.Command{
Runner: jiri.RunnerFunc(runAvailable),
Name: "available",
Short: "List the available profiles",
Long: "List the available profiles.",
}
}
func runUpdate(jirix *jiri.X, args []string) error {
return updateImpl(jirix, &updateFlags, args)
}
func runCleanup(jirix *jiri.X, args []string) error {
return cleanupImpl(jirix, &cleanupFlags, args)
}
func runPackages(jirix *jiri.X, args []string) error {
return packagesImpl(jirix, &packagesFlags, args)
}
func runInstall(jirix *jiri.X, args []string) error {
return installImpl(jirix, &installFlags, args)
}
func runUninstall(jirix *jiri.X, args []string) error {
return uninstallImpl(jirix, &uninstallFlags, args)
}
func runAvailable(jirix *jiri.X, args []string) error {
return availableImpl(jirix, &availableFlags, args)
}
type commonFlagValues struct {
// The value of --profiles-db
dbPath string
// The value of --profiles-dir
root string
}
func initCommon(flags *flag.FlagSet, c *commonFlagValues, installer, defaultDBPath, defaultProfilesPath string) {
RegisterDBPathFlag(flags, &c.dbPath, defaultDBPath)
flags.StringVar(&c.root, "profiles-dir", defaultProfilesPath, "the directory, relative to JIRI_ROOT, that profiles are installed in")
}
func (cv *commonFlagValues) args() []string {
a := append([]string{}, "--profiles-db="+cv.dbPath)
a = append(a, "--profiles-dir="+cv.root)
return a
}
type packagesFlagValues struct {
commonFlagValues
// The value of --target and --env
target profiles.Target
// Show commands for all required packages, rather than just the missing ones
allPackages bool
// Install the required packages
installPackages bool
}
func initPackagesCommand(flags *flag.FlagSet, installer, defaultDBPath, defaultProfilesPath string) {
initCommon(flags, &packagesFlags.commonFlagValues, installer, defaultDBPath, defaultProfilesPath)
profiles.RegisterTargetAndEnvFlags(flags, &packagesFlags.target)
flags.BoolVar(&packagesFlags.allPackages, "all", false, "print commands to install all required OS packages, not just those that are missing")
flags.BoolVar(&packagesFlags.installPackages, "install", false, "install the requested packages. This may need to be run as root.")
for _, name := range profilesmanager.Managers() {
profilesmanager.LookupManager(name).AddFlags(flags, profiles.Install)
}
}
func (pv *packagesFlagValues) args() []string {
a := pv.commonFlagValues.args()
if t := pv.target.String(); t != "" {
a = append(a, "--target="+t)
}
if e := pv.target.CommandLineEnv().String(); e != "" {
a = append(a, "--target="+e)
}
a = append(a, fmt.Sprintf("--%s=%v", "all", pv.allPackages))
return append(a, fmt.Sprintf("--%s=%v", "install", pv.installPackages))
}
type installFlagValues struct {
commonFlagValues
// The value of --target and --env
target profiles.Target
// The value of --force
force bool
}
func initInstallCommand(flags *flag.FlagSet, installer, defaultDBPath, defaultProfilesPath string) {
initCommon(flags, &installFlags.commonFlagValues, installer, defaultDBPath, defaultProfilesPath)
profiles.RegisterTargetAndEnvFlags(flags, &installFlags.target)
flags.BoolVar(&installFlags.force, "force", false, "force install the profile even if it is already installed")
for _, name := range profilesmanager.Managers() {
profilesmanager.LookupManager(name).AddFlags(flags, profiles.Install)
}
}
func (iv *installFlagValues) args() []string {
a := iv.commonFlagValues.args()
if t := iv.target.String(); t != "" {
a = append(a, "--target="+t)
}
if e := iv.target.CommandLineEnv().String(); e != "" {
a = append(a, "--target="+e)
}
return append(a, fmt.Sprintf("--%s=%v", "force", iv.force))
}
type uninstallFlagValues struct {
commonFlagValues
// The value of --target
target profiles.Target
// The value of --all-targets
allTargets bool
// The value of --v
verbose bool
// TODO(cnicolaou): add a flag to remove the profile only from the DB.
}
func initUninstallCommand(flags *flag.FlagSet, installer, defaultDBPath, defaultProfilesPath string) {
initCommon(flags, &uninstallFlags.commonFlagValues, installer, defaultDBPath, defaultProfilesPath)
profiles.RegisterTargetFlag(flags, &uninstallFlags.target)
flags.BoolVar(&uninstallFlags.allTargets, "all-targets", false, "apply to all targets for the specified profile(s)")
flags.BoolVar(&uninstallFlags.verbose, "v", false, "print more detailed information")
for _, name := range profilesmanager.Managers() {
profilesmanager.LookupManager(name).AddFlags(flags, profiles.Uninstall)
}
}
func (uv *uninstallFlagValues) args() []string {
a := uv.commonFlagValues.args()
if uv.target.String() != "" {
a = append(a, "--target="+uv.target.String())
}
a = append(a, fmt.Sprintf("--%s=%v", "all-targets", uv.allTargets))
return append(a, fmt.Sprintf("--%s=%v", "v", uv.verbose))
}
type cleanupFlagValues struct {
commonFlagValues
// The value of --gc
gc bool
// The value of --rewrite-profiles-db
rewriteDB bool
// The value of --rm-all
rmAll bool
// The value of --v
verbose bool
}
func initCleanupCommand(flags *flag.FlagSet, installer, defaultDBPath, defaultProfilesPath string) {
initCommon(flags, &cleanupFlags.commonFlagValues, installer, defaultDBPath, defaultProfilesPath)
flags.BoolVar(&cleanupFlags.gc, "gc", false, "uninstall profile targets that are older than the current default")
flags.BoolVar(&cleanupFlags.rmAll, "rm-all", false, "remove profiles database and all profile generated output files.")
flags.BoolVar(&cleanupFlags.rewriteDB, "rewrite-profiles-db", false, "rewrite the profiles database to use the latest schema version")
flags.BoolVar(&cleanupFlags.verbose, "v", false, "print more detailed information")
}
func (cv *cleanupFlagValues) args() []string {
return append(cv.commonFlagValues.args(),
fmt.Sprintf("--%s=%v", "gc", cv.gc),
fmt.Sprintf("--%s=%v", "rewrite-profiles-db", cv.rewriteDB),
fmt.Sprintf("--%s=%v", "v", cv.verbose),
fmt.Sprintf("--%s=%v", "rm-all", cv.rmAll))
}
type updateFlagValues struct {
commonFlagValues
// The value of --v
verbose bool
}
func initUpdateCommand(flags *flag.FlagSet, installer, defaultDBPath, defaultProfilesPath string) {
initCommon(flags, &updateFlags.commonFlagValues, installer, defaultDBPath, defaultProfilesPath)
flags.BoolVar(&updateFlags.verbose, "v", false, "print more detailed information")
}
func (uv *updateFlagValues) args() []string {
return append(uv.commonFlagValues.args(), fmt.Sprintf("--%s=%v", "v", uv.verbose))
}
type availableFlagValues struct {
// The value of --v
verbose bool
// The value of --describe
describe bool
}
func initAvailableCommand(flags *flag.FlagSet, installer, defaultDBPath, defaultProfilesPath string) {
flags.BoolVar(&availableFlags.verbose, "v", false, "print more detailed information")
flags.BoolVar(&availableFlags.describe, "describe", false, "print the profile description")
}
func (av *availableFlagValues) args() []string {
return []string{
fmt.Sprintf("--%s=%v", "v", av.verbose),
fmt.Sprintf("--%s=%v", "describe", av.describe),
}
}
var (
packagesFlags packagesFlagValues
installFlags installFlagValues
uninstallFlags uninstallFlagValues
cleanupFlags cleanupFlagValues
updateFlags updateFlagValues
availableFlags availableFlagValues
profileInstaller string
runSubcommands bool
)
// RegisterManagementCommands registers the management subcommands:
// uninstall, install, update and cleanup.
func RegisterManagementCommands(parent *cmdline.Command, useSubcommands bool, installer, defaultDBPath, defaultProfilesPath string) {
cmdOSPackages := newCmdOSPackages()
cmdInstall := newCmdInstall()
cmdUninstall := newCmdUninstall()
cmdUpdate := newCmdUpdate()
cmdCleanup := newCmdCleanup()
cmdAvailable := newCmdAvailable()
initPackagesCommand(&cmdOSPackages.Flags, installer, defaultDBPath, defaultProfilesPath)
initInstallCommand(&cmdInstall.Flags, installer, defaultDBPath, defaultProfilesPath)
initUninstallCommand(&cmdUninstall.Flags, installer, defaultDBPath, defaultProfilesPath)
initUpdateCommand(&cmdUpdate.Flags, installer, defaultDBPath, defaultProfilesPath)
initCleanupCommand(&cmdCleanup.Flags, installer, defaultDBPath, defaultProfilesPath)
initAvailableCommand(&cmdAvailable.Flags, installer, defaultDBPath, defaultProfilesPath)
parent.Children = append(parent.Children, cmdInstall, cmdOSPackages, cmdUninstall, cmdUpdate, cmdCleanup, cmdAvailable)
profileInstaller = installer
runSubcommands = useSubcommands
}
func findProfileSubcommands(jirix *jiri.X) []string {
if !runSubcommands {
return nil
}
cmds, _ := lookpath.LookPrefix(jirix.Env(), "jiri-profile-", nil)
return cmds
}
func allAvailableManagers(jirix *jiri.X) ([]string, error) {
names := profilesmanager.Managers()
if profileInstaller != "" {
return names, nil
}
subcommands := findProfileSubcommands(jirix)
s := jirix.NewSeq()
for _, sc := range subcommands {
var out bytes.Buffer
args := []string{"available"}
if err := s.Capture(&out, nil).Last(sc, args...); err != nil {
fmt.Fprintf(jirix.Stderr(), "failed to run %s %s: %v", sc, strings.Join(args, " "), err)
return nil, err
}
mgrs := out.String()
for _, m := range strings.Split(mgrs, ",") {
names = append(names, strings.TrimSpace(m))
}
}
return names, nil
}
// availableProfileManagers creates a profileManager for all available
// profiles, whether in this process or in a sub command.
func availableProfileManagers(jirix *jiri.X, dbpath string, args []string) ([]profileManager, *profiles.DB, error) {
db := profiles.NewDB()
if err := db.Read(jirix, dbpath); err != nil {
fmt.Fprintf(jirix.Stderr(), "Failed to read profiles database %q: %v\n", dbpath, err)
return nil, nil, err
}
mgrs := []profileManager{}
names := args
if len(names) == 0 {
var err error
names, err = allAvailableManagers(jirix)
if err != nil {
return nil, nil, err
}
}
for _, name := range names {
mgrs = append(mgrs, newProfileManager(name, db))
}
return mgrs, db, nil
}
// installedProfileManagers creates a profileManager for all installed
// profiles, whether in this process or in a sub command.
func installedProfileManagers(jirix *jiri.X, dbpath string, args []string) ([]profileManager, *profiles.DB, error) {
db := profiles.NewDB()
if err := db.Read(jirix, dbpath); err != nil {
fmt.Fprintf(jirix.Stderr(), "Failed to read profiles database %q: %v\n", dbpath, err)
return nil, nil, err
}
mgrs := []profileManager{}
names := args
if len(names) == 0 {
names = db.Names()
}
for _, name := range names {
mgrs = append(mgrs, newProfileManager(name, db))
}
return mgrs, db, nil
}
func targetAtDefaultVersion(mgr profiles.Manager, target profiles.Target) (profiles.Target, error) {
def := target
version, err := mgr.VersionInfo().Select(target.Version())
if err != nil {
return profiles.Target{}, err
}
def.SetVersion(version)
return def, nil
}
func writeDB(jirix *jiri.X, db *profiles.DB, installer, path string) error {
// Do nothing if the installer is not supplied. This will generally
// happen when/if writeDB is called from the top-level profile driver
// command rather than from a subcommand.
if installer == "" {
return nil
}
fi, err := os.Stat(path)
if err != nil {
if !os.IsNotExist(err) {
return err
}
// New setup, but the directory doesn't exist yet.
if err := os.MkdirAll(path, os.FileMode(0755)); err != nil {
return err
}
} else {
if !fi.IsDir() {
return fmt.Errorf("%s exists but is not a directory", path)
}
}
// New setup with installers writing their own file in a directory
return db.Write(jirix, installer, path)
}
func updateImpl(jirix *jiri.X, cl *updateFlagValues, args []string) error {
mgrs, db, err := availableProfileManagers(jirix, cl.dbPath, args)
if err != nil {
return err
}
root := jiri.NewRelPath(cl.root).Join(profileInstaller)
for _, mgr := range mgrs {
if err := mgr.update(jirix, cl, root); err != nil {
return err
}
}
return writeDB(jirix, db, profileInstaller, cl.dbPath)
}
func cleanupImpl(jirix *jiri.X, cl *cleanupFlagValues, args []string) error {
count := 0
if cl.gc {
count++
}
if cl.rewriteDB {
count++
}
if cl.rmAll {
count++
}
if count != 1 {
fmt.Errorf("exactly one option must be specified")
}
mgrs, db, err := installedProfileManagers(jirix, cl.dbPath, args)
if err != nil {
return err
}
root := jiri.NewRelPath(cl.root).Join(profileInstaller)
for _, mgr := range mgrs {
if err := mgr.cleanup(jirix, cl, root); err != nil {
return err
}
}
if !cl.rmAll {
return writeDB(jirix, db, profileInstaller, cl.dbPath)
}
return nil
}
func packagesImpl(jirix *jiri.X, cl *packagesFlagValues, args []string) error {
mgrs, _, err := availableProfileManagers(jirix, cl.dbPath, args)
if err != nil {
return err
}
cl.target.UseCommandLineEnv()
root := jiri.NewRelPath(cl.root).Join(profileInstaller)
s := jirix.NewSeq()
installPackages := cl.installPackages
// Never ask a subcommand to install packages.
cl.installPackages = false
for _, mgr := range mgrs {
cmds, err := mgr.packageCmds(jirix, cl, root)
if err != nil {
return err
}
for _, cmd := range cmds {
if installPackages {
if err := s.Verbose(true).Last(cmd[0], cmd[1:]...); err != nil {
return err
}
} else {
fmt.Fprintf(jirix.Stdout(), "%s\n", strings.TrimSpace(strings.Join(cmd, " ")))
}
}
}
return nil
}
func installImpl(jirix *jiri.X, cl *installFlagValues, args []string) error {
mgrs, db, err := availableProfileManagers(jirix, cl.dbPath, args)
if err != nil {
return err
}
cl.target.UseCommandLineEnv()
newMgrs := []profileManager{}
for _, mgr := range mgrs {
name := mgr.mgrName()
if !cl.force {
installer, profile := profiles.SplitProfileName(name)
if p := db.LookupProfileTarget(installer, profile, cl.target); p != nil {
fmt.Fprintf(jirix.Stdout(), "%v %v is already installed as %v\n", name, cl.target, p)
continue
}
}
newMgrs = append(newMgrs, mgr)
}
root := jiri.NewRelPath(cl.root).Join(profileInstaller)
for _, mgr := range newMgrs {
if err := mgr.install(jirix, cl, root); err != nil {
return err
}
}
return writeDB(jirix, db, profileInstaller, cl.dbPath)
}
func uninstallImpl(jirix *jiri.X, cl *uninstallFlagValues, args []string) error {
mgrs, db, err := availableProfileManagers(jirix, cl.dbPath, args)
if err != nil {
return err
}
if cl.allTargets && cl.target.IsSet() {
fmt.Fprintf(jirix.Stdout(), "ignore target (%v) when used in conjunction with --all-targets\n", cl.target)
}
root := jiri.NewRelPath(cl.root).Join(profileInstaller)
for _, mgr := range mgrs {
if err := mgr.uninstall(jirix, cl, root); err != nil {
return err
}
}
return writeDB(jirix, db, profileInstaller, cl.dbPath)
}
func availableImpl(jirix *jiri.X, cl *availableFlagValues, _ []string) error {
if profileInstaller == "" {
subcommands := findProfileSubcommands(jirix)
if cl.verbose {
fmt.Fprintf(jirix.Stdout(), "Available Subcommands: %s\n", strings.Join(subcommands, ", "))
}
s := jirix.NewSeq()
args := []string{"available"}
args = append(args, cl.args()...)
out := bytes.Buffer{}
for _, sc := range subcommands {
if err := s.Capture(&out, nil).Last(sc, args...); err != nil {
return err
}
}
if s := strings.TrimSpace(out.String()); s != "" {
fmt.Fprintln(jirix.Stdout(), s)
}
}
mgrs := profilesmanager.Managers()
if len(mgrs) == 0 {
return nil
}
if cl.verbose {
scname := ""
if profileInstaller != "" {
scname = profileInstaller + ": "
}
fmt.Fprintf(jirix.Stdout(), "%sAvailable Profiles:\n", scname)
for _, name := range mgrs {
mgr := profilesmanager.LookupManager(name)
vi := mgr.VersionInfo()
fmt.Fprintf(jirix.Stdout(), "%s: versions: %s\n", name, vi)
}
} else {
if cl.describe {
for _, name := range mgrs {
mgr := profilesmanager.LookupManager(name)
fmt.Fprintf(jirix.Stdout(), "%s: %s\n", name, strings.Replace(strings.TrimSpace(mgr.Info()), "\n", " ", -1))
}
} else {
fmt.Fprintf(jirix.Stdout(), "%s\n", strings.Join(mgrs, ", "))
}
}
return nil
}