@@ -18,7 +18,7 @@ import (
1818 update "github.com/coreos/updatectl/client/update/v1"
1919 "github.com/deis/deisctl/client"
2020 "github.com/deis/deisctl/cmd"
21- _ "github.com/deis/deisctl/constant"
21+ "github.com/deis/deisctl/constant"
2222 "github.com/deis/deisctl/utils"
2323)
2424
@@ -64,9 +64,9 @@ func init() {
6464 cmdInstanceDeis .Flags .StringVar (& instanceFlags .OEM , "oem" , "deisclient" , "oem to report" )
6565 // simulate reboot lock.
6666 cmdInstanceDeis .Flags .IntVar (& instanceFlags .pingOnly , "ping-only" , 0 , "halt update and just send ping requests this many times." )
67- cmdInstanceDeis .Flags .StringVar (& instanceFlags .appId , "app-id" , os . Getenv ( "DEISCTL_APP_ID" ), "Application ID to update." )
67+ cmdInstanceDeis .Flags .StringVar (& instanceFlags .appId , "app-id" , utils . GetKey ( constant . UpdatekeyDir , "app-id" , "DEISCTL_APP_ID" ), "Application ID to update." )
6868 //instanceFlags.appId.required = true
69- cmdInstanceDeis .Flags .StringVar (& instanceFlags .groupId , "group-id" , os . Getenv ( "DEISCTL_GROUP_ID" ), "Group ID to update." )
69+ cmdInstanceDeis .Flags .StringVar (& instanceFlags .groupId , "group-id" , utils . GetKey ( constant . UpdatekeyDir , "group-id" , "DEISCTL_GROUP_ID" ), "Group ID to update." )
7070 //instanceFlags.groupId.required = true
7171 cmdInstanceDeis .Flags .StringVar (& instanceFlags .version , "version" , utils .GetVersion (), "Version to report." )
7272}
@@ -93,40 +93,60 @@ func (c *Client) Log(format string, v ...interface{}) {
9393 fmt .Printf (format , v ... )
9494}
9595
96+ func (c * Client ) failed (tag string , err error ) {
97+ c .Log ("%s %v\n " , tag , err )
98+ c .MakeRequest ("3" , "0" , false , false )
99+ }
100+
96101func (c * Client ) getCodebaseUrl (uc * omaha.UpdateCheck ) string {
97102 return uc .Urls .Urls [0 ].CodeBase + uc .Manifest .Packages .Packages [0 ].Name
98103}
99104
100- func (c * Client ) updateservice () {
105+ func (c * Client ) updateservice () ( err error ) {
101106 fmt .Println ("starting systemd units" )
102107 // files, _ := utils.ListFiles(constant.UnitsDir + "*.service")
103108 deis , _ := client .NewClient ()
104109 localServices := deis .GetLocaljobs ()
105110 fmt .Printf ("local services: %v\n " , localServices )
106- // Services := utils.GetServices()
111+ Services := utils .GetServices ()
107112 if localServices .Len () == 0 {
108113 fmt .Println ("no local services" )
109114 return
110115 }
111116 for _ , service := range localServices {
112- if strings .HasSuffix (service ,"-data.service" ) {
117+ if strings .HasSuffix (service , "-data.service" ) {
113118 continue
114119 }
115120 localService := strings .Split (strings .Split (service , "-" )[1 ], ".service" )[0 ]
116- cmd .Uninstall (deis , []string {localService })
121+ err := cmd .Uninstall (deis , []string {localService })
122+ if err != nil {
123+ return err
124+ }
117125 time .Sleep (1 * time .Second )
118- cmd .Install (deis , []string {localService })
126+ err = cmd .Install (deis , []string {localService })
127+ if err != nil {
128+ return err
129+ }
119130 }
120- // var count int
121- // for _, service := range Services {
122- // count = 0
123- // for _, lserv := range localServices {
124- // if strings.Contains(lserv, strings.Split(strings.Split(service, "-")[1], ".")[0]) {
125- // count = count + 1
126- // }
127- // }
128- // }
129-
131+ var count int
132+ for _ , service := range Services {
133+ count = 0
134+ if strings .HasSuffix (service , "-data.service" ) {
135+ continue
136+ }
137+ for _ , lserv := range localServices {
138+ if strings .Contains (lserv , strings .Split (strings .Split (service , "-" )[1 ], "." )[0 ]) {
139+ count = count + 1
140+ }
141+ }
142+ if count == 0 {
143+ if err = cmd .PullImage (service ); err != nil {
144+ fmt .Println ("failed pulling image" , service )
145+ return err
146+ }
147+ }
148+ }
149+ return nil
130150 // pre-install hook (download all new docker images)
131151 // use systemd to list local deis-* units, ignore -data units
132152 // cmd.Unistall([]string{"router.3"})
@@ -137,7 +157,6 @@ func (c *Client) updateservice() {
137157func (c * Client ) downloadFromUrl (url , filePath string ) (err error ) {
138158 fmt .Printf ("Downloading %s to %s" , url , filePath )
139159
140- // TODO: check file existence first with io.IsExist
141160 output , err := os .Create (filePath )
142161 if err != nil {
143162 fmt .Println ("Error while creating" , filePath , "-" , err )
@@ -231,15 +250,15 @@ func (c *Client) SetVersion(resp *omaha.Response) {
231250 }
232251 }()
233252 uc := resp .Apps [0 ].UpdateCheck
234- url := c . getCodebaseUrl ( uc )
235- c . MakeRequest ( "13" , "1" , false , false )
236- c . downloadFromUrl ( url , "/tmp/deis.tar.gz" )
237- utils . Extract ( "/tmp/deis.tar.gz " , "/" )
238- c . MakeRequest ( "14" , "1" , false , false )
239- c . updateservice ()
240- fmt . Println ("Installation done" )
253+
254+ err := c . updateservice ( )
255+ if err != nil {
256+ c . failed ( "update failed " , err )
257+ return
258+ }
259+ c . Log ("Installation done " )
241260 c .MakeRequest ("2" , "1" , false , false )
242- fmt . Println ( "updated done" )
261+ c . Log ( "Update done " )
243262 c .MakeRequest ("3" , "1" , false , false )
244263 // installed
245264
@@ -253,8 +272,9 @@ func (c *Client) SetVersion(resp *omaha.Response) {
253272 c .Log ("updated from %s to %s\n " , c .Version , uc .Manifest .Version )
254273
255274 c .Version = uc .Manifest .Version
275+ utils .PutVersion (c .Version )
256276
257- _ , err : = c .MakeRequest ("3" , "2" , false , false ) // Send complete with new version.
277+ _ , err = c .MakeRequest ("3" , "2" , false , false ) // Send complete with new version.
258278 if err != nil {
259279 log .Println (err )
260280 }
@@ -275,6 +295,23 @@ func (c *Client) Loop(n, m int) {
275295 if uc .Status != "ok" {
276296 c .Log ("update check status: %s\n " , uc .Status )
277297 } else {
298+ url := c .getCodebaseUrl (uc )
299+ if ! strings .Contains (url , "deis" ) {
300+ c .failed ("Wrong Url" , err )
301+ continue
302+ }
303+ c .MakeRequest ("13" , "1" , false , false )
304+ err = c .downloadFromUrl (url , "/tmp/deis.tar.gz" )
305+ if err != nil {
306+ c .failed ("Download failed" , err )
307+ continue
308+ }
309+ err = utils .Extract ("/tmp/deis.tar.gz" , "/" )
310+ if err != nil {
311+ c .failed ("Extract failed" , err )
312+ continue
313+ }
314+ c .MakeRequest ("14" , "1" , false , false )
278315 c .SetVersion (resp )
279316 }
280317 }
0 commit comments