// ModeConflicted is responsible for watching and responding to: // * user resolution of charm upgrade conflicts // * forced charm upgrade requests func ModeConflicted(sch *state.Charm) Mode { return func(u *Uniter) (next Mode, err error) { defer modeContext("ModeConflicted", &err)() if err = u.unit.SetStatus(state.UnitError, "upgrade failed"); err != nil { return nil, err } u.f.WantResolvedEvent() u.f.WantUpgradeEvent(sch.URL(), true) for { select { case <-u.tomb.Dying(): return nil, tomb.ErrDying case <-u.f.ResolvedEvents(): err = u.charm.Snapshotf("Upgrade conflict resolved.") if e := u.unit.ClearResolved(); e != nil { return nil, e } if err != nil { return nil, err } return ModeUpgrading(sch), nil case upgrade := <-u.f.UpgradeEvents(): if err := u.charm.Revert(); err != nil { return nil, err } return ModeUpgrading(upgrade), nil } } panic("unreachable") } }
// ModeInstalling is responsible for the initial charm deployment. func ModeInstalling(sch *state.Charm) Mode { name := fmt.Sprintf("ModeInstalling %s", sch.URL()) return func(u *Uniter) (next Mode, err error) { defer modeContext(name, &err)() if err = u.deploy(sch, Install); err != nil { return nil, err } return ModeContinue, nil } }
// deploy deploys the supplied charm, and sets follow-up hook operation state // as indicated by reason. func (u *Uniter) deploy(sch *state.Charm, 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 } url := sch.URL() if u.s == nil || u.s.OpStep != Done { log.Printf("worker/uniter: fetching charm %q", url) bun, err := u.bundles.Read(sch, u.tomb.Dying()) if err != nil { return err } if err = u.deployer.Stage(bun, url); err != nil { return err } log.Printf("worker/uniter: deploying charm %q", url) if err = u.writeState(reason, Pending, hi, url); err != nil { return err } if err = u.deployer.Deploy(u.charm); err != nil { return err } if err = u.writeState(reason, Done, hi, url); err != nil { return err } } log.Printf("worker/uniter: charm %q is deployed", url) if err := u.unit.SetCharm(sch); err != nil { return err } 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 = hook.Install case Upgrade: hi.Kind = hook.UpgradeCharm } } return u.writeState(RunHook, status, hi, nil) }
// ModeUpgrading is responsible for upgrading the charm. func ModeUpgrading(sch *state.Charm) Mode { name := fmt.Sprintf("ModeUpgrading %s", sch.URL()) return func(u *Uniter) (next Mode, err error) { defer modeContext(name, &err)() if err = u.deploy(sch, Upgrade); err == charm.ErrConflict { return ModeConflicted(sch), nil } else if err != nil { return nil, err } return ModeContinue, nil } }
func assertCustomCharm(c *C, ch *state.Charm, series string, meta *charm.Meta, config *charm.Config, revision int) { // Check Charm interface method results. c.Assert(ch.Meta(), DeepEquals, meta) c.Assert(ch.Config(), DeepEquals, config) c.Assert(ch.Revision(), DeepEquals, revision) // Test URL matches charm and expected series. url := ch.URL() c.Assert(url.Series, Equals, series) c.Assert(url.Revision, Equals, ch.Revision()) // Ignore the BundleURL and BundleSHA256 methods, they're irrelevant. }
func (s *ServiceSuite) TestSetCharmConfig(c *C) { charms := map[string]*state.Charm{ stringConfig: s.AddConfigCharm(c, "wordpress", stringConfig, 1), emptyConfig: s.AddConfigCharm(c, "wordpress", emptyConfig, 2), floatConfig: s.AddConfigCharm(c, "wordpress", floatConfig, 3), newStringConfig: s.AddConfigCharm(c, "wordpress", newStringConfig, 4), } for i, t := range setCharmConfigTests { c.Logf("test %d: %s", i, t.summary) origCh := charms[t.startconfig] svc, err := s.State.AddService("wordpress", origCh) c.Assert(err, IsNil) err = svc.UpdateConfigSettings(t.startvalues) c.Assert(err, IsNil) newCh := charms[t.endconfig] err = svc.SetCharm(newCh, false) var expectVals charm.Settings var expectCh *state.Charm if t.err != "" { c.Assert(err, ErrorMatches, t.err) expectCh = origCh expectVals = t.startvalues } else { c.Assert(err, IsNil) expectCh = newCh expectVals = t.endvalues } sch, _, err := svc.Charm() c.Assert(err, IsNil) c.Assert(sch.URL(), DeepEquals, expectCh.URL()) settings, err := svc.ConfigSettings() c.Assert(err, IsNil) if len(expectVals) == 0 { c.Assert(settings, HasLen, 0) } else { c.Assert(settings, DeepEquals, expectVals) } err = svc.Destroy() c.Assert(err, IsNil) } }
// download fetches the supplied charm and checks that it has the correct sha256 // hash, then copies it into the directory. If a value is received on abort, the // download will be stopped. func (d *BundlesDir) download(sch *state.Charm, abort <-chan struct{}) (err error) { defer trivial.ErrorContextf(&err, "failed to download charm %q from %q", sch.URL(), sch.BundleURL()) dir := d.downloadsPath() if err := os.MkdirAll(dir, 0755); err != nil { return err } burl := sch.BundleURL().String() log.Printf("worker/uniter/charm: downloading %s from %s", sch.URL(), burl) dl := downloader.New(burl, dir) defer dl.Stop() for { select { case <-abort: log.Printf("worker/uniter/charm: download aborted") return fmt.Errorf("aborted") case st := <-dl.Done(): if st.Err != nil { return st.Err } log.Printf("worker/uniter/charm: download complete") defer st.File.Close() hash := sha256.New() if _, err = io.Copy(hash, st.File); err != nil { return err } actualSha256 := hex.EncodeToString(hash.Sum(nil)) if actualSha256 != sch.BundleSha256() { return fmt.Errorf( "expected sha256 %q, got %q", sch.BundleSha256(), actualSha256, ) } log.Printf("worker/uniter/charm: download verified") if err := os.MkdirAll(d.path, 0755); err != nil { return err } return os.Rename(st.File.Name(), d.bundlePath(sch)) } } panic("unreachable") }
// AddService creates a new service with the given name to run the given // charm. If svcName is empty, the charm name will be used. func (conn *Conn) AddService(name string, ch *state.Charm) (*state.Service, error) { if name == "" { name = ch.URL().Name // TODO ch.Meta().Name ? } svc, err := conn.State.AddService(name, ch) if err != nil { return nil, err } meta := ch.Meta() for rname, rel := range meta.Peers { ep := state.Endpoint{ name, rel.Interface, rname, state.RolePeer, rel.Scope, } if _, err := conn.State.AddRelation(ep); err != nil { return nil, fmt.Errorf("cannot add peer relation %q to service %q: %v", rname, name, err) } } return svc, nil }
// bundlePath returns the path to the location where the verified charm // bundle identified by sch will be, or has been, saved. func (d *BundlesDir) bundlePath(sch *state.Charm) string { return filepath.Join(d.path, charm.Quote(sch.URL().String())) }