Пример #1
0
func addUnitsWithHost(a provision.App, units uint, destinationHost ...string) ([]provision.Unit, error) {
	if units == 0 {
		return nil, errors.New("Cannot add 0 units")
	}
	length, err := getContainerCountForAppName(a.GetName())
	if err != nil {
		return nil, err
	}
	if length < 1 {
		return nil, errors.New("New units can only be added after the first deployment")
	}
	writer := app.LogWriter{App: a, Writer: ioutil.Discard}
	result := make([]provision.Unit, int(units))
	container, err := getOneContainerByAppName(a.GetName())
	if err != nil {
		return nil, err
	}
	imageId := container.Image
	for i := uint(0); i < units; i++ {
		container, err := start(a, imageId, &writer, destinationHost...)
		if err != nil {
			return nil, err
		}
		result[i] = provision.Unit{
			Name:    container.ID,
			AppName: a.GetName(),
			Type:    a.GetPlatform(),
			Ip:      container.HostAddr,
			Status:  provision.StatusBuilding,
		}
	}
	return result, nil
}
Пример #2
0
// getImage returns the image name or id from an app.
// when the container image is empty is returned the platform image.
// when a deploy is multiple of 10 is returned the platform image.
func getImage(app provision.App) string {
	c, err := getOneContainerByAppName(app.GetName())
	if err != nil || c.Image == "" || usePlatformImage(app) {
		return assembleImageName(app.GetPlatform())
	}
	return c.Image
}
Пример #3
0
func (p *FakeProvisioner) AddUnits(app provision.App, n uint) ([]provision.Unit, error) {
	if err := p.getError("AddUnits"); err != nil {
		return nil, err
	}
	if n == 0 {
		return nil, errors.New("Cannot add 0 units.")
	}
	p.mut.Lock()
	defer p.mut.Unlock()
	pApp, ok := p.apps[app.GetName()]
	if !ok {
		return nil, errNotProvisioned
	}
	name := app.GetName()
	platform := app.GetPlatform()
	length := uint(len(pApp.units))
	for i := uint(0); i < n; i++ {
		unit := provision.Unit{
			Name:    fmt.Sprintf("%s/%d", name, pApp.unitLen),
			AppName: name,
			Type:    platform,
			Status:  provision.StatusStarted,
			Ip:      fmt.Sprintf("10.10.10.%d", length+i),
		}
		pApp.units = append(pApp.units, unit)
		pApp.unitLen++
	}
	result := make([]provision.Unit, int(n))
	copy(result, pApp.units[length:])
	p.apps[app.GetName()] = pApp
	return result, nil
}
Пример #4
0
func (p *FakeProvisioner) Provision(app provision.App) error {
	if err := p.getError("Provision"); err != nil {
		return err
	}
	if p.Provisioned(app) {
		return &provision.Error{Reason: "App already provisioned."}
	}
	p.mut.Lock()
	defer p.mut.Unlock()
	p.apps[app.GetName()] = provisionedApp{
		app:     app,
		unitLen: 1,
		units: []provision.Unit{
			{
				Name:       app.GetName() + "/0",
				AppName:    app.GetName(),
				Type:       app.GetPlatform(),
				Status:     provision.StatusStarted,
				InstanceId: "i-080",
				Ip:         "10.10.10.1",
				Machine:    1,
			},
		},
	}
	return nil
}
Пример #5
0
func (c *container) asUnit(a provision.App) provision.Unit {
	return provision.Unit{
		Name:    c.ID,
		AppName: a.GetName(),
		Type:    a.GetPlatform(),
		Ip:      c.HostAddr,
		Status:  provision.StatusBuilding,
	}
}
Пример #6
0
func (p *FakeProvisioner) AddUnitsToNode(app provision.App, n uint, process string, w io.Writer, nodeAddr string) ([]provision.Unit, error) {
	if err := p.getError("AddUnits"); err != nil {
		return nil, err
	}
	if n == 0 {
		return nil, errors.New("Cannot add 0 units.")
	}
	p.mut.Lock()
	defer p.mut.Unlock()
	pApp, ok := p.apps[app.GetName()]
	if !ok {
		return nil, errNotProvisioned
	}
	name := app.GetName()
	platform := app.GetPlatform()
	length := uint(len(pApp.units))
	for i := uint(0); i < n; i++ {
		val := atomic.AddInt32(&uniqueIpCounter, 1)
		var hostAddr string
		if nodeAddr != "" {
			hostAddr = net.URLToHost(nodeAddr)
		} else if len(p.nodes) > 0 {
			for _, n := range p.nodes {
				hostAddr = net.URLToHost(n.Address())
				break
			}
		} else {
			hostAddr = fmt.Sprintf("10.10.10.%d", val)
		}
		unit := provision.Unit{
			ID:          fmt.Sprintf("%s-%d", name, pApp.unitLen),
			AppName:     name,
			Type:        platform,
			Status:      provision.StatusStarted,
			Ip:          hostAddr,
			ProcessName: process,
			Address: &url.URL{
				Scheme: "http",
				Host:   fmt.Sprintf("%s:%d", hostAddr, val),
			},
		}
		err := routertest.FakeRouter.AddRoute(name, unit.Address)
		if err != nil {
			return nil, err
		}
		pApp.units = append(pApp.units, unit)
		pApp.unitLen++
	}
	result := make([]provision.Unit, int(n))
	copy(result, pApp.units[length:])
	p.apps[app.GetName()] = pApp
	if w != nil {
		fmt.Fprintf(w, "added %d units", n)
	}
	return result, nil
}
Пример #7
0
// getBuildImage returns the image name from app or plaftorm.
// the platform image will be returned if:
// * there are no containers;
// * the container have an empty image name;
// * the deploy number is multiple of 10.
// in all other cases the app image name will be returne.
func (p *dockerProvisioner) getBuildImage(app provision.App) string {
	if p.usePlatformImage(app) {
		return platformImageName(app.GetPlatform())
	}
	appImageName, err := appCurrentImageName(app.GetName())
	if err != nil {
		return platformImageName(app.GetPlatform())
	}
	return appImageName
}
Пример #8
0
// GetBuildImage returns the image name from app or plaftorm.
// the platform image will be returned if:
// * there are no containers;
// * the container have an empty image name;
// * the deploy number is multiple of 10.
// in all other cases the app image name will be returne.
func GetBuildImage(app provision.App) string {
	if usePlatformImage(app) {
		return PlatformImageName(app.GetPlatform())
	}
	appImageName, err := AppCurrentImageName(app.GetName())
	if err != nil {
		return PlatformImageName(app.GetPlatform())
	}
	return appImageName
}
Пример #9
0
func taskToUnit(task *swarm.Task, service *swarm.Service, node *swarm.Node, a provision.App) provision.Unit {
	addr := node.Spec.Labels[labelNodeDockerAddr.String()]
	host := tsuruNet.URLToHost(addr)
	return provision.Unit{
		ID:          task.ID,
		AppName:     a.GetName(),
		ProcessName: service.Spec.Annotations.Labels[labelAppProcess.String()],
		Type:        a.GetPlatform(),
		Ip:          host,
		Status:      stateMap[task.Status.State],
		Address:     &url.URL{},
	}
}
Пример #10
0
// getImage returns the image name or id from an app.
// when the container image is empty is returned the platform image.
// when a deploy is multiple of 10 is returned the platform image.
func getImage(app provision.App) string {
	c, err := getOneContainerByAppName(app.GetName())
	if err != nil || c.Image == "" {
		return assembleImageName(app.GetPlatform())
	}
	if usePlatformImage(app) {
		err := removeImage(c.Image)
		if err != nil {
			log.Error(err.Error())
		}
		return assembleImageName(app.GetPlatform())
	}
	return c.Image
}
Пример #11
0
func (p *JujuProvisioner) Provision(app provision.App) error {
	var buf bytes.Buffer
	charms, err := config.GetString("juju:charms-path")
	if err != nil {
		return errors.New(`Setting "juju:charms-path" is not defined.`)
	}
	args := []string{
		"deploy", "--repository", charms,
		"local:" + app.GetPlatform(), app.GetName(),
	}
	err = runCmd(false, &buf, &buf, args...)
	out := buf.String()
	if err != nil {
		app.Log("Failed to create machine: "+out, "tsuru")
		return cmdError(out, err, args)
	}
	setOption := []string{
		"set", app.GetName(), "app-repo=" + repository.ReadOnlyURL(app.GetName()),
	}
	runCmd(true, &buf, &buf, setOption...)
	if p.elbSupport() {
		router, err := Router()
		if err != nil {
			return err
		}
		if err = router.AddBackend(app.GetName()); err != nil {
			return err
		}
		p.enqueueUnits(app.GetName())
	}
	return nil
}
Пример #12
0
// getImage returns the image name or id from an app.
// when the container image is empty is returned the platform image.
// when a deploy is multiple of 10 is returned the platform image.
func getImage(app provision.App) string {
	var c container
	coll := collection()
	defer coll.Close()
	coll.Find(bson.M{"appname": app.GetName()}).One(&c)
	if c.Image == "" {
		return assembleImageName(app.GetPlatform())
	}
	if usePlatformImage(app) {
		err := removeImage(c.Image)
		if err != nil {
			log.Error(err.Error())
		}
		return assembleImageName(app.GetPlatform())
	}
	return c.Image
}
Пример #13
0
func (c *container) asUnit(a provision.App) provision.Unit {
	status := provision.Status(c.Status)
	if c.Status == "" {
		status = provision.StatusBuilding
	}
	cType := c.Type
	if cType == "" {
		cType = a.GetPlatform()
	}
	return provision.Unit{
		Name:        c.ID,
		AppName:     a.GetName(),
		Type:        cType,
		Ip:          c.HostAddr,
		Status:      status,
		ProcessName: c.ProcessName,
	}
}