コード例 #1
0
func (p *RHAgentProvider) LoadAgent(file string, timeout time.Duration) (*RHAgent, error) {
	cmd := exec.Command(file, "-o", "metadata")
	var b bytes.Buffer
	cmd.Stdout = &b
	err := cmd.Start()
	if err != nil {
		return nil, err
	}
	if timeout == time.Duration(0) {
		err = cmd.Wait()
	} else {
		err = utils.WaitTimeout(cmd, timeout)
	}
	if err != nil {
		return nil, err
	}

	mdxml := b.Bytes()
	mda, err := parseMetadata(mdxml)
	if err != nil {
		return nil, err
	}

	a, err := mda.toResourceAgent()
	if err != nil {
		return nil, fmt.Errorf("Agent \"%s\": %s", mda.Name, err)
	}

	a.Command = file

	return a, nil
}
コード例 #2
0
func (p *RHAgentProvider) run(ac *fence.AgentConfig, action string, timeout time.Duration) ([]byte, error) {
	a, err := p.getRHAgent(ac.Name)
	if err != nil {
		return nil, err
	}
	command := a.Command
	cmd := exec.Command(command)
	cmdstdin, err := cmd.StdinPipe()
	var b bytes.Buffer
	cmd.Stdout = &b
	if err != nil {
		return nil, err
	}
	err = cmd.Start()
	if err != nil {
		return nil, err
	}

	_, err = cmdstdin.Write([]byte(fmt.Sprintf("action=%s\n", action)))
	if err != nil {
		return nil, err
	}
	if ac.Port != "" {
		_, err = cmdstdin.Write([]byte(fmt.Sprintf("port=%s\n", ac.Port)))
		if err != nil {
			return nil, err

		}
	}
	for name, values := range ac.Parameters {
		for _, value := range values {
			_, err = cmdstdin.Write([]byte(fmt.Sprintf("%s=%s\n", name, value)))
			if err != nil {
				return nil, err
			}
		}
	}

	cmdstdin.Close()
	if timeout == time.Duration(0) {
		err = cmd.Wait()
	} else {
		err = utils.WaitTimeout(cmd, timeout)
	}

	if err != nil {
		return b.Bytes(), err
	}

	return b.Bytes(), nil
}