Example #1
0
// Run runs a playbook and return's it's status as well the stdout and
// stderr outputs respectively.
func (r *Runner) Run(stdout, stderr io.Writer) error {
	hostsFile, err := NewInventoryFile(r.inventory)
	if err != nil {
		return err
	}
	defer os.Remove(hostsFile.Name())

	logrus.Debugf("going to run playbook: %q with hosts file: %q and vars: %s", r.playbook, hostsFile.Name(), r.extraVars)
	cmd := exec.Command("ansible-playbook", "-i", hostsFile.Name(), "--user", r.user,
		"--private-key", r.privKeyFile, "--extra-vars", r.extraVars, r.playbook)
	// turn off host key checking as we are in non-interactive mode
	cmd.Env = append(cmd.Env, "ANSIBLE_HOST_KEY_CHECKING=false")
	cmd.Stdout = stdout
	cmd.Stderr = stderr
	e := executor.New(cmd)
	res, err := e.Run(r.ctxt)
	if err != nil {
		return err
	}
	logrus.Debugf("executor result: %s", res)
	return nil
}
Example #2
0
func (s *configSuite) TestDumpTarball(c *C) {
	key := "/volplugin/foo"
	value := "baz"

	// add test key to etcd
	e := executor.New(exec.Command("/bin/sh", "-c", "etcdctl set "+key+" "+value))
	e.Start()
	result, err := e.Wait(context.Background())
	c.Assert(err, IsNil)
	c.Assert(result.ExitStatus, Equals, 0)

	// remove test key after the test completes
	defer func() {
		e := executor.New(exec.Command("/bin/sh", "-c", "etcdctl rm "+key))
		e.Start()
		result, err := e.Wait(context.Background())
		c.Assert(err, IsNil)
		c.Assert(result.ExitStatus, Equals, 0)
	}()

	tarballPath, err := s.tlc.DumpTarball()
	c.Assert(err, IsNil)
	defer os.Remove(tarballPath)

	// check that the tarball was created in the expected spot
	_, err = os.Stat(tarballPath)
	c.Assert(os.IsNotExist(err), Equals, false)

	tarballFilename := filepath.Base(tarballPath)
	// can't use filepath.Ext() here because it thinks ".gz" is the extension
	dirName := tarballFilename[:strings.LastIndex(tarballFilename, ".tar.gz")]

	testFilename := dirName + key

	data, err := ioutil.ReadFile(tarballPath)
	c.Assert(err, IsNil)

	// check that our test key is present and has the expected value
	reader := bytes.NewReader(data)
	gzReader, err := gzip.NewReader(reader)
	c.Assert(err, IsNil)
	tarReader := tar.NewReader(gzReader)

	found := false

	for {
		header, err := tarReader.Next()
		if err == io.EOF {
			break
		}
		c.Assert(err, IsNil)

		if header.Name == testFilename {
			var b bytes.Buffer
			_, err = io.Copy(&b, tarReader)
			c.Assert(err, IsNil)
			c.Assert(string(b.Bytes()), Equals, value)
			found = true
			break
		}
	}

	c.Assert(found, Equals, true)
}