func (c *SSHCommon) hostFromTarget(target string) (string, error) { // If the target is neither a machine nor a unit, // assume it's a hostname and try it directly. if !names.IsMachine(target) && !names.IsUnit(target) { return target, nil } // A target may not initially have an address (e.g. the // address updater hasn't yet run), so we must do this in // a loop. if _, err := c.ensureAPIClient(); err != nil { return "", err } var err error for a := sshHostFromTargetAttemptStrategy.Start(); a.Next(); { var addr string if c.proxy { addr, err = c.apiClient.PrivateAddress(target) } else { addr, err = c.apiClient.PublicAddress(target) } if err == nil { return addr, nil } } return "", err }
// PrivateAddress implements the server side of Client.PrivateAddress. func (c *Client) PrivateAddress(p params.PrivateAddress) (results params.PrivateAddressResults, err error) { switch { case names.IsMachine(p.Target): machine, err := c.api.state.Machine(p.Target) if err != nil { return results, err } addr := instance.SelectInternalAddress(machine.Addresses(), false) if addr == "" { return results, fmt.Errorf("machine %q has no internal address", machine) } return params.PrivateAddressResults{PrivateAddress: addr}, nil case names.IsUnit(p.Target): unit, err := c.api.state.Unit(p.Target) if err != nil { return results, err } addr, ok := unit.PrivateAddress() if !ok { return results, fmt.Errorf("unit %q has no internal address", unit) } return params.PrivateAddressResults{PrivateAddress: addr}, nil } return results, fmt.Errorf("unknown unit or machine %q", p.Target) }
func (s *serviceSuite) TestServiceNameFormats(c *gc.C) { assertService := func(s string, expect bool) { c.Assert(names.IsService(s), gc.Equals, expect) // Check that anything that is considered a valid service name // is also (in)valid if a(n) (in)valid unit designator is added // to it. c.Assert(names.IsUnit(s+"/0"), gc.Equals, expect) c.Assert(names.IsUnit(s+"/99"), gc.Equals, expect) c.Assert(names.IsUnit(s+"/-1"), gc.Equals, false) c.Assert(names.IsUnit(s+"/blah"), gc.Equals, false) c.Assert(names.IsUnit(s+"/"), gc.Equals, false) } for i, test := range serviceNameTests { c.Logf("test %d: %q", i, test.pattern) assertService(test.pattern, test.valid) } }
// Unit returns the service's unit with name. func (s *Service) Unit(name string) (*Unit, error) { if !names.IsUnit(name) { return nil, fmt.Errorf("%q is not a valid unit name", name) } udoc := &unitDoc{} sel := bson.D{{"_id", name}, {"service", s.doc.Name}} if err := s.st.units.Find(sel).One(udoc); err != nil { return nil, fmt.Errorf("cannot get unit %q from service %q: %v", name, s.doc.Name, err) } return newUnit(s.st, udoc), nil }
// Init initializes the command for running. func (a *UnitAgent) Init(args []string) error { if a.UnitName == "" { return requiredError("unit-name") } if !names.IsUnit(a.UnitName) { return fmt.Errorf(`--unit-name option expects "<service>/<n>" argument`) } if err := a.AgentConf.CheckArgs(args); err != nil { return err } a.runner = worker.NewRunner(isFatal, moreImportant) return nil }
// Unit returns a unit by name. func (st *State) Unit(name string) (*Unit, error) { if !names.IsUnit(name) { return nil, fmt.Errorf("%q is not a valid unit name", name) } doc := unitDoc{} err := st.units.FindId(name).One(&doc) if err == mgo.ErrNotFound { return nil, errors.NotFoundf("unit %q", name) } if err != nil { return nil, fmt.Errorf("cannot get unit %q: %v", name, err) } return newUnit(st, &doc), nil }
// ReadSettings returns a map holding the settings of the unit with the // supplied name within this relation. An error will be returned if the // relation no longer exists, or if the unit's service is not part of the // relation, or the settings are invalid; but mere non-existence of the // unit is not grounds for an error, because the unit settings are // guaranteed to persist for the lifetime of the relation, regardless // of the lifetime of the unit. func (ru *RelationUnit) ReadSettings(uname string) (m map[string]interface{}, err error) { defer errors.Maskf(&err, "cannot read settings for unit %q in relation %q", uname, ru.relation) if !names.IsUnit(uname) { return nil, fmt.Errorf("%q is not a valid unit name", uname) } key, err := ru.key(uname) if err != nil { return nil, err } node, err := readSettings(ru.st, key) if err != nil { return nil, err } return node.Map(), nil }
func (c *DestroyUnitCommand) Init(args []string) error { if err := c.EnsureEnvName(); err != nil { return err } c.UnitNames = args if len(c.UnitNames) == 0 { return errors.New("no units specified") } for _, name := range c.UnitNames { if !names.IsUnit(name) { return fmt.Errorf("invalid unit name %q", name) } } return nil }
func (c *ResolvedCommand) Init(args []string) error { if err := c.EnvCommandBase.EnsureEnvName(); err != nil { return err } if len(args) > 0 { c.UnitName = args[0] if !names.IsUnit(c.UnitName) { return fmt.Errorf("invalid unit name %q", c.UnitName) } args = args[1:] } else { return fmt.Errorf("no unit specified") } return cmd.CheckEmpty(args) }
func (c *RunCommand) Init(args []string) error { if err := c.EnvCommandBase.EnsureEnvName(); err != nil { return err } if len(args) == 0 { return errors.New("no commands specified") } c.commands, args = args[0], args[1:] if c.all { if len(c.machines) != 0 { return fmt.Errorf("You cannot specify --all and individual machines") } if len(c.services) != 0 { return fmt.Errorf("You cannot specify --all and individual services") } if len(c.units) != 0 { return fmt.Errorf("You cannot specify --all and individual units") } } else { if len(c.machines) == 0 && len(c.services) == 0 && len(c.units) == 0 { return fmt.Errorf("You must specify a target, either through --all, --machine, --service or --unit") } } var nameErrors []string for _, machineId := range c.machines { if !names.IsMachine(machineId) { nameErrors = append(nameErrors, fmt.Sprintf(" %q is not a valid machine id", machineId)) } } for _, service := range c.services { if !names.IsService(service) { nameErrors = append(nameErrors, fmt.Sprintf(" %q is not a valid service name", service)) } } for _, unit := range c.units { if !names.IsUnit(unit) { nameErrors = append(nameErrors, fmt.Sprintf(" %q is not a valid unit name", unit)) } } if len(nameErrors) > 0 { return fmt.Errorf("The following run targets are not valid:\n%s", strings.Join(nameErrors, "\n")) } return cmd.CheckEmpty(args) }
func (ctx *SimpleContext) deployedUnitsUpstartJobs() (map[string]string, error) { fis, err := ioutil.ReadDir(ctx.initDir) if err != nil { return nil, err } installed := make(map[string]string) for _, fi := range fis { if groups := deployedRe.FindStringSubmatch(fi.Name()); len(groups) == 4 { unitName := groups[2] + "/" + groups[3] if !names.IsUnit(unitName) { continue } installed[unitName] = groups[1] } } return installed, nil }
func (c *DebugHooksCommand) Init(args []string) error { if len(args) < 1 { return errors.New("no unit name specified") } c.Target = args[0] if !names.IsUnit(c.Target) { return fmt.Errorf("%q is not a valid unit name", c.Target) } // If any of the hooks is "*", then debug all hooks. c.hooks = append([]string{}, args[1:]...) for _, h := range c.hooks { if h == "*" { c.hooks = nil break } } return nil }
func (c *RunCommand) Init(args []string) error { // make sure we aren't in an existing hook context if contextId, err := getenv("JUJU_CONTEXT_ID"); err == nil && contextId != "" { return fmt.Errorf("juju-run cannot be called from within a hook, have context %q", contextId) } if !c.noContext { if len(args) < 1 { return fmt.Errorf("missing unit-name") } c.unit, args = args[0], args[1:] // If the command line param is a unit id (like service/2) we need to // change it to the unit tag as that is the format of the agent directory // on disk (unit-service-2). if names.IsUnit(c.unit) { c.unit = names.UnitTag(c.unit) } } if len(args) < 1 { return fmt.Errorf("missing commands") } c.commands, args = args[0], args[1:] return cmd.CheckEmpty(args) }
func (s *unitSuite) TestUnitNameFormats(c *gc.C) { for i, test := range unitNameTests { c.Logf("test %d: %q", i, test.pattern) c.Assert(names.IsUnit(test.pattern), gc.Equals, test.valid) } }