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 }
// Init initializes the command for running. func (a *MachineAgent) Init(args []string) error { if !names.IsMachine(a.MachineId) { return fmt.Errorf("--machine-id option must be set, and expects a non-negative integer") } if err := a.Conf.checkArgs(args); err != nil { return err } a.runner = worker.NewRunner(isFatal, moreImportant) return nil }
func (c *DestroyMachineCommand) Init(args []string) error { if len(args) == 0 { return fmt.Errorf("no machines specified") } for _, id := range args { if !names.IsMachine(id) { return fmt.Errorf("invalid machine id %q", id) } } c.MachineIds = args return nil }
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 }
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 }
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) }
func (c *AddMachineCommand) Init(args []string) error { if c.Constraints.Container != nil { return fmt.Errorf("container constraint %q not allowed when adding a machine", *c.Constraints.Container) } containerSpec, err := cmd.ZeroOrOneArgs(args) if err != nil { return err } if containerSpec == "" { return nil } // container arg can either be 'type:machine' or 'type' if c.ContainerType, err = instance.ParseSupportedContainerType(containerSpec); err != nil { if names.IsMachine(containerSpec) || !cmd.IsMachineOrNewContainer(containerSpec) { return fmt.Errorf("malformed container argument %q", containerSpec) } sep := strings.Index(containerSpec, ":") c.MachineId = containerSpec[sep+1:] c.ContainerType, err = instance.ParseSupportedContainerType(containerSpec[:sep]) } return err }
// AddUnits starts n units of the given service and allocates machines // to them as necessary. func (conn *Conn) AddUnits(svc *state.Service, n int, machineIdSpec string) ([]*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, fmt.Errorf("cannot add unit %d/%d to service %q: %v", i+1, n, svc.Name(), err) } if machineIdSpec != "" { if n != 1 { return nil, fmt.Errorf("cannot add multiple units of service %q to a single machine", svc.Name()) } // machineIdSpec may be an existing machine or container, eg 3/lxc/2 // or a new container on a machine, eg lxc:3 mid := machineIdSpec var containerType instance.ContainerType specParts := strings.Split(machineIdSpec, ":") if len(specParts) > 1 { firstPart := specParts[0] var err error if containerType, err = instance.ParseSupportedContainerType(firstPart); err == nil { mid = strings.Join(specParts[1:], "/") } else { mid = machineIdSpec } } if !names.IsMachine(mid) { return nil, fmt.Errorf("invalid force machine id %q", mid) } var err error var m *state.Machine // If a container is to be used, create it. if containerType != "" { params := state.AddMachineParams{ Series: unit.Series(), ParentId: mid, ContainerType: containerType, Jobs: []state.MachineJob{state.JobHostUnits}, } m, err = conn.State.AddMachineWithConstraints(¶ms) } else { m, err = conn.State.Machine(mid) } if err != nil { return nil, fmt.Errorf("cannot assign unit %q to machine: %v", unit.Name(), err) } err = unit.AssignToMachine(m) if err != nil { return nil, err } } else if err := conn.State.AssignUnit(unit, policy); err != nil { return nil, err } units[i] = unit } return units, nil }
func (s *machineSuite) TestMachineIdFormats(c *gc.C) { for i, test := range machineIdTests { c.Logf("test %d: %q", i, test.pattern) c.Assert(names.IsMachine(test.pattern), gc.Equals, test.valid) } }
func verifyConfig(cfg *MachineConfig) (err error) { defer utils.ErrorContextf(&err, "invalid machine configuration") if !names.IsMachine(cfg.MachineId) { return fmt.Errorf("invalid machine id") } if cfg.DataDir == "" { return fmt.Errorf("missing var directory") } if cfg.Tools == nil { return fmt.Errorf("missing tools") } if cfg.Tools.URL == "" { return fmt.Errorf("missing tools URL") } if cfg.StateInfo == nil { return fmt.Errorf("missing state info") } if len(cfg.StateInfo.CACert) == 0 { return fmt.Errorf("missing CA certificate") } if cfg.APIInfo == nil { return fmt.Errorf("missing API info") } if len(cfg.APIInfo.CACert) == 0 { return fmt.Errorf("missing API CA certificate") } if cfg.StateServer { if cfg.Config == nil { return fmt.Errorf("missing environment configuration") } if cfg.StateInfo.Tag != "" { return fmt.Errorf("entity tag must be blank when starting a state server") } if cfg.APIInfo.Tag != "" { return fmt.Errorf("entity tag must be blank when starting a state server") } if len(cfg.StateServerCert) == 0 { return fmt.Errorf("missing state server certificate") } if len(cfg.StateServerKey) == 0 { return fmt.Errorf("missing state server private key") } if cfg.StatePort == 0 { return fmt.Errorf("missing state port") } if cfg.APIPort == 0 { return fmt.Errorf("missing API port") } } else { if len(cfg.StateInfo.Addrs) == 0 { return fmt.Errorf("missing state hosts") } if cfg.StateInfo.Tag != names.MachineTag(cfg.MachineId) { return fmt.Errorf("entity tag must match started machine") } if len(cfg.APIInfo.Addrs) == 0 { return fmt.Errorf("missing API hosts") } if cfg.APIInfo.Tag != names.MachineTag(cfg.MachineId) { return fmt.Errorf("entity tag must match started machine") } } if cfg.MachineNonce == "" { return fmt.Errorf("missing machine nonce") } return nil }