Beispiel #1
0
func (c *NatExposeCommand) Run(ctx *cmd.Context) error {
	err := c.Connect(c.target)
	if err != nil {
		return err
	}
	c.forwards = make(map[string][]*natcmd.Forward)
	for _, uc := range c.ContainedUnits {
		if (names.IsUnit(c.target) && uc.Unit.Name() == c.target) ||
			(names.IsMachine(c.target) && (uc.HostMachine.Id() == c.target || uc.GatewayMachine.Id() == c.target)) {
			fwd, err := uc.NewForward()
			fwd.PortMap = c.portMap
			if err != nil {
				log.Println(err)
				continue
			}
			c.forwards[uc.GatewayMachine.Id()] = append(c.forwards[uc.GatewayMachine.Id()], fwd)
		}
	}
	if c.dryRun {
		c.printNatScripts()
	} else {
		c.execNatScripts()
	}
	return nil
}
Beispiel #2
0
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)
	}
}
Beispiel #3
0
// 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 := 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
}
Beispiel #4
0
func (c *ResolvedCommand) Init(args []string) error {
	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)
}
Beispiel #5
0
func (c *DestroyUnitCommand) Init(args []string) error {
	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
}
Beispiel #6
0
// 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.Conf.checkArgs(args); err != nil {
		return err
	}
	a.runner = worker.NewRunner(isFatal, moreImportant)
	return nil
}
Beispiel #7
0
// 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
}
Beispiel #8
0
// 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 utils.ErrorContextf(&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
}
Beispiel #9
0
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
}
Beispiel #10
0
func (c *NatOutboundCommand) Run(ctx *cmd.Context) error {
	err := c.Connect(c.target)
	if err != nil {
		return err
	}
	var pending []natcmd.UnitContainment
	for _, uc := range c.ContainedUnits {
		if (names.IsUnit(c.target) && uc.Unit.Name() == c.target) ||
			(names.IsMachine(c.target) && (uc.HostMachine.Id() == c.target || uc.GatewayMachine.Id() == c.target)) {
			pending = append(pending, uc)
		}
	}
	if c.dryRun {
		c.printNatScripts(pending)
	} else {
		c.execNatScripts(pending)
	}
	return nil
}
Beispiel #11
0
func (c *NatCommand) Connect(target string) error {
	var err error
	c.Conn, err = juju.NewConnFromName(c.EnvName)
	if err != nil {
		return fmt.Errorf("Unable to connect to environment %q: %v", c.EnvName, err)
	}
	defer c.Conn.Close()

	if !names.IsMachine(target) && !names.IsUnit(target) {
		return fmt.Errorf("invalid target: %q", target)
	}

	c.MachineMap = make(map[string]*state.Machine)
	st := c.Conn.State

	machines, err := st.AllMachines()
	for _, m := range machines {
		c.MachineMap[m.Id()] = m
	}

	services, err := st.AllServices()
	if err != nil {
		return err
	}
	for _, s := range services {
		units, err := s.AllUnits()
		if err != nil {
			return err
		}
		for _, u := range units {
			uc, err := c.UnitContainment(u)
			if err == ErrNoContainer {
				continue
			} else if err != nil {
				log.Println(err)
				continue
			}
			c.ContainedUnits = append(c.ContainedUnits, *uc)
		}
	}
	return nil
}
Beispiel #12
0
func (c *SSHCommon) hostFromTarget(target string) (string, error) {
	// is the target the id of a machine ?
	if names.IsMachine(target) {
		log.Infof("looking up address for machine %s...", target)
		// TODO(dfc) maybe we should have machine.PublicAddress() ?
		return c.machinePublicAddress(target)
	}
	// maybe the target is a unit ?
	if names.IsUnit(target) {
		log.Infof("looking up address for unit %q...", c.Target)
		unit, err := c.State.Unit(target)
		if err != nil {
			return "", err
		}
		addr, ok := unit.PublicAddress()
		if !ok {
			return "", fmt.Errorf("unit %q has no public address", unit)
		}
		return addr, nil
	}
	return "", fmt.Errorf("unknown unit or machine %q", target)
}
Beispiel #13
0
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)
	}
}