Skip to content

Commit

Permalink
added support for preCommand; added error rollback and cleanup support
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisMcKenzie committed Nov 4, 2015
1 parent ca12835 commit fdab667
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 7 deletions.
5 changes: 4 additions & 1 deletion commands/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ func agent(c *cobra.Command, args []string) {
shutdownCh := make(chan struct{})

for _, s := range services {
service.NewDispatcher(s, t, shutdownCh)
_, err := service.NewDispatcher(s, t, shutdownCh)
if err != nil {
log.Fatal(err)
}
}

sigs := make(chan os.Signal, 1)
Expand Down
19 changes: 18 additions & 1 deletion installer/install.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
package installer

import "io"
import (
"io"
"os"
"path/filepath"
)

// Installer is an interface that allows different methods of writing
// the given io.Reader to disk.
type Installer interface {
Install(dest string, r io.Reader) (int, error)
}

func moveOld(dest string) error {
return os.Rename(dest, filepath.Join(dest, ".old"))
}

func cleanup(dest string, err error) error {
oldPath := filepath.Join(dest, ".old")
if err != nil {
return os.Rename(oldPath, dest)
}

return os.RemoveAll(oldPath)
}
2 changes: 2 additions & 0 deletions installer/tar.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var ErrNilReader = errors.New("Install: must have a non-nil Reader")
type TarInstaller struct{}

func (i TarInstaller) Install(dest string, fr io.Reader) (count int, err error) {
moveOld(dest)
if fr == nil {
return count, ErrNilReader
}
Expand Down Expand Up @@ -65,5 +66,6 @@ func (i TarInstaller) Install(dest string, fr io.Reader) (count int, err error)
count++
}

defer cleanup(dest, err)
return
}
12 changes: 8 additions & 4 deletions package.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
mkdir -p packaging/output

arch=$1
os=$2
version=$3

set -x
GOARCH=$1 GOOS=$2 go build -o packaging/root/usr/local/bin/dropship main.go
GOARCH=$arch GOOS=$os go build -o packaging/root/usr/local/bin/dropship main.go

fpm -s dir -t deb -n dropship -v "$1" -p packaging/output/dropship.$2-$1.deb \
fpm -s dir -t deb -n dropship -v "$version" -p packaging/output/dropship-$version.$os-$arch.deb \
--deb-priority optional --category admin \
--force \
--after-install packaging/scripts/postinstall.deb \
Expand All @@ -13,8 +17,8 @@ fpm -s dir -t deb -n dropship -v "$1" -p packaging/output/dropship.$2-$1.deb \
--url https://github.com/chrismckenzie/dropship \
--description "Dropship automatically keeps you software up to date" \
-m "Chris McKenzie <chris@chrismckenzie.io>" \
-a $1 \
-a $arch \
--config-files /etc/dropship.d/dropship.hcl \
packaging/root/=/

cp packaging/output/dropship.$2-$1.deb .
cp packaging/output/dropship-$version.$os-$arch.deb .
10 changes: 9 additions & 1 deletion service/dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ func (w *Dispatcher) Work() {
defer l.Release()
}

if w.config.PreCommand != "" {
res, err := executeCommand(w.config.PreCommand)
if err != nil {
log.Printf("[ERR]: Unable to execute preComment. %v", err)
}
log.Printf("[INF]: preCommand executed successfully. %v", res)
}

log.Printf("[INF]: Installing update for %s...", w.config.Name)
fr, meta, err := u.Download(opts)
if err != nil {
Expand All @@ -109,7 +117,7 @@ func (w *Dispatcher) Work() {
if w.config.PostCommand != "" {
res, err := executeCommand(w.config.PostCommand)
if err != nil {
log.Printf("[ERR]: Unable to execute postComment. %v", err)
log.Printf("[ERR]: Unable to execute postCommand. %v", err)
}
log.Printf("[INF]: postCommand executed successfully. %v", res)
}
Expand Down
1 change: 1 addition & 0 deletions service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type Config struct {
Name string `hcl:",key"`
CheckInterval string `hcl:"checkInterval"`
PostCommand string `hcl:postCommand`
PreCommand string `hcl:preCommand`
Sequential bool `hcl:"sequentialUpdates"`
Artifact Artifact `hcl:"artifact,expand"`
}

0 comments on commit fdab667

Please sign in to comment.