Skip to content

Commit 9b2cecf

Browse files
zreigztest-cli-e2e-awsmaciaszczykm
authored
add OCI image upload/downlaod option (#620)
* add OCI image upload/downlaod option * move to constant * use current directory * Update cmd/command/edge/edge.go Co-authored-by: Marcin Maciaszczyk <[email protected]> * Update cmd/command/edge/image.go Co-authored-by: Marcin Maciaszczyk <[email protected]> * change param name --------- Co-authored-by: test-cli-e2e-aws <[email protected]> Co-authored-by: Marcin Maciaszczyk <[email protected]>
1 parent e9931fe commit 9b2cecf

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

cmd/command/edge/edge.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ func Commands(clients client.Plural, helmConfiguration *action.Configuration) []
9797
Usage: "the board model",
9898
Value: "rpi5",
9999
},
100+
cli.StringFlag{
101+
Name: "oci-url",
102+
Usage: "push an image to the repository",
103+
},
100104
},
101105
},
102106
{
@@ -133,5 +137,21 @@ func Commands(clients client.Plural, helmConfiguration *action.Configuration) []
133137
},
134138
},
135139
},
140+
{
141+
Name: "download",
142+
Action: p.handleEdgeDownload,
143+
Usage: "pull and extract the edge image",
144+
Flags: []cli.Flag{
145+
cli.StringFlag{
146+
Name: "url",
147+
Usage: "the url of the image to download",
148+
Required: true,
149+
},
150+
cli.StringFlag{
151+
Name: "to",
152+
Usage: "the image destination path",
153+
},
154+
},
155+
},
136156
}
137157
}

cmd/command/edge/image.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ stages:
3737
- name: Delete default Kairos user
3838
commands:
3939
- deluser --remove-home kairos`
40+
dockerfile = "FROM scratch\nWORKDIR /build\nCOPY kairos.img /build"
4041
)
4142

4243
type Configuration struct {
@@ -57,6 +58,7 @@ func (p *Plural) handleEdgeImage(c *cli.Context) error {
5758
wifiSsid := c.String("wifi-ssid")
5859
wifiPassword := c.String("wifi-password")
5960
model := c.String("model")
61+
imagePushURL := c.String("oci-url")
6062

6163
if err := p.InitConsoleClient(consoleToken, consoleURL); err != nil {
6264
return err
@@ -140,11 +142,27 @@ func (p *Plural) handleEdgeImage(c *cli.Context) error {
140142
return err
141143
}
142144

145+
if imagePushURL != "" {
146+
dockerfilePath := filepath.Join(buildDirPath, "Dockerfile")
147+
if err := os.WriteFile(dockerfilePath, []byte(dockerfile), 0644); err != nil {
148+
return fmt.Errorf("cannot create Dockerfile: %w", err)
149+
}
150+
if err = utils.Exec("docker", "build", "-t", imagePushURL, "-f", dockerfilePath, buildDirPath); err != nil {
151+
return err
152+
}
153+
if err = utils.Exec("docker", "push", imagePushURL); err != nil {
154+
return err
155+
}
156+
157+
utils.Success("image pushed successfully to %s\n", imagePushURL)
158+
}
159+
143160
if err = utils.CopyDir(buildDirPath, outputDirPath); err != nil {
144161
return fmt.Errorf("cannot move output files: %w", err)
145162
}
146163

147164
utils.Success("image saved to %s directory\n", outputDir)
165+
148166
return nil
149167
}
150168

@@ -246,3 +264,25 @@ func (p *Plural) writeCloudConfig(project, user, username, password, wifiSsid, w
246264
_, err = file.WriteString(template)
247265
return err
248266
}
267+
268+
func (p *Plural) handleEdgeDownload(c *cli.Context) error {
269+
var err error
270+
271+
outputDir := c.String("to")
272+
url := c.String("url")
273+
274+
if outputDir == "" {
275+
outputDir, err = os.Getwd()
276+
if err != nil {
277+
return err
278+
}
279+
}
280+
281+
utils.Highlight("unpacking image contents\n")
282+
if err := utils.Exec("docker", "run", "-i", "--rm", "--privileged", "-v", fmt.Sprintf("%s:/image", outputDir),
283+
"quay.io/luet/base", "util", "unpack", url, "/image"); err != nil {
284+
return err
285+
}
286+
287+
return nil
288+
}

0 commit comments

Comments
 (0)