Ejemplo n.º 1
0
func NewHostLocators(t transport.Transport, values ...string) (Locators, error) {
	out := make(Locators, 0, len(values))
	for i := range values {
		r, err := t.LocatorFor(values[i])
		if err != nil {
			return out, err
		}
		out = append(out, &ResourceLocator{"", "", r})
	}
	return out, nil
}
Ejemplo n.º 2
0
func NewResourceLocator(t transport.Transport, defaultType ResourceType, value string) (Locator, error) {
	res, host, id, errs := SplitTypeHostSuffix(value)
	if errs != nil {
		return nil, errs
	}
	if res == "" {
		res = defaultType
	}
	locator, err := t.LocatorFor(host)
	if err != nil {
		return nil, err
	}
	return &ResourceLocator{ResourceType(res), id, locator}, nil
}
Ejemplo n.º 3
0
func LocatorsForDeploymentInstances(t transport.Transport, instances deployment.InstanceRefs) (cmd.Locators, error) {
	locators := make(cmd.Locators, 0, len(instances))
	for _, instance := range instances {
		if instance.On != nil {
			locator, err := t.LocatorFor(*instance.On)
			if err != nil {
				return cmd.Locators{}, err
			}
			resource := &cmd.ResourceLocator{cloc.ResourceTypeContainer, string(instance.Id), locator}
			locators = append(locators, resource)
		}
	}
	return locators, nil
}
Ejemplo n.º 4
0
func (d Deployment) Describe(placement PlacementStrategy, t transport.Transport) (next *Deployment, removed InstanceRefs, err error) {
	// copy the container list and clear any intermediate state
	sources := d.Containers.Copy()

	// assign instances to containers or the remove list
	for i := range d.Instances {
		instance := &d.Instances[i]
		copied := *instance
		// is the instance invalid or no longer part of the cluster
		if instance.On == nil {
			continue
		}
		if instance.on == nil {
			locator, errl := t.LocatorFor(*instance.On)
			if errl != nil {
				err = errors.New(fmt.Sprintf("The host %s for instance %s is not recognized - you may be using a different transport than originally specified: %s", *instance.On, instance.Id, errl.Error()))
				return
			}
			instance.on = locator
		}
		if placement.RemoveFromLocation(instance.on) {
			removed = append(removed, &copied)
			continue
		}
		// locate the container
		c, found := sources.Find(instance.From)
		if !found {
			removed = append(removed, &copied)
			continue
		}
		c.AddInstance(&copied)
	}

	// create new instances for each container
	added := make(InstanceRefs, 0)
	for i := range sources {
		c := &sources[i]
		if errc := d.createInstances(c); errc != nil {
			err = errc
			return
		}
		for j := range c.instances {
			if c.instances[j].add {
				added = append(added, c.instances[j])
			}
		}
	}

	// assign to hosts
	errp := placement.Assign(added, sources)
	if errp != nil {
		err = errp
		return
	}

	// cull any instances flagged for removal and enforce upper limits
	for i := range sources {
		for _, instance := range sources[i].Instances() {
			if instance.On == nil && !instance.remove {
				err = errors.New("deployment: one or more instances were not assigned to a host")
				return
			}
		}
		removed = append(removed, sources[i].trimInstances()...)
	}

	// check for basic link consistency and ordering
	links, erro := sources.OrderLinks()
	if erro != nil {
		err = erro
		return
	}

	// expose ports for all links
	for i := range links {
		if erre := links[i].exposePorts(); erre != nil {
			err = erre
			return
		}
	}

	// load and reserve all ports
	table, errn := NewInstancePortTable(sources)
	if errn != nil {
		err = errn
		return
	}
	for i := range links {
		if errr := links[i].reserve(table); errr != nil {
			err = errr
			return
		}
	}

	// generate the links
	for i := range links {
		if erra := links[i].appendLinks(); erra != nil {
			err = erra
			return
		}
	}

	// create a copy of instances to return
	instances := make(Instances, 0, len(added))
	for i := range sources {
		existing := sources[i].instances
		for j := range existing {
			instances = append(instances, *existing[j])
		}
	}
	d.Containers = sources
	d.Instances = instances
	next = &d
	return
}