Skip to content

Commit

Permalink
Merge pull request #37 from kyleishie/ostree-updates
Browse files Browse the repository at this point in the history
Ostree updates
  • Loading branch information
ikaneshiro authored Jan 29, 2024
2 parents 170e0d5 + 8393eb4 commit db73ed5
Show file tree
Hide file tree
Showing 35 changed files with 254 additions and 39 deletions.
8 changes: 8 additions & 0 deletions internal/plugins/ostree/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ func (p *Plugin) DeleteRepository(ctx context.Context, repository string) (err e
return p.repositoryManager.Get(ctx, repository).DeleteRepository(ctx)
}

func (p *Plugin) ListRepositoryRefs(ctx context.Context, repository string) (refs []apiv1.OSTreeRef, err error) {
if err := checkRepository(repository); err != nil {
return nil, err
}

return p.repositoryManager.Get(ctx, repository).ListRepositoryRefs(ctx)
}

func (p *Plugin) AddRemote(ctx context.Context, repository string, properties *apiv1.OSTreeRemoteProperties) (err error) {
if err := checkRepository(repository); err != nil {
return err
Expand Down
6 changes: 3 additions & 3 deletions internal/plugins/ostree/pkg/libostree/generate-testdata.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ ostree --repo=testdata/repo init --mode=archive
echo "Test file in a simple ostree repo - branch test1" > ./testdata/tree/testfile.txt
ostree --repo=testdata/repo commit --branch=test1 ./testdata/tree/

echo "Test file in a simple ostree repo - branch test2" > ./testdata/tree/testfile.txt
ostree --repo=testdata/repo commit --branch=test2 ./testdata/tree/
echo "Test file in a simple ostree repo - branch long/branch/name/test2" > ./testdata/tree/testfile.txt
ostree --repo=testdata/repo commit --branch=long/branch/name/test2 ./testdata/tree/

echo "Another test file" > ./testdata/tree/another_testfile.txt
ostree --repo=testdata/repo commit --branch=test2 ./testdata/tree/
ostree --repo=testdata/repo commit --branch=long/branch/name/test2 ./testdata/tree/

ostree --repo=testdata/repo summary --update

Expand Down
36 changes: 18 additions & 18 deletions internal/plugins/ostree/pkg/libostree/pull_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"

Expand Down Expand Up @@ -154,36 +155,35 @@ func TestRepo_Pull(t *testing.T) {
})

t.Run("should list refs from original repo", func(t *testing.T) {
expectedChecksums := map[string]bool{}
test1Data, err := os.ReadFile("testdata/repo/refs/heads/test1")
test2Data, err := os.ReadFile("testdata/repo/refs/heads/test2")
repoHeadsPrefix := "testdata/repo/refs/heads"
branch1Name := "test1"
branch2Name := "long/branch/name/test2"
branch1Data, err := os.ReadFile(filepath.Join(repoHeadsPrefix, branch1Name))
branch2Data, err := os.ReadFile(filepath.Join(repoHeadsPrefix, branch2Name))
if err != nil {
t.Errorf("failed to read refs file: %s", err.Error())
}

// Update in case of changes to testdata
expectedChecksums[strings.TrimRight(string(test1Data), "\n")] = false
expectedChecksums[strings.TrimRight(string(test2Data), "\n")] = false
expectedBranches := map[string]string{
branch1Name: strings.TrimRight(string(branch1Data), "\n"),
branch2Name: strings.TrimRight(string(branch2Data), "\n"),
}

refs, err := repo.ListRefsExt(ListRefsExtFlagsNone)
assert.NoError(t, err)
if err != nil {
assert.Failf(t, "failed to list refs", "err: %s", err.Error())
}
assert.NotEmpty(t, refs)
assert.Len(t, refs, 2)

// This could be a static list of assert statements but the loop captures unexpected extra refs.
for _, ref := range refs {
checksum := ref.Checksum
assert.NotEmpty(t, checksum)
for sum := range expectedChecksums {
if sum == checksum {
expectedChecksums[sum] = true
}
}
}
assert.NotEmpty(t, ref.Name)
assert.NotEmpty(t, ref.Checksum)

for sum, exists := range expectedChecksums {
assert.True(t, exists, "checksum %s not found", sum)
// Ensure the ref is one of the expected branches
expectedChecksum, exists := expectedBranches[ref.Name]
assert.True(t, exists, "unexpected ref: %s", ref.Name)
assert.Equal(t, expectedChecksum, ref.Checksum)
}
})

Expand Down
7 changes: 3 additions & 4 deletions internal/plugins/ostree/pkg/libostree/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,17 +286,16 @@ func (r *Repo) ListRefsExt(flags ListRefsExtFlags, prefix ...string) ([]Ref, err
var iter C.GHashTableIter
C.g_hash_table_iter_init(&iter, outAllRefs)

var cRef, cChecksum C.gpointer
var cRef C.gpointer
var cChecksum C.gpointer
var ret []Ref
for C.g_hash_table_iter_next(&iter, &cRef, &cChecksum) == C.gboolean(1) {
if cRef == nil {
break
}

ref := (*C.OstreeCollectionRef)(unsafe.Pointer(&cRef))

ret = append(ret, Ref{
Name: C.GoString(ref.ref_name),
Name: C.GoString((*C.char)(cRef)),
Checksum: C.GoString((*C.char)(cChecksum)),
})
}
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
e7f3faeb246374c3cc23053bee0bd027c40ca43c3846a78acd0a3e0fe56a07b9
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0e1518ee5f0421ad34685958b662c4947ea18a96be03b406bc8eb9ccf913ff22
6b6824da30f8f46e3087d60a9d5393f14637ccaa668e810be8ad62f7e5b28ab4

This file was deleted.

Binary file modified internal/plugins/ostree/pkg/libostree/testdata/repo/summary
Binary file not shown.
62 changes: 58 additions & 4 deletions internal/plugins/ostree/pkg/ostreerepository/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,41 @@ func (h *Handler) DeleteRepository(ctx context.Context) (err error) {
return nil
}

func (h *Handler) ListRepositoryRefs(ctx context.Context) (refs []apiv1.OSTreeRef, err error) {
// Transition to provisioning state
if err := h.setState(StateProvisioning); err != nil {
return nil, err
}
defer h.clearState()

if !h.checkRepoExists(ctx) {
return nil, ctl.Errf("repository does not exist")
}

err = h.BeginLocalRepoTransaction(ctx, func(ctx context.Context, repo *libostree.Repo) (bool, error) {
// Get the refs from the local repo
loRefs, err := repo.ListRefsExt(libostree.ListRefsExtFlagsNone)
if err != nil {
return false, ctl.Errf("listing refs from ostree repository: %s", err)
}

// Convert the refs to the API type
for _, loRef := range loRefs {
if loRef.Name == "" || loRef.Checksum == "" {
return false, ctl.Errf("invalid ref data encountered")
}
refs = append(refs, apiv1.OSTreeRef{
Name: loRef.Name,
Commit: loRef.Checksum,
})
}

return false, nil
})

return refs, err
}

func (h *Handler) AddRemote(ctx context.Context, remote *apiv1.OSTreeRemoteProperties) (err error) {
// Transition to provisioning state
if err := h.setState(StateProvisioning); err != nil {
Expand Down Expand Up @@ -285,6 +320,15 @@ func (h *Handler) SyncRepository(_ context.Context, properties *apiv1.OSTreeRepo
return false, ctl.Errf("regenerating summary for ostree repository %s: %s", h.repoDir, err)
}

// List the refs in the repository and store in the repoSync
loRefs, err := repo.ListRefsExt(libostree.ListRefsExtFlagsNone)
if err != nil {
return false, ctl.Errf("listing refs from ostree repository: %s", err)
}
repoSync := *h.repoSync.Load()
repoSync.SyncedRefs = loRefs
h.setRepoSync(&repoSync)

return true, nil
})
}()
Expand All @@ -297,10 +341,20 @@ func (h *Handler) GetRepositorySyncStatus(_ context.Context) (syncStatus *apiv1.
if repoSync == nil {
return nil, ctl.Errf("repository sync status not available")
}

var refs []apiv1.OSTreeRef
for _, loRef := range repoSync.SyncedRefs {
refs = append(refs, apiv1.OSTreeRef{
Name: loRef.Name,
Commit: loRef.Checksum,
})
}

return &apiv1.SyncStatus{
Syncing: repoSync.Syncing,
StartTime: utils.TimeToString(repoSync.StartTime),
EndTime: utils.TimeToString(repoSync.EndTime),
SyncError: repoSync.SyncError,
Syncing: repoSync.Syncing,
StartTime: utils.TimeToString(repoSync.StartTime),
EndTime: utils.TimeToString(repoSync.EndTime),
SyncError: repoSync.SyncError,
SyncedRefs: refs,
}, nil
}
11 changes: 7 additions & 4 deletions internal/plugins/ostree/pkg/ostreerepository/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ package ostreerepository

import (
"time"

"go.ciq.dev/beskar/internal/plugins/ostree/pkg/libostree"
)

type RepoSync struct {
Syncing bool
StartTime int64
EndTime int64
SyncError string
Syncing bool
StartTime int64
EndTime int64
SyncError string
SyncedRefs []libostree.Ref
}

func (h *Handler) setRepoSync(repoSync *RepoSync) {
Expand Down
22 changes: 18 additions & 4 deletions pkg/plugins/ostree/api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ type OSTreeRepositoryProperties struct {
Remotes []OSTreeRemoteProperties `json:"remotes"`
}

type OSTreeRef struct {
// Name - The name of the ref.
Name string `json:"name"`

// Commit - The commit hash of the ref.
Commit string `json:"commit"`
}

type OSTreeRemoteProperties struct {
// Name - The name of the remote repository.
Name string `json:"name"`
Expand Down Expand Up @@ -58,10 +66,11 @@ type OSTreeRepositorySyncRequest struct {

// Mirror sync status.
type SyncStatus struct {
Syncing bool `json:"syncing"`
StartTime string `json:"start_time"`
EndTime string `json:"end_time"`
SyncError string `json:"sync_error"`
Syncing bool `json:"syncing"`
StartTime string `json:"start_time"`
EndTime string `json:"end_time"`
SyncError string `json:"sync_error"`
SyncedRefs []OSTreeRef `json:"synced_refs"`

// TODO: Implement these
// The data for these is present when performing a pull via the ostree cli, so it is in the libostree code base.
Expand All @@ -88,6 +97,11 @@ type OSTree interface {
//kun:success statusCode=202
DeleteRepository(ctx context.Context, repository string) (err error)

// List OSTree repository refs (A.K.A. Branches).
//kun:op GET /repository/refs
//kun:success statusCode=200
ListRepositoryRefs(ctx context.Context, repository string) (refs []OSTreeRef, err error)

// Add a new remote to the OSTree repository.
//kun:op POST /repository/remote
//kun:success statusCode=200
Expand Down
37 changes: 37 additions & 0 deletions pkg/plugins/ostree/api/v1/endpoint.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions pkg/plugins/ostree/api/v1/http.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit db73ed5

Please sign in to comment.