@@ -15,10 +15,11 @@ import (
1515
1616 "code.google.com/p/go-uuid/uuid"
1717 "github.com/coreos/go-omaha/omaha"
18- update "github.com/coreos/updatectl /client/update/v1"
18+ update "github.com/coreos/updateservicectl /client/update/v1"
1919 "github.com/deis/deisctl/client"
2020 "github.com/deis/deisctl/cmd"
2121 "github.com/deis/deisctl/constant"
22+ "github.com/deis/deisctl/lock"
2223 "github.com/deis/deisctl/utils"
2324)
2425
@@ -71,6 +72,14 @@ func init() {
7172 cmdInstanceDeis .Flags .StringVar (& instanceFlags .version , "version" , utils .GetVersion (), "Version to report." )
7273}
7374
75+ func expBackoff (interval time.Duration ) time.Duration {
76+ interval = interval * 2
77+ if interval > constant .MaxInterval {
78+ interval = constant .MaxInterval
79+ }
80+ return interval
81+ }
82+
7483//+ downloadDir + "deis.tar.gz"
7584
7685type serverConfig struct {
@@ -86,6 +95,7 @@ type Client struct {
8695 config * serverConfig
8796 errorRate int
8897 pingsRemaining int
98+ lock * lock.Lock
8999}
90100
91101func (c * Client ) Log (format string , v ... interface {}) {
@@ -102,6 +112,19 @@ func (c *Client) getCodebaseUrl(uc *omaha.UpdateCheck) string {
102112 return uc .Urls .Urls [0 ].CodeBase + uc .Manifest .Packages .Packages [0 ].Name
103113}
104114
115+ func (c * Client ) RequestLock () {
116+ elc , err := lock .NewEtcdLockClient (nil )
117+ if err != nil {
118+ fmt .Fprintln (os .Stderr , "Error initializing etcd client:" , err )
119+ }
120+ var mID string
121+ mID = utils .GetMachineID ("/" )
122+ if mID == "" {
123+ fmt .Fprintln (os .Stderr , "Cannot read machine-id" )
124+ }
125+ c .lock = lock .New (mID , elc )
126+ }
127+
105128func (c * Client ) updateservice () (err error ) {
106129 fmt .Println ("starting systemd units" )
107130 // files, _ := utils.ListFiles(constant.UnitsDir + "*.service")
@@ -111,7 +134,6 @@ func (c *Client) updateservice() (err error) {
111134 Services := utils .GetServices ()
112135 if localServices .Len () == 0 {
113136 fmt .Println ("no local services" )
114- return
115137 }
116138 for _ , service := range localServices {
117139 if strings .HasSuffix (service , "-data.service" ) {
@@ -122,8 +144,10 @@ func (c *Client) updateservice() (err error) {
122144 if err != nil {
123145 return err
124146 }
125- time .Sleep (1 * time .Second )
126- err = cmd .Install (deis , []string {localService })
147+
148+ err = utils .Timeout ("Install unit" + service , 300 * time .Second , func () {
149+ cmd .Install (deis , []string {localService })
150+ })
127151 if err != nil {
128152 return err
129153 }
@@ -284,6 +308,7 @@ func (c *Client) SetVersion(resp *omaha.Response) {
284308
285309// Sleep between n and m seconds
286310func (c * Client ) Loop (n , m int ) {
311+ interval := constant .InitialInterval
287312 for {
288313 randSleep (n , m )
289314 resp , err := c .MakeRequest ("3" , "2" , true , false )
@@ -312,7 +337,27 @@ func (c *Client) Loop(n, m int) {
312337 continue
313338 }
314339 c .MakeRequest ("14" , "1" , false , false )
340+ c .RequestLock ()
341+ for {
342+ err = c .lock .Lock ()
343+ if err != nil && err != lock .ErrExist {
344+ interval = expBackoff (interval )
345+ fmt .Printf ("Retrying in %v. Error locking: %v\n " , interval , err )
346+ time .Sleep (interval )
347+ continue
348+ } else {
349+ break
350+ }
351+ }
315352 c .SetVersion (resp )
353+ err = c .lock .Unlock ()
354+ if err == lock .ErrNotExist {
355+ fmt .Println ("no lock found" )
356+ } else if err == nil {
357+ fmt .Println ("Unlocked existing lock for this machine" )
358+ } else {
359+ fmt .Fprintln (os .Stderr , "Error unlocking:" , err )
360+ }
316361 }
317362 }
318363}
0 commit comments