|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/Azure/azure-container-networking/dropgz/pkg/embed" |
| 7 | + "github.com/Azure/azure-container-networking/dropgz/pkg/hash" |
| 8 | + "github.com/pkg/errors" |
| 9 | + "github.com/spf13/cobra" |
| 10 | + "go.uber.org/zap" |
| 11 | +) |
| 12 | + |
| 13 | +// list subcommand |
| 14 | +var list = &cobra.Command{ |
| 15 | + Use: "list", |
| 16 | + RunE: func(*cobra.Command, []string) error { |
| 17 | + contents, err := embed.Contents() |
| 18 | + if err != nil { |
| 19 | + return err |
| 20 | + } |
| 21 | + for _, c := range contents { |
| 22 | + fmt.Printf("\t%s\n", c) |
| 23 | + } |
| 24 | + return nil |
| 25 | + }, |
| 26 | +} |
| 27 | + |
| 28 | +func checksum(srcs, dests []string) error { |
| 29 | + if len(srcs) != len(dests) { |
| 30 | + return errors.Wrapf(embed.ErrArgsMismatched, "%d and %d", len(srcs), len(dests)) |
| 31 | + } |
| 32 | + r, c, err := embed.Extract("sum.txt") |
| 33 | + if err != nil { |
| 34 | + return errors.Wrap(err, "failed to extract checksum file") |
| 35 | + } |
| 36 | + defer c.Close() |
| 37 | + defer r.Close() |
| 38 | + |
| 39 | + checksums, err := hash.Parse(r) |
| 40 | + if err != nil { |
| 41 | + return errors.Wrap(err, "failed to parse checksums") |
| 42 | + } |
| 43 | + for i := range srcs { |
| 44 | + valid, err := checksums.Check(srcs[i], dests[i]) |
| 45 | + if err != nil { |
| 46 | + return errors.Wrapf(err, "failed to validate file at %s", dests[i]) |
| 47 | + } |
| 48 | + if !valid { |
| 49 | + return errors.Wrapf(err, "%s checksum validation failed", dests[i]) |
| 50 | + } |
| 51 | + } |
| 52 | + return nil |
| 53 | +} |
| 54 | + |
| 55 | +var ( |
| 56 | + skipVerify bool |
| 57 | + outs []string |
| 58 | +) |
| 59 | + |
| 60 | +// deploy subcommand |
| 61 | +var deploy = &cobra.Command{ |
| 62 | + Use: "deploy", |
| 63 | + RunE: func(_ *cobra.Command, srcs []string) error { |
| 64 | + if len(outs) == 0 { |
| 65 | + outs = srcs |
| 66 | + } |
| 67 | + if len(srcs) != len(outs) { |
| 68 | + return errors.Wrapf(embed.ErrArgsMismatched, "%d files, %d outputs", len(srcs), len(outs)) |
| 69 | + } |
| 70 | + log := z.With(zap.Strings("sources", srcs), zap.Strings("outputs", outs), zap.String("cmd", "deploy")) |
| 71 | + if err := embed.Deploy(log, srcs, outs); err != nil { |
| 72 | + log.Error("failed to deploy", zap.Error(err)) |
| 73 | + return errors.Wrapf(err, "failed to deploy %s", srcs) |
| 74 | + } |
| 75 | + log.Info("successfully wrote files") |
| 76 | + if skipVerify { |
| 77 | + return nil |
| 78 | + } |
| 79 | + if err := checksum(srcs, outs); err != nil { |
| 80 | + log.Error("failed to verify", zap.Error(err)) |
| 81 | + return err |
| 82 | + } |
| 83 | + log.Info("verified file integrity") |
| 84 | + return nil |
| 85 | + }, |
| 86 | + Args: cobra.OnlyValidArgs, |
| 87 | +} |
| 88 | + |
| 89 | +// verify subcommand |
| 90 | +var verify = &cobra.Command{ |
| 91 | + Use: "verify", |
| 92 | + RunE: func(_ *cobra.Command, srcs []string) error { |
| 93 | + if len(outs) == 0 { |
| 94 | + outs = srcs |
| 95 | + } |
| 96 | + if len(srcs) != len(outs) { |
| 97 | + return errors.Wrapf(embed.ErrArgsMismatched, "%d sources, %d destinations", len(srcs), len(outs)) |
| 98 | + } |
| 99 | + log := z.With(zap.Strings("sources", srcs), zap.String("cmd", "deploy")) |
| 100 | + if err := checksum(srcs, outs); err != nil { |
| 101 | + log.Error("failed to verify", zap.Error(err)) |
| 102 | + return err |
| 103 | + } |
| 104 | + return nil |
| 105 | + }, |
| 106 | + Args: cobra.OnlyValidArgs, |
| 107 | +} |
| 108 | + |
| 109 | +func init() { |
| 110 | + root.AddCommand(list) |
| 111 | + |
| 112 | + verify.ValidArgs, _ = embed.Contents() |
| 113 | + verify.Flags().StringSliceVarP(&outs, "output", "o", []string{}, "output file path") |
| 114 | + root.AddCommand(verify) |
| 115 | + |
| 116 | + deploy.ValidArgs, _ = embed.Contents() // setting this after the command is initialized is required |
| 117 | + deploy.Flags().BoolVar(&skipVerify, "skip-verify", false, "set to disable checksum validation") |
| 118 | + deploy.Flags().StringSliceVarP(&outs, "output", "o", []string{}, "output file path") |
| 119 | + root.AddCommand(deploy) |
| 120 | +} |
0 commit comments