本文整理汇总了Golang中github.com/juju/juju/state.Service类的典型用法代码示例。如果您正苦于以下问题:Golang Service类的具体用法?Golang Service怎么用?Golang Service使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Service类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: assertMachines
func (s *DeployLocalSuite) assertMachines(c *gc.C, service *state.Service, expectCons constraints.Value, expectIds ...string) {
units, err := service.AllUnits()
c.Assert(err, jc.ErrorIsNil)
c.Assert(units, gc.HasLen, len(expectIds))
// first manually tell state to assign all the units
for _, unit := range units {
id := unit.Tag().Id()
res, err := s.State.AssignStagedUnits([]string{id})
c.Assert(err, jc.ErrorIsNil)
c.Assert(res[0].Error, jc.ErrorIsNil)
c.Assert(res[0].Unit, gc.Equals, id)
}
// refresh the list of units from state
units, err = service.AllUnits()
c.Assert(err, jc.ErrorIsNil)
c.Assert(units, gc.HasLen, len(expectIds))
unseenIds := set.NewStrings(expectIds...)
for _, unit := range units {
id, err := unit.AssignedMachineId()
c.Assert(err, jc.ErrorIsNil)
unseenIds.Remove(id)
machine, err := s.State.Machine(id)
c.Assert(err, jc.ErrorIsNil)
cons, err := machine.Constraints()
c.Assert(err, jc.ErrorIsNil)
c.Assert(cons, gc.DeepEquals, expectCons)
}
c.Assert(unseenIds, gc.DeepEquals, set.NewStrings())
}
示例2: charmModifiedVersion
func (u *UniterAPIV3) charmModifiedVersion(tagStr string, canAccess func(names.Tag) bool) (int, error) {
tag, err := names.ParseTag(tagStr)
if err != nil {
return -1, common.ErrPerm
}
if !canAccess(tag) {
return -1, common.ErrPerm
}
unitOrService, err := u.st.FindEntity(tag)
if err != nil {
return -1, err
}
var service *state.Service
switch entity := unitOrService.(type) {
case *state.Service:
service = entity
case *state.Unit:
service, err = entity.Service()
if err != nil {
return -1, err
}
default:
return -1, errors.BadRequestf("type %t does not have a CharmModifiedVersion", entity)
}
return service.CharmModifiedVersion(), nil
}
示例3: addUnit
func (s *runSuite) addUnit(c *gc.C, service *state.Service) *state.Unit {
unit, err := service.AddUnit()
c.Assert(err, jc.ErrorIsNil)
err = unit.AssignToNewMachine()
c.Assert(err, jc.ErrorIsNil)
return unit
}
示例4: AddUnits
// AddUnits starts n units of the given service using the specified placement
// directives to allocate the machines.
func AddUnits(st *state.State, svc *state.Service, n int, placement []*instance.Placement) ([]*state.Unit, error) {
units := make([]*state.Unit, n)
// Hard code for now till we implement a different approach.
policy := state.AssignCleanEmpty
// TODO what do we do if we fail half-way through this process?
for i := 0; i < n; i++ {
unit, err := svc.AddUnit()
if err != nil {
return nil, errors.Annotatef(err, "cannot add unit %d/%d to service %q", i+1, n, svc.Name())
}
// Are there still placement directives to use?
if i > len(placement)-1 {
if err := st.AssignUnit(unit, policy); err != nil {
return nil, errors.Trace(err)
}
units[i] = unit
continue
}
if err := st.AssignUnitWithPlacement(unit, placement[i]); err != nil {
return nil, errors.Annotatef(err, "adding new machine to host unit %q", unit.Name())
}
units[i] = unit
}
return units, nil
}
示例5: buildServiceMatcherShims
func buildServiceMatcherShims(s *state.Service, patterns ...string) (shims []closurePredicate, _ error) {
// Match on name.
shims = append(shims, func() (bool, bool, error) {
for _, p := range patterns {
if strings.ToLower(s.Name()) == strings.ToLower(p) {
return true, true, nil
}
}
return false, false, nil
})
// Match on exposure.
shims = append(shims, func() (bool, bool, error) { return matchExposure(patterns, s) })
// If the service has an unit instance that matches any of the
// given criteria, consider the service a match as well.
unitShims, err := buildShimsForUnit(s.AllUnits, patterns...)
if err != nil {
return nil, err
}
shims = append(shims, unitShims...)
// Units may be able to match the pattern. Ultimately defer to
// that logic, and guard against breaking the predicate-chain.
if len(unitShims) <= 0 {
shims = append(shims, func() (bool, bool, error) { return false, true, nil })
}
return shims, nil
}
示例6: serviceSetSettingsYAML
// serviceSetSettingsYAML updates the settings for the given service,
// taking the configuration from a YAML string.
func serviceSetSettingsYAML(service *state.Service, settings string) error {
b := []byte(settings)
var all map[string]interface{}
if err := goyaml.Unmarshal(b, &all); err != nil {
return errors.Annotate(err, "parsing settings data")
}
// The file is already in the right format.
if _, ok := all[service.Name()]; !ok {
changes, err := settingsFromGetYaml(all)
if err != nil {
return errors.Annotate(err, "processing YAML generated by get")
}
return errors.Annotate(service.UpdateConfigSettings(changes), "updating settings with service YAML")
}
ch, _, err := service.Charm()
if err != nil {
return errors.Annotate(err, "obtaining charm for this service")
}
changes, err := ch.Config().ParseSettingsYAML(b, service.Name())
if err != nil {
return errors.Annotate(err, "creating config from YAML")
}
return errors.Annotate(service.UpdateConfigSettings(changes), "updating settings")
}
示例7: AddUnit
func (s *ContextSuite) AddUnit(c *gc.C, svc *state.Service) *state.Unit {
unit, err := svc.AddUnit()
c.Assert(err, jc.ErrorIsNil)
if s.machine != nil {
err = unit.AssignToMachine(s.machine)
c.Assert(err, jc.ErrorIsNil)
return unit
}
err = s.State.AssignUnit(unit, state.AssignCleanEmpty)
c.Assert(err, jc.ErrorIsNil)
machineId, err := unit.AssignedMachineId()
c.Assert(err, jc.ErrorIsNil)
s.machine, err = s.State.Machine(machineId)
c.Assert(err, jc.ErrorIsNil)
zone := "a-zone"
hwc := instance.HardwareCharacteristics{
AvailabilityZone: &zone,
}
err = s.machine.SetProvisioned("i-exist", "fake_nonce", &hwc)
c.Assert(err, jc.ErrorIsNil)
name := strings.Replace(unit.Name(), "/", "-", 1)
privateAddr := network.NewScopedAddress(name+".testing.invalid", network.ScopeCloudLocal)
err = s.machine.SetProviderAddresses(privateAddr)
c.Assert(err, jc.ErrorIsNil)
return unit
}
示例8: assertSetSuccess
// assertSetSuccess sets configuration options and checks the expected settings.
func assertSetSuccess(c *gc.C, dir string, svc *state.Service, args []string, expect charm.Settings) {
ctx := coretesting.ContextForDir(c, dir)
code := cmd.Main(envcmd.Wrap(&SetCommand{}), ctx, append([]string{"dummy-service"}, args...))
c.Check(code, gc.Equals, 0)
settings, err := svc.ConfigSettings()
c.Assert(err, gc.IsNil)
c.Assert(settings, gc.DeepEquals, expect)
}
示例9: matchExposure
func matchExposure(patterns []string, s *state.Service) (bool, bool, error) {
if len(patterns) >= 1 && patterns[0] == "exposed" {
return s.IsExposed(), true, nil
} else if len(patterns) >= 2 && patterns[0] == "not" && patterns[1] == "exposed" {
return !s.IsExposed(), true, nil
}
return false, false, nil
}
示例10: assertForceMachine
// assertForceMachine ensures that the result of assigning a unit with --to
// is as expected.
func (s *AddUnitSuite) assertForceMachine(c *gc.C, svc *state.Service, expectedNumMachines, unitNum int, machineId string) {
units, err := svc.AllUnits()
c.Assert(err, gc.IsNil)
c.Assert(units, gc.HasLen, expectedNumMachines)
mid, err := units[unitNum].AssignedMachineId()
c.Assert(err, gc.IsNil)
c.Assert(mid, gc.Equals, machineId)
}
示例11: assertUpgraded
func (s *BaseUpgradeCharmSuite) assertUpgraded(c *gc.C, riak *state.Service, revision int, forced bool) *charm.URL {
err := riak.Refresh()
c.Assert(err, jc.ErrorIsNil)
ch, force, err := riak.Charm()
c.Assert(err, jc.ErrorIsNil)
c.Assert(ch.Revision(), gc.Equals, revision)
c.Assert(force, gc.Equals, forced)
return ch.URL()
}
示例12: removeAllUnits
func removeAllUnits(c *gc.C, s *state.Service) {
us, err := s.AllUnits()
c.Assert(err, gc.IsNil)
for _, u := range us {
err = u.EnsureDead()
c.Assert(err, gc.IsNil)
err = u.Remove()
c.Assert(err, gc.IsNil)
}
}
示例13: serviceSetCharm
// serviceSetCharm sets the charm for the given service.
func (api *API) serviceSetCharm(service *state.Service, url string, forceSeries, forceUnits bool) error {
curl, err := charm.ParseURL(url)
if err != nil {
return errors.Trace(err)
}
sch, err := api.state.Charm(curl)
if err != nil {
return errors.Trace(err)
}
return service.SetCharm(sch, forceSeries, forceUnits)
}
示例14: serviceSetSettingsYAML
// serviceSetSettingsYAML updates the settings for the given service,
// taking the configuration from a YAML string.
func serviceSetSettingsYAML(service *state.Service, settings string) error {
ch, _, err := service.Charm()
if err != nil {
return err
}
changes, err := ch.Config().ParseSettingsYAML([]byte(settings), service.Name())
if err != nil {
return err
}
return service.UpdateConfigSettings(changes)
}
示例15: ServiceSetSettingsStrings
// ServiceSetSettingsStrings updates the settings for the given service,
// taking the configuration from a map of strings.
func ServiceSetSettingsStrings(service *state.Service, settings map[string]string) error {
ch, _, err := service.Charm()
if err != nil {
return errors.Trace(err)
}
// Parse config in a compatible way (see function comment).
changes, err := parseSettingsCompatible(ch, settings)
if err != nil {
return errors.Trace(err)
}
return service.UpdateConfigSettings(changes)
}