github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/state/gui.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package state 5 6 import ( 7 "github.com/juju/errors" 8 "github.com/juju/version" 9 "gopkg.in/mgo.v2" 10 "gopkg.in/mgo.v2/bson" 11 ) 12 13 // guiSettingsDoc represents the Juju GUI settings in MongoDB. 14 type guiSettingsDoc struct { 15 // CurrentVersion is the version of the Juju GUI currently served by 16 // the controller when requesting the GUI via HTTP. 17 CurrentVersion version.Number `bson:"current-version"` 18 } 19 20 // GUISetVersion sets the Juju GUI version that the controller must serve. 21 func (st *State) GUISetVersion(vers version.Number) error { 22 // Check that the provided version is actually present in the GUI storage. 23 storage, err := st.GUIStorage() 24 if err != nil { 25 return errors.Annotate(err, "cannot open GUI storage") 26 } 27 defer storage.Close() 28 if _, err = storage.Metadata(vers.String()); err != nil { 29 return errors.Annotatef(err, "cannot find %q GUI version in the storage", vers) 30 } 31 32 // Set the current version. 33 settings, closer := st.getCollection(guisettingsC) 34 defer closer() 35 if _, err = settings.Writeable().Upsert(nil, bson.D{{"current-version", vers}}); err != nil { 36 return errors.Annotate(err, "cannot set current GUI version") 37 } 38 return nil 39 } 40 41 // GUIVersion returns the Juju GUI version currently served by the controller. 42 func (st *State) GUIVersion() (vers version.Number, err error) { 43 settings, closer := st.getCollection(guisettingsC) 44 defer closer() 45 46 // Retrieve the settings document. 47 var doc guiSettingsDoc 48 err = settings.Find(nil).Select(bson.D{{"current-version", 1}}).One(&doc) 49 if err == nil { 50 return doc.CurrentVersion, nil 51 } 52 if err == mgo.ErrNotFound { 53 return vers, errors.NotFoundf("Juju GUI version") 54 } 55 return vers, errors.Trace(err) 56 }