Ejemplo n.º 1
0
func (id *InventoryDataRunner) Get() (client.InventoryData, error) {
	tools, err := listRunnable(id.dir)
	if err != nil {
		return nil, errors.Wrapf(err, "failed to list tools for inventory data")
	}

	idec := NewInventoryDataDecoder()
	for _, t := range tools {
		cmd := id.cmd.Command(t)
		out, err := cmd.StdoutPipe()
		if err != nil {
			log.Errorf("failed to open stdout for inventory tool %s: %v", t, err)
			continue
		}

		if err := cmd.Start(); err != nil {
			log.Errorf("inventory tool %s failed with status: %v", t, err)
			continue
		}

		p := utils.KeyValParser{}
		if err := p.Parse(out); err != nil {
			log.Warnf("inventory tool %s returned unparsable output: %v", t, err)
			continue
		}

		if err := cmd.Wait(); err != nil {
			log.Warnf("inventory tool %s wait failed: %v", t, err)
		}

		idec.AppendFromRaw(p.Collect())
	}
	return idec.GetInventoryData(), nil
}
Ejemplo n.º 2
0
// Obtain identity data by calling a suitable helper tool
func (id IdentityDataRunner) Get() (string, error) {
	helper := identityDataHelper

	if id.Helper != "" {
		helper = id.Helper
	}

	cmd := id.cmdr.Command(helper)

	out, err := cmd.StdoutPipe()
	if err != nil {
		return "", errors.Wrapf(err, "failed to open pipe for reading")
	}

	if err := cmd.Start(); err != nil {
		return "", errors.Wrapf(err, "failed to call %s", helper)
	}

	p := utils.KeyValParser{}
	if err := p.Parse(out); err != nil {
		return "", errors.Wrapf(err, "failed to parse identity data")
	}

	if err := cmd.Wait(); err != nil {
		return "", errors.Wrapf(err, "wait for helper failed")
	}

	collected := p.Collect()
	if len(collected) == 0 {
		return "", errors.New("no identity data colleted")
	}
	data := IdentityData{}
	data.AppendFromRaw(collected)

	encdata, err := json.Marshal(data)
	if err != nil {
		return "", errors.Wrapf(err, "failed to encode identity data")
	}

	return string(encdata), nil
}