Skip to content
This repository has been archived by the owner on Dec 12, 2023. It is now read-only.

Commit

Permalink
refactor: update doc comments and add update check
Browse files Browse the repository at this point in the history
  • Loading branch information
adriantpaez authored and AntiD2ta committed Sep 15, 2023
1 parent f068dcd commit bb594a5
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 6 deletions.
3 changes: 3 additions & 0 deletions internal/package_handler/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@ func (p *PackageHandler) LatestVersion() (string, error) {
return versions[len(versions)-1], nil
}

// CommitPrecedence returns true if the new commit hash is a descendant of the
// old commit hash. It returns an error if the commit hashes are not found.
func (p *PackageHandler) CommitPrecedence(oldCommitHash, newCommitHash string) (bool, error) {
err := p.CheckoutCommit(newCommitHash)
if err != nil {
Expand All @@ -205,6 +207,7 @@ func (p *PackageHandler) CommitPrecedence(oldCommitHash, newCommitHash string) (
if err != nil {
return false, err
}
// Skip first commit, because it is the new commit hash itself
_, err = commitIter.Next()
if err != nil {
if errors.Is(err, io.EOF) {
Expand Down
14 changes: 9 additions & 5 deletions pkg/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ type PullResult struct {
}

type PullUpdateResult struct {
Name string
Tag string
Url string
Profile string
// OldVersion is the version of the old package.
OldVersion string

Expand All @@ -174,7 +178,7 @@ type PullUpdateResult struct {
NewOptions []Option

// MergedOptions is the list of options of the new package merged with the
// old package. These options the ones that will be used for the new instance
// old package. These options are the ones that will be used for the new instance
// and should be filled by the user.
MergedOptions []Option

Expand All @@ -187,6 +191,10 @@ type InstallOptions struct {
// Name is the name of the AVS represented by the package.
Name string

// Tag is the tag to use for the instance, required to build the instance id
// with the format <package_name>-<tag>
Tag string

// URL is the URL of the git repository containing the node software package.
URL string

Expand All @@ -207,10 +215,6 @@ type InstallOptions struct {

// Options is the list of options to use for the instance.
Options []Option

// Tag is the tag to use for the instance, required to build the instance id
// with the format <package_name>-<tag>
Tag string
}

// LocalInstallOptions is a set of options for installing a node software package
Expand Down
25 changes: 24 additions & 1 deletion pkg/daemon/egn_daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,9 @@ func (d *EgnDaemon) Pull(url string, ref PullTarget, force bool) (result PullRes
}

func (d *EgnDaemon) PullUpdate(instanceID string, url string, ref PullTarget) (PullUpdateResult, error) {
if !d.dataDir.HasInstance(instanceID) {
return PullUpdateResult{}, fmt.Errorf("%w: %s", ErrInstanceNotFound, instanceID)
}
pkgHandler, err := d.pullPackage(url, true)
if err != nil {
return PullUpdateResult{}, err
Expand Down Expand Up @@ -419,7 +422,7 @@ func (d *EgnDaemon) PullUpdate(instanceID string, url string, ref PullTarget) (P
if v, ok := valuesOld[o.Target()]; ok {
err := o.Set(v)
if err != nil {
return PullUpdateResult{}, err
return PullUpdateResult{}, fmt.Errorf("error setting old option value: %v", err)
}
} else {
return PullUpdateResult{}, fmt.Errorf("%w: old option %s", ErrOptionWithoutValue, o.Name())
Expand All @@ -432,6 +435,11 @@ func (d *EgnDaemon) PullUpdate(instanceID string, url string, ref PullTarget) (P
}

return PullUpdateResult{
Name: instance.Name,
Tag: instance.Tag,
Url: instance.URL,
Profile: instance.Profile,
HasPlugin: instance.Plugin != nil,
OldVersion: instance.Version,
NewVersion: newVersion,
OldCommit: instance.Commit,
Expand All @@ -442,7 +450,22 @@ func (d *EgnDaemon) PullUpdate(instanceID string, url string, ref PullTarget) (P
}, nil
}

// mergeOptions merges the old options with the new ones following the next rules:
//
// 1. New option is not present in the old options: New option is added to the
// merged options without a value.
//
// 2. New option is present in the old options:
//
// 2.1: Old option value is valid for the new option: the new option is added
// to the merged options using the old option value.
//
// 2.2: Old option value is not valid for the new option: the new option is
// added to the merged options without a value and probably the user
// will need to fill it again or use its default value.
func mergeOptions(oldOptions, newOptions []Option) ([]Option, error) {
// mergedOptions will contain the result of merging the new options with the
// old ones
var mergedOptions []Option
for _, oNew := range newOptions {
var oOld Option
Expand Down
1 change: 1 addition & 0 deletions pkg/daemon/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var (
ErrInstanceAlreadyExists = errors.New("instance already exists")
ErrProfileDoesNotExist = errors.New("profile does not exist")
ErrInstanceNotRunning = errors.New("instance is not running")
ErrInstanceNotFound = errors.New("instance not found")
ErrOptionWithoutValue = errors.New("option without value")
ErrMonitoringTargetPortNotSet = errors.New("monitoring target port is not set")
ErrInstanceHasNoPlugin = errors.New("instance has no plugin")
Expand Down

0 comments on commit bb594a5

Please sign in to comment.