Skip to content

Commit f677817

Browse files
committed
fix(build/rofl): Fix artifact upgrade to not overwrite everything
1 parent 9d0338b commit f677817

File tree

4 files changed

+88
-32
lines changed

4 files changed

+88
-32
lines changed

build/rofl/artifacts.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,5 @@ var LatestContainerArtifacts = ArtifactsConfig{
1414
Stage2: "https://github.com/oasisprotocol/oasis-boot/releases/download/v0.6.2/stage2-podman.tar.bz2#b2ea2a0ca769b6b2d64e3f0c577ee9c08f0bb81a6e33ed5b15b2a7e50ef9a09f",
1515
Container: ContainerArtifactsConfig{
1616
Runtime: "https://github.com/oasisprotocol/oasis-sdk/releases/download/rofl-containers%2Fv0.7.1/rofl-containers#0a7f821f36793b9409c623d795ea0edd996ac5cd96f05f3e8a81da409deb1880",
17-
Compose: "compose.yaml",
1817
},
1918
}

build/rofl/manifest.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,10 +500,55 @@ type ArtifactsConfig struct {
500500
Container ContainerArtifactsConfig `yaml:"container,omitempty" json:"container,omitempty"`
501501
}
502502

503+
type artifactUpgrade struct {
504+
existing *string
505+
new string
506+
}
507+
508+
func upgradeArtifacts(upgrade []artifactUpgrade) bool {
509+
var changed bool
510+
for _, artifact := range upgrade {
511+
if artifact.new == "" {
512+
continue
513+
}
514+
if *artifact.existing == artifact.new {
515+
continue
516+
}
517+
*artifact.existing = artifact.new
518+
changed = true
519+
}
520+
return changed
521+
}
522+
523+
// UpgradeTo upgrades the artifacts to the latest version by updating any relevant fields.
524+
//
525+
// Returns true iff any artifacts have been updated.
526+
func (ac *ArtifactsConfig) UpgradeTo(latest *ArtifactsConfig) bool {
527+
var changed bool
528+
changed = upgradeArtifacts([]artifactUpgrade{
529+
{&ac.Builder, latest.Builder},
530+
{&ac.Firmware, latest.Firmware},
531+
{&ac.Kernel, latest.Kernel},
532+
{&ac.Stage2, latest.Stage2},
533+
})
534+
changed = ac.Container.UpgradeTo(&latest.Container) || changed
535+
return changed
536+
}
537+
503538
// ContainerArtifactsConfig is the container artifacts configuration.
504539
type ContainerArtifactsConfig struct {
505540
// Runtime is the URI/path to the container runtime artifact (empty to use default).
506541
Runtime string `yaml:"runtime,omitempty" json:"runtime,omitempty"`
507542
// Compose is the URI/path to the docker-compose.yaml artifact (empty to use default).
508543
Compose string `yaml:"compose,omitempty" json:"compose,omitempty"`
509544
}
545+
546+
// UpgradeTo upgrades the artifacts to the latest version by updating any relevant fields.
547+
//
548+
// Returns true iff any artifacts have been updated.
549+
func (cc *ContainerArtifactsConfig) UpgradeTo(latest *ContainerArtifactsConfig) bool {
550+
return upgradeArtifacts([]artifactUpgrade{
551+
{&cc.Compose, latest.Compose},
552+
{&cc.Runtime, latest.Runtime},
553+
})
554+
}

