예제 #1
0
func (p *JujuProvisioner) AddUnits(app provision.App, n uint) ([]provision.Unit, error) {
	if n < 1 {
		return nil, errors.New("Cannot add zero units.")
	}
	var (
		buf   bytes.Buffer
		units []provision.Unit
	)
	err := runCmd(true, &buf, &buf, "set", app.GetName(), "app-repo="+repository.GetReadOnlyUrl(app.GetName()))
	if err != nil {
		return nil, &provision.Error{Reason: buf.String(), Err: err}
	}
	buf.Reset()
	err = runCmd(false, &buf, &buf, "add-unit", app.GetName(), "--num-units", strconv.FormatUint(uint64(n), 10))
	if err != nil {
		return nil, &provision.Error{Reason: buf.String(), Err: err}
	}
	unitRe := regexp.MustCompile(fmt.Sprintf(`Unit '(%s/\d+)' added to service '%s'`, app.GetName(), app.GetName()))
	reader := bufio.NewReader(&buf)
	line, err := reader.ReadString('\n')
	for err == nil {
		matches := unitRe.FindStringSubmatch(line)
		if len(matches) > 1 {
			units = append(units, provision.Unit{Name: matches[1]})
		}
		line, err = reader.ReadString('\n')
	}
	if err != io.EOF {
		return nil, &provision.Error{Reason: buf.String(), Err: err}
	}
	return units, nil
}
예제 #2
0
파일: provisioner.go 프로젝트: nihao/tsuru
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.GetReadOnlyUrl(app.GetName()),
	}
	runCmd(true, &buf, &buf, setOption...)
	if p.elbSupport() {
		if err = p.LoadBalancer().Create(app); err != nil {
			return err
		}
		p.enqueueUnits(app.GetName())
	}
	return nil
}
예제 #3
0
파일: docker.go 프로젝트: nihao/tsuru
// DeployCmd combines settings and application attributes needed in
// order to make a deploy command
func deployContainerCmd(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
	}
	cmd, err := config.GetString("docker:deploy-cmd")
	if err != nil {
		return []string{}, err
	}
	cmd = fmt.Sprintf("%s %s", cmd, repository.GetReadOnlyUrl(app.GetName()))
	imageName := fmt.Sprintf("%s/%s", repoNamespace, app.GetPlatform()) // TODO (flaviamissi): should use same algorithm as image.repositoryName
	wholeCmd := []string{docker, "run", "-d", imageName, cmd}
	return wholeCmd, nil
}
예제 #4
0
func (s *S) TestProvision(c *gocheck.C) {
	config.Set("juju:charms-path", "/etc/juju/charms")
	defer config.Unset("juju:charms-path")
	config.Set("host", "somehost")
	defer config.Unset("host")
	tmpdir, err := commandmocker.Add("juju", "$*")
	c.Assert(err, gocheck.IsNil)
	defer commandmocker.Remove(tmpdir)
	app := testing.NewFakeApp("trace", "python", 0)
	p := JujuProvisioner{}
	err = p.Provision(app)
	c.Assert(err, gocheck.IsNil)
	c.Assert(commandmocker.Ran(tmpdir), gocheck.Equals, true)
	expectedParams := []string{
		"deploy", "--repository", "/etc/juju/charms", "local:python", "trace",
		"set", "trace", "app-repo=" + repository.GetReadOnlyUrl("trace"),
	}
	c.Assert(commandmocker.Parameters(tmpdir), gocheck.DeepEquals, expectedParams)
}
예제 #5
0
func (s *S) TestAddUnits(c *C) {
	tmpdir, err := commandmocker.Add("juju", addUnitsOutput)
	c.Assert(err, IsNil)
	defer commandmocker.Remove(tmpdir)
	app := NewFakeApp("resist", "rush", 0)
	p := JujuProvisioner{}
	units, err := p.AddUnits(app, 4)
	c.Assert(err, IsNil)
	c.Assert(units, HasLen, 4)
	names := make([]string, len(units))
	for i, unit := range units {
		names[i] = unit.Name
	}
	expected := []string{"resist/3", "resist/4", "resist/5", "resist/6"}
	c.Assert(names, DeepEquals, expected)
	c.Assert(commandmocker.Ran(tmpdir), Equals, true)
	expectedParams := []string{
		"set", "resist", "app-repo=" + repository.GetReadOnlyUrl("resist"),
		"add-unit", "resist", "--num-units", "4",
	}
	c.Assert(commandmocker.Parameters(tmpdir), DeepEquals, expectedParams)
}
예제 #6
0
func (s *S) TestProvision(c *gocheck.C) {
	fexec := &etesting.FakeExecutor{}
	execut = fexec
	defer func() {
		execut = nil
	}()
	config.Set("juju:charms-path", "/etc/juju/charms")
	defer config.Unset("juju:charms-path")
	config.Set("host", "somehost")
	defer config.Unset("host")
	app := testing.NewFakeApp("trace", "python", 0)
	p := JujuProvisioner{}
	err := p.Provision(app)
	c.Assert(err, gocheck.IsNil)
	args := []string{
		"deploy", "--repository", "/etc/juju/charms", "local:python", "trace",
	}
	c.Assert(fexec.ExecutedCmd("juju", args), gocheck.Equals, true)
	args = []string{
		"set", "trace", "app-repo=" + repository.GetReadOnlyUrl("trace"),
	}
	c.Assert(fexec.ExecutedCmd("juju", args), gocheck.Equals, true)
}