func (s *storageResolver) nextHookOp( tag names.StorageTag, snap remotestate.StorageSnapshot, opFactory operation.Factory, ) (operation.Operation, error) { logger.Debugf("next hook op for %v: %+v", tag, snap) if !snap.Attached { return nil, resolver.ErrNoOperation } storageAttachment, ok := s.storage.storageAttachments[tag] if !ok { return nil, resolver.ErrNoOperation } switch snap.Life { case params.Alive: if storageAttachment.attached { // Storage attachments currently do not change // (apart from lifecycle) after being provisioned. // We don't process unprovisioned storage here, // so there's nothing to do. return nil, resolver.ErrNoOperation } case params.Dying: if !storageAttachment.attached { // Nothing to do: attachment is dying, but // the storage-attached hook has not been // consumed. return nil, resolver.ErrNoOperation } case params.Dead: // Storage must have been Dying to become Dead; // no further action is required. return nil, resolver.ErrNoOperation } hookInfo := hook.Info{ StorageId: tag.Id(), } if snap.Life == params.Alive { hookInfo.Kind = hooks.StorageAttached } else { hookInfo.Kind = hooks.StorageDetaching } context := &contextStorage{ tag: tag, kind: storage.StorageKind(snap.Kind), location: snap.Location, } storageAttachment.ContextStorageAttachment = context s.storage.storageAttachments[tag] = storageAttachment logger.Debugf("queued hook: %v", hookInfo) return opFactory.NewRunHook(hookInfo) }
// Commit updates relation state to include the fact of the hook's execution, // records the impact of start and collect-metrics hooks, and queues follow-up // config-changed hooks to directly follow install and upgrade-charm hooks. // Commit is part of the Operation interface. func (rh *runHook) Commit(state State) (*State, error) { if err := rh.callbacks.CommitHook(rh.info); err != nil { return nil, err } change := stateChange{ Kind: Continue, Step: Pending, } var hi *hook.Info = &hook.Info{Kind: hooks.ConfigChanged} switch rh.info.Kind { case hooks.ConfigChanged: if state.Started { break } hi.Kind = hooks.Start fallthrough case hooks.UpgradeCharm: change = stateChange{ Kind: RunHook, Step: Queued, Hook: hi, } } newState := change.apply(state) switch rh.info.Kind { case hooks.Start: newState.Started = true case hooks.Stop: newState.Stopped = true case hooks.CollectMetrics: newState.CollectMetricsTime = time.Now().Unix() case hooks.UpdateStatus: newState.UpdateStatusTime = time.Now().Unix() } return newState, nil }
func (s *storageResolver) nextHookOp( tag names.StorageTag, snap remotestate.StorageSnapshot, opFactory operation.Factory, ) (operation.Operation, error) { logger.Debugf("next hook op for %v: %+v", tag, snap) if snap.Life == params.Dead { // Storage must have been Dying to become Dead; // no further action is required. return nil, resolver.ErrNoOperation } hookInfo := hook.Info{StorageId: tag.Id()} switch snap.Life { case params.Alive: storageAttachment, ok := s.storage.storageAttachments[tag] if ok && storageAttachment.attached { // Once the storage is attached, we only care about // lifecycle state changes. return nil, resolver.ErrNoOperation } // The storage-attached hook has not been committed, so add the // storage to the pending set. s.storage.pending.Add(tag) if !snap.Attached { // The storage attachment has not been provisioned yet, // so just ignore it for now. We'll be notified again // when it has been provisioned. return nil, resolver.ErrNoOperation } // The storage is alive, but we haven't previously run the // "storage-attached" hook. Do so now. hookInfo.Kind = hooks.StorageAttached case params.Dying: storageAttachment, ok := s.storage.storageAttachments[tag] if !ok || !storageAttachment.attached { // Nothing to do: attachment is dying, but // the storage-attached hook has not been // issued. return nil, resolver.ErrNoOperation } // The storage is dying, but we haven't previously run the // "storage-detached" hook. Do so now. hookInfo.Kind = hooks.StorageDetaching } // Update the local state to reflect what we're about to report // to a hook. stateFile, err := readStateFile(s.storage.storageStateDir, tag) if err != nil { return nil, errors.Trace(err) } s.storage.storageAttachments[tag] = storageAttachment{ stateFile, &contextStorage{ tag: tag, kind: storage.StorageKind(snap.Kind), location: snap.Location, }, } return opFactory.NewRunHook(hookInfo) }
// deploy deploys the supplied charm URL, and sets follow-up hook operation state // as indicated by reason. func (u *Uniter) deploy(curl *corecharm.URL, reason Op) error { if reason != Install && reason != Upgrade { panic(fmt.Errorf("%q is not a deploy operation", reason)) } var hi *hook.Info if u.s != nil && (u.s.Op == RunHook || u.s.Op == Upgrade) { // If this upgrade interrupts a RunHook, we need to preserve the hook // info so that we can return to the appropriate error state. However, // if we're resuming (or have force-interrupted) an Upgrade, we also // need to preserve whatever hook info was preserved when we initially // started upgrading, to ensure we still return to the correct state. hi = u.s.Hook } if u.s == nil || u.s.OpStep != Done { // Get the new charm bundle before announcing intention to use it. logger.Infof("fetching charm %q", curl) sch, err := u.st.Charm(curl) if err != nil { return err } if err = u.deployer.Stage(sch, u.tomb.Dying()); err != nil { return err } // Set the new charm URL - this returns when the operation is complete, // at which point we can refresh the local copy of the unit to get a // version with the correct charm URL, and can go ahead and deploy // the charm proper. if err := u.f.SetCharm(curl); err != nil { return err } if err := u.unit.Refresh(); err != nil { return err } logger.Infof("deploying charm %q", curl) if err = u.writeState(reason, Pending, hi, curl); err != nil { return err } if err = u.deployer.Deploy(); err != nil { return err } if err = u.writeState(reason, Done, hi, curl); err != nil { return err } } logger.Infof("charm %q is deployed", curl) status := Queued if hi != nil { // If a hook operation was interrupted, restore it. status = Pending } else { // Otherwise, queue the relevant post-deploy hook. hi = &hook.Info{} switch reason { case Install: hi.Kind = hooks.Install case Upgrade: hi.Kind = hooks.UpgradeCharm } } return u.writeState(RunHook, status, hi, nil) }