build/rofl/manifest_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,3 +211,37 @@ func TestLoadManifest(t *testing.T) {
211211
require.Contains(m.Deployments, "default")
212212
require.Equal("rofl1qpa9ydy3qmka3yrqzx0pxuvyfexf9mlh75hker5j", m.Deployments["default"].AppID)
213213
}
214+
215+
func TestUpgradeArtifacts(t *testing.T) {
216+
require := require.New(t)
217+
218+
existing := ArtifactsConfig{
219+
Builder: "a",
220+
Firmware: "b",
221+
Kernel: "c",
222+
Stage2: "d",
223+
Container: ContainerArtifactsConfig{
224+
Runtime: "e",
225+
Compose: "f",
226+
},
227+
}
228+
latest := ArtifactsConfig{
229+
Firmware: "b2",
230+
Kernel: "c2",
231+
Stage2: "d2",
232+
Container: ContainerArtifactsConfig{
233+
Runtime: "e2",
234+
},
235+
}
236+
changed := existing.UpgradeTo(&latest)
237+
require.True(changed)
238+
require.Equal("a", existing.Builder)
239+
require.Equal("b2", existing.Firmware)
240+
require.Equal("c2", existing.Kernel)
241+
require.Equal("d2", existing.Stage2)
242+
require.Equal("e2", existing.Container.Runtime)
243+
require.Equal("f", existing.Container.Compose)
244+
245+
changed = existing.UpgradeTo(&latest)
246+
require.False(changed)
247+
}

cmd/rofl/mgmt.go

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package rofl
22

33
import (
44
"bufio"
5-
"bytes"
65
"context"
76
_ "embed"
87
"encoding/json"
@@ -521,33 +520,30 @@ var (
521520
manifest, err := buildRofl.LoadManifest()
522521
cobra.CheckErr(err)
523522

524-
var changes bool
523+
var latestArtifacts buildRofl.ArtifactsConfig
525524
switch manifest.TEE {
526525
case buildRofl.TEETypeTDX:
527526
switch manifest.Kind {
528527
case buildRofl.AppKindRaw:
529-
artifacts := buildRofl.LatestBasicArtifacts // Copy.
530-
changes, err = replaceArtifacts(manifest, &artifacts)
531-
cobra.CheckErr(err)
528+
latestArtifacts = buildRofl.LatestBasicArtifacts // Copy.
532529
case buildRofl.AppKindContainer:
533-
artifacts := buildRofl.LatestContainerArtifacts // Copy.
534-
changes, err = replaceArtifacts(manifest, &artifacts)
535-
cobra.CheckErr(err)
530+
latestArtifacts = buildRofl.LatestContainerArtifacts // Copy.
536531
default:
537532
}
538533
default:
539534
}
540535

536+
if !manifest.Artifacts.UpgradeTo(&latestArtifacts) {
537+
fmt.Printf("Artifacts already up-to-date.\n")
538+
return
539+
}
540+
541541
// Update manifest.
542542
if err := manifest.Save(); err != nil {
543543
cobra.CheckErr(fmt.Errorf("failed to update manifest: %w", err))
544544
}
545545

546-
if changes {
547-
fmt.Printf("Run `oasis rofl build` to build your ROFL app.\n")
548-
} else {
549-
fmt.Printf("Artifacts already up-to-date.\n")
550-
}
546+
fmt.Printf("Run `oasis rofl build` to build your ROFL app.\n")
551547
},
552548
}
553549

@@ -806,24 +802,6 @@ var (
806802
}
807803
)
808804

809-
// replaceArtifacts replaces existing manifest artifacts with new ones and returns true, if there were changes.
810-
func replaceArtifacts(manifest *buildRofl.Manifest, newArtifacts *buildRofl.ArtifactsConfig) (bool, error) {
811-
oldRaw, err := json.Marshal(manifest.Artifacts)
812-
if err != nil {
813-
return false, err
814-
}
815-
816-
newRaw, err := json.Marshal(newArtifacts)
817-
if err != nil {
818-
return false, err
819-
}
820-
821-
changes := !bytes.Equal(oldRaw, newRaw)
822-
manifest.Artifacts = newArtifacts
823-
824-
return changes, nil
825-
}
826-
827805
// detectOrCreateComposeFile detects the existing compose.yaml-like file and returns its filename. If it doesn't exist, it creates compose.yaml and populates it.
828806
func detectOrCreateComposeFile() string {
829807
for _, filename := range []string{"rofl-compose.yaml", "rofl-compose.yml", "docker-compose.yaml", "docker-compose.yml", "compose.yml", "compose.yaml"} {

0 commit comments

Comments
 (0)