예제 #1
0
func (p *FakeProvisioner) RemoveUnit(app provision.App, name string) error {
	if err := p.getError("RemoveUnit"); err != nil {
		return err
	}
	index := -1
	appName := app.GetName()
	if index := p.FindApp(app); index < 0 {
		return errors.New("App is not provisioned.")
	}
	p.unitMut.Lock()
	defer p.unitMut.Unlock()
	for i, unit := range p.units[appName] {
		if unit.Name == name {
			index = i
			break
		}
	}
	if index == -1 {
		return errors.New("Unit not found.")
	}
	copy(p.units[appName][index:], p.units[appName][index+1:])
	p.units[appName] = p.units[appName][:len(p.units[appName])-1]
	p.unitLen--
	return nil
}
예제 #2
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.")
	}
	index := p.FindApp(app)
	if index < 0 {
		return nil, errors.New("App is not provisioned.")
	}
	name := app.GetName()
	platform := app.GetPlatform()
	p.unitMut.Lock()
	defer p.unitMut.Unlock()
	length := uint(len(p.units[name]))
	for i := uint(0); i < n; i++ {
		unit := provision.Unit{
			Name:       fmt.Sprintf("%s/%d", name, p.unitLen),
			AppName:    name,
			Type:       platform,
			Status:     provision.StatusStarted,
			InstanceId: fmt.Sprintf("i-08%d", length+i),
			Ip:         fmt.Sprintf("10.10.10.%d", length+i),
			Machine:    int(length + i),
		}
		p.units[name] = append(p.units[name], unit)
		p.unitLen++
	}
	result := make([]provision.Unit, int(n))
	copy(result, p.units[name][length:])
	return result, nil
}
예제 #3
0
파일: docker.go 프로젝트: prodigeni/tsuru
func deploy(app provision.App, version string, w io.Writer) (string, error) {
	commands, err := deployCmds(app, version)
	if err != nil {
		return "", err
	}
	imageId := getImage(app)
	actions := []*action.Action{&createContainer, &startContainer, &insertContainer}
	pipeline := action.NewPipeline(actions...)
	err = pipeline.Execute(app, imageId, commands)
	if err != nil {
		log.Errorf("error on execute deploy pipeline for app %s - %s", app.GetName(), err)
		return "", err
	}
	c := pipeline.Result().(container)
	err = c.logs(w)
	if err != nil {
		log.Errorf("error on get logs for container %s - %s", c.ID, err)
		return "", err
	}
	_, err = dockerCluster().WaitContainer(c.ID)
	if err != nil {
		log.Errorf("Process failed for container %q: %s", c.ID, err)
		return "", err
	}
	imageId, err = c.commit()
	if err != nil {
		log.Errorf("error on commit container %s - %s", c.ID, err)
		return "", err
	}
	c.remove()
	return imageId, nil
}
예제 #4
0
파일: provisioner.go 프로젝트: nemx/tsuru
func (p *FakeProvisioner) RemoveUnit(app provision.App, name string) error {
	if err := p.getError("RemoveUnit"); err != nil {
		return err
	}
	index := -1
	p.mut.Lock()
	defer p.mut.Unlock()
	pApp, ok := p.apps[app.GetName()]
	if !ok {
		return errNotProvisioned
	}
	for i, unit := range pApp.units {
		if unit.Name == name {
			index = i
			break
		}
	}
	if index == -1 {
		return errors.New("Unit not found.")
	}
	copy(pApp.units[index:], pApp.units[index+1:])
	pApp.units = pApp.units[:len(pApp.units)-1]
	pApp.unitLen--
	p.apps[app.GetName()] = pApp
	return nil
}
예제 #5
0
파일: provisioner.go 프로젝트: ngtuna/tsuru
func (p *dockerProvisioner) Destroy(app provision.App) error {
	containers, _ := listAppContainers(app.GetName())
	go func(c []container) {
		var containersGroup sync.WaitGroup
		containersGroup.Add(len(containers))
		for _, c := range containers {
			go func(c container) {
				defer containersGroup.Done()
				err := removeContainer(&c)
				if err != nil {
					log.Error(err.Error())
				}
			}(c)
		}
		containersGroup.Wait()
		err := removeImage(assembleImageName(app.GetName()))
		if err != nil {
			log.Error(err.Error())
		}
	}(containers)
	r, err := getRouter()
	if err != nil {
		log.Errorf("Failed to get router: %s", err)
		return err
	}
	return r.RemoveBackend(app.GetName())
}
예제 #6
0
func needsFlatten(a provision.App) bool {
	deploys := a.GetDeploys()
	if deploys != 0 && deploys%20 == 0 {
		return true
	}
	return false
}
예제 #7
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.")
	}
	index := p.FindApp(app)
	if index < 0 {
		return nil, errors.New("App is not provisioned.")
	}
	name := app.GetName()
	framework := app.GetFramework()
	p.unitMut.Lock()
	defer p.unitMut.Unlock()
	length := uint(len(p.units[name]))
	for i := uint(0); i < n; i++ {
		unit := provision.Unit{
			Name:    fmt.Sprintf("%s/%d", name, length+i),
			AppName: name,
			Type:    framework,
			Status:  provision.StatusStarted,
			Ip:      fmt.Sprintf("10.10.10.%d", length+i),
			Machine: int(length + i),
		}
		p.units[name] = append(p.units[name], unit)
	}
	return p.units[name][length:], nil
}
예제 #8
0
파일: docker.go 프로젝트: nihao/tsuru
func runContainerCmd(app provision.App) ([]string, error) {
	docker, err := config.GetString("docker:binary")
	if err != nil {
		return []string{}, err
	}
	repoNamespace, err := config.GetString("docker:repository-namespace")
	if err != nil {
		return []string{}, err
	}
	runBin, err := config.GetString("docker:run-cmd:bin")
	if err != nil {
		return []string{}, err
	}
	runArgs, err := config.GetString("docker:run-cmd:args")
	if err != nil {
		return []string{}, err
	}
	port, err := config.GetString("docker:run-cmd:port")
	if err != nil {
		return []string{}, err
	}
	cmd := fmt.Sprintf("%s %s", runBin, runArgs)
	imageName := fmt.Sprintf("%s/%s", repoNamespace, app.GetName()) // TODO (flaviamissi): should be external function
	wholeCmd := []string{docker, "run", "-d", "-p", port, imageName, cmd}
	return wholeCmd, nil
}
예제 #9
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
}
예제 #10
0
// newContainer creates a new container in Docker and stores it in the database.
func newContainer(app provision.App, imageId string, cmds []string) (container, error) {
	cont := container{
		AppName: app.GetName(),
		Type:    app.GetPlatform(),
	}
	port, err := getPort()
	if err != nil {
		log.Printf("error on getting port for container %s - %s", cont.AppName, port)
		return container{}, err
	}
	config := docker.Config{
		Image:        imageId,
		Cmd:          cmds,
		PortSpecs:    []string{port},
		AttachStdin:  false,
		AttachStdout: false,
		AttachStderr: false,
	}
	hostID, c, err := dockerCluster().CreateContainer(&config)
	if err != nil {
		log.Printf("error on creating container in docker %s - %s", cont.AppName, err.Error())
		return container{}, err
	}
	cont.ID = c.ID
	cont.Port = port
	cont.HostAddr = getHostAddr(hostID)
	return cont, nil
}
예제 #11
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)
}
예제 #12
0
파일: git.go 프로젝트: richardjoo/tsuru
func Git(provisioner provision.Provisioner, app provision.App, w io.Writer) error {
	log.Write(w, []byte("\n ---> Tsuru receiving push\n"))
	log.Write(w, []byte("\n ---> Replicating the application repository across units\n"))
	out, err := clone(provisioner, app)
	if err != nil {
		out, err = pull(provisioner, app)
	}
	if err != nil {
		msg := fmt.Sprintf("Got error while clonning/pulling repository: %s -- \n%s", err.Error(), string(out))
		log.Write(w, []byte(msg))
		return errors.New(msg)
	}
	log.Write(w, out)
	log.Write(w, []byte("\n ---> Installing dependencies\n"))
	if err := provisioner.InstallDeps(app, w); err != nil {
		log.Write(w, []byte(err.Error()))
		return err
	}
	log.Write(w, []byte("\n ---> Restarting application\n"))
	if err := app.Restart(w); err != nil {
		log.Write(w, []byte(err.Error()))
		return err
	}
	return log.Write(w, []byte("\n ---> Deploy done!\n\n"))
}
예제 #13
0
파일: docker.go 프로젝트: prodigeni/tsuru
func usePlatformImage(app provision.App) bool {
	deploys := app.GetDeploys()
	if deploys != 0 && deploys%20 == 0 {
		return true
	}
	return false
}
예제 #14
0
파일: provisioner.go 프로젝트: nemx/tsuru
func (JujuProvisioner) Swap(app1, app2 provision.App) error {
	r, err := Router()
	if err != nil {
		return err
	}
	return r.Swap(app1.GetName(), app2.GetName())
}
예제 #15
0
func (p *LXCProvisioner) Addr(app provision.App) (string, error) {
	r, err := p.router()
	if err != nil {
		return "", err
	}
	return r.Addr(app.GetName())
}
예제 #16
0
func (p *FakeProvisioner) FindApp(app provision.App) int {
	for i, a := range p.apps {
		if a.GetName() == app.GetName() {
			return i
		}
	}
	return -1
}
예제 #17
0
// getImage returns the image name or id from an app.
func getImage(app provision.App) string {
	var c container
	collection().Find(bson.M{"appname": app.GetName()}).One(&c)
	if c.Image != "" {
		return c.Image
	}
	return assembleImageName(app.GetPlatform())
}
예제 #18
0
파일: provisioner.go 프로젝트: nemx/tsuru
func (p *JujuProvisioner) Deploy(a provision.App, version string, w io.Writer) error {
	var buf bytes.Buffer
	setOption := []string{"set", a.GetName(), "app-version=" + version}
	if err := runCmd(true, &buf, &buf, setOption...); err != nil {
		log.Errorf("juju: Failed to set app-version. Error: %s.\nCommand output: %s", err, &buf)
	}
	return deploy.Git(p, a, version, w)
}
예제 #19
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]...)
}
예제 #20
0
파일: provisioner.go 프로젝트: nihao/tsuru
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
}
예제 #21
0
func (p *LocalProvisioner) Restart(app provision.App) error {
	var buf bytes.Buffer
	err := p.ExecuteCommand(&buf, &buf, app, "/var/lib/tsuru/hooks/restart")
	if err != nil {
		msg := fmt.Sprintf("Failed to restart the app (%s): %s", err, buf.String())
		app.Log(msg, "tsuru-provisioner")
		return &provision.Error{Reason: buf.String(), Err: err}
	}
	return nil
}
예제 #22
0
func (p *FakeProvisioner) Start(app provision.App) error {
	p.mut.Lock()
	defer p.mut.Unlock()
	pApp, ok := p.apps[app.GetName()]
	if !ok {
		return errNotProvisioned
	}
	pApp.starts++
	p.apps[app.GetName()] = pApp
	return nil
}
예제 #23
0
func (p *FakeProvisioner) Restart(app provision.App) error {
	if err := p.getError("Restart"); err != nil {
		return err
	}
	p.restMut.Lock()
	v := p.restarts[app.GetName()]
	v++
	p.restarts[app.GetName()] = v
	p.restMut.Unlock()
	return nil
}
예제 #24
0
// newContainer creates a new container in Docker and stores it in the database.
func newContainer(app provision.App, imageId string, cmds []string) (container, error) {
	contName := containerName()
	cont := container{
		AppName: app.GetName(),
		Type:    app.GetPlatform(),
		Name:    contName,
		Status:  "created",
	}
	coll := collection()
	defer coll.Close()
	if err := coll.Insert(cont); err != nil {
		log.Errorf("error on inserting container into database %s - %s", cont.Name, err)
		return container{}, err
	}
	port, err := getPort()
	if err != nil {
		log.Errorf("error on getting port for container %s - %s", cont.AppName, port)
		return container{}, err
	}
	user, _ := config.GetString("docker:ssh:user")
	exposedPorts := make(map[docker.Port]struct{}, 1)
	p := docker.Port(fmt.Sprintf("%s/tcp", port))
	exposedPorts[p] = struct{}{}
	config := docker.Config{
		Image:        imageId,
		Cmd:          cmds,
		User:         user,
		ExposedPorts: exposedPorts,
		AttachStdin:  false,
		AttachStdout: false,
		AttachStderr: false,
	}
	opts := dclient.CreateContainerOptions{Name: contName}
	hostID, c, err := dockerCluster().CreateContainer(opts, &config)
	if err == dclient.ErrNoSuchImage {
		var buf bytes.Buffer
		pullOpts := dclient.PullImageOptions{Repository: imageId}
		dockerCluster().PullImage(pullOpts, &buf)
		hostID, c, err = dockerCluster().CreateContainer(opts, &config)
	}
	if err != nil {
		log.Errorf("error on creating container in docker %s - %s", cont.AppName, err)
		return container{}, err
	}
	cont.ID = c.ID
	cont.Port = port
	cont.HostAddr = getHostAddr(hostID)
	err = coll.Update(bson.M{"name": cont.Name}, cont)
	if err != nil {
		log.Errorf("error on updating container into database %s - %s", cont.ID, err)
		return container{}, err
	}
	return cont, nil
}
예제 #25
0
func (p *FakeProvisioner) InstallDeps(app provision.App, w io.Writer) error {
	if err := p.getError("InstallDeps"); err != nil {
		return err
	}
	p.depsMut.Lock()
	v := p.installDeps[app.GetName()]
	v++
	p.installDeps[app.GetName()] = v
	p.depsMut.Unlock()
	return nil
}
예제 #26
0
파일: provisioner.go 프로젝트: nemx/tsuru
// Returns the number of calls to restart.
// GetCmds returns a list of commands executed in an app. If you don't specify
// the command (an empty string), it will return all commands executed in the
// given app.
func (p *FakeProvisioner) GetCmds(cmd string, app provision.App) []Cmd {
	var cmds []Cmd
	p.cmdMut.Lock()
	for _, c := range p.cmds {
		if (cmd == "" || c.Cmd == cmd) && app.GetName() == c.App.GetName() {
			cmds = append(cmds, c)
		}
	}
	p.cmdMut.Unlock()
	return cmds
}
예제 #27
0
파일: git.go 프로젝트: richardjoo/tsuru
// Clone runs a git clone to clone the app repository in an ap.
func clone(p provision.Provisioner, app provision.App) ([]byte, error) {
	var buf bytes.Buffer
	path, err := repository.GetPath()
	if err != nil {
		return nil, fmt.Errorf("Tsuru is misconfigured: %s", err)
	}
	cmd := fmt.Sprintf("git clone %s %s --depth 1", repository.ReadOnlyURL(app.GetName()), path)
	err = p.ExecuteCommand(&buf, &buf, app, cmd)
	b := buf.Bytes()
	log.Printf(`"git clone" output: %s`, b)
	return b, err
}
예제 #28
0
파일: provisioner.go 프로젝트: nemx/tsuru
func (p *FakeProvisioner) Destroy(app provision.App) error {
	if err := p.getError("Destroy"); err != nil {
		return err
	}
	if !p.Provisioned(app) {
		return errNotProvisioned
	}
	p.mut.Lock()
	defer p.mut.Unlock()
	delete(p.apps, app.GetName())
	return nil
}
예제 #29
0
파일: provisioner.go 프로젝트: nemx/tsuru
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)
}
예제 #30
0
func (p *LXCProvisioner) Destroy(app provision.App) error {
	c := container{name: app.GetName()}
	go func(c container) {
		log.Printf("stoping container %s", c.name)
		c.stop()
		log.Printf("destroying container %s", c.name)
		c.destroy()
		log.Printf("removing container %s from the database", c.name)
		p.collection().Remove(bson.M{"name": c.name})
	}(c)
	return nil
}