Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(4)

Delta Between Two Patch Sets: state/machine.go

Issue 54230044: state: deprecate JobManageState
Left Patch Set: state: deprecate JobManageState Created 11 years, 3 months ago
Right Patch Set: state: deprecate JobManageState Created 11 years, 3 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « state/cleanup_test.go ('k') | state/machine_test.go » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 // Copyright 2012, 2013 Canonical Ltd. 1 // Copyright 2012, 2013 Canonical Ltd.
2 // Licensed under the AGPLv3, see LICENCE file for details. 2 // Licensed under the AGPLv3, see LICENCE file for details.
3 3
4 package state 4 package state
5 5
6 import ( 6 import (
7 "fmt" 7 "fmt"
8 "strings" 8 "strings"
9 "time" 9 "time"
10 10
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
142 142
143 // globalKey returns the global database key for the machine. 143 // globalKey returns the global database key for the machine.
144 func (m *Machine) globalKey() string { 144 func (m *Machine) globalKey() string {
145 return machineGlobalKey(m.doc.Id) 145 return machineGlobalKey(m.doc.Id)
146 } 146 }
147 147
148 // instanceData holds attributes relevant to a provisioned machine. 148 // instanceData holds attributes relevant to a provisioned machine.
149 type instanceData struct { 149 type instanceData struct {
150 Id string `bson:"_id"` 150 Id string `bson:"_id"`
151 InstanceId instance.Id `bson:"instanceid"` 151 InstanceId instance.Id `bson:"instanceid"`
152 Status string `bson:"status,omitempty"`
152 Arch *string `bson:"arch,omitempty"` 153 Arch *string `bson:"arch,omitempty"`
153 Mem *uint64 `bson:"mem,omitempty"` 154 Mem *uint64 `bson:"mem,omitempty"`
154 RootDisk *uint64 `bson:"rootdisk,omitempty"` 155 RootDisk *uint64 `bson:"rootdisk,omitempty"`
155 CpuCores *uint64 `bson:"cpucores,omitempty"` 156 CpuCores *uint64 `bson:"cpucores,omitempty"`
156 CpuPower *uint64 `bson:"cpupower,omitempty"` 157 CpuPower *uint64 `bson:"cpupower,omitempty"`
157 Tags *[]string `bson:"tags,omitempty"` 158 Tags *[]string `bson:"tags,omitempty"`
158 } 159 }
159 160
160 // TODO(wallyworld): move this method to a service. 161 // TODO(wallyworld): move this method to a service.
161 func (m *Machine) HardwareCharacteristics() (*instance.HardwareCharacteristics, error) { 162 func (m *Machine) HardwareCharacteristics() (*instance.HardwareCharacteristics, error) {
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
195 // Life returns whether the machine is Alive, Dying or Dead. 196 // Life returns whether the machine is Alive, Dying or Dead.
196 func (m *Machine) Life() Life { 197 func (m *Machine) Life() Life {
197 return m.doc.Life 198 return m.doc.Life
198 } 199 }
199 200
200 // Jobs returns the responsibilities that must be fulfilled by m's agent. 201 // Jobs returns the responsibilities that must be fulfilled by m's agent.
201 func (m *Machine) Jobs() []MachineJob { 202 func (m *Machine) Jobs() []MachineJob {
202 return m.doc.Jobs 203 return m.doc.Jobs
203 } 204 }
204 205
205 // IsManager returns true if the machine has JJobManageEnviron. 206 // IsManager returns true if the machine has JJobManageEnviron.
mue 2014/01/24 13:05:51 s/JJobManageEnviron/JobManageEnviron
rog 2014/01/24 13:10:35 I've fixed that in a downstream branch; will leave
dimitern 2014/01/24 13:31:38 As long as it doesn't land like this eventually..
206 func (m *Machine) IsManager() bool { 207 func (m *Machine) IsManager() bool {
207 for _, job := range m.doc.Jobs { 208 for _, job := range m.doc.Jobs {
208 switch job { 209 switch job {
209 case JobManageEnviron: 210 case JobManageEnviron:
210 return true 211 return true
211 } 212 }
212 } 213 }
213 return false 214 return false
214 } 215 }
215 216
(...skipping 388 matching lines...) Expand 10 before | Expand all | Expand 10 after
604 // InstanceId returns the provider specific instance id for this 605 // InstanceId returns the provider specific instance id for this
605 // machine, or a NotProvisionedError, if not set. 606 // machine, or a NotProvisionedError, if not set.
606 func (m *Machine) InstanceId() (instance.Id, error) { 607 func (m *Machine) InstanceId() (instance.Id, error) {
607 // SCHEMACHANGE 608 // SCHEMACHANGE
608 // TODO(wallyworld) - remove this backward compatibility code when schem a upgrades are possible 609 // TODO(wallyworld) - remove this backward compatibility code when schem a upgrades are possible
609 // (we first check for InstanceId stored on the machineDoc) 610 // (we first check for InstanceId stored on the machineDoc)
610 if m.doc.InstanceId != "" { 611 if m.doc.InstanceId != "" {
611 return m.doc.InstanceId, nil 612 return m.doc.InstanceId, nil
612 } 613 }
613 instData, err := getInstanceData(m.st, m.Id()) 614 instData, err := getInstanceData(m.st, m.Id())
614 » if (err == nil && instData.InstanceId == "") || (err != nil && errors.Is NotFoundError(err)) { 615 » if (err == nil && instData.InstanceId == "") || errors.IsNotFoundError(e rr) {
615 err = NotProvisionedError(m.Id()) 616 err = NotProvisionedError(m.Id())
616 } 617 }
617 if err != nil { 618 if err != nil {
618 return "", err 619 return "", err
619 } 620 }
620 return instData.InstanceId, nil 621 return instData.InstanceId, nil
622 }
623
624 // InstanceStatus returns the provider specific instance status for this machine ,
625 // or a NotProvisionedError if instance is not yet provisioned.
626 func (m *Machine) InstanceStatus() (string, error) {
627 // SCHEMACHANGE
628 // InstanceId may not be stored in the instanceData doc, so we
629 // get it using an API on machine which knows to look in the old
630 // place if necessary.
631 instId, err := m.InstanceId()
632 if err != nil {
633 return "", err
634 }
635 instData, err := getInstanceData(m.st, m.Id())
636 if (err == nil && instId == "") || errors.IsNotFoundError(err) {
637 err = NotProvisionedError(m.Id())
638 }
639 if err != nil {
640 return "", err
641 }
642 return instData.Status, nil
643 }
644
645 // SetInstanceStatus sets the provider specific instance status for a machine.
646 func (m *Machine) SetInstanceStatus(status string) (err error) {
647 defer utils.ErrorContextf(&err, "cannot set instance status for machine %q", m)
648
649 // SCHEMACHANGE - we can't do this yet until the schema is updated
650 // so just do a txn.DocExists for now.
651 // provisioned := D{{"instanceid", D{{"$ne", ""}}}}
652 ops := []txn.Op{
653 {
654 C: m.st.instanceData.Name,
655 Id: m.doc.Id,
656 Assert: txn.DocExists,
657 Update: D{{"$set", D{{"status", status}}}},
658 },
659 }
660
661 if err = m.st.runTransaction(ops); err == nil {
662 return nil
663 } else if err != txn.ErrAborted {
664 return err
665 }
666 return NotProvisionedError(m.Id())
621 } 667 }
622 668
623 // Units returns all the units that have been assigned to the machine. 669 // Units returns all the units that have been assigned to the machine.
624 func (m *Machine) Units() (units []*Unit, err error) { 670 func (m *Machine) Units() (units []*Unit, err error) {
625 defer utils.ErrorContextf(&err, "cannot get units assigned to machine %v ", m) 671 defer utils.ErrorContextf(&err, "cannot get units assigned to machine %v ", m)
626 pudocs := []unitDoc{} 672 pudocs := []unitDoc{}
627 err = m.st.units.Find(D{{"machineid", m.doc.Id}}).All(&pudocs) 673 err = m.st.units.Find(D{{"machineid", m.doc.Id}}).All(&pudocs)
628 if err != nil { 674 if err != nil {
629 return nil, err 675 return nil, err
630 } 676 }
(...skipping 22 matching lines...) Expand all
653 func (m *Machine) SetProvisioned(id instance.Id, nonce string, characteristics * instance.HardwareCharacteristics) (err error) { 699 func (m *Machine) SetProvisioned(id instance.Id, nonce string, characteristics * instance.HardwareCharacteristics) (err error) {
654 defer utils.ErrorContextf(&err, "cannot set instance data for machine %q ", m) 700 defer utils.ErrorContextf(&err, "cannot set instance data for machine %q ", m)
655 701
656 if id == "" || nonce == "" { 702 if id == "" || nonce == "" {
657 return fmt.Errorf("instance id and nonce cannot be empty") 703 return fmt.Errorf("instance id and nonce cannot be empty")
658 } 704 }
659 705
660 if characteristics == nil { 706 if characteristics == nil {
661 characteristics = &instance.HardwareCharacteristics{} 707 characteristics = &instance.HardwareCharacteristics{}
662 } 708 }
663 » hc := &instanceData{ 709 » instData := &instanceData{
664 Id: m.doc.Id, 710 Id: m.doc.Id,
665 InstanceId: id, 711 InstanceId: id,
666 Arch: characteristics.Arch, 712 Arch: characteristics.Arch,
667 Mem: characteristics.Mem, 713 Mem: characteristics.Mem,
668 RootDisk: characteristics.RootDisk, 714 RootDisk: characteristics.RootDisk,
669 CpuCores: characteristics.CpuCores, 715 CpuCores: characteristics.CpuCores,
670 CpuPower: characteristics.CpuPower, 716 CpuPower: characteristics.CpuPower,
671 Tags: characteristics.Tags, 717 Tags: characteristics.Tags,
672 } 718 }
673 // SCHEMACHANGE 719 // SCHEMACHANGE
674 // TODO(wallyworld) - do not check instanceId on machineDoc after schema is upgraded 720 // TODO(wallyworld) - do not check instanceId on machineDoc after schema is upgraded
675 notSetYet := D{{"instanceid", ""}, {"nonce", ""}} 721 notSetYet := D{{"instanceid", ""}, {"nonce", ""}}
676 ops := []txn.Op{ 722 ops := []txn.Op{
677 { 723 {
678 C: m.st.machines.Name, 724 C: m.st.machines.Name,
679 Id: m.doc.Id, 725 Id: m.doc.Id,
680 Assert: append(isAliveDoc, notSetYet...), 726 Assert: append(isAliveDoc, notSetYet...),
681 Update: D{{"$set", D{{"instanceid", id}, {"nonce", nonce }}}}, 727 Update: D{{"$set", D{{"instanceid", id}, {"nonce", nonce }}}},
682 }, { 728 }, {
683 C: m.st.instanceData.Name, 729 C: m.st.instanceData.Name,
684 Id: m.doc.Id, 730 Id: m.doc.Id,
685 Assert: txn.DocMissing, 731 Assert: txn.DocMissing,
686 » » » Insert: hc, 732 » » » Insert: instData,
687 }, 733 },
688 } 734 }
689 735
690 if err = m.st.runTransaction(ops); err == nil { 736 if err = m.st.runTransaction(ops); err == nil {
691 m.doc.Nonce = nonce 737 m.doc.Nonce = nonce
692 // SCHEMACHANGE 738 // SCHEMACHANGE
693 // TODO(wallyworld) - remove this backward compatibility code wh en schema upgrades are possible 739 // TODO(wallyworld) - remove this backward compatibility code wh en schema upgrades are possible
694 // (InstanceId is stored on the instanceData document but we dup licate the value on the machineDoc. 740 // (InstanceId is stored on the instanceData document but we dup licate the value on the machineDoc.
695 m.doc.InstanceId = id 741 m.doc.InstanceId = id
696 return nil 742 return nil
(...skipping 274 matching lines...) Expand 10 before | Expand all | Expand 10 after
971 containerType := ContainerTypeFromId(containerId ) 1017 containerType := ContainerTypeFromId(containerId )
972 container.SetStatus( 1018 container.SetStatus(
973 params.StatusError, "unsupported contain er", params.StatusData{"type": containerType}) 1019 params.StatusError, "unsupported contain er", params.StatusData{"type": containerType})
974 } else { 1020 } else {
975 logger.Errorf("unsupported container %v has unex pected status %v", containerId, status) 1021 logger.Errorf("unsupported container %v has unex pected status %v", containerId, status)
976 } 1022 }
977 } 1023 }
978 } 1024 }
979 return nil 1025 return nil
980 } 1026 }
LEFTRIGHT

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b