func (s *InstanceSuite) TestParseSupportedContainerType(c *C) { ctype, err := instance.ParseSupportedContainerType("lxc") c.Assert(err, IsNil) c.Assert(ctype, Equals, instance.ContainerType("lxc")) ctype, err = instance.ParseSupportedContainerType("none") c.Assert(err, Not(IsNil)) }
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 !state.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 }