Skip to content

Commit d3b0e0a

Browse files
Fix context usage in shim package
Signed-off-by: Matías Insaurralde <[email protected]>
1 parent bd9fe78 commit d3b0e0a

File tree

4 files changed

+15
-11
lines changed

4 files changed

+15
-11
lines changed

runner/internal/shim/backends/aws.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package backends
22

33
import (
44
"bytes"
5+
"context"
56
"fmt"
67
"os"
78
"os/exec"
@@ -23,10 +24,10 @@ func NewAWSBackend() *AWSBackend {
2324
// * Red Hat and CentOS: may increment trailing letters in some versions – not supported.
2425
// * Other legacy systems: /dev/sda => /dev/sda.
2526
// More: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/device_naming.html
26-
func (e *AWSBackend) GetRealDeviceName(volumeID, deviceName string) (string, error) {
27+
func (e *AWSBackend) GetRealDeviceName(ctx context.Context, volumeID, deviceName string) (string, error) {
2728
// Run the lsblk command to get block device information
2829
// On AWS, SERIAL contains volume id.
29-
cmd := exec.Command("lsblk", "-o", "NAME,SERIAL")
30+
cmd := exec.CommandContext(ctx, "lsblk", "-o", "NAME,SERIAL")
3031
var out bytes.Buffer
3132
cmd.Stdout = &out
3233
if err := cmd.Run(); err != nil {
@@ -67,7 +68,7 @@ func (e *AWSBackend) GetRealDeviceName(volumeID, deviceName string) (string, err
6768
}
6869

6970
// Run lsblk again to check for partitions on the base device
70-
cmd = exec.Command("lsblk", "-ln", "-o", "NAME", baseDevice)
71+
cmd = exec.CommandContext(ctx, "lsblk", "-ln", "-o", "NAME", baseDevice)
7172
out.Reset()
7273
cmd.Stdout = &out
7374
if err := cmd.Run(); err != nil {
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package backends
22

3+
import "context"
4+
35
type Backend interface {
46
// GetRealDeviceName returns the real device name for the given volume ID and virtual device name.
5-
GetRealDeviceName(volumeID, deviceName string) (string, error)
7+
GetRealDeviceName(ctx context.Context, volumeID, deviceName string) (string, error)
68
}

runner/internal/shim/backends/gcp.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package backends
22

33
import (
4+
"context"
45
"fmt"
56
"os"
67
"path/filepath"
@@ -15,7 +16,7 @@ func NewGCPBackend() *GCPBackend {
1516
}
1617

1718
// GetRealDeviceName resolves device names according to https://cloud.google.com/compute/docs/disks/disk-symlinks
18-
func (e *GCPBackend) GetRealDeviceName(volumeID, deviceName string) (string, error) {
19+
func (e *GCPBackend) GetRealDeviceName(ctx context.Context, volumeID, deviceName string) (string, error) {
1920
// Try resolving first partition or external volumes
2021
realDeviceName, err := os.Readlink(fmt.Sprintf("/dev/disk/by-id/google-%s-part1", deviceName))
2122
if err != nil {

runner/internal/shim/docker.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -535,12 +535,12 @@ func unmountVolumes(ctx context.Context, taskConfig TaskConfig) error {
535535
var failed []string
536536
for _, volume := range taskConfig.Volumes {
537537
mountPoint := getVolumeMountPoint(volume.Name)
538-
cmd := exec.Command("mountpoint", mountPoint)
538+
cmd := exec.CommandContext(ctx, "mountpoint", mountPoint)
539539
if output, err := cmd.CombinedOutput(); err != nil {
540540
log.Info(ctx, "skipping", "mountpoint", mountPoint, "output", output)
541541
continue
542542
}
543-
cmd = exec.Command("umount", "-qf", mountPoint)
543+
cmd = exec.CommandContext(ctx, "umount", "-qf", mountPoint)
544544
if output, err := cmd.CombinedOutput(); err != nil {
545545
log.Error(ctx, "failed to unmount", "mountpoint", mountPoint, "output", output)
546546
failed = append(failed, mountPoint)
@@ -559,7 +559,7 @@ func formatAndMountVolume(ctx context.Context, volume VolumeInfo) error {
559559
if err != nil {
560560
return tracerr.Wrap(err)
561561
}
562-
deviceName, err := backend.GetRealDeviceName(volume.VolumeId, volume.DeviceName)
562+
deviceName, err := backend.GetRealDeviceName(ctx, volume.VolumeId, volume.DeviceName)
563563
if err != nil {
564564
return tracerr.Wrap(err)
565565
}
@@ -618,7 +618,7 @@ func prepareInstanceMountPoints(taskConfig TaskConfig) error {
618618
// Returns true if the file system is created.
619619
func initFileSystem(ctx context.Context, deviceName string, errorIfNotExists bool) (bool, error) {
620620
// Run the lsblk command to get filesystem type
621-
cmd := exec.Command("lsblk", "-no", "FSTYPE", deviceName)
621+
cmd := exec.CommandContext(ctx, "lsblk", "-no", "FSTYPE", deviceName)
622622
var out bytes.Buffer
623623
cmd.Stdout = &out
624624
if err := cmd.Run(); err != nil {
@@ -636,7 +636,7 @@ func initFileSystem(ctx context.Context, deviceName string, errorIfNotExists boo
636636
}
637637

638638
log.Debug(ctx, "formatting disk with ext4 filesystem...", "device", deviceName)
639-
cmd = exec.Command("mkfs.ext4", "-F", deviceName)
639+
cmd = exec.CommandContext(ctx, "mkfs.ext4", "-F", deviceName)
640640
if output, err := cmd.CombinedOutput(); err != nil {
641641
return false, fmt.Errorf("failed to format disk: %w, output: %s", err, string(output))
642642
}
@@ -655,7 +655,7 @@ func mountDisk(ctx context.Context, deviceName, mountPoint string, fsRootPerms o
655655

656656
// Mount the disk to the mount point
657657
log.Debug(ctx, "mounting disk...", "device", deviceName, "mountpoint", mountPoint)
658-
cmd := exec.Command("mount", deviceName, mountPoint)
658+
cmd := exec.CommandContext(ctx, "mount", deviceName, mountPoint)
659659
if output, err := cmd.CombinedOutput(); err != nil {
660660
return fmt.Errorf("failed to mount disk: %w, output: %s", err, string(output))
661661
}

0 commit comments

Comments
 (0)