scan

package
v0.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 8, 2023 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultWatcherInterval = 2 * time.Minute
	DefaultMountTimeout    = 10 * time.Minute
)

Variables

View Source
var ScanCmd = &cobra.Command{
	Use:   "scan",
	Short: "Scan",
	Long:  `Run scanner families`,
	RunE: func(cmd *cobra.Command, args []string) error {
		logutil.Logger.Infof("Running...")

		ctx := log.SetLoggerForContext(cmd.Context(), logutil.Logger)

		cfgFile, err := cmd.Flags().GetString("config")
		if err != nil {
			logutil.Logger.Fatalf("Unable to get config file name: %v", err)
		}
		server, err := cmd.Flags().GetString("server")
		if err != nil {
			logutil.Logger.Fatalf("Unable to get VMClarity server address: %v", err)
		}
		output, err := cmd.Flags().GetString("output")
		if err != nil {
			logutil.Logger.Fatalf("Unable to get output file name: %v", err)
		}
		assetScanID, err := cmd.Flags().GetString("asset-scan-id")
		if err != nil {
			logutil.Logger.Fatalf("Unable to get asset scan ID: %v", err)
		}
		mountVolume, err := cmd.Flags().GetBool("mount-attached-volume")
		if err != nil {
			logutil.Logger.Fatalf("Unable to get mount attached volume flag: %v", err)
		}

		config := loadConfig(cfgFile)
		cli, err := newCli(config, server, assetScanID, output)
		if err != nil {
			return fmt.Errorf("failed to initialize CLI: %w", err)
		}

		abortCtx, cancel := context.WithCancel(ctx)
		defer cancel()

		cli.WatchForAbort(ctx, cancel, DefaultWatcherInterval)

		if err := cli.WaitForReadyState(abortCtx); err != nil {
			err = fmt.Errorf("failed to wait for AssetScan being ready to scan: %w", err)
			if e := cli.MarkDone(ctx, []error{err}); e != nil {
				logutil.Logger.Errorf("Failed to update AssetScan status to completed with errors: %v", e)
			}
			return err
		}

		if mountVolume {

			mountCtx, mountCancel := context.WithTimeout(abortCtx, DefaultMountTimeout)
			defer mountCancel()

			mountPoints, err := cli.MountVolumes(mountCtx)
			if err != nil {
				err = fmt.Errorf("failed to mount attached volume: %w", err)
				if e := cli.MarkDone(ctx, []error{err}); e != nil {
					logutil.Logger.Errorf("Failed to update asset scan stat to completed with errors: %v", e)
				}
				return err
			}
			families.SetMountPointsForFamiliesInput(mountPoints, config)
		}

		err = cli.MarkInProgress(ctx, config)
		if err != nil {
			return fmt.Errorf("failed to inform server %v scan has started: %w", server, err)
		}

		logutil.Logger.Infof("Running scanners...")
		runErrors := families.New(config).Run(abortCtx, cli)

		err = cli.MarkDone(ctx, runErrors)
		if err != nil {
			return fmt.Errorf("failed to inform the server %v the scan was completed: %w", server, err)
		}

		if len(runErrors) > 0 {
			logutil.Logger.Errorf("Errors when running families: %+v", runErrors)
		}

		return nil
	},
}

ScanCmd represents the scan command.

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL