Skip to content

Commit

Permalink
Add progress bars for download operations in binaries updates
Browse files Browse the repository at this point in the history
  • Loading branch information
nkryuchkov committed Feb 12, 2020
1 parent 8677292 commit 6a4a28d
Show file tree
Hide file tree
Showing 17 changed files with 1,146 additions and 5 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ require (
github.com/pkg/profile v1.3.0
github.com/prometheus/client_golang v1.3.0
github.com/prometheus/common v0.7.0
github.com/schollz/progressbar/v2 v2.15.0
github.com/sirupsen/logrus v1.4.2
github.com/spf13/cobra v0.0.5
github.com/stretchr/testify v1.4.0
Expand Down
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b/go.mod h1:01TrycV0kFyex
github.com/mholt/archiver v3.1.1+incompatible h1:1dCVxuqs0dJseYEhi5pl7MYPH9zDa1wBi7mF09cbNkU=
github.com/mholt/archiver/v3 v3.3.0 h1:vWjhY8SQp5yzM9P6OJ/eZEkmi3UAbRrxCq48MxjAzig=
github.com/mholt/archiver/v3 v3.3.0/go.mod h1:YnQtqsp+94Rwd0D/rk5cnLrxusUBUXg+08Ebtr1Mqao=
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db h1:62I3jR2EmQ4l5rM/4FEfDWcRD+abF5XlKShorW5LRoQ=
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db/go.mod h1:l0dey0ia/Uv7NcFFVbCLtqEBQbrT4OCwCSKTEv6enCw=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
Expand Down Expand Up @@ -141,6 +143,9 @@ github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsT
github.com/prometheus/procfs v0.0.8 h1:+fpWZdT24pJBiqJdAwYBjPSk+5YmQzYNPYzQsdzLkt8=
github.com/prometheus/procfs v0.0.8/go.mod h1:7Qr8sr6344vo1JqZ6HhLceV9o3AJ1Ff+GxbHq6oeK9A=
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
github.com/schollz/progressbar v1.0.0 h1:gbyFReLHDkZo8mxy/dLWMr+Mpb1MokGJ1FqCiqacjZM=
github.com/schollz/progressbar/v2 v2.15.0 h1:dVzHQ8fHRmtPjD3K10jT3Qgn/+H+92jhPrhmxIJfDz8=
github.com/schollz/progressbar/v2 v2.15.0/go.mod h1:UdPq3prGkfQ7MOzZKlDRpYKcFqEMczbD7YmbPgpzKMI=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
Expand Down
25 changes: 23 additions & 2 deletions pkg/util/updater/updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ import (
"path/filepath"
"runtime"
"strings"
"sync/atomic"
"time"
"unicode"

"github.com/SkycoinProject/skycoin/src/util/logging"
"github.com/mholt/archiver/v3"
"github.com/schollz/progressbar/v2"

"github.com/SkycoinProject/skywire-mainnet/pkg/restart"
"github.com/SkycoinProject/skywire-mainnet/pkg/util/buildinfo"
Expand All @@ -45,13 +47,16 @@ var (
ErrNoChecksumFound = errors.New("no checksum found")
// ErrMalformedChecksumFile happens when checksum is malformed.
ErrMalformedChecksumFile = errors.New("malformed checksum file")
// ErrAlreadyStarted is returned when updating is already started.
ErrAlreadyStarted = errors.New("updating already started")
)

// Updater checks if a new version of skywire is available, downloads its binary files
// and runs them, substituting the current binary files.
type Updater struct {
log *logging.Logger
restartCtx *restart.Context
updating uint32
}

// New returns a new Updater.
Expand All @@ -65,6 +70,10 @@ func New(log *logging.Logger, restartCtx *restart.Context) *Updater {
// Update performs an update operation.
// NOTE: Update may call os.Exit.
func (u *Updater) Update() (err error) {
if !atomic.CompareAndSwapUint32(&u.updating, 0, 1) {
return ErrAlreadyStarted
}

u.log.Infof("Looking for updates")

lastVersion, err := lastVersion()
Expand Down Expand Up @@ -294,14 +303,24 @@ func downloadChecksums(url string) (checksums string, err error) {
return "", fmt.Errorf("received bad status code: %d", resp.StatusCode)
}

data, err := ioutil.ReadAll(resp.Body)
r := io.TeeReader(resp.Body, progressBar(resp.ContentLength))

data, err := ioutil.ReadAll(r)
if err != nil {
return "", err
}

return string(data), nil
}

func progressBar(contentLength int64) io.Writer {
newline := progressbar.OptionOnCompletion(func() {
fmt.Println()
})

return progressbar.NewOptions64(contentLength, progressbar.OptionSetBytes64(contentLength), newline)
}

func downloadFile(url, filename string) (path string, err error) {
resp, err := http.Get(url) // nolint:gosec
if err != nil {
Expand Down Expand Up @@ -333,7 +352,9 @@ func downloadFile(url, filename string) (path string, err error) {
return "", err
}

if _, err := io.Copy(f, resp.Body); err != nil {
out := io.MultiWriter(f, progressBar(resp.ContentLength))

if _, err := io.Copy(out, resp.Body); err != nil {
return "", err
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/visor/visor.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ type Visor struct {

startedAt time.Time
restartCtx *restart.Context
updater *updater.Updater

pidMu sync.Mutex

Expand Down Expand Up @@ -119,6 +120,7 @@ func NewVisor(cfg *Config, logger *logging.MasterLogger, restartCtx *restart.Con
restartCtx.RegisterLogger(visor.logger)

visor.restartCtx = restartCtx
visor.updater = updater.New(visor.logger, visor.restartCtx)

pk := cfg.Visor.StaticPubKey
sk := cfg.Visor.StaticSecKey
Expand Down Expand Up @@ -546,9 +548,7 @@ func (visor *Visor) Exec(command string) ([]byte, error) {
// Update checks if visor update is available.
// If it is, the method downloads a new visor versions, starts it and kills the current process.
func (visor *Visor) Update() error {
u := updater.New(visor.logger, visor.restartCtx)

if err := u.Update(); err != nil {
if err := visor.updater.Update(); err != nil {
visor.logger.Errorf("Failed to update visor: %v", err)
return err
}
Expand Down
15 changes: 15 additions & 0 deletions vendor/github.com/mitchellh/colorstring/.travis.yml

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

21 changes: 21 additions & 0 deletions vendor/github.com/mitchellh/colorstring/LICENSE

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

30 changes: 30 additions & 0 deletions vendor/github.com/mitchellh/colorstring/README.md

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

Loading

0 comments on commit 6a4a28d

Please sign in to comment.