From c0e6bd6f0516ee0583d57085fa0f03926aa52d3f Mon Sep 17 00:00:00 2001 From: Alf-Rune Siqveland Date: Thu, 24 Oct 2024 15:23:36 +0200 Subject: [PATCH 1/2] fix(write): mender-artifact writes to output file instead of replacing it Changelog: Title Ticket: MEN-7660 Signed-off-by: Alf-Rune Siqveland --- cli/write.go | 51 +++++++++------------------------------------------ 1 file changed, 9 insertions(+), 42 deletions(-) diff --git a/cli/write.go b/cli/write.go index bc8cfbb3..0c345b26 100644 --- a/cli/write.go +++ b/cli/write.go @@ -178,19 +178,14 @@ func writeBootstrapArtifact(c *cli.Context) error { Log.Debugf("creating bootstrap artifact [%s], version: %d", name, version) - f, err := os.Create(name + ".tmp") + f, err := os.Create(name) if err != nil { return cli.NewExitError( "can not create bootstrap artifact file: "+err.Error(), errArtifactCreate, ) } - defer func() { - f.Close() - // in case of success `.tmp` suffix will be removed and below - // will not remove valid artifact - os.Remove(name + ".tmp") - }() + defer f.Close() aw, err := artifactWriter(c, comp, f, version) if err != nil { @@ -241,12 +236,6 @@ func writeBootstrapArtifact(c *cli.Context) error { if err != nil { return cli.NewExitError(err.Error(), 1) } - - f.Close() - err = os.Rename(name+".tmp", name) - if err != nil { - return cli.NewExitError(err.Error(), 1) - } return nil } @@ -298,19 +287,14 @@ func writeRootfs(c *cli.Context) error { Updates: []handlers.Composer{h}, } - f, err := os.Create(name + ".tmp") + f, err := os.Create(name) if err != nil { return cli.NewExitError( "can not create artifact file: "+err.Error(), errArtifactCreate, ) } - defer func() { - f.Close() - // in case of success `.tmp` suffix will be removed and below - // will not remove valid artifact - os.Remove(name + ".tmp") - }() + defer f.Close() aw, err := artifactWriter(c, comp, f, version) if err != nil { @@ -370,12 +354,6 @@ func writeRootfs(c *cli.Context) error { if err != nil { return cli.NewExitError(err.Error(), 1) } - - f.Close() - err = os.Rename(name+".tmp", name) - if err != nil { - return cli.NewExitError(err.Error(), 1) - } return nil } @@ -400,7 +378,7 @@ func reportProgress(c context.Context, state chan string) { } } -func artifactWriter(c *cli.Context, comp artifact.Compressor, f *os.File, +func artifactWriter(c *cli.Context, comp artifact.Compressor, w io.Writer, ver int) (*awriter.Writer, error) { privateKey, err := getKey(c) if err != nil { @@ -411,9 +389,9 @@ func artifactWriter(c *cli.Context, comp artifact.Compressor, f *os.File, // check if we are having correct version return nil, errors.New("can not use signed artifact with version 0") } - return awriter.NewWriterSigned(f, comp, privateKey), nil + return awriter.NewWriterSigned(w, comp, privateKey), nil } - return awriter.NewWriter(f, comp), nil + return awriter.NewWriter(w, comp), nil } func makeUpdates(ctx *cli.Context) (*awriter.Updates, error) { @@ -765,19 +743,14 @@ func writeModuleImage(ctx *cli.Context) error { return err } - f, err := os.Create(name + ".tmp") + f, err := os.Create(name) if err != nil { return cli.NewExitError( "can not create artifact file: "+err.Error(), errArtifactCreate, ) } - defer func() { - f.Close() - // in case of success `.tmp` suffix will be removed and below - // will not remove valid artifact - os.Remove(name + ".tmp") - }() + defer f.Close() aw, err := artifactWriter(ctx, comp, f, version) if err != nil { @@ -828,12 +801,6 @@ func writeModuleImage(ctx *cli.Context) error { if err != nil { return cli.NewExitError(err.Error(), 1) } - - f.Close() - err = os.Rename(name+".tmp", name) - if err != nil { - return cli.NewExitError(err.Error(), 1) - } return nil } From 2df9f9357b7cba4aaf7c3eead5623d720b880fa2 Mon Sep 17 00:00:00 2001 From: Alf-Rune Siqveland Date: Thu, 24 Oct 2024 15:28:59 +0200 Subject: [PATCH 2/2] feat(write): Allow writing artifacts to standard output Changelog: Setting `--output-file` to `-` will write the output to standard out. Ticket: MEN-7661 Signed-off-by: Alf-Rune Siqveland --- cli/artifact.mender | Bin 0 -> 5120 bytes cli/cli.go | 6 ++-- cli/write.go | 66 ++++++++++++++++++++++++++++---------------- 3 files changed, 45 insertions(+), 27 deletions(-) create mode 100644 cli/artifact.mender diff --git a/cli/artifact.mender b/cli/artifact.mender new file mode 100644 index 0000000000000000000000000000000000000000..9bbcf8849e660419fe0a59be147c8c6e396e50fb GIT binary patch literal 5120 zcmXR*Eh^5;&tsq^FaQEG0|N#S4Fu@4fq}6(ObjA#2vly$pkP2tN5RZ4EiOqc0$LB_ zlS_lWhU7VN&7rftYNfRNqTIw1B`c-e)V!3`A|)NAGDwW(gT*xdg)bMmS#XbmS(1wCg!GQmL>)!=9U&o z$w@$C3{BDu4UAKgOcMu?1=s(Ez_bR>^Jb>z48W)|G&3|XH8ui@8=4pznbA4_Lux={9tm z=C*rTVUCKbK-2%~!Z*Hq{Jiviy~Ou-w@sh^(Dv|baiOj=o9?{2wOd?3Q2EH^?(6z& zHdg1=?|rvqyT|@Tu3>KJp^A=rYJ2J9&K3Jx@p1^_k#8|1;*g(ez#L2JJXxY zq8kj>U2)pA$MEaEkQ7DBxD#tv?5eli^e)=G@5}jR_e>Ax$M5=Cy`SOMM2|q{=+f-; zs<>Y}um9LEBZy}ctH!PAlRn+6=eoL3U19gPNpWX$B#^1tKplK(Q2^MC)J@sEFM`|a8{Sr^S8Y0K7lxE{jS`R4Q=7;Mt1SC0n0q@pyPo*>`OZ$J9@j@oSaB4C2-_ZJn^% zqU-t-cb~(L-oCPxjJ_7_k#L^v@L^f0rBXA@H|lAXIhS&&nXB}i&41#(eEIXMp>;)5 z=QI1?scTvDSN-q$6P-(i{A{-U58r$~N$%X=`G(2Ida56Z1Jla?|I7@t1b8YK_-K%D z;H5XTprwHo;4A^H|FPzOLvzghZ)QB2|7j5y=w>42|4W8kha3c2AC^y<%W=;7nQ61+?-fQ)f2O{F+ZumH4NL9~m5c7TR2WQtnsju=rH&0D z$0C`3H6`bJi{{kD?cMk5^RE4!E$LJ0j+9CNVi9rTcsTzKBP2ba+