selfupdater

package module
v0.0.0-...-63475ab Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 3, 2021 License: MIT Imports: 15 Imported by: 0

README

selfupdater

Go Reference

Build self-updating golang programs with customizable over-the-air(OTA) updates protocol.

SelfUpdater performs following tasks in sequence:

  1. Calculating local checksum of the current executable file.
  2. Fetch the remote checksum.
  3. Compare the local checksum with the remote checksum, proceed if there is a difference.
  4. Download remote executable file to a temporary file.
  5. Calculating the checksum of the downloaded temporary file, proceed if it's the same to the remote checksum we obtained on step 2.
  6. Overwrite the current executable file with the downloaded temporary file.
  7. Cleanup the temporary file.
  8. Safely restart the current executable program by using rollover.

Customize Update Source

Implement UpdateProvider interface to establish customized updates protocol.

S3UpdateProvider is an out-of-the-box example of a customized UpdateProvider based on AWS S3 and md5 checksum. Call NewS3UpdateProvider() to specify the S3 bucket and region, paths of the executable and checksum file and create the UpdateProvider, feed it to NewSelfUpdate(provider UpdateProvider).

Unit Test

  1. Build the base version of the executable file.

    go build -o ./bin/selfupdater ./example/base

  2. Build the update version of the executable file.

    go build -o ./bin/selfupdater-update ./example/update

  3. Calculate the checksum and push the updates to S3 by using awscli.

    md5 -q ./bin/selfupdater-update | tr -d '\n' > ./bin/selfupdater-update.md5
    aws s3 cp ./bin/selfupdater-update  s3://selfupdater-tomasen/selfupdater
    aws s3 cp ./bin/selfupdater-update.md5  s3://selfupdater-tomasen/selfupdater.md5
    
  4. Run the base version.

    ./bin/selfupdater

  5. The base is expected to be updated to the update version, with stdout like

    ...
    this is the base version
    this is the base version
    2021/03/12 03:39:11 child started running, pid: 53466
    this is the update version
    this is the update version
    ...
    
  6. Run the update version

    ./bin/selfupdater-update

    should expect stdout something similar likes:

    this is the update version
    this is the update version
    update finished
    this is the update version
    this is the update version
    ...
    

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type S3UpdateProvider

type S3UpdateProvider struct {
	BucketName     string
	ExecutablePath string
	ChecksumPath   string
	S3Downloader   *s3manager.Downloader
}

func NewS3UpdateProvider

func NewS3UpdateProvider(bucket string, region string, executable string, checksum string) *S3UpdateProvider

func (*S3UpdateProvider) DownloadTo

func (p *S3UpdateProvider) DownloadTo(file *os.File) error

func (*S3UpdateProvider) Hash

func (p *S3UpdateProvider) Hash() hash.Hash

func (*S3UpdateProvider) RemoteChecksum

func (p *S3UpdateProvider) RemoteChecksum() (string, error)

type SelfUpdater

type SelfUpdater struct {
	LocalExecutablePath     string
	LocalExecutableChecksum string
	Provider                UpdateProvider
}

func NewSelfUpdater

func NewSelfUpdater(provider UpdateProvider) *SelfUpdater

func (*SelfUpdater) CalcFileChecksum

func (s *SelfUpdater) CalcFileChecksum(file string) string

func (*SelfUpdater) Restart

func (s *SelfUpdater) Restart()

func (*SelfUpdater) Update

func (s *SelfUpdater) Update() error

func (*SelfUpdater) UpdateLocalExecutableChecksum

func (s *SelfUpdater) UpdateLocalExecutableChecksum()

type UpdateProvider

type UpdateProvider interface {
	// Download latest executable file from remote source to the open file descriptor provide by SelfUpdater.
	DownloadTo(file *os.File) error

	// Fetch the checksum of the executable file from remote source.
	RemoteChecksum() (string, error)

	// Return the hash.Hash interface that should be used to calculate the checksum of executable files.
	Hash() hash.Hash
}

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL