github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/api/reboot/reboot.go (about) 1 // Copyright 2014 Cloudbase Solutions 2 // Copyright 2014 Canonical Ltd. 3 // Licensed under the AGPLv3, see LICENCE file for details. 4 5 package reboot 6 7 import ( 8 "github.com/juju/errors" 9 "gopkg.in/juju/names.v2" 10 11 "github.com/juju/juju/api/base" 12 apiwatcher "github.com/juju/juju/api/watcher" 13 "github.com/juju/juju/apiserver/params" 14 "github.com/juju/juju/watcher" 15 ) 16 17 // State provides access to an reboot worker's view of the state. 18 // NOTE: This is defined as an interface due to PPC64 bug #1533469 - 19 // if it were a type build errors happen (due to a linker bug). 20 type State interface { 21 // WatchForRebootEvent returns a watcher.NotifyWatcher that 22 // reacts to reboot flag changes. 23 WatchForRebootEvent() (watcher.NotifyWatcher, error) 24 25 // RequestReboot sets the reboot flag for the calling machine. 26 RequestReboot() error 27 28 // ClearReboot clears the reboot flag for the calling machine. 29 ClearReboot() error 30 31 // GetRebootAction returns the reboot action for the calling machine. 32 GetRebootAction() (params.RebootAction, error) 33 } 34 35 var _ State = (*state)(nil) 36 37 // state implements State. 38 type state struct { 39 machineTag names.Tag 40 facade base.FacadeCaller 41 } 42 43 // NewState returns a version of the state that provides functionality 44 // required by the reboot worker. 45 func NewState(caller base.APICaller, machineTag names.MachineTag) State { 46 return &state{ 47 facade: base.NewFacadeCaller(caller, "Reboot"), 48 machineTag: machineTag, 49 } 50 } 51 52 // WatchForRebootEvent implements State.WatchForRebootEvent 53 func (st *state) WatchForRebootEvent() (watcher.NotifyWatcher, error) { 54 var result params.NotifyWatchResult 55 56 if err := st.facade.FacadeCall("WatchForRebootEvent", nil, &result); err != nil { 57 return nil, err 58 } 59 if result.Error != nil { 60 return nil, result.Error 61 } 62 63 w := apiwatcher.NewNotifyWatcher(st.facade.RawAPICaller(), result) 64 return w, nil 65 } 66 67 // RequestReboot implements State.RequestReboot 68 func (st *state) RequestReboot() error { 69 var results params.ErrorResults 70 args := params.Entities{ 71 Entities: []params.Entity{{Tag: st.machineTag.String()}}, 72 } 73 74 err := st.facade.FacadeCall("RequestReboot", args, &results) 75 if err != nil { 76 return errors.Trace(err) 77 } 78 if len(results.Results) != 1 { 79 return errors.Errorf("expected 1 result, got %d", len(results.Results)) 80 } 81 82 if results.Results[0].Error != nil { 83 return errors.Trace(results.Results[0].Error) 84 } 85 return nil 86 } 87 88 // ClearReboot implements State.ClearReboot 89 func (st *state) ClearReboot() error { 90 var results params.ErrorResults 91 args := params.Entities{ 92 Entities: []params.Entity{{Tag: st.machineTag.String()}}, 93 } 94 95 err := st.facade.FacadeCall("ClearReboot", args, &results) 96 if err != nil { 97 return errors.Trace(err) 98 } 99 100 if len(results.Results) != 1 { 101 return errors.Errorf("expected 1 result, got %d", len(results.Results)) 102 } 103 104 if results.Results[0].Error != nil { 105 return errors.Trace(results.Results[0].Error) 106 } 107 108 return nil 109 } 110 111 // GetRebootAction implements State.GetRebootAction 112 func (st *state) GetRebootAction() (params.RebootAction, error) { 113 var results params.RebootActionResults 114 args := params.Entities{ 115 Entities: []params.Entity{{Tag: st.machineTag.String()}}, 116 } 117 118 err := st.facade.FacadeCall("GetRebootAction", args, &results) 119 if err != nil { 120 return params.ShouldDoNothing, err 121 } 122 if len(results.Results) != 1 { 123 return params.ShouldDoNothing, errors.Errorf("expected 1 result, got %d", len(results.Results)) 124 } 125 126 if results.Results[0].Error != nil { 127 return params.ShouldDoNothing, errors.Trace(results.Results[0].Error) 128 } 129 130 return results.Results[0].Result, nil 131 }