16306: Merge branch 'master'
[arvados.git] / cmd / arvados-package / cmd.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package main
6
7 import (
8         "context"
9         "flag"
10         "fmt"
11         "io"
12         "os"
13         "path/filepath"
14         "strings"
15
16         "git.arvados.org/arvados.git/lib/cmd"
17         "git.arvados.org/arvados.git/lib/install"
18         "git.arvados.org/arvados.git/sdk/go/ctxlog"
19 )
20
21 var (
22         handler = cmd.Multi(map[string]cmd.Handler{
23                 "version":   cmd.Version,
24                 "-version":  cmd.Version,
25                 "--version": cmd.Version,
26
27                 "build":       cmdFunc(build),
28                 "testinstall": cmdFunc(testinstall),
29                 "_fpm":        cmdFunc(fpm),    // internal use
30                 "_install":    install.Command, // internal use
31         })
32 )
33
34 func main() {
35         if len(os.Args) < 2 || strings.HasPrefix(os.Args[1], "-") {
36                 parseFlags([]string{"-help"})
37                 os.Exit(2)
38         }
39         os.Exit(handler.RunCommand(os.Args[0], os.Args[1:], os.Stdin, os.Stdout, os.Stderr))
40 }
41
42 type cmdFunc func(ctx context.Context, opts opts, stdin io.Reader, stdout, stderr io.Writer) error
43
44 func (cf cmdFunc) RunCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int {
45         logger := ctxlog.New(stderr, "text", "info")
46         ctx := ctxlog.Context(context.Background(), logger)
47         opts, err := parseFlags(args)
48         if err != nil {
49                 logger.WithError(err).Error("error parsing command line flags")
50                 return 1
51         }
52         err = cf(ctx, opts, stdin, stdout, stderr)
53         if err != nil {
54                 logger.WithError(err).Error("failed")
55                 return 1
56         }
57         return 0
58 }
59
60 type opts struct {
61         PackageVersion string
62         PackageDir     string
63         PackageChown   string
64         RebuildImage   bool
65         SourceDir      string
66         TargetOS       string
67 }
68
69 func parseFlags(args []string) (opts, error) {
70         opts := opts{
71                 TargetOS: "debian:10",
72         }
73         flags := flag.NewFlagSet("", flag.ContinueOnError)
74         flags.StringVar(&opts.PackageVersion, "package-version", opts.PackageVersion, "package version to build/test, like \"1.2.3\"")
75         flags.StringVar(&opts.SourceDir, "source", opts.SourceDir, "arvados source tree location")
76         flags.StringVar(&opts.PackageDir, "package-dir", opts.PackageDir, "destination directory for new package (default is cwd)")
77         flags.StringVar(&opts.PackageChown, "package-chown", opts.PackageChown, "desired uid:gid for new package (default is current user:group)")
78         flags.StringVar(&opts.TargetOS, "target-os", opts.TargetOS, "target operating system vendor:version")
79         flags.BoolVar(&opts.RebuildImage, "rebuild-image", opts.RebuildImage, "rebuild docker image(s) instead of using existing")
80         flags.Usage = func() {
81                 fmt.Fprint(flags.Output(), `Usage: arvados-package <subcommand> [options]
82
83 Subcommands:
84         build
85                 use a docker container to build a package from a checked
86                 out version of the arvados source tree
87         testinstall
88                 use a docker container to install a package and confirm
89                 the resulting installation is functional
90         version
91                 show program version
92
93 Internally used subcommands:
94         _fpm
95                 build a package
96         _install
97                 equivalent to "arvados-server install"
98
99 Automation/integration notes:
100         The first time a given machine runs "build" or "testinstall" (and
101         any time the -rebuild-image is used), new docker images are built,
102         which is quite slow. If you use on-demand VMs to run automated builds,
103         run "build" and "testinstall" once when setting up your initial VM
104         image, and be prepared to rebuild that VM image when package-building
105         slows down (this will happen when new dependencies are introduced).
106
107 Options:
108 `)
109                 flags.PrintDefaults()
110         }
111         err := flags.Parse(args)
112         if err != nil {
113                 return opts, err
114         }
115         if len(flags.Args()) > 0 {
116                 return opts, fmt.Errorf("unrecognized command line arguments: %v", flags.Args())
117         }
118         if opts.SourceDir == "" {
119                 d, err := os.Getwd()
120                 if err != nil {
121                         return opts, fmt.Errorf("Getwd: %w", err)
122                 }
123                 opts.SourceDir = d
124         }
125         opts.PackageDir = filepath.Clean(opts.PackageDir)
126         return opts, nil
127 }