-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #190 from orbs-network/feature/autoupdate
Autoupdate
- Loading branch information
Showing
16 changed files
with
324 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
v1.7.0 | ||
v1.8.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package services | ||
|
||
import ( | ||
"encoding/hex" | ||
"fmt" | ||
"github.com/inconshreveable/go-update" | ||
"github.com/orbs-network/boyarin/boyar/config" | ||
"github.com/orbs-network/boyarin/crypto" | ||
"github.com/orbs-network/boyarin/strelets/adapter" | ||
"github.com/orbs-network/scribe/log" | ||
"net/http" | ||
) | ||
|
||
func (coreBoyar *BoyarService) SelfUpdate(targetPath string, image adapter.ExecutableImageOptions) error { | ||
checksum, err := hex.DecodeString(image.Sha256) | ||
if err != nil { | ||
return fmt.Errorf("could not decode boyar binary SHA256 checksum \"%s\": %s", image.Sha256, err) | ||
} | ||
|
||
coreBoyar.logger.Info("downloading new boyar binary", log.String("url", image.Url)) | ||
resp, err := http.Get(image.Url) | ||
if err != nil { | ||
return err | ||
} | ||
defer resp.Body.Close() | ||
|
||
return update.Apply(resp.Body, update.Options{ | ||
TargetPath: targetPath, | ||
Checksum: checksum, | ||
}) | ||
} | ||
|
||
func (coreBoyar *BoyarService) CheckForUpdates(flags *config.Flags, options adapter.ExecutableImageOptions) (shouldExit bool) { | ||
shouldExit = false | ||
if flags.AutoUpdate { | ||
currentHash, err := crypto.CalculateFileHash(flags.BoyarBinaryPath) | ||
if err != nil { | ||
coreBoyar.logger.Error("failed to calculate boyar binary hash", log.Error(err)) | ||
return | ||
} | ||
|
||
if currentHash == options.Sha256 { // already the correct version | ||
return | ||
} | ||
|
||
if err := coreBoyar.SelfUpdate(flags.BoyarBinaryPath, options); err != nil { | ||
coreBoyar.logger.Error("failed to update self", log.Error(err)) | ||
return | ||
} else { | ||
coreBoyar.logger.Info("successfully replaced boyar binary", log.String("path", flags.BoyarBinaryPath)) | ||
} | ||
|
||
return flags.ShutdownAfterUpdate | ||
} | ||
|
||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
package services | ||
|
||
import ( | ||
"github.com/orbs-network/boyarin/boyar/config" | ||
"github.com/orbs-network/boyarin/crypto" | ||
"github.com/orbs-network/boyarin/strelets/adapter" | ||
"github.com/orbs-network/scribe/log" | ||
"github.com/stretchr/testify/require" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
) | ||
|
||
var VALID_UPDATE_OPTIONS = adapter.ExecutableImageOptions{ | ||
Url: "https://github.com/orbs-network/boyarin/releases/download/v1.4.0/boyar-v1.4.0.bin", | ||
Sha256: "1998cc1f7721acfe1954ab2878cc0ad8062cd6d919cd61fa22401c6750e195fe", | ||
} | ||
|
||
func TestBoyarService_SelfUpdateHappyFlow(t *testing.T) { | ||
targetPath, _ := prepareSelfUpdateTest(t) | ||
|
||
logger := log.GetLogger() | ||
coreBoyar := NewCoreBoyarService(logger) | ||
|
||
flags := &config.Flags{ | ||
AutoUpdate: true, | ||
BoyarBinaryPath: targetPath, | ||
ShutdownAfterUpdate: true, | ||
} | ||
|
||
shouldExit := coreBoyar.CheckForUpdates(flags, VALID_UPDATE_OPTIONS) | ||
|
||
require.True(t, shouldExit) | ||
newChecksum, _ := crypto.CalculateFileHash(targetPath) | ||
require.EqualValues(t, VALID_UPDATE_OPTIONS.Sha256, newChecksum) | ||
} | ||
|
||
func prepareSelfUpdateTest(t *testing.T) (targetPath string, checksum string) { | ||
os.RemoveAll("./_tmp") | ||
os.MkdirAll("./_tmp", 0755) | ||
|
||
targetPath = filepath.Join("./_tmp", "boyar.bin") | ||
err := ioutil.WriteFile(targetPath, []byte("fake binary"), 0755) | ||
require.NoError(t, err) | ||
|
||
checksum, err = crypto.CalculateFileHash(targetPath) | ||
require.NoError(t, err) | ||
|
||
return targetPath, checksum | ||
} | ||
|
||
func TestBoyarService_SelfUpdateWithDisabledAutoUpdate(t *testing.T) { | ||
targetPath, checksum := prepareSelfUpdateTest(t) | ||
|
||
logger := log.GetLogger() | ||
coreBoyar := NewCoreBoyarService(logger) | ||
|
||
flags := &config.Flags{ | ||
AutoUpdate: false, | ||
BoyarBinaryPath: targetPath, | ||
ShutdownAfterUpdate: true, | ||
} | ||
|
||
shouldExit := coreBoyar.CheckForUpdates(flags, VALID_UPDATE_OPTIONS) | ||
|
||
// nothing changed | ||
require.False(t, shouldExit) | ||
newChecksum, _ := crypto.CalculateFileHash(targetPath) | ||
require.EqualValues(t, checksum, newChecksum) | ||
} | ||
|
||
func TestBoyarService_SelfUpdateWithWrongBinaryPath(t *testing.T) { | ||
targetPath, checksum := prepareSelfUpdateTest(t) | ||
|
||
logger := log.GetLogger() | ||
coreBoyar := NewCoreBoyarService(logger) | ||
|
||
flags := &config.Flags{ | ||
AutoUpdate: true, | ||
BoyarBinaryPath: targetPath + "0", | ||
ShutdownAfterUpdate: true, | ||
} | ||
|
||
shouldExit := coreBoyar.CheckForUpdates(flags, VALID_UPDATE_OPTIONS) | ||
|
||
// nothing changed | ||
require.False(t, shouldExit) | ||
newChecksum, _ := crypto.CalculateFileHash(targetPath) | ||
require.EqualValues(t, checksum, newChecksum) | ||
} | ||
|
||
func TestBoyarService_SelfUpdateWithMalformedUrl(t *testing.T) { | ||
targetPath, checksum := prepareSelfUpdateTest(t) | ||
|
||
logger := log.GetLogger() | ||
coreBoyar := NewCoreBoyarService(logger) | ||
|
||
flags := &config.Flags{ | ||
AutoUpdate: true, | ||
BoyarBinaryPath: targetPath, | ||
ShutdownAfterUpdate: true, | ||
} | ||
|
||
shouldExit := coreBoyar.CheckForUpdates(flags, adapter.ExecutableImageOptions{ | ||
Url: "http://localhost/does-not-exist", | ||
Sha256: VALID_UPDATE_OPTIONS.Sha256, | ||
}) | ||
|
||
// nothing changed | ||
require.False(t, shouldExit) | ||
newChecksum, _ := crypto.CalculateFileHash(targetPath) | ||
require.EqualValues(t, checksum, newChecksum) | ||
} | ||
|
||
func TestBoyarService_SelfUpdateWithWrongChecksum(t *testing.T) { | ||
targetPath, checksum := prepareSelfUpdateTest(t) | ||
|
||
logger := log.GetLogger() | ||
coreBoyar := NewCoreBoyarService(logger) | ||
|
||
flags := &config.Flags{ | ||
AutoUpdate: true, | ||
BoyarBinaryPath: targetPath, | ||
ShutdownAfterUpdate: true, | ||
} | ||
|
||
shouldExit := coreBoyar.CheckForUpdates(flags, adapter.ExecutableImageOptions{ | ||
Url: VALID_UPDATE_OPTIONS.Url, | ||
Sha256: "0000cc1f7721acfe1954ab2878cc0ad8062cd6d919cd61fa22401c6750e195fe", | ||
}) | ||
|
||
// nothing changed | ||
require.False(t, shouldExit) | ||
newChecksum, _ := crypto.CalculateFileHash(targetPath) | ||
require.EqualValues(t, checksum, newChecksum) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.