|
| 1 | +/* |
| 2 | +Copyright © 2024 thin-edge.io <info@thin-edge.io> |
| 3 | +*/ |
| 4 | +package container_image |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "encoding/json" |
| 9 | + "fmt" |
| 10 | + "io" |
| 11 | + "log/slog" |
| 12 | + "os" |
| 13 | + "strings" |
| 14 | + "time" |
| 15 | + |
| 16 | + "github.com/docker/docker/client" |
| 17 | + "github.com/spf13/cobra" |
| 18 | + "github.com/spf13/viper" |
| 19 | + "github.com/thin-edge/tedge-container-plugin/pkg/cli" |
| 20 | + "github.com/thin-edge/tedge-container-plugin/pkg/container" |
| 21 | +) |
| 22 | + |
| 23 | +type InstallCommand struct { |
| 24 | + *cobra.Command |
| 25 | + |
| 26 | + CommandContext cli.Cli |
| 27 | + ModuleVersion string |
| 28 | + File string |
| 29 | +} |
| 30 | + |
| 31 | +type ImageResponse struct { |
| 32 | + Stream string `json:"stream"` |
| 33 | +} |
| 34 | + |
| 35 | +// installCmd represents the install command |
| 36 | +func NewInstallCommand(cliContext cli.Cli) *cobra.Command { |
| 37 | + command := &InstallCommand{ |
| 38 | + CommandContext: cliContext, |
| 39 | + } |
| 40 | + cmd := &cobra.Command{ |
| 41 | + Use: "install <MODULE_NAME>", |
| 42 | + Short: "Install a container image", |
| 43 | + Example: ` |
| 44 | +Example 1: Install a container and pull in the image from any available registries |
| 45 | +
|
| 46 | + $ tedge-container container-image install docker.io/nginx --module-version latest |
| 47 | +
|
| 48 | + `, |
| 49 | + Args: cobra.ExactArgs(1), |
| 50 | + PreRunE: IsEnabled(cliContext), |
| 51 | + RunE: command.RunE, |
| 52 | + } |
| 53 | + |
| 54 | + cmd.Flags().StringVar(&command.ModuleVersion, "module-version", "latest", "Software version to install") |
| 55 | + cmd.Flags().StringVar(&command.File, "file", "", "File") |
| 56 | + viper.SetDefault("container.alwaysPull", false) |
| 57 | + command.Command = cmd |
| 58 | + return cmd |
| 59 | +} |
| 60 | + |
| 61 | +func (c *InstallCommand) RunE(cmd *cobra.Command, args []string) error { |
| 62 | + slog.Info("Executing", "cmd", cmd.CalledAs(), "args", args) |
| 63 | + imageName := args[0] |
| 64 | + imageRef := fmt.Sprintf("%s:%s", imageName, c.ModuleVersion) |
| 65 | + |
| 66 | + // Only enable pulling if the user is providing a file |
| 67 | + disablePull := c.File != "" |
| 68 | + |
| 69 | + cli, err := container.NewContainerClient(context.TODO()) |
| 70 | + if err != nil { |
| 71 | + return err |
| 72 | + } |
| 73 | + |
| 74 | + ctx := context.Background() |
| 75 | + |
| 76 | + if c.File != "" { |
| 77 | + slog.Info("Loading image from file.", "file", c.File) |
| 78 | + file, err := os.Open(c.File) |
| 79 | + if err != nil { |
| 80 | + return err |
| 81 | + } |
| 82 | + defer file.Close() |
| 83 | + |
| 84 | + imageResp, err := cli.Client.ImageLoad(ctx, file, client.ImageLoadWithQuiet(true)) |
| 85 | + if err != nil { |
| 86 | + return err |
| 87 | + } |
| 88 | + defer imageResp.Body.Close() |
| 89 | + if imageResp.JSON { |
| 90 | + b, err := io.ReadAll(imageResp.Body) |
| 91 | + if err != nil { |
| 92 | + return nil |
| 93 | + } |
| 94 | + imageDetails := &ImageResponse{} |
| 95 | + if err := json.Unmarshal(b, &imageDetails); err != nil { |
| 96 | + return err |
| 97 | + } |
| 98 | + |
| 99 | + slog.Info("Loaded image.", "stream", imageDetails.Stream) |
| 100 | + images := make([]string, 0) |
| 101 | + moduleVersionFound := false |
| 102 | + for _, line := range strings.Split(imageDetails.Stream, "\n") { |
| 103 | + if strings.HasPrefix(line, "Loaded image: ") { |
| 104 | + imageName := strings.TrimPrefix(line, "Loaded image: ") |
| 105 | + slog.Info("Found image reference in file.", "file", c.File, "image", imageName) |
| 106 | + images = append(images, imageName) |
| 107 | + if imageName == c.ModuleVersion { |
| 108 | + moduleVersionFound = true |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + // Check if the user has given correct image to use from the |
| 114 | + if !moduleVersionFound { |
| 115 | + switch count := len(images); count { |
| 116 | + case 0: |
| 117 | + slog.Warn("No images detected in stream output. Aborting to prevent accidentally load image from network.", "file", c.File, "images", images) |
| 118 | + // Fail hard to prevent potentially trying to pull in the image (as the user has opted into file based images) |
| 119 | + return fmt.Errorf("no image detected in file. name=%s, version=%s, file=%s", imageName, c.ModuleVersion, c.File) |
| 120 | + default: |
| 121 | + if count > 1 { |
| 122 | + slog.Warn("More than 1 image detected in file. Only using the first image.", "file", c.File, "images", images, "image_count", count) |
| 123 | + } |
| 124 | + |
| 125 | + imageRef = images[0] |
| 126 | + slog.Info("Detected image reference does not match the module-version. Using first imageRef from loaded image.", "imageRef", imageRef, "version", c.ModuleVersion) |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + // |
| 133 | + // Check and pull image if it is not present |
| 134 | + if !disablePull { |
| 135 | + if _, err := cli.ImagePullWithRetries(ctx, imageRef, c.CommandContext.ImageAlwaysPull(), container.ImagePullOptions{ |
| 136 | + AuthFunc: c.CommandContext.GetContainerRepositoryCredentialsFunc(imageRef), |
| 137 | + MaxAttempts: 2, |
| 138 | + Wait: 5 * time.Second, |
| 139 | + }); err != nil { |
| 140 | + return err |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + slog.Info("Installed image.", "name", imageName, "version", c.ModuleVersion, "imageRef", imageRef) |
| 145 | + return nil |
| 146 | +} |
0 commit comments