Skip to content

Commit

Permalink
targets: offline-update: Check if dest dir already has data
Browse files Browse the repository at this point in the history
This commit prevents the execution of "targets offlile-update" using a
destination directory that already contains update data (i.e.,
"ostree_repo" or "apps" subdirectories). There is no check for the tuf
subdirectory, since the metadata in that subdir is updated by default,
and there is no conflict.
It is possible to override the default behavior by using a new
--allow-multiple-targets option.

Signed-off-by: Andre Detsch <andre.detsch@foundries.io>
  • Loading branch information
detsch committed Oct 10, 2023
1 parent 92cd99b commit 4ef6f2b
Showing 1 changed file with 27 additions and 5 deletions.
32 changes: 27 additions & 5 deletions subcommands/targets/offline-update.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ type (
)

var (
ouTag string
ouProd bool
ouExpiresIn int
ouTufOnly bool
ouNoApps bool
ouTag string
ouProd bool
ouExpiresIn int
ouTufOnly bool
ouNoApps bool
ouAllowMultipleTargets bool
)

func init() {
Expand Down Expand Up @@ -62,6 +63,8 @@ func init() {
"Fetch only TUF metadata")
offlineUpdateCmd.Flags().BoolVarP(&ouNoApps, "no-apps", "", false,
"Skip fetching Target Apps")
offlineUpdateCmd.Flags().BoolVarP(&ouAllowMultipleTargets, "allow-multiple-targets", "", false,
"Allow multiple targets to be stored in the same <dst> directory")
}

func doOfflineUpdate(cmd *cobra.Command, args []string) {
Expand All @@ -84,6 +87,14 @@ func doOfflineUpdate(cmd *cobra.Command, args []string) {
fmt.Println("Successfully refreshed and downloaded TUF metadata")

if !ouTufOnly {
if !isDstDirClean(dstDir) {
if !ouAllowMultipleTargets {
subcommands.DieNotNil(errors.New(`Destination directory already has update data.
Provide a clean destination directory or re-run with --allow-multiple-targets to add a new target to a directory which already has update data.
Notice that multiple targets in the same directory is only supported in LmP >= v92.`))
}
}

fmt.Printf("Downloading an ostree repo from the Target's OE build %d...\n", ti.ostreeVersion)
subcommands.DieNotNil(downloadOstree(factory, ti.ostreeVersion, ti.hardwareID, dstDir), "Failed to download Target's ostree repo:")
if !ouNoApps {
Expand Down Expand Up @@ -152,6 +163,17 @@ func getTargetInfo(factory string, targetName string) (*ouTargetInfo, error) {
return &info, nil
}

func isDstDirClean(dstDir string) bool {
for _, subDir := range []string{"ostree_repo", "apps"} {
fullPath := path.Join(dstDir, subDir)
if _, err := os.Stat(fullPath); !os.IsNotExist(err) {
fmt.Println(fullPath + " already exists")
return false
}
}
return true
}

func downloadTufRepo(factory string, target string, tag string, prod bool, expiresIn int, dstDir string) error {
// v1 - auto-generated by tuf_keyserver (default, on Factory creation);
// v2 - auto-generated by ota-lite to take keys online (default, on Factory creation);
Expand Down

0 comments on commit 4ef6f2b

Please sign in to comment.