Example #1
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.ProvisionedUnits()[0].GetIp())
	arguments = append(arguments, cmd)
	arguments = append(arguments, args...)
	return executor().Execute("ssh", arguments, nil, stdout, stderr)
}
Example #2
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.ProvisionedUnits()
	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
}
Example #3
0
// ExecuteCommand will pretend to execute the given command, recording data
// about it.
//
// The output of the command must be prepared with PrepareOutput, and failures
// must be prepared with PrepareFailure. In case of failure, the prepared
// output will be sent to the standard error stream, otherwise, it will be sent
// to the standard error stream.
//
// When there is no output nor failure prepared, ExecuteCommand will return a
// timeout error.
func (p *FakeProvisioner) ExecuteCommand(stdout, stderr io.Writer, app provision.App, cmd string, args ...string) error {
	var (
		output []byte
		err    error
	)
	command := Cmd{
		Cmd:  cmd,
		Args: args,
		App:  app,
	}
	p.cmdMut.Lock()
	p.cmds = append(p.cmds, command)
	p.cmdMut.Unlock()
	for _ = range app.ProvisionedUnits() {
		select {
		case output = <-p.outputs:
			select {
			case fail := <-p.failures:
				if fail.method == "ExecuteCommand" {
					stderr.Write(output)
					return fail.err
				}
				p.failures <- fail
			default:
				stdout.Write(output)
			}
		case fail := <-p.failures:
			if fail.method == "ExecuteCommand" {
				err = fail.err
				select {
				case output = <-p.outputs:
					stderr.Write(output)
				default:
				}
			} else {
				p.failures <- fail
			}
		case <-time.After(2e9):
			return errors.New("FakeProvisioner timed out waiting for output.")
		}
	}
	return err
}
Example #4
0
func (*JujuProvisioner) startedUnits(app provision.App) []provision.AppUnit {
	units := []provision.AppUnit{}
	allUnits := app.ProvisionedUnits()
	for _, unit := range allUnits {
		if unit.Available() {
			units = append(units, unit)
		}
	}
	return units
}
Example #5
0
func (p *JujuProvisioner) deleteUnits(app provision.App) {
	units := app.ProvisionedUnits()
	names := make([]string, len(units))
	for i, u := range units {
		names[i] = u.GetName()
	}
	collection := p.unitsCollection()
	defer collection.Close()
	collection.RemoveAll(bson.M{"_id": bson.M{"$in": names}})
}
Example #6
0
func (*JujuProvisioner) startedUnits(app provision.App) []provision.AppUnit {
	units := []provision.AppUnit{}
	allUnits := app.ProvisionedUnits()
	for _, unit := range allUnits {
		if status := unit.GetStatus(); status == provision.StatusStarted {
			units = append(units, unit)
		}
	}
	return units
}
Example #7
0
func (p *JujuProvisioner) RemoveUnit(app provision.App, name string) error {
	var unit provision.AppUnit
	for _, unit = range app.ProvisionedUnits() {
		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.removeUnit(app, unit)
}
Example #8
0
func (p *JujuProvisioner) Addr(app provision.App) (string, error) {
	if p.elbSupport() {
		router, err := Router()
		if err != nil {
			return "", err
		}
		return router.Addr(app.GetName())
	}
	units := app.ProvisionedUnits()
	if len(units) < 1 {
		return "", fmt.Errorf("App %q has no units.", app.GetName())
	}
	return units[0].GetIp(), nil
}
Example #9
0
func (p *JujuProvisioner) Addr(app provision.App) (string, error) {
	if p.elbSupport() {
		router, err := Router()
		if err != nil {
			return "", err
		}
		addr, err := router.Addr(app.GetName())
		if err != nil {
			return "", fmt.Errorf("There is no ACTIVE Load Balancer named %s", app.GetName())
		}
		return addr, nil
	}
	units := app.ProvisionedUnits()
	if len(units) < 1 {
		return "", fmt.Errorf("App %q has no units.", app.GetName())
	}
	return units[0].GetIp(), nil
}
Example #10
0
func (p *JujuProvisioner) terminateMachines(app provision.App, units ...provision.AppUnit) error {
	var buf bytes.Buffer
	if len(units) < 1 {
		units = app.ProvisionedUnits()
	}
	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.Errorf("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
}