github.com/GoogleCloudPlatform/compute-image-tools/cli_tools@v0.0.0-20240516224744-de2dabc4ed1b/common/imagefile/inspector.go (about)

     1  //  Copyright 2020 Google Inc. All Rights Reserved.
     2  //
     3  //  Licensed under the Apache License, Version 2.0 (the "License");
     4  //  you may not use this file except in compliance with the License.
     5  //  You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  //  Unless required by applicable law or agreed to in writing, software
    10  //  distributed under the License is distributed on an "AS IS" BASIS,
    11  //  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  //  See the License for the specific language governing permissions and
    13  //  limitations under the License.
    14  
    15  package imagefile
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"math"
    21  	"path"
    22  	"time"
    23  
    24  	"github.com/GoogleCloudPlatform/compute-image-tools/cli_tools/common/gcsfuse"
    25  	"github.com/GoogleCloudPlatform/compute-image-tools/cli_tools/common/utils/files"
    26  	"github.com/GoogleCloudPlatform/compute-image-tools/cli_tools/common/utils/storage"
    27  	"github.com/cenkalti/backoff/v4"
    28  )
    29  
    30  const bytesPerGB = int64(1024 * 1024 * 1024)
    31  
    32  // Metadata contains metadata about an image file.
    33  type Metadata struct {
    34  	// PhysicalSizeGB is the size of the file itself, rounded up to the nearest GB.
    35  	PhysicalSizeGB int64
    36  
    37  	// VirtualSizeGB is the size of the disk, after inflation. Rounded up to the nearest GB.
    38  	VirtualSizeGB int64
    39  
    40  	// FileFormat is the format used for encoding the VM disk.
    41  	FileFormat string
    42  
    43  	Checksum string
    44  }
    45  
    46  // Inspector returns metadata about image files.
    47  type Inspector interface {
    48  	// Inspect returns Metadata for the image file associated
    49  	// with a reference. IO operations will be retried until the context is cancelled.
    50  	Inspect(ctx context.Context, reference string) (Metadata, error)
    51  }
    52  
    53  // NewGCSInspector returns an inspector that inspects image
    54  // files that are stored in the GCS bucket. The Inspect method expects
    55  // a GCS URI to the file to be inspected.
    56  func NewGCSInspector() Inspector {
    57  	return gcsInspector{
    58  		qemuClient: NewInfoClient(),
    59  		fuseClient: gcsfuse.NewClient()}
    60  }
    61  
    62  // gcsInspector implements inspector using qemu-img gcsfuse.
    63  type gcsInspector struct {
    64  	qemuClient InfoClient
    65  	fuseClient gcsfuse.Client
    66  }
    67  
    68  func (inspector gcsInspector) Inspect(ctx context.Context, gcsURI string) (metadata Metadata, err error) {
    69  	operation := func() error {
    70  		metadata, err = inspector.inspectOnce(ctx, gcsURI)
    71  		return err
    72  	}
    73  	return metadata, backoff.Retry(operation,
    74  		backoff.WithContext(backoff.NewConstantBackOff(50*time.Millisecond), ctx))
    75  }
    76  
    77  func (inspector gcsInspector) inspectOnce(ctx context.Context, gcsURI string) (metadata Metadata, err error) {
    78  	bucket, object, err := storage.GetGCSObjectPathElements(gcsURI)
    79  	if err != nil {
    80  		return metadata, err
    81  	}
    82  	mountedDirectory, err := inspector.fuseClient.MountToTemp(ctx, bucket)
    83  	defer inspector.fuseClient.Unmount(mountedDirectory)
    84  	if err != nil {
    85  		return metadata, err
    86  	}
    87  	absPath := path.Join(mountedDirectory, object)
    88  	if !files.Exists(absPath) {
    89  		return metadata, fmt.Errorf("the file %q was not found", gcsURI)
    90  	}
    91  	imageInfo, err := inspector.qemuClient.GetInfo(ctx, absPath)
    92  	if err != nil {
    93  		return metadata, err
    94  	}
    95  	return Metadata{
    96  		PhysicalSizeGB: bytesToGB(imageInfo.ActualSizeBytes),
    97  		VirtualSizeGB:  bytesToGB(imageInfo.VirtualSizeBytes),
    98  		FileFormat:     imageInfo.Format,
    99  		Checksum:       imageInfo.Checksum,
   100  	}, nil
   101  }
   102  
   103  // bytesToGB rounds up to the nearest GB.
   104  func bytesToGB(bytes int64) int64 {
   105  	return int64(math.Ceil(float64(bytes) / float64(bytesPerGB)))
   106  }