Beispiel #1
0
func (p *JujuProvisioner) ExecuteCommand(stdout, stderr io.Writer, app provision.App, cmd string, args ...string) error {
	arguments := []string{"ssh", "-o", "StrictHostKeyChecking no", "-q"}
	units := app.ProvisionUnits()
	length := len(units)
	for i, unit := range units {
		if length > 1 {
			if i > 0 {
				fmt.Fprintln(stdout)
			}
			fmt.Fprintf(stdout, "Output from unit %q:\n\n", unit.GetName())
			if status := unit.GetStatus(); status != provision.StatusStarted {
				fmt.Fprintf(stdout, "Unit state is %q, it must be %q for running commands.\n",
					status, provision.StatusStarted)
				continue
			}
		}
		var cmdargs []string
		cmdargs = append(cmdargs, arguments...)
		cmdargs = append(cmdargs, strconv.Itoa(unit.GetMachine()), cmd)
		cmdargs = append(cmdargs, args...)
		err := runCmd(true, stdout, stderr, cmdargs...)
		fmt.Fprintln(stdout)
		if err != nil {
			return err
		}
	}
	return nil
}
Beispiel #2
0
func (*LXCProvisioner) ExecuteCommand(stdout, stderr io.Writer, app provision.App, cmd string, args ...string) error {
	arguments := []string{"-l", "ubuntu", "-q", "-o", "StrictHostKeyChecking no"}
	arguments = append(arguments, app.ProvisionUnits()[0].GetIp())
	arguments = append(arguments, cmd)
	arguments = append(arguments, args...)
	return executor().Execute("ssh", arguments, nil, stdout, stderr)
}
Beispiel #3
0
func (p *JujuProvisioner) RemoveUnits(app provision.App, n uint) error {
	units := app.ProvisionUnits()
	length := uint(len(units))
	if length == n {
		return errors.New("You can't remove all units from an app.")
	} else if length < n {
		return fmt.Errorf("You can't remove %d units from this app because it has only %d units.", n, length)
	}
	return p.removeUnits(app, units[:n]...)
}
Beispiel #4
0
func (p *JujuProvisioner) RemoveUnit(app provision.App, name string) error {
	var unit provision.AppUnit
	for _, unit = range app.ProvisionUnits() {
		if unit.GetName() == name {
			break
		}
	}
	if unit.GetName() != name {
		return fmt.Errorf("App %q does not have a unit named %q.", app.GetName(), name)
	}
	return p.removeUnits(app, unit)
}
Beispiel #5
0
func (*LocalProvisioner) ExecuteCommand(stdout, stderr io.Writer, app provision.App, cmd string, args ...string) error {
	arguments := []string{"-l", "ubuntu", "-q", "-o", "StrictHostKeyChecking no"}
	arguments = append(arguments, app.ProvisionUnits()[0].GetIp())
	arguments = append(arguments, cmd)
	arguments = append(arguments, args...)
	c := exec.Command("ssh", arguments...)
	c.Stdout = stdout
	c.Stderr = stderr
	err := c.Run()
	if err != nil {
		return err
	}
	return nil
}
Beispiel #6
0
func (p *JujuProvisioner) terminateMachines(app provision.App) error {
	var buf bytes.Buffer
	for _, u := range app.ProvisionUnits() {
		buf.Reset()
		err := runCmd(false, &buf, &buf, "terminate-machine", strconv.Itoa(u.GetMachine()))
		out := buf.String()
		if err != nil {
			msg := fmt.Sprintf("Failed to destroy unit %s: %s", u.GetName(), out)
			app.Log(msg, "tsuru")
			return &provision.Error{Reason: out, Err: err}
		}
	}
	return nil
}
Beispiel #7
0
func (p *JujuProvisioner) RemoveUnits(app provision.App, n uint) ([]int, error) {
	units := app.ProvisionUnits()
	length := uint(len(units))
	if length == n {
		return nil, errors.New("You can't remove all units from an app.")
	} else if length < n {
		return nil, fmt.Errorf("You can't remove %d units from this app because it has only %d units.", n, length)
	}
	result := make([]int, n)
	if err := p.removeUnits(app, units[:n]...); err != nil {
		return nil, err
	}
	for i := 0; i < len(result); i++ {
		result[i] = i
	}
	return result, nil
}
Beispiel #8
0
func (p *JujuProvisioner) deleteUnits(app provision.App) {
	units := app.ProvisionUnits()
	names := make([]string, len(units))
	for i, u := range units {
		names[i] = u.GetName()
	}
	p.unitsCollection().RemoveAll(bson.M{"_id": bson.M{"$in": names}})
}
Beispiel #9
0
func (p *JujuProvisioner) Addr(app provision.App) (string, error) {
	if p.elbSupport() {
		return p.LoadBalancer().Addr(app)
	}
	units := app.ProvisionUnits()
	if len(units) < 1 {
		return "", fmt.Errorf("App %q has no units.", app.GetName())
	}
	return units[0].GetIp(), nil
}
Beispiel #10
0
func (p *DockerProvisioner) Destroy(app provision.App) error {
	units := app.ProvisionUnits()
	for _, u := range units {
		go func(u provision.AppUnit) {
			c := container{
				name: app.GetName(),
				// TODO: get actual c.id
				id: u.GetInstanceId(),
			}
			log.Printf("stoping container %s", u.GetInstanceId())
			if err := c.stop(); err != nil {
				log.Print("Could not stop container. Trying to remove it anyway.")
				log.Print(err.Error())
			}

			log.Printf("removing container %s", u.GetInstanceId())
			if err := c.remove(); err != nil {
				log.Print("Could not remove container. Aborting...")
				log.Print(err.Error())
				return
			}

			log.Printf("removing container %s from the database", u.GetName())
			if err := collection().Remove(bson.M{"name": u.GetName()}); err != nil {
				log.Printf("Could not remove container from database. Error %s", err.Error())
			}
			log.Print("Units successfuly removed.")
		}(u)
	}
	img := &image{Name: app.GetName()}
	log.Printf("removing image %s from the database", app.GetName())
	if err := img.remove(); err != nil {
		return err
	}
	return nil
}
Beispiel #11
0
func (p *JujuProvisioner) terminateMachines(app provision.App, units ...provision.AppUnit) error {
	var buf bytes.Buffer
	if len(units) < 1 {
		units = app.ProvisionUnits()
	}
	for _, u := range units {
		buf.Reset()
		err := runCmd(false, &buf, &buf, "terminate-machine", strconv.Itoa(u.GetMachine()))
		out := buf.String()
		if err != nil {
			msg := fmt.Sprintf("Failed to destroy unit %s: %s", u.GetName(), out)
			app.Log(msg, "tsuru")
			log.Printf("Failed to destroy unit %q from the app %q: %s", u.GetName(), app.GetName(), out)
			return cmdError(out, err, []string{"terminate-machine", strconv.Itoa(u.GetMachine())})
		}
	}
	return nil
}
Beispiel #12
0
func (*LocalProvisioner) Addr(app provision.App) (string, error) {
	units := app.ProvisionUnits()
	return units[0].GetIp(), nil
}