func (context *statusContext) processUnit(unit *state.Unit, serviceCharm string) (status api.UnitStatus) { status.PublicAddress, _ = unit.PublicAddress() for _, port := range unit.OpenedPorts() { status.OpenedPorts = append(status.OpenedPorts, port.String()) } if unit.IsPrincipal() { status.Machine, _ = unit.AssignedMachineId() } curl, _ := unit.CharmURL() if serviceCharm != "" && curl != nil && curl.String() != serviceCharm { status.Charm = curl.String() } status.Agent, status.AgentState, status.AgentStateInfo = processAgent(unit) status.AgentVersion = status.Agent.Version status.Life = status.Agent.Life status.Err = status.Agent.Err if subUnits := unit.SubordinateNames(); len(subUnits) > 0 { status.Subordinates = make(map[string]api.UnitStatus) for _, name := range subUnits { subUnit := context.unitByName(name) // subUnit may be nil if subordinate was filtered out. if subUnit != nil { status.Subordinates[name] = context.processUnit(subUnit, serviceCharm) } } } return }
func (context *statusContext) processUnit(unit *state.Unit, serviceCharm string) params.UnitStatus { var result params.UnitStatus addr, err := unit.PublicAddress() if err != nil { // Usually this indicates that no addresses have been set on the // machine yet. addr = network.Address{} logger.Debugf("error fetching public address: %v", err) } result.PublicAddress = addr.Value unitPorts, _ := unit.OpenedPorts() for _, port := range unitPorts { result.OpenedPorts = append(result.OpenedPorts, port.String()) } if unit.IsPrincipal() { result.Machine, _ = unit.AssignedMachineId() } curl, _ := unit.CharmURL() if serviceCharm != "" && curl != nil && curl.String() != serviceCharm { result.Charm = curl.String() } processUnitAndAgentStatus(unit, &result) if subUnits := unit.SubordinateNames(); len(subUnits) > 0 { result.Subordinates = make(map[string]params.UnitStatus) for _, name := range subUnits { subUnit := context.unitByName(name) // subUnit may be nil if subordinate was filtered out. if subUnit != nil { result.Subordinates[name] = context.processUnit(subUnit, serviceCharm) } } } return result }
func (s *MachineSuite) waitProvisioned(c *gc.C, unit *state.Unit) (*state.Machine, instance.Id) { c.Logf("waiting for unit %q to be provisioned", unit) machineId, err := unit.AssignedMachineId() c.Assert(err, gc.IsNil) m, err := s.State.Machine(machineId) c.Assert(err, gc.IsNil) w := m.Watch() defer w.Stop() timeout := time.After(coretesting.LongWait) for { select { case <-timeout: c.Fatalf("timed out waiting for provisioning") case _, ok := <-w.Changes(): c.Assert(ok, jc.IsTrue) err := m.Refresh() c.Assert(err, gc.IsNil) if instId, err := m.InstanceId(); err == nil { c.Logf("unit provisioned with instance %s", instId) return m, instId } else { c.Check(err, jc.Satisfies, state.IsNotProvisionedError) } } } }
func (s *AssignSuite) assertAssignedUnit(c *gc.C, unit *state.Unit) string { // Get service networks. service, err := unit.Service() c.Assert(err, jc.ErrorIsNil) serviceNetworks, err := service.Networks() c.Assert(err, jc.ErrorIsNil) serviceCons, err := service.Constraints() c.Assert(err, jc.ErrorIsNil) // Check the machine on the unit is set. machineId, err := unit.AssignedMachineId() c.Assert(err, jc.ErrorIsNil) // Check that the principal is set on the machine. machine, err := s.State.Machine(machineId) c.Assert(err, jc.ErrorIsNil) machineCons, err := machine.Constraints() c.Assert(err, jc.ErrorIsNil) machineNetworks, err := machine.RequestedNetworks() c.Assert(err, jc.ErrorIsNil) c.Assert(machineNetworks, gc.DeepEquals, serviceNetworks) c.Assert(serviceCons.IncludeNetworks(), gc.DeepEquals, machineCons.IncludeNetworks()) c.Assert(serviceCons.ExcludeNetworks(), gc.DeepEquals, machineCons.ExcludeNetworks()) machineUnits, err := machine.Units() c.Assert(err, jc.ErrorIsNil) c.Assert(machineUnits, gc.HasLen, 1) // Make sure it is the right unit. c.Assert(machineUnits[0].Name(), gc.Equals, unit.Name()) return machineId }
func (context *statusContext) processUnit(unit *state.Unit, serviceCharm string) api.UnitStatus { var result api.UnitStatus result.PublicAddress, _ = unit.PublicAddress() unitPorts, _ := unit.OpenedPorts() for _, port := range unitPorts { result.OpenedPorts = append(result.OpenedPorts, port.String()) } if unit.IsPrincipal() { result.Machine, _ = unit.AssignedMachineId() } curl, _ := unit.CharmURL() if serviceCharm != "" && curl != nil && curl.String() != serviceCharm { result.Charm = curl.String() } processUnitAndAgentStatus(unit, &result) if subUnits := unit.SubordinateNames(); len(subUnits) > 0 { result.Subordinates = make(map[string]api.UnitStatus) for _, name := range subUnits { subUnit := context.unitByName(name) // subUnit may be nil if subordinate was filtered out. if subUnit != nil { result.Subordinates[name] = context.processUnit(subUnit, serviceCharm) } } } return result }
func (s *SSHCommonSuite) getMachineForUnit(c *gc.C, u *state.Unit) *state.Machine { machineId, err := u.AssignedMachineId() c.Assert(err, jc.ErrorIsNil) m, err := s.State.Machine(machineId) c.Assert(err, jc.ErrorIsNil) return m }
func (s *DeployLocalSuite) assertAssignedUnit(c *gc.C, u *state.Unit, mId string, cons constraints.Value) { id, err := u.AssignedMachineId() c.Assert(err, jc.ErrorIsNil) machine, err := s.State.Machine(id) c.Assert(err, jc.ErrorIsNil) machineCons, err := machine.Constraints() c.Assert(err, jc.ErrorIsNil) c.Assert(machineCons, gc.DeepEquals, cons) }
func (s *ConnSuite) assertAssignedMachineRequestedNetworks(c *gc.C, unit *state.Unit, includeNets, excludeNets []string) { machineId, err := unit.AssignedMachineId() c.Assert(err, gc.IsNil) machine, err := s.conn.State.Machine(machineId) c.Assert(err, gc.IsNil) networks, err := machine.RequestedNetworks() c.Assert(err, gc.IsNil) c.Assert(networks, jc.DeepEquals, includeNets) cons, err := machine.Constraints() c.Assert(err, gc.IsNil) c.Assert(cons.ExcludeNetworks(), jc.DeepEquals, excludeNets) }
func (s *AssignSuite) assertAssignedUnit(c *gc.C, unit *state.Unit) string { // Check the machine on the unit is set. machineId, err := unit.AssignedMachineId() c.Assert(err, jc.ErrorIsNil) // Check that the principal is set on the machine. machine, err := s.State.Machine(machineId) c.Assert(err, jc.ErrorIsNil) machineUnits, err := machine.Units() c.Assert(err, jc.ErrorIsNil) c.Assert(machineUnits, gc.HasLen, 1) // Make sure it is the right unit. c.Assert(machineUnits[0].Name(), gc.Equals, unit.Name()) return machineId }
func (s *storageAddSuite) assignUnit(c *gc.C, u *state.Unit) { // Assign unit to machine to get volumes and filesystems err := s.State.AssignUnit(u, state.AssignCleanEmpty) c.Assert(err, jc.ErrorIsNil) machineId, err := u.AssignedMachineId() c.Assert(err, jc.ErrorIsNil) m, err := s.State.Machine(machineId) c.Assert(err, jc.ErrorIsNil) s.machineTag = m.MachineTag() volumes, err := s.State.AllVolumes() c.Assert(err, jc.ErrorIsNil) s.originalVolumeCount = len(volumes) filesystems, err := s.State.MachineFilesystemAttachments(s.machineTag) c.Assert(err, jc.ErrorIsNil) s.originalFilesystemCount = len(filesystems) }
func (s *UnitSuite) setAssignedMachineAddresses(c *gc.C, u *state.Unit) { err := u.AssignToNewMachine() c.Assert(err, jc.ErrorIsNil) mid, err := u.AssignedMachineId() c.Assert(err, jc.ErrorIsNil) machine, err := s.State.Machine(mid) c.Assert(err, jc.ErrorIsNil) err = machine.SetProvisioned("i-exist", "fake_nonce", nil) c.Assert(err, jc.ErrorIsNil) err = machine.SetProviderAddresses(network.Address{ Type: network.IPv4Address, Scope: network.ScopeCloudLocal, Value: "private.address.example.com", }, network.Address{ Type: network.IPv4Address, Scope: network.ScopePublic, Value: "public.address.example.com", }) c.Assert(err, jc.ErrorIsNil) }
// GetAssignedMachine returns the assigned machine tag (if any) for // each given unit. func (f *FirewallerAPI) GetAssignedMachine(args params.Entities) (params.StringResults, error) { result := params.StringResults{ Results: make([]params.StringResult, len(args.Entities)), } canAccess, err := f.accessUnit() if err != nil { return params.StringResults{}, err } for i, entity := range args.Entities { var unit *state.Unit unit, err = f.getUnit(canAccess, entity.Tag) if err == nil { var machineId string machineId, err = unit.AssignedMachineId() if err == nil { result.Results[i].Result = names.NewMachineTag(machineId).String() } } result.Results[i].Error = common.ServerError(err) } return result, nil }
func (s *VolumeStateSuite) assertMachineVolume(c *gc.C, unit *state.Unit) { assignedMachineId, err := unit.AssignedMachineId() c.Assert(err, jc.ErrorIsNil) storageAttachments, err := s.State.UnitStorageAttachments(unit.UnitTag()) c.Assert(err, jc.ErrorIsNil) c.Assert(storageAttachments, gc.HasLen, 1) storageInstance, err := s.State.StorageInstance(storageAttachments[0].StorageInstance()) c.Assert(err, jc.ErrorIsNil) volume := s.storageInstanceVolume(c, storageInstance.StorageTag()) c.Assert(volume.VolumeTag(), gc.Equals, names.NewVolumeTag("0/0")) volumeStorageTag, err := volume.StorageInstance() c.Assert(err, jc.ErrorIsNil) c.Assert(volumeStorageTag, gc.Equals, storageInstance.StorageTag()) _, err = volume.Info() c.Assert(err, jc.Satisfies, errors.IsNotProvisioned) _, ok := volume.Params() c.Assert(ok, jc.IsTrue) machine, err := s.State.Machine(assignedMachineId) c.Assert(err, jc.ErrorIsNil) volumeAttachments, err := s.State.MachineVolumeAttachments(machine.MachineTag()) c.Assert(err, jc.ErrorIsNil) c.Assert(volumeAttachments, gc.HasLen, 1) c.Assert(volumeAttachments[0].Volume(), gc.Equals, volume.VolumeTag()) c.Assert(volumeAttachments[0].Machine(), gc.Equals, machine.MachineTag()) _, err = volumeAttachments[0].Info() c.Assert(err, jc.Satisfies, errors.IsNotProvisioned) _, ok = volumeAttachments[0].Params() c.Assert(ok, jc.IsTrue) _, err = s.State.VolumeAttachment(machine.MachineTag(), volume.VolumeTag()) c.Assert(err, jc.ErrorIsNil) assertMachineStorageRefs(c, s.State, machine.MachineTag()) }
// newUniterBaseAPI creates a new instance of the uniter base API. func newUniterBaseAPI(st *state.State, resources *common.Resources, authorizer common.Authorizer) (*uniterBaseAPI, error) { if !authorizer.AuthUnitAgent() { return nil, common.ErrPerm } var unit *state.Unit var err error switch tag := authorizer.GetAuthTag().(type) { case names.UnitTag: unit, err = st.Unit(tag.Id()) if err != nil { return nil, errors.Trace(err) } default: return nil, errors.Errorf("expected names.UnitTag, got %T", tag) } accessUnit := func() (common.AuthFunc, error) { return authorizer.AuthOwner, nil } accessService := func() (common.AuthFunc, error) { switch tag := authorizer.GetAuthTag().(type) { case names.UnitTag: entity, err := st.Unit(tag.Id()) if err != nil { return nil, errors.Trace(err) } serviceName := entity.ServiceName() serviceTag := names.NewServiceTag(serviceName) return func(tag names.Tag) bool { return tag == serviceTag }, nil default: return nil, errors.Errorf("expected names.UnitTag, got %T", tag) } } accessMachine := func() (common.AuthFunc, error) { machineId, err := unit.AssignedMachineId() if err != nil { return nil, errors.Trace(err) } machine, err := st.Machine(machineId) if err != nil { return nil, errors.Trace(err) } return func(tag names.Tag) bool { return tag == machine.Tag() }, nil } msAPI, err := meterstatus.NewMeterStatusAPI(st, resources, authorizer) if err != nil { return nil, errors.Annotate(err, "could not create meter status API handler") } accessUnitOrService := common.AuthEither(accessUnit, accessService) return &uniterBaseAPI{ LifeGetter: common.NewLifeGetter(st, accessUnitOrService), DeadEnsurer: common.NewDeadEnsurer(st, accessUnit), AgentEntityWatcher: common.NewAgentEntityWatcher(st, resources, accessUnitOrService), APIAddresser: common.NewAPIAddresser(st, resources), EnvironWatcher: common.NewEnvironWatcher(st, resources, authorizer), RebootRequester: common.NewRebootRequester(st, accessMachine), LeadershipSettingsAccessor: leadershipSettingsAccessorFactory(st, resources, authorizer), MeterStatus: msAPI, // TODO(fwereade): so *every* unit should be allowed to get/set its // own status *and* its service's? This is not a pleasing arrangement. StatusAPI: NewStatusAPI(st, accessUnitOrService), st: st, auth: authorizer, resources: resources, accessUnit: accessUnit, accessService: accessService, unit: unit, }, nil }
// newUniterBaseAPI creates a new instance of the uniter base API. func newUniterBaseAPI(st *state.State, resources *common.Resources, authorizer common.Authorizer) (*uniterBaseAPI, error) { if !authorizer.AuthUnitAgent() { return nil, common.ErrPerm } var unit *state.Unit var err error switch tag := authorizer.GetAuthTag().(type) { case names.UnitTag: unit, err = st.Unit(tag.Id()) if err != nil { return nil, errors.Trace(err) } default: return nil, errors.Errorf("expected names.UnitTag, got %T", tag) } accessUnit := func() (common.AuthFunc, error) { return authorizer.AuthOwner, nil } accessService := func() (common.AuthFunc, error) { switch tag := authorizer.GetAuthTag().(type) { case names.UnitTag: entity, err := st.Unit(tag.Id()) if err != nil { return nil, errors.Trace(err) } serviceName := entity.ServiceName() serviceTag := names.NewServiceTag(serviceName) return func(tag names.Tag) bool { return tag == serviceTag }, nil default: return nil, errors.Errorf("expected names.UnitTag, got %T", tag) } } accessMachine := func() (common.AuthFunc, error) { machineId, err := unit.AssignedMachineId() if err != nil { return nil, errors.Trace(err) } machine, err := st.Machine(machineId) if err != nil { return nil, errors.Trace(err) } return func(tag names.Tag) bool { return tag == machine.Tag() }, nil } accessUnitOrService := common.AuthEither(accessUnit, accessService) return &uniterBaseAPI{ LifeGetter: common.NewLifeGetter(st, accessUnitOrService), StatusAPI: NewStatusAPI(st, accessUnitOrService), DeadEnsurer: common.NewDeadEnsurer(st, accessUnit), AgentEntityWatcher: common.NewAgentEntityWatcher(st, resources, accessUnitOrService), APIAddresser: common.NewAPIAddresser(st, resources), EnvironWatcher: common.NewEnvironWatcher(st, resources, authorizer), RebootRequester: common.NewRebootRequester(st, accessMachine), LeadershipSettingsAccessor: leadershipSettingsAccessorFactory(st, resources, authorizer), st: st, auth: authorizer, resources: resources, accessUnit: accessUnit, accessService: accessService, unit: unit, }, nil }