Skip to content

Commit

Permalink
API to check status.
Browse files Browse the repository at this point in the history
  • Loading branch information
ExternalReality committed Jun 30, 2018
1 parent 667df47 commit 7591243
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 1 deletion.
19 changes: 19 additions & 0 deletions api/uniter/unit.go
Expand Up @@ -664,6 +664,25 @@ func (u *Unit) WatchUpgradeSeriesNotifications() (watcher.NotifyWatcher, error)
return w, nil
}

func (u *Unit) UpgradeSeriesStatus() (string, error) {
var results params.UpgradeSeriesStatusResults
args := params.Entities{
Entities: []params.Entity{{Tag: u.tag.String()}},
}
err := u.st.facade.FacadeCall("UpgradeSeriesStatus", args, &results)
if err != nil {
return "", err
}
if len(results.Results) != 1 {
return "", errors.Errorf("expected 1 result, got %d", len(results.Results))
}
result := results.Results[0]
if result.Error != nil {
return "", result.Error
}
return result.Status, nil
}

// RequestReboot sets the reboot flag for its machine agent
func (u *Unit) RequestReboot() error {
machineId, err := u.AssignedMachine()
Expand Down
13 changes: 13 additions & 0 deletions api/uniter/unit_test.go
Expand Up @@ -710,6 +710,19 @@ func (s *unitSuite) TestWatchUpgradeSeriesNotifications(c *gc.C) {
notifyWatcher.AssertOneChange()
}

func (s *unitSuite) TestUpgradeSeriesStatus(c *gc.C) {
// First we create the prepare lock
err := s.wordpressMachine.CreateUpgradeSeriesLock()
c.Assert(err, jc.ErrorIsNil)

// Then we check to see the status of our upgrade. We note that creating
// the lock essentially kicks off an upgrade for the perspective of assigned units.
status, err := s.apiUnit.UpgradeSeriesStatus()
c.Assert(err, jc.ErrorIsNil)

c.Assert(status, gc.Equals, "preparing")
}

func (s *unitSuite) TestApplicationNameAndTag(c *gc.C) {
c.Assert(s.apiUnit.ApplicationName(), gc.Equals, s.wordpressApplication.Name())
c.Assert(s.apiUnit.ApplicationTag(), gc.Equals, s.wordpressApplication.Tag())
Expand Down
30 changes: 30 additions & 0 deletions apiserver/facades/agent/uniter/uniter.go
Expand Up @@ -1020,6 +1020,36 @@ func (u *UniterAPI) WatchUpgradeSeriesNotifications(args params.Entities) (param
return result, nil
}

func (u *UniterAPI) UpgradeSeriesStatus(args params.Entities) (params.UpgradeSeriesStatusResults, error) {
result := params.UpgradeSeriesStatusResults{
Results: make([]params.UpgradeSeriesStatusResults, len(args.Entities)),
}
canAccess, err := u.accessUnit()
if err != nil {
return params.UpgradeSeriesStatusResults{}, err
}
for i, entity := range args.Entities {
tag, err := names.ParseUnitTag(entity.Tag)
if err != nil {
result.Results[i].Error = common.ServerError(common.ErrPerm)
continue
}

if !canAccess(tag) {
result.Results[i].Error = common.ServerError(common.ErrPerm)
continue
}
unit, err := u.getUnit(tag)
unit.GetMachineUpgradeSeriesStatus()
if err != nil {
result.Results[i].Error = common.ServerError(err)
continue
}
}

return result, nil
}

// ConfigSettings returns the complete set of application charm config
// settings available to each given unit.
func (u *UniterAPI) ConfigSettings(args params.Entities) (params.ConfigSettingsResults, error) {
Expand Down
9 changes: 9 additions & 0 deletions apiserver/params/params.go
Expand Up @@ -1276,3 +1276,12 @@ type DumpModelRequest struct {
Entities []Entity `json:"entities"`
Simplified bool `json:"simplified"`
}

type UpgradeSeriesStatusResult struct {
Error *Error `json:"error,omitempty"`
Status string `json:"status,omitempty"`
}

type UpgradeSeriesStatusResults struct {
Results []UpgradeSeriesStatusResults `json:"results,omitempty"`
}
4 changes: 4 additions & 0 deletions state/machine.go
Expand Up @@ -2120,6 +2120,10 @@ func (m *Machine) RemoveUpgradeSeriesLock() error {
return nil
}

func (m *Machine) UpgradeSeriesStatus() (string, error) {

}

func createUpgradeSeriesLockTxnOps(machineDocId string, data *upgradeSeriesLock) []txn.Op {
return []txn.Op{
{
Expand Down
9 changes: 9 additions & 0 deletions state/watcher.go
Expand Up @@ -1646,6 +1646,15 @@ func (u *Unit) WatchUpgradeSeriesNotifications() (NotifyWatcher, error) {
return newEntityWatcher(machine.st, machineUpgradeSeriesLocksC, machine.doc.DocID), nil
}

// UpgradeSeriesStatus returns the upgrade status of the units assigned machine.
func (u *Unit) UpgradeSeriesStatus() (string, error) {
machine, err := u.machine()
if err != nil {
return nil, err
}
return machine.GetUpgradeSeriesStatus()
}

func newEntityWatcher(backend modelBackend, collName string, key interface{}) NotifyWatcher {
return newDocWatcher(backend, []docKey{{collName, key}})
}
Expand Down
5 changes: 5 additions & 0 deletions worker/uniter/remotestate/mock_test.go
Expand Up @@ -204,6 +204,7 @@ type mockUnit struct {
addressesWatcher *mockNotifyWatcher
configSettingsWatcher *mockNotifyWatcher
applicationConfigSettingsWatcher *mockNotifyWatcher
upgradeSeriesWatcher *mockNotifyWatcher
storageWatcher *mockStringsWatcher
actionWatcher *mockStringsWatcher
relationsWatcher *mockStringsWatcher
Expand Down Expand Up @@ -261,6 +262,10 @@ func (u *mockUnit) WatchRelations() (watcher.StringsWatcher, error) {
return u.relationsWatcher, nil
}

func (u *mockUnit) WatchUpgradeSeriesNotifications() (watcher.NotifyWatcher, error) {
return u.upgradeSeriesWatcher, nil
}

type mockApplication struct {
tag names.ApplicationTag
life params.Life
Expand Down
2 changes: 2 additions & 0 deletions worker/uniter/remotestate/state.go
Expand Up @@ -42,11 +42,13 @@ type Unit interface {
WatchAddresses() (watcher.NotifyWatcher, error)
WatchConfigSettings() (watcher.NotifyWatcher, error)
WatchTrustConfigSettings() (watcher.NotifyWatcher, error)
WatchUpgradeSeriesNotifications() (watcher.NotifyWatcher, error)
WatchStorage() (watcher.StringsWatcher, error)
WatchActionNotifications() (watcher.StringsWatcher, error)
// WatchRelation returns a watcher that fires when relations
// relevant for this unit change.
WatchRelations() (watcher.StringsWatcher, error)
UpgradeSeriesStatus() string
}

type Application interface {
Expand Down
19 changes: 18 additions & 1 deletion worker/uniter/remotestate/watcher.go
Expand Up @@ -170,7 +170,7 @@ func (w *RemoteStateWatcher) CommandCompleted(completed string) {
}

func (w *RemoteStateWatcher) setUp(unitTag names.UnitTag) (err error) {
// TODO(dfc) named return value is a time bomb
// TODO(dfc) named return value is a time bomb (externalreality) I second the notion
// TODO(axw) move this logic.
defer func() {
cause := errors.Cause(err)
Expand Down Expand Up @@ -317,6 +317,17 @@ func (w *RemoteStateWatcher) loop(unitTag names.UnitTag) (err error) {
// returned by the leadership tracker.
requiredEvents++

// TODO(externalreality) This pattern should probably be extracted
var seenUpgradeSeriesChange bool
upgradeSeriesw, err := w.unit.WatchUpgradeSeriesNotifications()
if err != nil {
return errors.Trace(err)
}
if err := w.catacomb.Add(upgradeSeriesw); err != nil {
return errors.Trace(err)
}
requiredEvents++

var eventsObserved int
observedEvent := func(flag *bool) {
if flag != nil && !*flag {
Expand Down Expand Up @@ -407,6 +418,12 @@ func (w *RemoteStateWatcher) loop(unitTag names.UnitTag) (err error) {
if err != nil {
return err
}
case _, ok := <- upgradeSeriesw.Changes():
logger.Debugf("got upgrade series change")
if !ok {
return errors.New("upgrades series watcher closed")
}
observedEvent(&seenUpgradeSeriesChange)
case _, ok := <-addressesChanges:
logger.Debugf("got address change: ok=%t", ok)
if !ok {
Expand Down
3 changes: 3 additions & 0 deletions worker/uniter/remotestate/watcher_test.go
Expand Up @@ -60,6 +60,7 @@ func (s *WatcherSuite) SetUpTest(c *gc.C) {
addressesWatcher: newMockNotifyWatcher(),
configSettingsWatcher: newMockNotifyWatcher(),
applicationConfigSettingsWatcher: newMockNotifyWatcher(),
upgradeSeriesWatcher: newMockNotifyWatcher(),
storageWatcher: newMockStringsWatcher(),
actionWatcher: newMockStringsWatcher(),
relationsWatcher: newMockStringsWatcher(),
Expand Down Expand Up @@ -151,6 +152,7 @@ func (s *WatcherSuite) TestInitialSignal(c *gc.C) {
s.st.unit.addressesWatcher.changes <- struct{}{}
s.st.unit.configSettingsWatcher.changes <- struct{}{}
s.st.unit.applicationConfigSettingsWatcher.changes <- struct{}{}
s.st.unit.upgradeSeriesWatcher.changes <- struct{}{}
s.st.unit.storageWatcher.changes <- []string{}
s.st.unit.actionWatcher.changes <- []string{}
if s.st.unit.application.applicationWatcher != nil {
Expand All @@ -167,6 +169,7 @@ func (s *WatcherSuite) signalAll() {
s.st.unit.unitWatcher.changes <- struct{}{}
s.st.unit.configSettingsWatcher.changes <- struct{}{}
s.st.unit.applicationConfigSettingsWatcher.changes <- struct{}{}
s.st.unit.upgradeSeriesWatcher.changes <- struct{}{}
s.st.unit.actionWatcher.changes <- []string{}
s.st.unit.application.leaderSettingsWatcher.changes <- struct{}{}
s.st.unit.relationsWatcher.changes <- []string{}
Expand Down

0 comments on commit 7591243

Please sign in to comment.