Skip to content

Commit 6e04e87

Browse files
authored
Merge pull request #665 from bahaa-ghazal/MEN-7225
fix: Include all provides with prefix rootfs when writing artifact via SSH
2 parents a927618 + 9f4eb7d commit 6e04e87

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

cli/write.go

+75
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ func writeRootfs(c *cli.Context) error {
268268
name = c.String("output-path")
269269
}
270270
version := c.Int("version")
271+
var showprovides map[string]string
271272

272273
Log.Debugf("creating artifact [%s], version: %d", name, version)
273274
rootfsFilename := c.String("file")
@@ -277,6 +278,10 @@ func writeRootfs(c *cli.Context) error {
277278
if err != nil {
278279
return cli.NewExitError(err.Error(), errArtifactCreate)
279280
}
281+
showprovides, err = showProvides(c)
282+
if err != nil {
283+
return cli.NewExitError(err.Error(), errArtifactCreate)
284+
}
280285
}
281286

282287
var h handlers.Composer
@@ -336,6 +341,12 @@ func writeRootfs(c *cli.Context) error {
336341
if err != nil {
337342
return err
338343
}
344+
for k, v := range showprovides {
345+
_, exist := typeInfoV3.ArtifactProvides[k]
346+
if !exist {
347+
typeInfoV3.ArtifactProvides[k] = v
348+
}
349+
}
339350

340351
if !c.Bool("no-checksum-provide") {
341352
legacy := c.Bool("legacy-rootfs-image-checksum")
@@ -960,6 +971,70 @@ func getDeviceSnapshot(c *cli.Context) (string, error) {
960971

961972
return filePath, err
962973
}
974+
func showProvides(c *cli.Context) (map[string]string, error) {
975+
var userAtHost string
976+
providesMap := make(map[string]string)
977+
port := "22"
978+
host := strings.TrimPrefix(c.String("file"), "ssh://")
979+
980+
if remotePort := strings.Split(host, ":"); len(remotePort) == 2 {
981+
port = remotePort[1]
982+
userAtHost = remotePort[0]
983+
} else {
984+
userAtHost = host
985+
}
986+
987+
args := c.StringSlice("ssh-args")
988+
// Check if port is specified explicitly with the --ssh-args flag
989+
addPort := true
990+
for _, arg := range args {
991+
if strings.Contains(arg, "-p") {
992+
addPort = false
993+
break
994+
}
995+
}
996+
if addPort {
997+
args = append(args, "-p", port)
998+
}
999+
args = append(args, userAtHost)
1000+
1001+
providesArgs := ` 'if which mender-update 1> /dev/null` +
1002+
`; then mender-update show-provides` +
1003+
`; elif which mender 1> /dev/null` +
1004+
`; then mender show-provides` +
1005+
`; else echo "Mender not found: Please check that Mender is installed" >&2 &&` +
1006+
` exit 1; fi'`
1007+
1008+
args = append(
1009+
args,
1010+
"/bin/sh",
1011+
"-c",
1012+
providesArgs)
1013+
1014+
cmd := exec.Command("ssh", args...)
1015+
1016+
stdout, err := cmd.CombinedOutput()
1017+
1018+
if err != nil {
1019+
return nil, err
1020+
}
1021+
if len(stdout) == 0 {
1022+
return nil, nil
1023+
}
1024+
provides := strings.Split(string(stdout), "\n")
1025+
1026+
for _, p := range provides {
1027+
if p == "" || !strings.HasPrefix(p, "rootfs-image.") {
1028+
continue
1029+
}
1030+
info := strings.Split(p, "=")
1031+
if len(info) != 2 {
1032+
continue
1033+
}
1034+
providesMap[info[0]] = info[1]
1035+
}
1036+
return providesMap, nil
1037+
}
9631038

9641039
// Reads from src waiting for the string specified by signal, writing all other
9651040
// output appearing at src to sink. The function returns an error if occurs

0 commit comments

Comments
 (0)