github.com/Pankov404/juju@v0.0.0-20150703034450-be266991dceb/worker/uniter/operation/state.go (about) 1 // Copyright 2012-2014 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package operation 5 6 import ( 7 "os" 8 "time" 9 10 "github.com/juju/errors" 11 "github.com/juju/utils" 12 "gopkg.in/juju/charm.v5" 13 14 "github.com/juju/juju/worker/uniter/hook" 15 ) 16 17 // Kind enumerates the operations the uniter can perform. 18 type Kind string 19 20 const ( 21 // Install indicates that the uniter is installing the charm. 22 Install Kind = "install" 23 24 // RunHook indicates that the uniter is running a hook. 25 RunHook Kind = "run-hook" 26 27 // RunAction indicates that the uniter is running an action. 28 RunAction Kind = "run-action" 29 30 // Upgrade indicates that the uniter is upgrading the charm. 31 Upgrade Kind = "upgrade" 32 33 // Continue indicates that the uniter should run ModeContinue 34 // to determine the next operation. 35 Continue Kind = "continue" 36 ) 37 38 // Step describes the recorded progression of an operation. 39 type Step string 40 41 const ( 42 // Queued indicates that the uniter should undertake the operation 43 // as soon as possible. 44 Queued Step = "queued" 45 46 // Pending indicates that the uniter has started, but not completed, 47 // the operation. 48 Pending Step = "pending" 49 50 // Done indicates that the uniter has completed the operation, 51 // but may not yet have synchronized all necessary secondary state. 52 Done Step = "done" 53 ) 54 55 // State defines the local persistent state of the uniter, excluding relation 56 // state. 57 type State struct { 58 59 // Leader indicates whether a leader-elected hook has been queued to run, and 60 // no more recent leader-deposed hook has completed. 61 Leader bool `yaml:"leader"` 62 63 // Started indicates whether the start hook has run. 64 Started bool `yaml:"started"` 65 66 // Stopped indicates whether the stop hook has run. 67 Stopped bool `yaml:"stopped"` 68 69 // StatusSet indicates whether the charm being deployed has ever invoked 70 // the status-set hook tool. 71 StatusSet bool `yaml:"status-set"` 72 73 // Kind indicates the current operation. 74 Kind Kind `yaml:"op"` 75 76 // Step indicates the current operation's progression. 77 Step Step `yaml:"opstep"` 78 79 // Hook holds hook information relevant to the current operation. If Kind 80 // is Continue, it holds the last hook that was executed; if Kind is RunHook, 81 // it holds the running hook; if Kind is Upgrade, a non-nil hook indicates 82 // that the uniter should return to that hook's Pending state after the 83 // upgrade is complete (instead of running an upgrade-charm hook). 84 Hook *hook.Info `yaml:"hook,omitempty"` 85 86 // ActionId holds action information relevant to the current operation. If 87 // Kind is Continue, it holds the last action that was executed; if Kind is 88 // RunAction, it holds the running action. 89 ActionId *string `yaml:"action-id,omitempty"` 90 91 // Charm describes the charm being deployed by an Install or Upgrade 92 // operation, and is otherwise blank. 93 CharmURL *charm.URL `yaml:"charm,omitempty"` 94 95 // CollectMetricsTime records the time the collect metrics hook was last run. 96 // It's set to nil if the hook was not run at all. Recording time as int64 97 // because the yaml encoder cannot encode the time.Time struct. 98 CollectMetricsTime int64 `yaml:"collectmetricstime,omitempty"` 99 100 // UpdateStatusTime records the time the update status hook was last run. 101 // It's set to nil if the hook was not run at all. 102 UpdateStatusTime int64 `yaml:"updatestatustime,omitempty"` 103 } 104 105 // validate returns an error if the state violates expectations. 106 func (st State) validate() (err error) { 107 defer errors.DeferredAnnotatef(&err, "invalid operation state") 108 hasHook := st.Hook != nil 109 hasActionId := st.ActionId != nil 110 hasCharm := st.CharmURL != nil 111 switch st.Kind { 112 case Install: 113 if hasHook { 114 return errors.New("unexpected hook info with Kind Install") 115 } 116 fallthrough 117 case Upgrade: 118 switch { 119 case !hasCharm: 120 return errors.New("missing charm URL") 121 case hasActionId: 122 return errors.New("unexpected action id") 123 } 124 case RunAction: 125 switch { 126 case !hasActionId: 127 return errors.New("missing action id") 128 case hasCharm: 129 return errors.New("unexpected charm URL") 130 } 131 case RunHook: 132 switch { 133 case !hasHook: 134 return errors.New("missing hook info with Kind RunHook") 135 case hasCharm: 136 return errors.New("unexpected charm URL") 137 case hasActionId: 138 return errors.New("unexpected action id") 139 } 140 case Continue: 141 // TODO(jw4) LP-1438489 142 // ModeContinue should no longer have a Hook, but until the upgrade is 143 // fixed we can't fail the validation if it does. 144 if hasHook { 145 logger.Errorf("unexpected hook info with Kind Continue") 146 } 147 switch { 148 case hasCharm: 149 return errors.New("unexpected charm URL") 150 case hasActionId: 151 return errors.New("unexpected action id") 152 } 153 default: 154 return errors.Errorf("unknown operation %q", st.Kind) 155 } 156 switch st.Step { 157 case Queued, Pending, Done: 158 default: 159 return errors.Errorf("unknown operation step %q", st.Step) 160 } 161 if hasHook { 162 return st.Hook.Validate() 163 } 164 return nil 165 } 166 167 func (st State) CollectedMetricsAt() time.Time { 168 return time.Unix(st.CollectMetricsTime, 0) 169 } 170 171 // stateChange is useful for a variety of Operation implementations. 172 type stateChange struct { 173 Kind Kind 174 Step Step 175 Hook *hook.Info 176 ActionId *string 177 CharmURL *charm.URL 178 HasRunStatusSet bool 179 } 180 181 func (change stateChange) apply(state State) *State { 182 state.Kind = change.Kind 183 state.Step = change.Step 184 state.Hook = change.Hook 185 state.ActionId = change.ActionId 186 state.CharmURL = change.CharmURL 187 state.StatusSet = state.StatusSet || change.HasRunStatusSet 188 return &state 189 } 190 191 // StateFile holds the disk state for a uniter. 192 type StateFile struct { 193 path string 194 } 195 196 // NewStateFile returns a new StateFile using path. 197 func NewStateFile(path string) *StateFile { 198 return &StateFile{path} 199 } 200 201 // Read reads a State from the file. If the file does not exist it returns 202 // ErrNoStateFile. 203 func (f *StateFile) Read() (*State, error) { 204 var st State 205 if err := utils.ReadYaml(f.path, &st); err != nil { 206 if os.IsNotExist(err) { 207 return nil, ErrNoStateFile 208 } 209 } 210 if err := st.validate(); err != nil { 211 return nil, errors.Errorf("cannot read %q: %v", f.path, err) 212 } 213 return &st, nil 214 } 215 216 // Write stores the supplied state to the file. 217 func (f *StateFile) Write(st *State) error { 218 if err := st.validate(); err != nil { 219 panic(err) 220 } 221 return utils.WriteYaml(f.path, st) 222 }