Skip to content

Commit

Permalink
Merge pull request #9 from linyows/support-downgrade
Browse files Browse the repository at this point in the history
Support downgrade of APP version
  • Loading branch information
linyows authored Feb 8, 2019
2 parents 1e2a8b8 + 80b3783 commit 3100932
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 33 deletions.
21 changes: 10 additions & 11 deletions dewy.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,18 +106,17 @@ func (d *Dewy) Run() error {
return err
}

if !d.repo.IsDownloadNecessary() {
log.Print("[DEBUG] Download skipped")
return nil
}

key, err := d.repo.Download()
key, err := d.repo.GetDeploySourceKey()
if err != nil {
log.Printf("[DEBUG] Download failure: %#v", err)
if err.Error() == "No need to deploy" {
log.Print("[DEBUG] Deploy skipped")
} else {
log.Printf("[DEBUG] Download failure: %#v", err)
}
return nil
}

d.notice.Notify(ctx, fmt.Sprintf("New release <%s|%s> was downloaded",
d.notice.Notify(ctx, fmt.Sprintf("New shipping <%s|%s> was detected",
d.repo.ReleaseURL(), d.repo.ReleaseTag()))

if err := d.deploy(key); err != nil {
Expand All @@ -134,10 +133,10 @@ func (d *Dewy) Run() error {
}
}

log.Print("[DEBUG] Record shipment")
err = d.repo.RecordShipment()
log.Print("[DEBUG] Record shipping")
err = d.repo.RecordShipping()
if err != nil {
log.Printf("[ERROR] Record shipment failure: %#v", err)
log.Printf("[ERROR] Record shipping failure: %#v", err)
}

log.Printf("[INFO] Keep releases as %d", keepReleases)
Expand Down
7 changes: 3 additions & 4 deletions kvs/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"sync"
Expand Down Expand Up @@ -73,10 +74,6 @@ func (f *File) Write(key string, data []byte) error {
}

p := filepath.Join(f.dir, key)
if IsFileExist(p) {
return fmt.Errorf("File already exists: %s", p)
}

file, err := os.OpenFile(p, os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return err
Expand All @@ -85,6 +82,8 @@ func (f *File) Write(key string, data []byte) error {
defer file.Close()
file.Write(data)

log.Printf("[INFO] Write file to %s", p)

return nil
}

Expand Down
48 changes: 33 additions & 15 deletions repo/github_release.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,38 +169,56 @@ func (g *GithubRelease) setCacheKey() error {
return nil
}

// IsDownloadNecessary checks necessary for download
func (g *GithubRelease) IsDownloadNecessary() bool {
// GetDeploySourceKey returns cache key
func (g *GithubRelease) GetDeploySourceKey() (string, error) {
currentKey := "current.txt"
currentSourceKey, _ := g.cache.Read(currentKey)
found := false

list, err := g.cache.List()
if err != nil {
return false
return "", err
}

for _, key := range list {
if string(currentSourceKey) == g.cacheKey && key == g.cacheKey {
return "", fmt.Errorf("No need to deploy")
}

if key == g.cacheKey {
return false
found = true
break
}
}

if !found {
if err := g.download(); err != nil {
return "", err
}
}

return true
if err := g.cache.Write(currentKey, []byte(g.cacheKey)); err != nil {
return "", err
}

return g.cacheKey, nil
}

// Download artifact from github
func (g *GithubRelease) Download() (string, error) {
func (g *GithubRelease) download() error {
ctx := context.Background()
c, err := g.client(ctx)
if err != nil {
return "", err
return err
}

reader, url, err := c.Repositories.DownloadReleaseAsset(ctx, g.owner, g.name, g.assetID)
if err != nil {
return "", err
return err
}
if url != "" {
res, err := http.Get(url)
if err != nil {
return "", err
return err
}
reader = res.Body
}
Expand All @@ -209,15 +227,15 @@ func (g *GithubRelease) Download() (string, error) {
buf := new(bytes.Buffer)
_, err = io.Copy(buf, reader)
if err != nil {
return "", err
return err
}

if err := g.cache.Write(g.cacheKey, buf.Bytes()); err != nil {
return "", err
return err
}
log.Printf("[INFO] Cached as %s", g.cacheKey)

return g.cacheKey, nil
return nil
}

func (g *GithubRelease) client(ctx context.Context) (*github.Client, error) {
Expand All @@ -243,8 +261,8 @@ func (g *GithubRelease) client(ctx context.Context) (*github.Client, error) {
return g.cl, nil
}

// RecordShipment save shipment to github
func (g *GithubRelease) RecordShipment() error {
// RecordShipping save shipping to github
func (g *GithubRelease) RecordShipping() error {
ctx := context.Background()
c, err := g.client(ctx)
if err != nil {
Expand Down
5 changes: 2 additions & 3 deletions repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ import (
type Repo interface {
String() string
Fetch() error
Download() (string, error)
IsDownloadNecessary() bool
RecordShipment() error
GetDeploySourceKey() (string, error)
RecordShipping() error
ReleaseTag() string
ReleaseURL() string
OwnerURL() string
Expand Down

0 comments on commit 3100932

Please sign in to comment.